fix(home): 修复 feed 更新触发的 build 期 setState
This commit is contained in:
1 parent
862b7e92dd
commit
d227ac69fb
6 files changed
+174
-10
No files matched your search
@@ -149,3 +149,29 @@ Running Gradle task 'assembleDebug'... 28.7s
|
|||||||
|
|
||||||
解读:静态分析零告警,561 项全量测试通过,debug APK 构建成功;
|
解读:静态分析零告警,561 项全量测试通过,debug APK 构建成功;
|
||||||
`pubspec.lock` 已还原,无依赖变更,未安装或操作真机。
|
`pubspec.lock` 已还原,无依赖变更,未安装或操作真机。
|
||||||
|
|
||||||
|
## 2026-10-21 K98.2 修复 feed 更新触发 build 期通知
|
||||||
|
|
||||||
|
- 已从最新 `origin/develop` 创建 `fix/k98.2-feed-build-notify`。
|
||||||
|
- PlayerPage 的 feed 更新改为帧后调度,同一帧多次变化继续由 PlayerLogic 合并。
|
||||||
|
- PlayerLogic 在非 idle 调度阶段延后 feed 通知与 PageController 跳页,并给延迟通知去重。
|
||||||
|
- HomeLogic.onDramaChanged 在 build/layout/paint 阶段改为帧后通知。
|
||||||
|
- 已补 build 阶段 updateFeed、最终 PageController 位置及 HomeLogic 延后通知回归测试。
|
||||||
|
|
||||||
|
门禁原始输出:
|
||||||
|
|
||||||
|
```text
|
||||||
|
$ dart analyze lib test
|
||||||
|
Analyzing lib, test...
|
||||||
|
No issues found!
|
||||||
|
|
||||||
|
$ flutter test --no-pub
|
||||||
|
00:55 +563: All tests passed!
|
||||||
|
|
||||||
|
$ flutter build apk --debug
|
||||||
|
Running Gradle task 'assembleDebug'... 26.4s
|
||||||
|
✓ Built build/app/outputs/flutter-apk/app-debug.apk
|
||||||
|
```
|
||||||
|
|
||||||
|
解读:静态分析零告警,563 项全量测试通过,debug APK 构建成功;
|
||||||
|
`pubspec.lock` 已还原,无依赖变更,未安装或操作真机。
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/scheduler.dart';
|
||||||
|
|
||||||
import '../../core/data/models/drama.dart';
|
import '../../core/data/models/drama.dart';
|
||||||
import '../discover/discover_types.dart';
|
import '../discover/discover_types.dart';
|
||||||
@@ -22,6 +23,7 @@ class HomeLogic extends ChangeNotifier {
|
|||||||
bool drawerOpen = false;
|
bool drawerOpen = false;
|
||||||
bool detailOpen = false;
|
bool detailOpen = false;
|
||||||
bool videoImmersive = false;
|
bool videoImmersive = false;
|
||||||
|
bool _dramaNotifyScheduled = false;
|
||||||
bool _disposed = false;
|
bool _disposed = false;
|
||||||
|
|
||||||
void notify() {
|
void notify() {
|
||||||
@@ -120,7 +122,17 @@ class HomeLogic extends ChangeNotifier {
|
|||||||
|
|
||||||
void onDramaChanged(Drama drama) {
|
void onDramaChanged(Drama drama) {
|
||||||
currentDrama = drama;
|
currentDrama = drama;
|
||||||
notify();
|
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle) {
|
||||||
|
notify();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_dramaNotifyScheduled || _disposed) return;
|
||||||
|
_dramaNotifyScheduled = true;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
_dramaNotifyScheduled = false;
|
||||||
|
if (!_disposed) notify();
|
||||||
|
});
|
||||||
|
SchedulerBinding.instance.scheduleFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onImmersiveChanged(bool value) {
|
void onImmersiveChanged(bool value) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../../core/data/models/drama.dart';
|
import '../../core/data/models/drama.dart';
|
||||||
@@ -108,6 +109,8 @@ class PlayerLogic extends ChangeNotifier {
|
|||||||
bool _latestResetToFirst = false;
|
bool _latestResetToFirst = false;
|
||||||
bool _feedUpdateRunning = false;
|
bool _feedUpdateRunning = false;
|
||||||
bool _hasPendingFeed = false;
|
bool _hasPendingFeed = false;
|
||||||
|
bool _deferredFeedNotifyScheduled = false;
|
||||||
|
int _feedJumpSerial = 0;
|
||||||
Future<void> _feedUpdateFuture = Future<void>.value();
|
Future<void> _feedUpdateFuture = Future<void>.value();
|
||||||
|
|
||||||
/// [start] 是否已经跑过:没跑过时 session/pages 还是 late 未初始化,
|
/// [start] 是否已经跑过:没跑过时 session/pages 还是 late 未初始化,
|
||||||
@@ -259,7 +262,7 @@ class PlayerLogic extends ChangeNotifier {
|
|||||||
if (dramas.isNotEmpty) pageDrama = dramas.first;
|
if (dramas.isNotEmpty) pageDrama = dramas.first;
|
||||||
if (nextQueue.length == 0) {
|
if (nextQueue.length == 0) {
|
||||||
session.setRouteVisible(false);
|
session.setRouteVisible(false);
|
||||||
notify();
|
_notifyAfterFrameIfNeeded();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final keepIndex =
|
final keepIndex =
|
||||||
@@ -275,7 +278,7 @@ class PlayerLogic extends ChangeNotifier {
|
|||||||
preloadIndex: nextVisibleIndex(keepIndex),
|
preloadIndex: nextVisibleIndex(keepIndex),
|
||||||
);
|
);
|
||||||
_jumpToFeedPage(keepIndex);
|
_jumpToFeedPage(keepIndex);
|
||||||
notify();
|
_notifyAfterFrameIfNeeded();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await _replaceSession(
|
await _replaceSession(
|
||||||
@@ -303,7 +306,7 @@ class PlayerLogic extends ChangeNotifier {
|
|||||||
final nextSession = _createSession(factory, coordinator);
|
final nextSession = _createSession(factory, coordinator);
|
||||||
session = nextSession;
|
session = nextSession;
|
||||||
nextSession.setRouteVisible(active && visibleIndices.isNotEmpty);
|
nextSession.setRouteVisible(active && visibleIndices.isNotEmpty);
|
||||||
notify();
|
_notifyAfterFrameIfNeeded();
|
||||||
await previous.close();
|
await previous.close();
|
||||||
if (revision != feedRevision || !identical(session, nextSession)) return;
|
if (revision != feedRevision || !identical(session, nextSession)) return;
|
||||||
_jumpToFeedPage(targetIndex);
|
_jumpToFeedPage(targetIndex);
|
||||||
@@ -312,13 +315,32 @@ class PlayerLogic extends ChangeNotifier {
|
|||||||
|
|
||||||
void _jumpToFeedPage(int index) {
|
void _jumpToFeedPage(int index) {
|
||||||
if (!_started) return;
|
if (!_started) return;
|
||||||
if (pages.hasClients) {
|
final serial = ++_feedJumpSerial;
|
||||||
|
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle &&
|
||||||
|
pages.hasClients) {
|
||||||
pages.jumpToPage(index);
|
pages.jumpToPage(index);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (_disposed || serial != _feedJumpSerial || !_started) return;
|
||||||
if (pages.hasClients) pages.jumpToPage(index);
|
if (pages.hasClients) pages.jumpToPage(index);
|
||||||
});
|
});
|
||||||
|
SchedulerBinding.instance.scheduleFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _notifyAfterFrameIfNeeded() {
|
||||||
|
if (_disposed) return;
|
||||||
|
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle) {
|
||||||
|
notify();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_deferredFeedNotifyScheduled) return;
|
||||||
|
_deferredFeedNotifyScheduled = true;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
_deferredFeedNotifyScheduled = false;
|
||||||
|
if (!_disposed) notify();
|
||||||
|
});
|
||||||
|
SchedulerBinding.instance.scheduleFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 服务端续看位置:进播放页时取一次,用于"定位到上次那一集的那一秒"。
|
/// 服务端续看位置:进播放页时取一次,用于"定位到上次那一集的那一秒"。
|
||||||
|
|||||||
@@ -139,11 +139,17 @@ class _PlayerPageState extends State<PlayerPage>
|
|||||||
if (widget.homeMode &&
|
if (widget.homeMode &&
|
||||||
widget.feedDramas != null &&
|
widget.feedDramas != null &&
|
||||||
(feedChanged || channelChanged)) {
|
(feedChanged || channelChanged)) {
|
||||||
logic.updateFeed(
|
final feedDramas = widget.feedDramas!;
|
||||||
widget.feedDramas!,
|
final keepDramaId = channelChanged ? null : logic.currentDramaId;
|
||||||
keepDramaId: channelChanged ? null : logic.currentDramaId,
|
final resetToFirst = channelChanged;
|
||||||
resetToFirst: channelChanged,
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
);
|
if (!mounted) return;
|
||||||
|
logic.updateFeed(
|
||||||
|
feedDramas,
|
||||||
|
keepDramaId: keepDramaId,
|
||||||
|
resetToFirst: resetToFirst,
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
logic.onPageUpdated(oldWidget);
|
logic.onPageUpdated(oldWidget);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// Author: fruit.com.cn
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:fruitshorts/core/data/models/drama.dart';
|
import 'package:fruitshorts/core/data/models/drama.dart';
|
||||||
import 'package:fruitshorts/pages/discover/discover_types.dart';
|
import 'package:fruitshorts/pages/discover/discover_types.dart';
|
||||||
@@ -121,6 +122,40 @@ void main() {
|
|||||||
logic.dispose();
|
logic.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('should_defer_drama_notification_when_changed_during_build', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final logic = HomeLogic(dramas);
|
||||||
|
var notifications = 0;
|
||||||
|
var immediateNotifications = -1;
|
||||||
|
var changed = false;
|
||||||
|
logic.addListener(() => notifications++);
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: ListenableBuilder(
|
||||||
|
listenable: logic,
|
||||||
|
builder: (context, _) {
|
||||||
|
if (!changed) {
|
||||||
|
changed = true;
|
||||||
|
logic.onDramaChanged(dramas[1]);
|
||||||
|
immediateNotifications = notifications;
|
||||||
|
}
|
||||||
|
return const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(immediateNotifications, 0);
|
||||||
|
expect(tester.takeException(), isNull);
|
||||||
|
expect(logic.currentDrama.id, 'city');
|
||||||
|
await tester.pump();
|
||||||
|
expect(notifications, 1);
|
||||||
|
expect(tester.takeException(), isNull);
|
||||||
|
logic.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
|
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
|
||||||
final logic = HomeLogic(dramas);
|
final logic = HomeLogic(dramas);
|
||||||
|
|
||||||
|
|||||||
@@ -236,6 +236,7 @@ void main() {
|
|||||||
logic.dispose();
|
logic.dispose();
|
||||||
await _drain(logic);
|
await _drain(logic);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
group('history writes', () {
|
group('history writes', () {
|
||||||
@@ -618,6 +619,68 @@ void main() {
|
|||||||
logic.dispose();
|
logic.dispose();
|
||||||
await _drain(logic);
|
await _drain(logic);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets(
|
||||||
|
'should_defer_feed_update_side_effects_when_called_during_build',
|
||||||
|
(tester) async {
|
||||||
|
final factory = _RecordingFactory();
|
||||||
|
final initial = [_drama('a'), _drama('b'), _drama('c')];
|
||||||
|
final latest = [_drama('d'), _drama('c'), _drama('a')];
|
||||||
|
final logic = _logic(FakeWatchHistory());
|
||||||
|
logic.attach(
|
||||||
|
PlayerPage(
|
||||||
|
drama: initial.first,
|
||||||
|
initialIndex: 0,
|
||||||
|
homeMode: true,
|
||||||
|
feedDramas: initial,
|
||||||
|
),
|
||||||
|
isMounted: () => true,
|
||||||
|
);
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
logic.start(
|
||||||
|
factory: factory.create,
|
||||||
|
coordinator: PlaybackCoordinator(),
|
||||||
|
);
|
||||||
|
await _drain(logic);
|
||||||
|
});
|
||||||
|
expect(logic.currentDramaId, 'a');
|
||||||
|
expect(logic.session.active, isNotNull);
|
||||||
|
var updateScheduled = false;
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: ListenableBuilder(
|
||||||
|
listenable: logic,
|
||||||
|
builder: (context, _) {
|
||||||
|
if (!updateScheduled) {
|
||||||
|
updateScheduled = true;
|
||||||
|
logic.updateFeed(latest, keepDramaId: 'a');
|
||||||
|
}
|
||||||
|
return Scaffold(
|
||||||
|
body: PageView.builder(
|
||||||
|
controller: logic.pages,
|
||||||
|
itemCount: logic.visibleIndices.length,
|
||||||
|
itemBuilder: (_, index) =>
|
||||||
|
ColoredBox(color: Colors.black, key: ValueKey(index)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect(tester.takeException(), isNull);
|
||||||
|
expect(logic.pages.hasClients, isTrue);
|
||||||
|
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 100));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
expect(tester.takeException(), isNull);
|
||||||
|
expect(logic.queue.entryAt(2).drama.id, 'a');
|
||||||
|
expect(logic.pages.page, closeTo(2, 0.01));
|
||||||
|
logic.dispose();
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
group('lifecycle', () {
|
group('lifecycle', () {
|
||||||
|
|||||||
Reference in new issue
Block a user