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
@@ -3,6 +3,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../core/data/models/drama.dart';
|
||||
@@ -108,6 +109,8 @@ class PlayerLogic extends ChangeNotifier {
|
||||
bool _latestResetToFirst = false;
|
||||
bool _feedUpdateRunning = false;
|
||||
bool _hasPendingFeed = false;
|
||||
bool _deferredFeedNotifyScheduled = false;
|
||||
int _feedJumpSerial = 0;
|
||||
Future<void> _feedUpdateFuture = Future<void>.value();
|
||||
|
||||
/// [start] 是否已经跑过:没跑过时 session/pages 还是 late 未初始化,
|
||||
@@ -259,7 +262,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
if (dramas.isNotEmpty) pageDrama = dramas.first;
|
||||
if (nextQueue.length == 0) {
|
||||
session.setRouteVisible(false);
|
||||
notify();
|
||||
_notifyAfterFrameIfNeeded();
|
||||
return;
|
||||
}
|
||||
final keepIndex =
|
||||
@@ -275,7 +278,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
preloadIndex: nextVisibleIndex(keepIndex),
|
||||
);
|
||||
_jumpToFeedPage(keepIndex);
|
||||
notify();
|
||||
_notifyAfterFrameIfNeeded();
|
||||
return;
|
||||
}
|
||||
await _replaceSession(
|
||||
@@ -303,7 +306,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
final nextSession = _createSession(factory, coordinator);
|
||||
session = nextSession;
|
||||
nextSession.setRouteVisible(active && visibleIndices.isNotEmpty);
|
||||
notify();
|
||||
_notifyAfterFrameIfNeeded();
|
||||
await previous.close();
|
||||
if (revision != feedRevision || !identical(session, nextSession)) return;
|
||||
_jumpToFeedPage(targetIndex);
|
||||
@@ -312,13 +315,32 @@ class PlayerLogic extends ChangeNotifier {
|
||||
|
||||
void _jumpToFeedPage(int index) {
|
||||
if (!_started) return;
|
||||
if (pages.hasClients) {
|
||||
final serial = ++_feedJumpSerial;
|
||||
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle &&
|
||||
pages.hasClients) {
|
||||
pages.jumpToPage(index);
|
||||
return;
|
||||
}
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (_disposed || serial != _feedJumpSerial || !_started) return;
|
||||
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();
|
||||
}
|
||||
|
||||
/// 服务端续看位置:进播放页时取一次,用于"定位到上次那一集的那一秒"。
|
||||
|
||||
Reference in new issue
Block a user