diff --git a/lib/pages/articles/article_list.dart b/lib/pages/articles/article_list.dart index 01bc8ad5..4986943f 100644 --- a/lib/pages/articles/article_list.dart +++ b/lib/pages/articles/article_list.dart @@ -82,13 +82,6 @@ class _ArticleListState extends ConsumerState { controller: _scroller, itemCount: ref.watch(queryMetaProvider.selectAsync((it) => it.count)), itemBuilder: (context, index) async { - if (index == 0) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ref.read(queryMetaProvider.future).then((meta) { - ref.read(currentArticleProvider.notifier).maybeInit(meta.ids[0]); - }); - }); - } return AsyncArticleItem( articleId: await ref .watch(queryMetaProvider.selectAsync((it) => it.ids[index])), diff --git a/lib/providers/article.dart b/lib/providers/article.dart index f46b0deb..7170f46f 100644 --- a/lib/providers/article.dart +++ b/lib/providers/article.dart @@ -20,8 +20,11 @@ class ArticleData extends _$ArticleData { @override Future build(int articleId) async { final stopwatch = Stopwatch()..start(); + _watcher?.cancel(); _watch(articleId); + ref.onDispose(() => _watcher?.cancel()); + final t1 = DB().managers.articles; final ret = await t1.filter((f) => f.id.equals(articleId)).getSingleOrNull(); @@ -35,75 +38,48 @@ class ArticleData extends _$ArticleData { void _watch(int articleId) { final q = DB().managers.articles.filter((f) => f.id.equals(articleId)); _watcher = q.watchSingleOrNull(distinct: false).listen((article) { - final stateArticle = state.maybeWhen(orElse: () => null, data: (a) => a); + if (state.isLoading || !state.hasValue) return; + final stateArticle = state.value; if (stateArticle == null || article != stateArticle) { state = AsyncValue.data(article); } }); - ref.onDispose(() => _watcher?.cancel()); } } @riverpod class CurrentArticle extends _$CurrentArticle { - int? _articleId; - StreamSubscription? _watcher; - @override Future build() async { - _watcher?.cancel(); - - _articleId ??= + var articleId = ref.watch(settingsProvider.select((it) => it[Sk.selectedArticleId])); - if (_articleId == null) { - final meta = await ref.read(queryMetaProvider.future); - if (meta.ids.isNotEmpty) { - _articleId = meta.ids.first; + if (articleId != null) { + final db = DB(); + final exists = await db.managers.articles + .filter((f) => f.id.equals(articleId)) + .exists(); + if (!exists) { + articleId = null; } } - if (_articleId != null) { - _watch(); - return ref.watch(articleDataProvider(_articleId!).future); - } - - return null; - } - - void _watch() { - final q = DB().managers.articles.filter((f) => f.id.equals(_articleId!)); - _watcher = q.watchSingleOrNull(distinct: false).listen((article) { - if (article == null) { - _articleId = null; - ref.invalidateSelf(); - return; + if (articleId == null) { + final meta = await ref.read(queryMetaProvider.future); + if (meta.ids.isNotEmpty) { + articleId = meta.ids.first; } + } - final stateArticle = state.maybeWhen(orElse: () => null, data: (a) => a); - if (stateArticle == null) { - state = AsyncValue.data(article); - } else if (article.id != stateArticle.id) { - change(article.id); - } - }); - ref.onDispose(() => _watcher?.cancel()); + return articleId != null + ? ref.watch(articleDataProvider(articleId!).future) + : null; } void change(int articleId) { - if (_articleId == articleId) return; - - _articleId = articleId; - - ref.read(settingsProvider.notifier).set(Sk.selectedArticleId, articleId); - ref.invalidateSelf(); - } - - bool maybeInit(int articleId) { - if (_articleId != null && _articleId! > 0) return false; - - change(articleId); - return true; + if (state.value?.id != articleId) { + ref.read(settingsProvider.notifier).set(Sk.selectedArticleId, articleId); + } } } diff --git a/lib/providers/article.g.dart b/lib/providers/article.g.dart index 47d2f224..89e89bd6 100644 --- a/lib/providers/article.g.dart +++ b/lib/providers/article.g.dart @@ -6,7 +6,7 @@ part of 'article.dart'; // RiverpodGenerator // ************************************************************************** -String _$articleDataHash() => r'09fd45c15c8422c131da66e9ab2391c2dfd95019'; +String _$articleDataHash() => r'72cf426783f218b987b89bb1373db5c4fb521695'; /// Copied from Dart SDK class _SystemHash { @@ -173,7 +173,7 @@ class _ArticleDataProviderElement int get articleId => (origin as ArticleDataProvider).articleId; } -String _$currentArticleHash() => r'8f33426bc3b0686762ae8007f3924b88d2f96b9c'; +String _$currentArticleHash() => r'406f48e0443d21a0ddcb3dc86aa8402e73e520f9'; /// See also [CurrentArticle]. @ProviderFor(CurrentArticle)