perf(following): 追剧页不可见时不再提前拉观看历史
首页用 IndexedStack 常驻四个 tab,追剧页一启动就会发一次观看历史请求,和首页 feed 抢网络; 改为切到该 tab 时再加载,收藏列表也只在首次可见且为空时补拉。
This commit is contained in:
1 parent
63191b4692
commit
9f7301305e
2 files changed
+38
-4
No files matched your search
@@ -69,10 +69,14 @@ class _FollowingPageState extends State<FollowingPage> {
|
||||
history: context.read<WatchHistoryRepository>(),
|
||||
catalog: context.read<CatalogRepository>(),
|
||||
);
|
||||
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<FollowingPage> {
|
||||
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((_) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user