Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add typed memory specs to memories cache

laurenspriem900556fed0f1
diff --git a/mobile/apps/photos/lib/models/memories/filler_memory.dart b/mobile/apps/photos/lib/models/memories/filler_memory.dart
index 176f948e51..835849252a 100644
--- a/mobile/apps/photos/lib/models/memories/filler_memory.dart
+++ b/mobile/apps/photos/lib/models/memories/filler_memory.dart
@@ -10,6 +10,7 @@ class FillerMemory extends SmartMemory {
this.yearsAgo,
int firstDateToShow,
int lastDateToShow, {
+ String? id,
super.firstCreationTime,
super.lastCreationTime,
}) : super(
@@ -18,6 +19,7 @@ class FillerMemory extends SmartMemory {
'filler',
firstDateToShow,
lastDateToShow,
+ id: id,
);
@override
diff --git a/mobile/apps/photos/lib/models/memories/memories_cache.dart b/mobile/apps/photos/lib/models/memories/memories_cache.dart
index 5774dfbb8e..10d4fca5e7 100644
--- a/mobile/apps/photos/lib/models/memories/memories_cache.dart
+++ b/mobile/apps/photos/lib/models/memories/memories_cache.dart
@@ -4,6 +4,8 @@ import "package:photos/models/base/id.dart";
import "package:photos/models/base_location.dart";
import "package:photos/models/location/location.dart";
import "package:photos/models/memories/clip_memory.dart";
+import "package:photos/models/memories/memory.dart";
+import "package:photos/models/memories/memory_spec.dart";
import "package:photos/models/memories/people_memory.dart";
import "package:photos/models/memories/smart_memory.dart";
import "package:photos/models/memories/smart_memory_constants.dart";
@@ -86,6 +88,7 @@ class ToShowMemory {
final PeopleMemoryType? peopleMemoryType;
final ClipMemoryType? clipMemoryType;
final Location? location;
+ final MemorySpec? spec;
bool get isOld {
final now = DateTime.now().microsecondsSinceEpoch;
@@ -116,8 +119,10 @@ class ToShowMemory {
this.peopleMemoryType,
this.clipMemoryType,
this.location,
+ this.spec,
}) : assert(
- (type == MemoryType.people &&
+ (spec != null) ||
+ (type == MemoryType.people &&
personID != null &&
peopleMemoryType != null) ||
(type == MemoryType.trips && location != null) ||
@@ -151,6 +156,7 @@ class ToShowMemory {
} else if (memory is ClipMemory) {
clipMemoryType = memory.clipMemoryType;
}
+ final spec = MemorySpec.fromSmartMemory(memory);
final fileUploadedIDs = memory.memories
.where((m) => m.file.uploadedFileID != null)
.map((m) => m.file.uploadedFileID!)
@@ -171,10 +177,14 @@ class ToShowMemory {
peopleMemoryType: peopleMemoryType,
clipMemoryType: clipMemoryType,
location: location,
+ spec: spec,
);
}
factory ToShowMemory.fromJson(Map<String, dynamic> json) {
+ final spec = MemorySpec.fromJson(
+ json['spec'] != null ? Map<String, dynamic>.from(json['spec']) : null,
+ );
return ToShowMemory(
json['title'],
List<int>.from(json['fileUploadedIDs']),
@@ -202,6 +212,7 @@ class ToShowMemory {
longitude: json['location']['longitude'],
)
: null,
+ spec: spec,
);
}
@@ -221,6 +232,7 @@ class ToShowMemory {
'isUnnamedCluster': isUnnamedCluster,
'peopleMemoryType': peopleMemoryType?.toString().split('.').last,
'clipMemoryType': clipMemoryType?.toString().split('.').last,
+ if (spec != null) 'spec': spec!.toJson(),
'location': location != null
? {
'latitude': location!.latitude!,
@@ -239,6 +251,43 @@ class ToShowMemory {
final jsonList = jsonDecode(jsonString) as List;
return jsonList.map((json) => ToShowMemory.fromJson(json)).toList();
}
+
+ bool get hasTypedSpec => spec != null;
+
+ SmartMemory toSmartMemory(List<Memory> memories) {
+ if (spec != null) {
+ return spec!.toSmartMemory(
+ memories,
+ firstDateToShow: firstTimeToShow,
+ lastDateToShow: lastTimeToShow,
+ title: title,
+ id: id,
+ );
+ }
+
+ if (type == MemoryType.people) {
+ return PeopleMemory(
+ memories,
+ firstTimeToShow,
+ lastTimeToShow,
+ peopleMemoryType!,
+ personID!,
+ personName,
+ isUnnamedCluster: isUnnamedCluster ?? false,
+ title: title,
+ id: id,
+ );
+ }
+
+ return SmartMemory(
+ memories,
+ type,
+ title,
+ firstTimeToShow,
+ lastTimeToShow,
+ id: id,
+ );
+ }
}
class PeopleShownLog {
diff --git a/mobile/apps/photos/lib/models/memories/memory_spec.dart b/mobile/apps/photos/lib/models/memories/memory_spec.dart
new file mode 100644
index 0000000000..36518ae7d8
--- /dev/null
+++ b/mobile/apps/photos/lib/models/memories/memory_spec.dart
@@ -0,0 +1,425 @@
+import "package:photos/models/location/location.dart";
+import "package:photos/models/memories/clip_memory.dart";
+import "package:photos/models/memories/filler_memory.dart";
+import "package:photos/models/memories/memory.dart";
+import "package:photos/models/memories/on_this_day_memory.dart";
+import "package:photos/models/memories/people_memory.dart";
+import "package:photos/models/memories/smart_memory.dart";
+import "package:photos/models/memories/time_memory.dart";
+import "package:photos/models/memories/trip_memory.dart";
+
+abstract class MemorySpec {
+ const MemorySpec();
+
+ String get kind;
+
+ MemoryType get type;
+
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ });
+
+ Map<String, dynamic> toJson();
+
+ static MemorySpec? fromSmartMemory(SmartMemory memory) {
+ if (memory is PeopleMemory) {
+ return PeopleMemorySpec(
+ personID: memory.personID,
+ personName: memory.personName,
+ isUnnamedCluster: memory.isUnnamedCluster,
+ isBirthday: memory.isBirthday,
+ newAge: memory.newAge,
+ peopleMemoryType: memory.peopleMemoryType,
+ activity: memory.activity,
+ );
+ }
+ if (memory is TripMemory) {
+ return TripMemorySpec(
+ location: memory.location,
+ locationName: memory.locationName,
+ tripYear: memory.tripYear,
+ );
+ }
+ if (memory is ClipMemory) {
+ return ClipMemorySpec(memory.clipMemoryType);
+ }
+ if (memory is TimeMemory) {
+ return TimeMemorySpec(
+ day: memory.day,
+ month: memory.month,
+ yearsAgo: memory.yearsAgo,
+ );
+ }
+ if (memory is FillerMemory) {
+ return FillerMemorySpec(memory.yearsAgo);
+ }
+ if (memory is OnThisDayMemory) {
+ return const OnThisDayMemorySpec();
+ }
+ return null;
+ }
+
+ static MemorySpec? fromJson(Map<String, dynamic>? json) {
+ if (json == null) return null;
+ final kind = json["kind"] as String?;
+ switch (kind) {
+ case PeopleMemorySpec.kindValue:
+ return PeopleMemorySpec.fromJson(json);
+ case TripMemorySpec.kindValue:
+ return TripMemorySpec.fromJson(json);
+ case ClipMemorySpec.kindValue:
+ return ClipMemorySpec.fromJson(json);
+ case TimeMemorySpec.kindValue:
+ return TimeMemorySpec.fromJson(json);
+ case FillerMemorySpec.kindValue:
+ return FillerMemorySpec.fromJson(json);
+ case OnThisDayMemorySpec.kindValue:
+ return const OnThisDayMemorySpec();
+ default:
+ return null;
+ }
+ }
+}
+
+class PeopleMemorySpec extends MemorySpec {
+ static const kindValue = "people";
+
+ final String personID;
+ final String? personName;
+ final bool isUnnamedCluster;
+ final bool? isBirthday;
+ final int? newAge;
+ final PeopleMemoryType peopleMemoryType;
+ final PeopleActivity? activity;
+
+ const PeopleMemorySpec({
+ required this.personID,
+ required this.personName,
+ required this.isUnnamedCluster,
+ required this.isBirthday,
+ required this.newAge,
+ required this.peopleMemoryType,
+ required this.activity,
+ });
+
+ factory PeopleMemorySpec.fromJson(Map<String, dynamic> json) {
+ return PeopleMemorySpec(
+ personID: json["personID"] as String,
+ personName: json["personName"] as String?,
+ isUnnamedCluster: json["isUnnamedCluster"] as bool? ?? false,
+ isBirthday: json["isBirthday"] as bool?,
+ newAge: json["newAge"] as int?,
+ peopleMemoryType: peopleMemoryTypeFromString(
+ json["peopleMemoryType"] as String,
+ ),
+ activity: json["activity"] != null
+ ? PeopleActivity.values.byName(json["activity"] as String)
+ : null,
+ );
+ }
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.people;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ return PeopleMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ peopleMemoryType,
+ personID,
+ personName,
+ title: title,
+ id: id,
+ activity: activity,
+ isUnnamedCluster: isUnnamedCluster,
+ isBirthday: isBirthday,
+ newAge: newAge,
+ );
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ "personID": personID,
+ "personName": personName,
+ "isUnnamedCluster": isUnnamedCluster,
+ "isBirthday": isBirthday,
+ "newAge": newAge,
+ "peopleMemoryType": peopleMemoryType.name,
+ "activity": activity?.name,
+ };
+ }
+}
+
+class TripMemorySpec extends MemorySpec {
+ static const kindValue = "trip";
+
+ final Location location;
+ final String? locationName;
+ final int? tripYear;
+
+ const TripMemorySpec({
+ required this.location,
+ required this.locationName,
+ required this.tripYear,
+ });
+
+ factory TripMemorySpec.fromJson(Map<String, dynamic> json) {
+ final location = Map<String, dynamic>.from(json["location"] as Map);
+ return TripMemorySpec(
+ location: Location(
+ latitude: location["latitude"] as double?,
+ longitude: location["longitude"] as double?,
+ ),
+ locationName: json["locationName"] as String?,
+ tripYear: json["tripYear"] as int?,
+ );
+ }
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.trips;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ final tripMemory = TripMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ location,
+ id: id,
+ locationName: locationName,
+ tripYear: tripYear,
+ );
+ tripMemory.title = title;
+ return tripMemory;
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ "location": {
+ "latitude": location.latitude,
+ "longitude": location.longitude,
+ },
+ "locationName": locationName,
+ "tripYear": tripYear,
+ };
+ }
+}
+
+class ClipMemorySpec extends MemorySpec {
+ static const kindValue = "clip";
+
+ final ClipMemoryType clipMemoryType;
+
+ const ClipMemorySpec(this.clipMemoryType);
+
+ factory ClipMemorySpec.fromJson(Map<String, dynamic> json) {
+ return ClipMemorySpec(
+ clipMemoryTypeFromString(json["clipMemoryType"] as String),
+ );
+ }
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.clip;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ final clipMemory = ClipMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ clipMemoryType,
+ );
+ clipMemory.title = title;
+ return clipMemory;
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ "clipMemoryType": clipMemoryType.name,
+ };
+ }
+}
+
+class TimeMemorySpec extends MemorySpec {
+ static const kindValue = "time";
+
+ final DateTime? day;
+ final DateTime? month;
+ final int? yearsAgo;
+
+ const TimeMemorySpec({
+ required this.day,
+ required this.month,
+ required this.yearsAgo,
+ });
+
+ factory TimeMemorySpec.fromJson(Map<String, dynamic> json) {
+ return TimeMemorySpec(
+ day: json["day"] != null
+ ? DateTime.fromMicrosecondsSinceEpoch(json["day"] as int)
+ : null,
+ month: json["month"] != null
+ ? DateTime.fromMicrosecondsSinceEpoch(json["month"] as int)
+ : null,
+ yearsAgo: json["yearsAgo"] as int?,
+ );
+ }
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.time;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ final timeMemory = TimeMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ id: id,
+ day: day,
+ month: month,
+ yearsAgo: yearsAgo,
+ );
+ timeMemory.title = title;
+ return timeMemory;
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ "day": day?.microsecondsSinceEpoch,
+ "month": month?.microsecondsSinceEpoch,
+ "yearsAgo": yearsAgo,
+ };
+ }
+}
+
+class FillerMemorySpec extends MemorySpec {
+ static const kindValue = "filler";
+
+ final int yearsAgo;
+
+ const FillerMemorySpec(this.yearsAgo);
+
+ factory FillerMemorySpec.fromJson(Map<String, dynamic> json) {
+ return FillerMemorySpec(json["yearsAgo"] as int);
+ }
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.filler;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ final fillerMemory = FillerMemory(
+ memories,
+ yearsAgo,
+ firstDateToShow,
+ lastDateToShow,
+ id: id,
+ );
+ fillerMemory.title = title;
+ return fillerMemory;
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ "yearsAgo": yearsAgo,
+ };
+ }
+}
+
+class OnThisDayMemorySpec extends MemorySpec {
+ static const kindValue = "onThisDay";
+
+ const OnThisDayMemorySpec();
+
+ @override
+ String get kind => kindValue;
+
+ @override
+ MemoryType get type => MemoryType.onThisDay;
+
+ @override
+ SmartMemory toSmartMemory(
+ List<Memory> memories, {
+ required int firstDateToShow,
+ required int lastDateToShow,
+ required String title,
+ required String id,
+ }) {
+ final onThisDayMemory = OnThisDayMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ id: id,
+ );
+ onThisDayMemory.title = title;
+ return onThisDayMemory;
+ }
+
+ @override
+ Map<String, dynamic> toJson() {
+ return {
+ "kind": kind,
+ };
+ }
+}
diff --git a/mobile/apps/photos/lib/models/memories/on_this_day_memory.dart b/mobile/apps/photos/lib/models/memories/on_this_day_memory.dart
index 544c11594b..1f421c760f 100644
--- a/mobile/apps/photos/lib/models/memories/on_this_day_memory.dart
+++ b/mobile/apps/photos/lib/models/memories/on_this_day_memory.dart
@@ -7,6 +7,7 @@ class OnThisDayMemory extends SmartMemory {
List<Memory> memories,
int firstDateToShow,
int lastDateToShow, {
+ String? id,
super.firstCreationTime,
super.lastCreationTime,
}) : super(
@@ -15,6 +16,7 @@ class OnThisDayMemory extends SmartMemory {
'',
firstDateToShow,
lastDateToShow,
+ id: id,
);
@override
diff --git a/mobile/apps/photos/lib/models/memories/time_memory.dart b/mobile/apps/photos/lib/models/memories/time_memory.dart
index 6b88e293f7..c5eaacea90 100644
--- a/mobile/apps/photos/lib/models/memories/time_memory.dart
+++ b/mobile/apps/photos/lib/models/memories/time_memory.dart
@@ -13,6 +13,7 @@ class TimeMemory extends SmartMemory {
List<Memory> memories,
int firstDateToShow,
int lastDateToShow, {
+ String? id,
this.day,
this.month,
this.yearsAgo,
@@ -24,6 +25,7 @@ class TimeMemory extends SmartMemory {
'',
firstDateToShow,
lastDateToShow,
+ id: id,
);
@override
diff --git a/mobile/apps/photos/lib/models/memories/trip_memory.dart b/mobile/apps/photos/lib/models/memories/trip_memory.dart
index 9dbab7f950..367fa8498a 100644
--- a/mobile/apps/photos/lib/models/memories/trip_memory.dart
+++ b/mobile/apps/photos/lib/models/memories/trip_memory.dart
@@ -15,6 +15,7 @@ class TripMemory extends SmartMemory {
int firstDateToShow,
int lastDateToShow,
this.location, {
+ String? id,
this.locationName,
this.tripYear,
super.firstCreationTime,
@@ -25,6 +26,7 @@ class TripMemory extends SmartMemory {
'',
firstDateToShow,
lastDateToShow,
+ id: id,
);
TripMemory copyWith({
diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dart
index aafc2e96e1..2868715d3a 100644
--- a/mobile/apps/photos/lib/services/memories_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart
@@ -14,6 +14,7 @@ import "package:photos/events/files_updated_event.dart";
import "package:photos/events/memories_changed_event.dart";
import "package:photos/events/memories_setting_changed.dart";
import "package:photos/events/memory_seen_event.dart";
+import "package:photos/l10n/l10n.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/memories/memories_cache.dart";
import "package:photos/models/memories/memory.dart";
@@ -378,6 +379,7 @@ class MemoriesCacheService {
try {
_logger.info('Processing disk cache memories to smart memories');
final List<SmartMemory> memories = [];
+ final List<(ToShowMemory, SmartMemory)> typedMemories = [];
final seenTimes = await (isOfflineMode
? MemoriesDB.offlineInstance
: MemoriesDB.instance)
@@ -426,79 +428,49 @@ class MemoriesCacheService {
memory.fileLocalIntIDs!.isNotEmpty;
final fileIds =
useLocalIntIds ? memory.fileLocalIntIDs! : memory.fileUploadedIDs;
- late final SmartMemory smartMemory;
- if (memory.type == MemoryType.people) {
- smartMemory = PeopleMemory(
- fileIds
- .where(
- (fileID) => useLocalIntIds
- ? true
- : uploadedIdToFile.containsKey(fileID),
- )
- .map(
- (fileID) {
- final file = useLocalIntIds
- ? _fileFromLocalIntId(
- fileID,
- localIdToFile,
- )
- : uploadedIdToFile[fileID];
- return file == null
- ? null
- : Memory.fromFile(
- file,
- seenTimes,
- seenTimeKey: useLocalIntIds ? fileID : null,
- );
- },
- )
- .whereType<Memory>()
- .toList(),
- memory.firstTimeToShow,
- memory.lastTimeToShow,
- memory.peopleMemoryType!,
- memory.personID!,
- memory.personName,
- isUnnamedCluster: memory.isUnnamedCluster ?? false,
- title: memory.title,
- id: memory.id,
- );
- } else {
- smartMemory = SmartMemory(
- fileIds
- .where(
- (fileID) => useLocalIntIds
- ? true
- : uploadedIdToFile.containsKey(fileID),
- )
- .map(
- (fileID) {
- final file = useLocalIntIds
- ? _fileFromLocalIntId(
- fileID,
- localIdToFile,
- )
- : uploadedIdToFile[fileID];
- return file == null
- ? null
- : Memory.fromFile(
- file,
- seenTimes,
- seenTimeKey: useLocalIntIds ? fileID : null,
- );
- },
- )
- .whereType<Memory>()
- .toList(),
- memory.type,
- memory.title,
- memory.firstTimeToShow,
- memory.lastTimeToShow,
- id: memory.id,
- );
- }
+ final hydratedMemories = fileIds
+ .where(
+ (fileID) => useLocalIntIds
+ ? true
+ : uploadedIdToFile.containsKey(fileID),
+ )
+ .map(
+ (fileID) {
+ final file = useLocalIntIds
+ ? _fileFromLocalIntId(
+ fileID,
+ localIdToFile,
+ )
+ : uploadedIdToFile[fileID];
+ return file == null
+ ? null
+ : Memory.fromFile(
+ file,
+ seenTimes,
+ seenTimeKey: useLocalIntIds ? fileID : null,
+ );
+ },
+ )
+ .whereType<Memory>()
+ .toList();
+ final smartMemory = memory.toSmartMemory(hydratedMemories);
if (smartMemory.memories.isNotEmpty) {
memories.add(smartMemory);
+ if (memory.hasTypedSpec) {
+ typedMemories.add((memory, smartMemory));
+ }
+ }
+ }
+ }
+ if (typedMemories.isNotEmpty) {
+ final locale = await getLocale();
+ final languageCode = locale?.languageCode ?? "en";
+ final s = await LanguageService.locals;
+ for (final typedMemory in typedMemories) {
+ try {
+ typedMemory.$2.title = typedMemory.$2.createTitle(s, languageCode);
+ } catch (_, __) {
+ typedMemory.$2.title = typedMemory.$1.title;
}
}
}