From 064440b0ee1df7a6792233d4991ea28c7728457f Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Sat, 22 Nov 2025 17:39:57 +0900 Subject: [PATCH] refresh lists on source change and sync --- lib/app/hooks/use_on_source.dart | 22 ++++++++++++++++++++++ lib/app/lists/albums_grid.dart | 11 +++-------- lib/app/lists/artists_list.dart | 11 +++-------- lib/app/state/database.dart | 2 +- lib/services/sync_service.dart | 4 +++- 5 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 lib/app/hooks/use_on_source.dart diff --git a/lib/app/hooks/use_on_source.dart b/lib/app/hooks/use_on_source.dart new file mode 100644 index 0000000..935b9fc --- /dev/null +++ b/lib/app/hooks/use_on_source.dart @@ -0,0 +1,22 @@ +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; + +import '../state/services.dart'; +import '../state/source.dart'; + +void useOnSourceChange(WidgetRef ref, void Function(int sourceId) callback) { + final sourceId = ref.watch(sourceIdProvider); + + useEffect(() { + callback(sourceId); + return; + }, [sourceId]); +} + +void useOnSourceSync(WidgetRef ref, void Function() callback) { + final syncService = ref.watch(syncServiceProvider); + + useOnListenableChange(syncService, () { + callback(); + }); +} diff --git a/lib/app/lists/albums_grid.dart b/lib/app/lists/albums_grid.dart index d2afc86..b508b5e 100644 --- a/lib/app/lists/albums_grid.dart +++ b/lib/app/lists/albums_grid.dart @@ -1,13 +1,12 @@ import 'package:flutter/material.dart'; -import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; import '../../sources/models.dart'; +import '../hooks/use_on_source.dart'; import '../hooks/use_paging_controller.dart'; import '../state/database.dart'; -import '../state/source.dart'; import 'list_items.dart'; const kPageSize = 60; @@ -18,8 +17,6 @@ class AlbumsGrid extends HookConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final db = ref.watch(databaseProvider); - final sourceId = ref.watch(sourceIdProvider); - final controller = usePagingController( getNextPageKey: (state) => state.lastPageIsEmpty ? null : state.nextIntPageKey, @@ -29,10 +26,8 @@ class AlbumsGrid extends HookConsumerWidget { ), ); - useEffect(() { - controller.refresh(); - return; - }, [sourceId]); + useOnSourceChange(ref, (_) => controller.refresh()); + useOnSourceSync(ref, controller.refresh); return PagingListener( controller: controller, diff --git a/lib/app/lists/artists_list.dart b/lib/app/lists/artists_list.dart index 3ff808a..c398c5e 100644 --- a/lib/app/lists/artists_list.dart +++ b/lib/app/lists/artists_list.dart @@ -1,13 +1,12 @@ import 'package:flutter/material.dart'; -import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; import '../../sources/models.dart'; +import '../hooks/use_on_source.dart'; import '../hooks/use_paging_controller.dart'; import '../state/database.dart'; -import '../state/source.dart'; import 'list_items.dart'; const kPageSize = 30; @@ -20,8 +19,6 @@ class ArtistsList extends HookConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final db = ref.watch(databaseProvider); - final sourceId = ref.watch(sourceIdProvider); - final controller = usePagingController( getNextPageKey: (state) => state.lastPageIsEmpty ? null : state.nextIntPageKey, @@ -31,10 +28,8 @@ class ArtistsList extends HookConsumerWidget { ), ); - useEffect(() { - controller.refresh(); - return; - }, [sourceId]); + useOnSourceChange(ref, (_) => controller.refresh()); + useOnSourceSync(ref, controller.refresh); return PagingListener( controller: controller, diff --git a/lib/app/state/database.dart b/lib/app/state/database.dart index 62b98e5..ea0bc44 100644 --- a/lib/app/state/database.dart +++ b/lib/app/state/database.dart @@ -12,7 +12,7 @@ final databaseInitializer = FutureProvider((ref) async { SourcesCompanion.insert( id: Value(1), name: 'test subsonic', - // isActive: Value(true), + isActive: Value(true), ), ); await db diff --git a/lib/services/sync_service.dart b/lib/services/sync_service.dart index a0b82f9..7696d23 100644 --- a/lib/services/sync_service.dart +++ b/lib/services/sync_service.dart @@ -1,13 +1,14 @@ import 'package:async/async.dart'; import 'package:collection/collection.dart'; import 'package:drift/drift.dart'; +import 'package:flutter/foundation.dart'; import '../database/database.dart'; import '../sources/music_source.dart'; const kSliceSize = 200; -class SyncService { +class SyncService with ChangeNotifier { SyncService({ required this.source, required this.db, @@ -28,6 +29,7 @@ class SyncService { syncPlaylistSongs(), ]); }); + notifyListeners(); } Future syncArtists() async {