fix(mobile): reset asset index on timeline refresh (#25729)

The current asset changes when the timeline refreshes, which can be
quite jarring. Assets are tracked by their index, and that index becomes
stale when the timeline refreshes. This can be resolved by updating the
index of asset based on a unique identifier (like the hero tag).
This commit is contained in:
Thomas 2026-02-05 17:46:38 +00:00 committed by GitHub
parent 3c77c724c5
commit a42c08ed84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 5 deletions

View file

@ -227,6 +227,13 @@ class TimelineService {
return _buffer.elementAt(index - _bufferOffset);
}
/// Finds the index of an asset by its heroTag within the current buffer.
/// Returns null if the asset is not found in the buffer.
int? getIndex(String heroTag) {
final index = _buffer.indexWhere((a) => a.heroTag == heroTag);
return index >= 0 ? _bufferOffset + index : null;
}
Future<void> dispose() async {
await _bucketSubscription?.cancel();
_bucketSubscription = null;

View file

@ -451,21 +451,31 @@ class _AssetViewerState extends ConsumerState<AssetViewer> {
}
void _onTimelineReloadEvent() {
totalAssets = ref.read(timelineServiceProvider).totalAssets;
final timelineService = ref.read(timelineServiceProvider);
totalAssets = timelineService.totalAssets;
if (totalAssets == 0) {
context.maybePop();
return;
}
var index = pageController.page?.round() ?? 0;
final currentAsset = ref.read(currentAssetNotifier);
if (currentAsset != null) {
final newIndex = timelineService.getIndex(currentAsset.heroTag);
if (newIndex != null && newIndex != index) {
index = newIndex;
pageController.jumpToPage(index);
}
}
if (assetReloadRequested) {
assetReloadRequested = false;
_onAssetReloadEvent();
return;
_onAssetReloadEvent(index);
}
}
void _onAssetReloadEvent() async {
final index = pageController.page?.round() ?? 0;
void _onAssetReloadEvent(int index) async {
final timelineService = ref.read(timelineServiceProvider);
final newAsset = await timelineService.getAssetAsync(index);