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
@@ -1,6 +1,7 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
|
||||
import '../../core/data/models/drama.dart';
|
||||
import '../discover/discover_types.dart';
|
||||
@@ -22,6 +23,7 @@ class HomeLogic extends ChangeNotifier {
|
||||
bool drawerOpen = false;
|
||||
bool detailOpen = false;
|
||||
bool videoImmersive = false;
|
||||
bool _dramaNotifyScheduled = false;
|
||||
bool _disposed = false;
|
||||
|
||||
void notify() {
|
||||
@@ -120,7 +122,17 @@ class HomeLogic extends ChangeNotifier {
|
||||
|
||||
void onDramaChanged(Drama 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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
/// 服务端续看位置:进播放页时取一次,用于"定位到上次那一集的那一秒"。
|
||||
|
||||
@@ -139,11 +139,17 @@ class _PlayerPageState extends State<PlayerPage>
|
||||
if (widget.homeMode &&
|
||||
widget.feedDramas != null &&
|
||||
(feedChanged || channelChanged)) {
|
||||
logic.updateFeed(
|
||||
widget.feedDramas!,
|
||||
keepDramaId: channelChanged ? null : logic.currentDramaId,
|
||||
resetToFirst: channelChanged,
|
||||
);
|
||||
final feedDramas = widget.feedDramas!;
|
||||
final keepDramaId = channelChanged ? null : logic.currentDramaId;
|
||||
final resetToFirst = channelChanged;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
logic.updateFeed(
|
||||
feedDramas,
|
||||
keepDramaId: keepDramaId,
|
||||
resetToFirst: resetToFirst,
|
||||
);
|
||||
});
|
||||
}
|
||||
logic.onPageUpdated(oldWidget);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user