fix(player): 分离首页与播放页播放上下文

This commit is contained in:
Zhou Weidong committed 2026-10-26 16:37:37 +08:00
1 parent 440188f261
commit 6667330ce0
8 files changed
+312 -362

No files matched your search

+34 -88
View File
@@ -15,7 +15,6 @@ import '../../core/data/services/favorite_controller.dart';
import '../../core/data/services/download_controller.dart';
import '../../core/data/services/interaction_controller.dart';
import '../../core/data/services/playback_position_cache.dart';
import '../../core/data/services/playback_queue.dart';
import '../../core/data/services/playback_session.dart';
import '../../core/data/services/sprite_thumbnail_axis.dart';
import '../../core/data/services/playback_thumbnail_service.dart';
@@ -59,26 +58,12 @@ const double _feedRefreshTopGap = 56;
class PlayerPageHandoff {
PlayerPageHandoff({
required this.logic,
required this.sourceQueue,
required this.sourceEpisodes,
required this.sourceIndex,
required this.seriesDrama,
required this.seriesEpisodes,
required this.episodeIndex,
required this.prepareSource,
required this.sourceUserWantsPlay,
});
final PlayerLogic logic;
final PlaybackQueue sourceQueue;
final List<Episode> sourceEpisodes;
final int sourceIndex;
final Drama seriesDrama;
final List<Episode> seriesEpisodes;
final int episodeIndex;
final Future<void> Function() prepareSource;
PageController? destinationController;
VoidCallback? activateDestination;
final bool sourceUserWantsPlay;
}
class PlayerPage extends StatefulWidget {
@@ -109,6 +94,7 @@ class PlayerPage extends StatefulWidget {
this.wallet,
this.downloads,
this.handoff,
this.isolatePlaybackSession = false,
});
final Drama drama;
@@ -136,6 +122,7 @@ class PlayerPage extends StatefulWidget {
final WalletController? wallet;
final DownloadController? downloads;
final PlayerPageHandoff? handoff;
final bool isolatePlaybackSession;
@override
State<PlayerPage> createState() => _PlayerPageState();
@@ -144,7 +131,6 @@ class PlayerPage extends StatefulWidget {
class _PlayerPageState extends State<PlayerPage>
with WidgetsBindingObserver, RouteAware {
late final PlayerLogic logic;
late final bool _ownsLogic;
late PageController _pageController;
late final PlaybackThumbnailService _thumbnailService;
late final SpriteThumbnailAxisService _spriteAxisService;
@@ -163,8 +149,6 @@ class _PlayerPageState extends State<PlayerPage>
bool _presentationReturning = false;
bool _leavingHandoff = false;
bool _preparingFullPlayer = false;
PlayerPageHandoff? _activeHandoff;
PageController? _returnPageController;
final Set<String> _precachedPosterUrls = {};
/// 收藏切换后的本地增量:基准是详情快照,切换成功按服务端状态 ±1。
@@ -175,16 +159,13 @@ class _PlayerPageState extends State<PlayerPage>
void initState() {
super.initState();
final handoff = widget.handoff;
_ownsLogic = handoff == null;
logic =
handoff?.logic ??
PlayerBinding().create(
history: context.read<WatchHistoryRepository>(),
playback: context.read<PlaybackRepository>(),
positionCache: context.read<PlaybackPositionCache>(),
wallet: widget.wallet,
downloads: widget.downloads,
);
logic = PlayerBinding().create(
history: context.read<WatchHistoryRepository>(),
playback: context.read<PlaybackRepository>(),
positionCache: context.read<PlaybackPositionCache>(),
wallet: widget.wallet,
downloads: widget.downloads,
);
logic.attach(widget, isMounted: () => mounted);
_thumbnailService = PlaybackThumbnailService(
sourceResolver: _resolveThumbnailSource,
@@ -192,22 +173,14 @@ class _PlayerPageState extends State<PlayerPage>
_spriteAxisService = SpriteThumbnailAxisService(spriteCacheCapacity: 8)
..addListener(_handleSpriteFrameChanged);
WidgetsBinding.instance.addObserver(this);
if (handoff == null) {
logic.start(
factory: context.read<PlayerFactory>(),
coordinator: context.read<PlaybackCoordinator>(),
);
_pageController = logic.pages;
} else {
logic.adoptSeriesHandoff(
drama: handoff.seriesDrama,
seriesEpisodes: handoff.seriesEpisodes,
episodeIndex: handoff.episodeIndex,
);
_pageController = PageController(initialPage: handoff.episodeIndex);
handoff.destinationController = _pageController;
logic.session.setRouteVisible(widget.active);
}
logic.start(
factory: context.read<PlayerFactory>(),
coordinator: handoff == null && !widget.isolatePlaybackSession
? context.read<PlaybackCoordinator>()
: PlaybackCoordinator(),
initialUserWantsPlay: handoff?.sourceUserWantsPlay ?? true,
);
_pageController = logic.pages;
}
@override
@@ -328,11 +301,7 @@ class _PlayerPageState extends State<PlayerPage>
..removeListener(_handleSpriteFrameChanged)
..dispose();
_thumbnailService.dispose();
if (_ownsLogic) {
logic.dispose();
} else {
_pageController.dispose();
}
logic.dispose();
super.dispose();
}
@@ -769,11 +738,10 @@ class _PlayerPageState extends State<PlayerPage>
Widget _buildPresentationReturnLayer() {
final indices = logic.visibleIndices;
final controller = _returnPageController ?? _pageController;
return AnimatedBuilder(
animation: logic.session,
builder: (context, _) => PlayerRenderStack(
pageController: controller,
pageController: _pageController,
slots: _renderSlots(indices),
),
);
@@ -1188,7 +1156,6 @@ class _PlayerPageState extends State<PlayerPage>
final sourceIndex = logic.session.currentIndex;
final sourceEpisode = logic.session.currentEpisode;
final sourceQueue = logic.queue;
final sourceEpisodes = List<Episode>.of(logic.sessionEpisodes);
var seriesDrama = sourceQueue.entryAt(sourceIndex).drama;
try {
seriesDrama = await context.read<CatalogController>().requireDetail(
@@ -1217,22 +1184,17 @@ class _PlayerPageState extends State<PlayerPage>
seriesEpisodes.insert(0, sourceEpisode);
episodeIndex = 0;
}
final sourcePageController = _pageController;
final sourceUserWantsPlay = logic.session.userWantsPlay;
try {
await logic.pauseForFullPlayer();
} finally {
_preparingFullPlayer = false;
}
if (!mounted || !context.mounted) return;
final handoff = PlayerPageHandoff(
logic: logic,
sourceQueue: sourceQueue,
sourceEpisodes: sourceEpisodes,
sourceIndex: sourceIndex,
seriesDrama: seriesDrama,
seriesEpisodes: seriesEpisodes,
episodeIndex: episodeIndex,
prepareSource: _prepareSourceAfterFullPlayer,
sourceUserWantsPlay: sourceUserWantsPlay,
);
_activeHandoff = handoff;
handoff.activateDestination = () {
final destination = handoff.destinationController;
if (destination != null) logic.pages = destination;
};
_logFullPlayerRoute('present');
setState(() => _presentationActive = true);
final route = Navigator.of(context).push<void>(
@@ -1251,41 +1213,24 @@ class _PlayerPageState extends State<PlayerPage>
setState(() => _presentationSuspended = true);
await WidgetsBinding.instance.endOfFrame;
}
sourcePageController.dispose();
handoff.activateDestination?.call();
await route;
if (!mounted) return;
final currentPage = logic.visibleIndices.indexOf(
logic.session.currentIndex,
);
final restoredPage =
_returnPageController ??
PageController(initialPage: currentPage < 0 ? 0 : currentPage);
logic.pages = restoredPage;
_pageController = restoredPage;
logic.attach(widget, isMounted: () => mounted);
setState(() {
_presentationActive = false;
_presentationSuspended = false;
_presentationReturning = false;
});
_activeHandoff = null;
logic.session.setRouteVisible(widget.active);
await logic.resumeAfterFullPlayer();
if (!mounted) return;
_logFullPlayerRoute('restored');
}
Future<void> _prepareSourceAfterFullPlayer() async {
if (!mounted) return;
final handoff = _activeHandoff;
if (handoff == null) return;
logic.attach(widget, isMounted: () => mounted);
logic.restoreFeedHandoff(
page: widget,
sourceQueue: handoff.sourceQueue,
sourceEpisodes: handoff.sourceEpisodes,
sourceIndex: handoff.sourceIndex,
);
_returnPageController = PageController(initialPage: handoff.sourceIndex);
await logic.prepareFeedResume();
if (!mounted) return;
setState(() {
_presentationSuspended = false;
_presentationReturning = true;
@@ -1296,6 +1241,7 @@ class _PlayerPageState extends State<PlayerPage>
Future<void> _leaveHandoff(BuildContext context) async {
if (_leavingHandoff) return;
setState(() => _leavingHandoff = true);
await logic.session.pauseForRouteHandoff();
await widget.handoff?.prepareSource();
if (context.mounted) Navigator.of(context).pop();
}