chore(mobile): cleanup asset viewer state (#26300)

initState was quite noisy, so I've moved some things around and made the
intention a bit clearer.

Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
Thomas
2026-02-19 18:26:21 +00:00
committed by GitHub
parent 7394fa1491
commit aa02310d63

View File

@@ -87,39 +87,37 @@ class AssetViewer extends ConsumerStatefulWidget {
} }
class _AssetViewerState extends ConsumerState<AssetViewer> { class _AssetViewerState extends ConsumerState<AssetViewer> {
late PageController pageController; late final _heroOffset = widget.heroOffset ?? TabsRouterScope.of(context)?.controller.activeIndex ?? 0;
late final _pageController = PageController(initialPage: widget.initialIndex);
late final _preloader = AssetPreloader(timelineService: ref.read(timelineServiceProvider), mounted: () => mounted);
StreamSubscription? _reloadSubscription; StreamSubscription? _reloadSubscription;
late final int heroOffset;
bool _assetReloadRequested = false;
int _totalAssets = 0;
late final AssetPreloader _preloader;
KeepAliveLink? _stackChildrenKeepAlive; KeepAliveLink? _stackChildrenKeepAlive;
bool _assetReloadRequested = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
assert(ref.read(currentAssetNotifier) != null, "Current asset should not be null when opening the AssetViewer");
pageController = PageController(initialPage: widget.initialIndex);
final timelineService = ref.read(timelineServiceProvider);
_totalAssets = timelineService.totalAssets;
_preloader = AssetPreloader(timelineService: timelineService, mounted: () => mounted);
WidgetsBinding.instance.addPostFrameCallback(_onAssetInit);
_reloadSubscription = EventStream.shared.listen(_onEvent);
heroOffset = widget.heroOffset ?? TabsRouterScope.of(context)?.controller.activeIndex ?? 0;
final asset = ref.read(currentAssetNotifier); final asset = ref.read(currentAssetNotifier);
assert(asset != null, "Current asset should not be null when opening the AssetViewer");
if (asset != null) _stackChildrenKeepAlive = ref.read(stackChildrenNotifier(asset).notifier).ref.keepAlive(); if (asset != null) _stackChildrenKeepAlive = ref.read(stackChildrenNotifier(asset).notifier).ref.keepAlive();
_reloadSubscription = EventStream.shared.listen(_onEvent);
WidgetsBinding.instance.addPostFrameCallback(_onAssetInit);
} }
@override @override
void dispose() { void dispose() {
pageController.dispose(); _pageController.dispose();
_preloader.dispose(); _preloader.dispose();
_reloadSubscription?.cancel(); _reloadSubscription?.cancel();
_stackChildrenKeepAlive?.close(); _stackChildrenKeepAlive?.close();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
super.dispose(); super.dispose();
} }
@@ -176,26 +174,26 @@ class _AssetViewerState extends ConsumerState<AssetViewer> {
void _onTimelineReloadEvent() { void _onTimelineReloadEvent() {
final timelineService = ref.read(timelineServiceProvider); final timelineService = ref.read(timelineServiceProvider);
_totalAssets = timelineService.totalAssets; final totalAssets = timelineService.totalAssets;
if (_totalAssets == 0) { if (totalAssets == 0) {
context.maybePop(); context.maybePop();
return; return;
} }
var index = pageController.page?.round() ?? 0; var index = _pageController.page?.round() ?? 0;
final currentAsset = ref.read(currentAssetNotifier); final currentAsset = ref.read(currentAssetNotifier);
if (currentAsset != null) { if (currentAsset != null) {
final newIndex = timelineService.getIndex(currentAsset.heroTag); final newIndex = timelineService.getIndex(currentAsset.heroTag);
if (newIndex != null && newIndex != index) { if (newIndex != null && newIndex != index) {
index = newIndex; index = newIndex;
pageController.jumpToPage(index); _pageController.jumpToPage(index);
} }
} }
if (index >= _totalAssets) { if (index >= totalAssets) {
index = _totalAssets - 1; index = totalAssets - 1;
pageController.jumpToPage(index); _pageController.jumpToPage(index);
} }
if (_assetReloadRequested) { if (_assetReloadRequested) {
@@ -264,15 +262,15 @@ class _AssetViewerState extends ConsumerState<AssetViewer> {
PhotoViewGestureDetectorScope( PhotoViewGestureDetectorScope(
axis: Axis.horizontal, axis: Axis.horizontal,
child: PageView.builder( child: PageView.builder(
controller: pageController, controller: _pageController,
physics: isZoomed physics: isZoomed
? const NeverScrollableScrollPhysics() ? const NeverScrollableScrollPhysics()
: CurrentPlatform.isIOS : CurrentPlatform.isIOS
? const FastScrollPhysics() ? const FastScrollPhysics()
: const FastClampingScrollPhysics(), : const FastClampingScrollPhysics(),
itemCount: _totalAssets, itemCount: ref.read(timelineServiceProvider).totalAssets,
onPageChanged: (index) => _onAssetChanged(index), onPageChanged: (index) => _onAssetChanged(index),
itemBuilder: (context, index) => AssetPage(index: index, heroOffset: heroOffset), itemBuilder: (context, index) => AssetPage(index: index, heroOffset: _heroOffset),
), ),
), ),
if (!CurrentPlatform.isIOS) if (!CurrentPlatform.isIOS)