Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove ML based memories when ML gets disabled

laurenspriem4b7d638458da
diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dart
index 3f6a05f055..f5c2a499df 100644
--- a/mobile/apps/photos/lib/services/memories_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart
@@ -250,6 +250,82 @@ class MemoriesCacheService {
unawaited(_prefs.setBool(_shouldUpdateKey, true));
}
+ Future<void> purgeMlOnlyMemoriesFromCache() async {
+ await _memoriesUpdateLock.synchronized(() async {
+ bool cacheChanged = false;
+ final removedPersonIDs = <String>{};
+
+ if (_cachedMemories != null && _cachedMemories!.isNotEmpty) {
+ final filtered = <SmartMemory>[];
+ for (final memory in _cachedMemories!) {
+ if (memory.type == MemoryType.people ||
+ memory.type == MemoryType.clip) {
+ if (memory is PeopleMemory) {
+ removedPersonIDs.add(memory.personID);
+ }
+ continue;
+ }
+ filtered.add(memory);
+ }
+ if (filtered.length != _cachedMemories!.length) {
+ _cachedMemories = filtered;
+ cacheChanged = true;
+ }
+ }
+
+ final cache = await _readCacheFromDisk();
+ if (cache != null) {
+ final originalToShowLength = cache.toShowMemories.length;
+ final activeRemovedPeopleLogs = <PeopleShownLog>[];
+ final activeRemovedClipLogs = <ClipShownLog>[];
+
+ for (final memory in cache.toShowMemories) {
+ if (memory.type == MemoryType.people) {
+ if (memory.personID != null) {
+ removedPersonIDs.add(memory.personID!);
+ }
+ if (memory.shouldShowNow()) {
+ activeRemovedPeopleLogs.add(
+ PeopleShownLog.fromOldCacheMemory(memory),
+ );
+ }
+ } else if (memory.type == MemoryType.clip && memory.shouldShowNow()) {
+ activeRemovedClipLogs.add(
+ ClipShownLog.fromOldCacheMemory(memory),
+ );
+ }
+ }
+
+ cache.toShowMemories.removeWhere(
+ (memory) =>
+ memory.type == MemoryType.people ||
+ memory.type == MemoryType.clip,
+ );
+ if (cache.toShowMemories.length != originalToShowLength) {
+ cache.peopleShownLogs.addAll(activeRemovedPeopleLogs);
+ cache.clipShownLogs.addAll(activeRemovedClipLogs);
+ await writeToJsonFile<MemoriesCache>(
+ await _getCachePath(),
+ cache,
+ MemoriesCache.encodeToJsonString,
+ );
+ cacheChanged = true;
+ }
+ }
+
+ for (final personID in removedPersonIDs) {
+ await NotificationService.instance.clearAllScheduledNotifications(
+ containingPayload: personID,
+ logLines: false,
+ );
+ }
+
+ if (cacheChanged) {
+ Bus.instance.fire(MemoriesChangedEvent());
+ }
+ });
+ }
+
Future<void> purgePersonFromMemoriesCache(String personID) async {
await _memoriesUpdateLock.synchronized(() async {
final removedMemoryIDs = <String>{};
diff --git a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
index fe2bcfd41a..6f51c1d31e 100644
--- a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
+++ b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
@@ -228,8 +228,10 @@ class _MachineLearningSettingsPageState
Future<void> toggleMlConsent() async {
final oldMlConsent = hasGrantedMLConsent;
+ final oldMlEnabled = oldMlConsent && localSettings.isMLLocalIndexingEnabled;
final mlConsent = !oldMlConsent;
await setMLConsent(mlConsent);
+ final newMlEnabled = mlConsent && localSettings.isMLLocalIndexingEnabled;
// Queue a memories cache refresh so People/Clip memories appear or
// disappear on the next scheduled recompute. We intentionally only queue
// here β€” the actual recompute will be picked up by the next updateCache
@@ -241,6 +243,9 @@ class _MachineLearningSettingsPageState
unawaited(
MLIndexingIsolate.instance.cleanupLocalIndexingModels(),
);
+ if (oldMlEnabled && !newMlEnabled) {
+ await memoriesCacheService.purgeMlOnlyMemoriesFromCache();
+ }
} else {
await MLService.instance.init();
await SemanticSearchService.instance.init();
@@ -362,7 +367,10 @@ class _MachineLearningSettingsPageState
trailingWidget: ToggleSwitchWidget(
value: () => localSettings.isMLLocalIndexingEnabled,
onChanged: () async {
+ final oldMlEnabled =
+ hasGrantedMLConsent && localSettings.isMLLocalIndexingEnabled;
final localIndexing = await localSettings.toggleLocalMLIndexing();
+ final newMlEnabled = hasGrantedMLConsent && localIndexing;
memoriesCacheService.queueUpdateCache();
Bus.instance.fire(NotificationEvent());
if (localIndexing) {
@@ -372,6 +380,9 @@ class _MachineLearningSettingsPageState
unawaited(
MLIndexingIsolate.instance.cleanupLocalIndexingModels(),
);
+ if (oldMlEnabled && !newMlEnabled) {
+ await memoriesCacheService.purgeMlOnlyMemoriesFromCache();
+ }
}
if (mounted) {