Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Refresh ML caches on resume

Prateek Sunal8774dae5d6ac
diff --git a/mobile/apps/photos/lib/app.dart b/mobile/apps/photos/lib/app.dart
index 21f7acdb15..aba650356b 100644
--- a/mobile/apps/photos/lib/app.dart
+++ b/mobile/apps/photos/lib/app.dart
@@ -194,11 +194,33 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
void didChangeAppLifecycleState(AppLifecycleState state) {
final String stateChangeReason = 'app -> $state';
if (state == AppLifecycleState.resumed) {
+ final lastAppOpenTime = AppLifecycleService.instance.getLastAppOpenTime();
AppLifecycleService.instance
.onAppInForeground(stateChangeReason + ': sync now');
+ unawaited(_reloadCachesUpdatedInBackground(lastAppOpenTime));
SyncService.instance.sync();
} else {
AppLifecycleService.instance.onAppInBackground(stateChangeReason);
}
}
+
+ Future<void> _reloadCachesUpdatedInBackground(
+ int lastAppOpenTimeInMicroseconds,
+ ) async {
+ await ServiceLocator.instance.prefs.reload();
+
+ final futures = <Future<void>>[];
+ if (magicCacheService.lastMagicCacheUpdateTimeInMicroseconds >
+ lastAppOpenTimeInMicroseconds) {
+ futures.add(magicCacheService.updateCache(forced: true));
+ }
+ if (memoriesCacheService.lastMemoriesCacheUpdateTime >
+ lastAppOpenTimeInMicroseconds) {
+ futures.add(memoriesCacheService.updateCache(forced: true));
+ }
+ if (futures.isEmpty) {
+ return;
+ }
+ await Future.wait(futures);
+ }
}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index b70158fef5..582c28eb03 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -234,12 +234,10 @@ class MLService {
_logger.info("App mode changed during ML run, stopping");
return;
}
- if (!isProcessBg) {
- // refresh discover section
- magicCacheService.updateCache().ignore();
- // refresh memories section (only runs if forced is true)
- memoriesCacheService.updateCache(forced: force).ignore();
- }
+ // Persist refreshed caches after ML so foreground can pick them up
+ // on the next resume, even when the work ran headlessly in background.
+ magicCacheService.updateCache(forced: force).ignore();
+ memoriesCacheService.updateCache(forced: force).ignore();
}
} catch (e, s) {
_logger.severe("runAllML failed", e, s);
diff --git a/mobile/apps/photos/lib/services/magic_cache_service.dart b/mobile/apps/photos/lib/services/magic_cache_service.dart
index c98fbe80be..64d9e2704c 100644
--- a/mobile/apps/photos/lib/services/magic_cache_service.dart
+++ b/mobile/apps/photos/lib/services/magic_cache_service.dart
@@ -230,6 +230,10 @@ class MagicCacheService {
return _prefs.getInt(_lastMagicCacheUpdateKey) ?? 0;
}
+ int get lastMagicCacheUpdateTimeInMicroseconds {
+ return lastMagicCacheUpdateTime * 1000;
+ }
+
bool get enableDiscover => hasGrantedMLConsent;
void queueUpdate(String reason) {