Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] memories cache first iteration

laurenspriema5cb78bf9557
diff --git a/mobile/lib/models/people_memory.dart b/mobile/lib/models/people_memory.dart
index 8b4dddde1e..ecb639137d 100644
--- a/mobile/lib/models/people_memory.dart
+++ b/mobile/lib/models/people_memory.dart
@@ -8,14 +8,23 @@ enum PeopleMemoryType {
lastTimeYouSawThem,
}
-enum PeopleActivity {
- party,
- hiking,
- feast,
- selfies,
- sports
+PeopleMemoryType peopleMemoryTypeFromString(String type) {
+ switch (type) {
+ case "youAndThem":
+ return PeopleMemoryType.youAndThem;
+ case "doingSomethingTogether":
+ return PeopleMemoryType.doingSomethingTogether;
+ case "spotlight":
+ return PeopleMemoryType.spotlight;
+ case "lastTimeYouSawThem":
+ return PeopleMemoryType.lastTimeYouSawThem;
+ default:
+ throw ArgumentError("Invalid people memory type: $type");
+ }
}
+enum PeopleActivity { party, hiking, feast, selfies, sports }
+
String activityQuery(PeopleActivity activity) {
switch (activity) {
case PeopleActivity.party:
diff --git a/mobile/lib/models/smart_memory.dart b/mobile/lib/models/smart_memory.dart
index fa5f2524a7..aa42e111f8 100644
--- a/mobile/lib/models/smart_memory.dart
+++ b/mobile/lib/models/smart_memory.dart
@@ -7,6 +7,21 @@ enum MemoryType {
filler,
}
+MemoryType memoryTypeFromString(String type) {
+ switch (type) {
+ case "people":
+ return MemoryType.people;
+ case "trips":
+ return MemoryType.trips;
+ case "time":
+ return MemoryType.time;
+ case "filler":
+ return MemoryType.filler;
+ default:
+ throw ArgumentError("Invalid memory type: $type");
+ }
+}
+
abstract class SmartMemory {
final List<Memory> memories;
final MemoryType type;
@@ -14,6 +29,11 @@ abstract class SmartMemory {
int? firstCreationTime;
int? lastCreationTime;
+ int? firstDateToShow;
+ int? lastDateToShow;
+ // TODO: lau: actually use this in calculated filters
+
+
SmartMemory(
this.memories,
this.type, {
@@ -21,6 +41,7 @@ abstract class SmartMemory {
this.firstCreationTime,
this.lastCreationTime,
}) : name = name != null ? name + "(I)" : null;
+ // TODO: lau: remove (I) from name when opening up the feature flag
int averageCreationTime() {
if (firstCreationTime != null && lastCreationTime != null) {
diff --git a/mobile/lib/service_locator.dart b/mobile/lib/service_locator.dart
index d33bef747e..037e7c9a7e 100644
--- a/mobile/lib/service_locator.dart
+++ b/mobile/lib/service_locator.dart
@@ -10,6 +10,7 @@ import "package:photos/services/location_service.dart";
import "package:photos/services/machine_learning/face_ml/face_recognition_service.dart";
import "package:photos/services/machine_learning/machine_learning_controller.dart";
import "package:photos/services/magic_cache_service.dart";
+import "package:photos/services/memories_cache_service.dart";
import "package:photos/services/storage_bonus_service.dart";
import "package:photos/services/trash_sync_service.dart";
import "package:photos/services/update_service.dart";
@@ -118,6 +119,14 @@ MagicCacheService get magicCacheService {
return _magicCacheService!;
}
+MemoriesCacheService? _memoriesCacheService;
+MemoriesCacheService get memoriesCacheService {
+ _memoriesCacheService ??= MemoriesCacheService(
+ ServiceLocator.instance.prefs,
+ );
+ return _memoriesCacheService!;
+}
+
BillingService? _billingService;
BillingService get billingService {
_billingService ??= BillingService(
diff --git a/mobile/lib/services/machine_learning/ml_service.dart b/mobile/lib/services/machine_learning/ml_service.dart
index d6dcdb3bfc..df8a11163e 100644
--- a/mobile/lib/services/machine_learning/ml_service.dart
+++ b/mobile/lib/services/machine_learning/ml_service.dart
@@ -143,6 +143,8 @@ class MLService {
if (_mlControllerStatus == true) {
// refresh discover section
magicCacheService.updateCache(forced: force).ignore();
+ // refresh memories section
+ memoriesCacheService.updateCache(forced: force).ignore();
}
if (canFetch()) {
await fetchAndIndexAllImages();
@@ -153,6 +155,8 @@ class MLService {
if (_mlControllerStatus == true) {
// refresh discover section
magicCacheService.updateCache().ignore();
+ // refresh memories section
+ memoriesCacheService.updateCache(forced: force).ignore();
}
} catch (e, s) {
_logger.severe("runAllML failed", e, s);
diff --git a/mobile/lib/services/memories_cache_service.dart b/mobile/lib/services/memories_cache_service.dart
new file mode 100644
index 0000000000..9517fcb4b7
--- /dev/null
+++ b/mobile/lib/services/memories_cache_service.dart
@@ -0,0 +1,365 @@
+import "dart:convert";
+import "dart:io" show File;
+
+import "package:flutter/foundation.dart" show kDebugMode;
+import "package:logging/logging.dart";
+import "package:path_provider/path_provider.dart";
+import "package:photos/extensions/stop_watch.dart";
+import "package:photos/models/location/location.dart";
+import "package:photos/models/people_memory.dart";
+import "package:photos/models/smart_memory.dart";
+import "package:photos/models/trip_memory.dart";
+import "package:photos/services/location_service.dart";
+import "package:photos/services/smart_memories_service.dart";
+import "package:shared_preferences/shared_preferences.dart";
+
+class MemoriesCache {
+ final List<ToShowMemory> toShowMemories;
+ final List<PeopleShownLogs> peopleShownLogs;
+ final List<TripsShownLogs> tripsShownLogs;
+
+ MemoriesCache({
+ required this.toShowMemories,
+ required this.peopleShownLogs,
+ required this.tripsShownLogs,
+ });
+
+ factory MemoriesCache.fromJson(Map<String, dynamic> json) {
+ return MemoriesCache(
+ toShowMemories: ToShowMemory.decodeJsonToList(json['toShowMemories']),
+ peopleShownLogs:
+ PeopleShownLogs.decodeJsonToList(json['peopleShownLogs']),
+ tripsShownLogs: TripsShownLogs.decodeJsonToList(json['tripsShownLogs']),
+ );
+ }
+
+ Map<String, dynamic> toJson() {
+ return {
+ 'toShowMemories': ToShowMemory.encodeListToJson(toShowMemories),
+ 'peopleShownLogs': PeopleShownLogs.encodeListToJson(peopleShownLogs),
+ 'tripsShownLogs': TripsShownLogs.encodeListToJson(tripsShownLogs),
+ };
+ }
+
+ static String encodeToJsonString(MemoriesCache cache) {
+ return jsonEncode(cache.toJson());
+ }
+
+ static MemoriesCache decodeFromJsonString(String jsonString) {
+ return MemoriesCache.fromJson(jsonDecode(jsonString));
+ }
+}
+
+class ToShowMemory {
+ final String title;
+ final List<int> fileUploadedIDs;
+ final MemoryType type;
+ final int firstTimeToShow;
+ final int lastTimeToShow;
+
+ bool get shouldShowNow {
+ final now = DateTime.now().microsecondsSinceEpoch;
+ return now >= firstTimeToShow && now <= lastTimeToShow;
+ }
+
+ ToShowMemory(
+ this.title,
+ this.fileUploadedIDs,
+ this.type,
+ this.firstTimeToShow,
+ this.lastTimeToShow,
+ );
+
+ factory ToShowMemory.fromJson(Map<String, dynamic> json) {
+ return ToShowMemory(
+ json['title'],
+ List<int>.from(json['fileUploadedIDs']),
+ memoryTypeFromString(json['type']),
+ json['firstTimeToShow'],
+ json['lastTimeToShow'],
+ );
+ }
+
+ Map<String, dynamic> toJson() {
+ return {
+ 'title': title,
+ 'fileUploadedIDs': fileUploadedIDs.toList(),
+ 'type': type.toString().split('.').last,
+ 'firstTimeToShow': firstTimeToShow,
+ 'lastTimeToShow': lastTimeToShow,
+ };
+ }
+
+ static String encodeListToJson(List<ToShowMemory> toShowMemories) {
+ final jsonList = toShowMemories.map((memory) => memory.toJson()).toList();
+ return jsonEncode(jsonList);
+ }
+
+ static List<ToShowMemory> decodeJsonToList(String jsonString) {
+ final jsonList = jsonDecode(jsonString) as List;
+ return jsonList.map((json) => ToShowMemory.fromJson(json)).toList();
+ }
+}
+
+class PeopleShownLogs {
+ final String personID;
+ final PeopleMemoryType peopleMemoryType;
+ final int lastTimeShown;
+
+ PeopleShownLogs(
+ this.personID,
+ this.peopleMemoryType,
+ this.lastTimeShown,
+ );
+
+ factory PeopleShownLogs.fromJson(Map<String, dynamic> json) {
+ return PeopleShownLogs(
+ json['personID'],
+ peopleMemoryTypeFromString(json['peopleMemoryType']),
+ json['lastTimeShown'],
+ );
+ }
+
+ Map<String, dynamic> toJson() {
+ return {
+ 'personID': personID,
+ 'peopleMemoryType': peopleMemoryType.toString().split('.').last,
+ 'lastTimeShown': lastTimeShown,
+ };
+ }
+
+ static String encodeListToJson(List<PeopleShownLogs> shownLogs) {
+ final jsonList = shownLogs.map((log) => log.toJson()).toList();
+ return jsonEncode(jsonList);
+ }
+
+ static List<PeopleShownLogs> decodeJsonToList(String jsonString) {
+ final jsonList = jsonDecode(jsonString) as List;
+ return jsonList.map((json) => PeopleShownLogs.fromJson(json)).toList();
+ }
+}
+
+class TripsShownLogs {
+ final Location location;
+ final int lastTimeShown;
+
+ TripsShownLogs(
+ this.location,
+ this.lastTimeShown,
+ );
+
+ factory TripsShownLogs.fromJson(Map<String, dynamic> json) {
+ return TripsShownLogs(
+ Location(
+ latitude: json['location']['latitude'],
+ longitude: json['location']['longitude'],
+ ),
+ json['lastTimeShown'],
+ );
+ }
+
+ Map<String, dynamic> toJson() {
+ return {
+ 'location': {
+ 'latitude': location.latitude!,
+ 'longitude': location.longitude!,
+ },
+ 'lastTimeShown': lastTimeShown,
+ };
+ }
+
+ static String encodeListToJson(List<TripsShownLogs> shownLogs) {
+ final jsonList = shownLogs.map((log) => log.toJson()).toList();
+ return jsonEncode(jsonList);
+ }
+
+ static List<TripsShownLogs> decodeJsonToList(String jsonString) {
+ final jsonList = jsonDecode(jsonString) as List;
+ return jsonList.map((json) => TripsShownLogs.fromJson(json)).toList();
+ }
+}
+
+class MemoriesCacheService {
+ static const _lastMemoriesCacheUpdateTimeKey = "lastMemoriesCacheUpdateTime";
+
+ /// Delay is for cache update to be done not during app init, during which a
+ /// lot of other things are happening.
+ static const _kCacheUpdateDelay = Duration(seconds: 10);
+
+ static const _kUpdateFrequency = Duration(days: 7);
+
+ final SharedPreferences _prefs;
+ late final Logger _logger = Logger("MemoriesCacheService");
+
+ Future<MemoriesCache?>? _memoriesCacheFuture;
+ bool _shouldUpdate = false;
+ bool _isUpdateInProgress = false;
+
+ MemoriesCacheService(this._prefs) {
+ _logger.fine("MemoriesCacheService constructor");
+ Future.delayed(_kCacheUpdateDelay, () {
+ _updateCacheIfTheTimeHasCome();
+ });
+ }
+
+ Future<void> _resetLastMemoriesCacheUpdateTime() async {
+ await _prefs.setInt(
+ _lastMemoriesCacheUpdateTimeKey,
+ DateTime.now().microsecondsSinceEpoch,
+ );
+ }
+
+ int get lastMemoriesCacheUpdateTime {
+ return _prefs.getInt(_lastMemoriesCacheUpdateTimeKey) ?? 0;
+ }
+
+ Future<void> _updateCacheIfTheTimeHasCome() async {
+ if (lastMemoriesCacheUpdateTime <
+ DateTime.now().subtract(_kUpdateFrequency).microsecondsSinceEpoch) {
+ _shouldUpdate = true;
+ }
+ }
+
+ Future<String> _getCachePath() async {
+ return (await getApplicationSupportDirectory()).path +
+ "/cache//test1/memories_cache";
+ // TODO: lau: remove the test1 directory after testing
+ }
+
+ Future<void> updateCache({bool forced = false}) async {
+ if (!SmartMemoriesService.instance.enableSmartMemories) {
+ return;
+ }
+ try {
+ if ((!_shouldUpdate && !forced) || _isUpdateInProgress) {
+ _logger.info(
+ "No update needed as shouldUpdate: $_shouldUpdate, forced: $forced and isUpdateInProgress $_isUpdateInProgress",
+ );
+ return;
+ }
+ _logger.info("updating memories cache");
+ _isUpdateInProgress = true;
+ final EnteWatch? w = kDebugMode ? EnteWatch("memoriesCacheWatch") : null;
+ w?.start();
+ final memories = await SmartMemoriesService.instance.getMemories(null);
+ w?.log("calculated new memories");
+ final oldCache = await getMemoriesCache();
+ w?.log("gotten old cache");
+ final MemoriesCache memoryCache =
+ getCacheFromMemories(memories, oldCache);
+ w?.log("gotten cache from memories");
+ final file = File(await _getCachePath());
+ if (!file.existsSync()) {
+ file.createSync(recursive: true);
+ }
+ _memoriesCacheFuture = Future.value(memoryCache);
+ await file.writeAsBytes(
+ MemoriesCache.encodeToJsonString(memoryCache).codeUnits,
+ );
+ w?.log("cacheWritten");
+ await _resetLastMemoriesCacheUpdateTime();
+ w?.logAndReset('done');
+ _shouldUpdate = false;
+ } catch (e, s) {
+ _logger.info("Error updating memories cache", e, s);
+ } finally {
+ _isUpdateInProgress = false;
+ }
+ }
+
+ MemoriesCache getCacheFromMemories(
+ List<SmartMemory> memories,
+ MemoriesCache? oldCache,
+ ) {
+ final List<ToShowMemory> toShowMemories = [];
+ final List<PeopleShownLogs> peopleShownLogs = [];
+ final List<TripsShownLogs> tripsShownLogs = [];
+ final now = DateTime.now().microsecondsSinceEpoch;
+ for (final memory in memories) {
+ if (memory.lastDateToShow != null && memory.lastDateToShow! < now) {
+ if (memory is PeopleMemory) {
+ peopleShownLogs.add(
+ PeopleShownLogs(
+ memory.personID,
+ memory.peopleMemoryType,
+ memory.lastDateToShow!,
+ ),
+ );
+ } else if (memory is TripMemory) {
+ tripsShownLogs.add(
+ TripsShownLogs(
+ memory.location,
+ memory.lastDateToShow!,
+ ),
+ );
+ }
+ } else {
+ if (memory.firstDateToShow != null && memory.lastDateToShow != null) {
+ toShowMemories.add(
+ ToShowMemory(
+ memory.name!,
+ memory.memories.map((m) => m.file.uploadedFileID!).toList(),
+ memory.type,
+ memory.firstDateToShow!,
+ memory.lastDateToShow!,
+ ),
+ );
+ } else {
+ _logger.severe('Memory has no first or last date to show');
+ }
+ }
+ }
+ if (oldCache != null) {
+ for (final shownPerson in oldCache.peopleShownLogs) {
+ if (peopleShownLogs.any(
+ (person) =>
+ (person.personID == shownPerson.personID) &&
+ (person.peopleMemoryType == shownPerson.peopleMemoryType),
+ )) {
+ continue;
+ }
+ peopleShownLogs.add(shownPerson);
+ }
+ for (final shownTrip in oldCache.tripsShownLogs) {
+ if (tripsShownLogs.any(
+ (trip) => isFileInsideLocationTag(
+ shownTrip.location,
+ trip.location,
+ 10.0,
+ ),
+ )) {
+ continue;
+ }
+ tripsShownLogs.add(shownTrip);
+ }
+ }
+ return MemoriesCache(
+ toShowMemories: toShowMemories,
+ peopleShownLogs: peopleShownLogs,
+ tripsShownLogs: tripsShownLogs,
+ );
+ }
+
+ Future<MemoriesCache?> getMemoriesCache() async {
+ if (_memoriesCacheFuture != null) {
+ return _memoriesCacheFuture!;
+ }
+ _memoriesCacheFuture = _readResultFromDisk();
+ return _memoriesCacheFuture!;
+ }
+
+ Future<MemoriesCache?> _readResultFromDisk() async {
+ _logger.info("Reading memories cache result from disk");
+ final file = File(await _getCachePath());
+ if (!file.existsSync()) {
+ _logger.info("No memories cache found");
+ return null;
+ }
+ final jsonString = file.readAsStringSync();
+ return MemoriesCache.decodeFromJsonString(jsonString);
+ }
+
+ Future<void> clearMemoriesCache() async {
+ await File(await _getCachePath()).delete();
+ }
+}
diff --git a/mobile/lib/services/smart_memories_service.dart b/mobile/lib/services/smart_memories_service.dart
index 97e960bb75..9f5ad6558f 100644
--- a/mobile/lib/services/smart_memories_service.dart
+++ b/mobile/lib/services/smart_memories_service.dart
@@ -1091,7 +1091,8 @@ class SmartMemoriesService {
int offset = 0;
for (int i = 0; i < numBuckets; i++) {
final int bucketSize = quotient + (i < remainder ? 1 : 0);
- timeBuckets.add(sortedTimeMemories.sublist(offset, offset + bucketSize));
+ timeBuckets
+ .add(sortedTimeMemories.sublist(offset, offset + bucketSize));
offset += bucketSize;
}
@@ -1163,7 +1164,8 @@ class SmartMemoriesService {
.sort((a, b) => b.file.creationTime!.compareTo(a.file.creationTime!));
_logger.finest(
- 'People memories selection done, returning ${finalSelection.length} memories',);
+ 'People memories selection done, returning ${finalSelection.length} memories',
+ );
return finalSelection;
} catch (e, s) {
_logger.severe('Error in _bestSelectionPeople', e, s);
diff --git a/mobile/lib/ui/home/memories/memories_widget.dart b/mobile/lib/ui/home/memories/memories_widget.dart
index 655f4896c5..76a74a0e72 100644
--- a/mobile/lib/ui/home/memories/memories_widget.dart
+++ b/mobile/lib/ui/home/memories/memories_widget.dart
@@ -6,7 +6,6 @@ import "package:photos/core/event_bus.dart";
import "package:photos/events/memories_setting_changed.dart";
import 'package:photos/models/memory.dart';
import "package:photos/models/smart_memory.dart";
-import "package:photos/service_locator.dart";
import 'package:photos/services/memories_service.dart';
import "package:photos/services/smart_memories_service.dart";
import "package:photos/ui/common/loading_widget.dart";
diff --git a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
index d89ee548a1..ad5f85ce15 100644
--- a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
+++ b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
@@ -285,6 +285,23 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {
},
),
sectionOptionSpacing,
+ MenuItemWidget(
+ captionedTextWidget: const CaptionedTextWidget(
+ title: "Update memories",
+ ),
+ pressedColor: getEnteColorScheme(context).fillFaint,
+ trailingIcon: Icons.chevron_right_outlined,
+ trailingIconIsMuted: true,
+ onTap: () async {
+ try {
+ await memoriesCacheService.updateCache(forced: true);
+ } catch (e, s) {
+ logger.warning('Update memories failed', e, s);
+ await showGenericErrorDialog(context: context, error: e);
+ }
+ },
+ ),
+ sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Sync person mappings ",