From 9f7301305ef1a5b7a042489118e0569ccd4dafe3 Mon Sep 17 00:00:00 2001 From: Vicky <10212746+haifeng-jiang@user.noreply.gitee.com> Date: Thu, 29 Oct 2026 11:16:13 +0800 Subject: [PATCH] =?UTF-8?q?perf(following):=20=E8=BF=BD=E5=89=A7=E9=A1=B5?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E8=A7=81=E6=97=B6=E4=B8=8D=E5=86=8D=E6=8F=90?= =?UTF-8?q?=E5=89=8D=E6=8B=89=E8=A7=82=E7=9C=8B=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 首页用 IndexedStack 常驻四个 tab,追剧页一启动就会发一次观看历史请求,和首页 feed 抢网络; 改为切到该 tab 时再加载,收藏列表也只在首次可见且为空时补拉。 --- lib/pages/following/following_page.dart | 19 +++++++++++++++---- test/following/following_page_test.dart | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) 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 {