diff --git a/lib/pages/following/following_page.dart b/lib/pages/following/following_page.dart index b467784..d099fb9 100644 --- a/lib/pages/following/following_page.dart +++ b/lib/pages/following/following_page.dart @@ -69,10 +69,14 @@ class _FollowingPageState extends State { history: context.read(), catalog: context.read(), ); - unawaited(_history.load()); - WidgetsBinding.instance.addPostFrameCallback((_) { - if (mounted) unawaited(widget.favoriteController.load()); - }); + // IndexedStack 启动时就会建好本页:不可见时先不拉数据,等切到本 tab 再加载, + // 免得冷启动和首页 feed 抢网络。 + if (widget.visible) { + unawaited(_history.load()); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) unawaited(widget.favoriteController.load()); + }); + } } void _onAuthChanged() { @@ -107,6 +111,13 @@ class _FollowingPageState extends State { super.didUpdateWidget(oldWidget); if (!oldWidget.visible && widget.visible) { unawaited(_history.load()); + // 启动时本页不可见,收藏没在这里拉过;首次可见时补齐(已有数据就不重复请求)。 + // 放在帧后,避免构建期间触发 controller 通知。 + if (widget.favoriteController.cards.isEmpty) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) unawaited(widget.favoriteController.load()); + }); + } } if (oldWidget.historyRequest == widget.historyRequest) return; WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/test/following/following_page_test.dart b/test/following/following_page_test.dart index ef5db17..5acbf0e 100644 --- a/test/following/following_page_test.dart +++ b/test/following/following_page_test.dart @@ -23,6 +23,29 @@ import '../support/fake_interaction_repositories.dart'; import '../support/widget_player.dart'; void main() { + testWidgets('should_defer_watch_history_until_following_tab_is_opened', ( + tester, + ) async { + SharedPreferencesAsyncPlatform.instance = + InMemorySharedPreferencesAsync.empty(); + final catalog = FakeCatalogRepository(); + final history = _IdentityWatchHistoryRepository( + device: [_historyProgress('the-signal-demo', 'Device history')], + account: const [], + ); + await tester.pumpWidget(_app(catalog: catalog, history: history)); + await tester.pumpAndSettle(); + + // 首页默认停在推荐 tab:追剧页虽在 IndexedStack 里,也不该提前拉观看历史。 + // 启动时只有「我的」页会拉一次,追剧页若也拉就会变成 2。 + expect(history.recentCalls, 1); + + await tester.tap(find.byKey(const Key('tab-follow'))); + await tester.pumpAndSettle(); + + expect(history.recentCalls, greaterThan(0)); + }); + testWidgets('should_render_following_tabs_grid_and_edit_actions', ( tester, ) async {