mirror of
https://github.com/immich-app/immich.git
synced 2025-03-01 15:11:21 +01:00
feat(mobile): Adds better precaching for assets in gallery view and memory lane (#7486)
Adds better precaching for assets in gallery view and memory lane
This commit is contained in:
parent
d28abaad7b
commit
b1a896ba61
2 changed files with 30 additions and 7 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'dart:ui' as ui;
|
import 'dart:ui' as ui;
|
||||||
|
@ -132,7 +133,7 @@ class GalleryViewerPage extends HookConsumerWidget {
|
||||||
void toggleFavorite(Asset asset) =>
|
void toggleFavorite(Asset asset) =>
|
||||||
ref.read(assetProvider.notifier).toggleFavorite([asset]);
|
ref.read(assetProvider.notifier).toggleFavorite([asset]);
|
||||||
|
|
||||||
void precacheNextImage(int index) {
|
Future<void> precacheNextImage(int index) async {
|
||||||
void onError(Object exception, StackTrace? stackTrace) {
|
void onError(Object exception, StackTrace? stackTrace) {
|
||||||
// swallow error silently
|
// swallow error silently
|
||||||
debugPrint('Error precaching next image: $exception, $stackTrace');
|
debugPrint('Error precaching next image: $exception, $stackTrace');
|
||||||
|
@ -140,7 +141,7 @@ class GalleryViewerPage extends HookConsumerWidget {
|
||||||
|
|
||||||
if (index < totalAssets && index >= 0) {
|
if (index < totalAssets && index >= 0) {
|
||||||
final asset = loadAsset(index);
|
final asset = loadAsset(index);
|
||||||
precacheImage(
|
await precacheImage(
|
||||||
ImmichImage.imageProvider(asset: asset),
|
ImmichImage.imageProvider(asset: asset),
|
||||||
context,
|
context,
|
||||||
onError: onError,
|
onError: onError,
|
||||||
|
@ -711,6 +712,21 @@ class GalleryViewerPage extends HookConsumerWidget {
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() {
|
||||||
|
// No need to await this
|
||||||
|
unawaited(
|
||||||
|
// Delay this a bit so we can finish loading the page
|
||||||
|
Future.delayed(const Duration(milliseconds: 400)).then(
|
||||||
|
// Precache the next image
|
||||||
|
(_) => precacheNextImage(currentIndex.value + 1),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
ref.listen(showControlsProvider, (_, show) {
|
ref.listen(showControlsProvider, (_, show) {
|
||||||
if (show) {
|
if (show) {
|
||||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||||
|
@ -761,12 +777,16 @@ class GalleryViewerPage extends HookConsumerWidget {
|
||||||
),
|
),
|
||||||
itemCount: totalAssets,
|
itemCount: totalAssets,
|
||||||
scrollDirection: Axis.horizontal,
|
scrollDirection: Axis.horizontal,
|
||||||
onPageChanged: (value) {
|
onPageChanged: (value) async {
|
||||||
final next = currentIndex.value < value ? value + 1 : value - 1;
|
final next = currentIndex.value < value ? value + 1 : value - 1;
|
||||||
precacheNextImage(next);
|
HapticFeedback.selectionClick();
|
||||||
currentIndex.value = value;
|
currentIndex.value = value;
|
||||||
stackIndex.value = -1;
|
stackIndex.value = -1;
|
||||||
HapticFeedback.selectionClick();
|
|
||||||
|
// Wait for page change animation to finish
|
||||||
|
await Future.delayed(const Duration(milliseconds: 400));
|
||||||
|
// Then precache the next image
|
||||||
|
unawaited(precacheNextImage(next));
|
||||||
},
|
},
|
||||||
builder: (context, index) {
|
builder: (context, index) {
|
||||||
final a =
|
final a =
|
||||||
|
|
|
@ -124,11 +124,14 @@ class MemoryPage extends HookConsumerWidget {
|
||||||
.then((_) => precacheAsset(1));
|
.then((_) => precacheAsset(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
onAssetChanged(int otherIndex) {
|
Future<void> onAssetChanged(int otherIndex) async {
|
||||||
HapticFeedback.selectionClick();
|
HapticFeedback.selectionClick();
|
||||||
currentAssetPage.value = otherIndex;
|
currentAssetPage.value = otherIndex;
|
||||||
precacheAsset(otherIndex + 1);
|
|
||||||
updateProgressText();
|
updateProgressText();
|
||||||
|
// Wait for page change animation to finish
|
||||||
|
await Future.delayed(const Duration(milliseconds: 400));
|
||||||
|
// And then precache the next asset
|
||||||
|
await precacheAsset(otherIndex + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Notification listener is used instead of OnPageChanged callback since OnPageChanged is called
|
/* Notification listener is used instead of OnPageChanged callback since OnPageChanged is called
|
||||||
|
|
Loading…
Add table
Reference in a new issue