Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Separate memory types

laurensprieme13b88881e48
diff --git a/mobile/lib/models/smart_memory.dart b/mobile/lib/models/smart_memory.dart
new file mode 100644
index 0000000000..a1e0ce0ebf
--- /dev/null
+++ b/mobile/lib/models/smart_memory.dart
@@ -0,0 +1,45 @@
+import "package:photos/models/file/file.dart";
+
+enum MemoryType {
+ people,
+ trips,
+ time,
+ filler,
+}
+
+abstract class SmartMemory {
+ final List<EnteFile> files;
+ final MemoryType type;
+ int? firstCreationTime;
+ int? lastCreationTime;
+
+ SmartMemory(
+ this.files,
+ this.type, {
+ this.firstCreationTime,
+ this.lastCreationTime,
+ });
+
+ int averageCreationTime() {
+ if (firstCreationTime != null && lastCreationTime != null) {
+ return (firstCreationTime! + lastCreationTime!) ~/ 2;
+ }
+ final List<int> creationTimes = files
+ .where((file) => file.creationTime != null)
+ .map((file) => file.creationTime!)
+ .toList();
+ if (creationTimes.length < 2) {
+ return creationTimes.isEmpty ? 0 : creationTimes.first;
+ }
+ creationTimes.sort();
+ firstCreationTime ??= creationTimes.first;
+ lastCreationTime ??= creationTimes.last;
+ return (firstCreationTime! + lastCreationTime!) ~/ 2;
+ }
+
+ SmartMemory copyWith({
+ List<EnteFile>? files,
+ int? firstCreationTime,
+ int? lastCreationTime,
+ });
+}
diff --git a/mobile/lib/models/trip_memory.dart b/mobile/lib/models/trip_memory.dart
index fb6aa044fe..5315faa82a 100644
--- a/mobile/lib/models/trip_memory.dart
+++ b/mobile/lib/models/trip_memory.dart
@@ -1,22 +1,19 @@
import "package:photos/models/file/file.dart";
import "package:photos/models/location/location.dart";
+import "package:photos/models/smart_memory.dart";
-class TripMemory {
- final List<EnteFile> files;
+class TripMemory extends SmartMemory {
final Location location;
- final int firstCreationTime;
- final int lastCreationTime;
TripMemory(
- this.files,
- this.location,
- this.firstCreationTime,
- this.lastCreationTime,
- );
+ files,
+ this.location, {
+ super.firstCreationTime,
+ super.lastCreationTime,
+ }) : super(files, MemoryType.trips);
- int get averageCreationTime => (firstCreationTime + lastCreationTime) ~/ 2;
-
- TripMemory copyWith({
+ @override
+ SmartMemory copyWith({
List<EnteFile>? files,
Location? location,
int? firstCreationTime,
@@ -25,8 +22,8 @@ class TripMemory {
return TripMemory(
files ?? this.files,
location ?? this.location,
- firstCreationTime ?? this.firstCreationTime,
- lastCreationTime ?? this.lastCreationTime,
+ firstCreationTime: firstCreationTime ?? super.firstCreationTime,
+ lastCreationTime: lastCreationTime ?? super.lastCreationTime,
);
}
}
diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dart
index 62576eae03..3257f8f6d7 100644
--- a/mobile/lib/services/search_service.dart
+++ b/mobile/lib/services/search_service.dart
@@ -1367,8 +1367,8 @@ class SearchService {
TripMemory(
List.from(currentBlockFiles),
location,
- blockStart,
- lastTime,
+ firstCreationTime: blockStart,
+ lastCreationTime: lastTime,
),
);
}
@@ -1391,8 +1391,8 @@ class SearchService {
TripMemory(
List.from(currentBlockFiles),
location,
- blockStart,
- lastTime,
+ firstCreationTime: blockStart,
+ lastCreationTime: lastTime,
),
);
}
@@ -1402,18 +1402,18 @@ class SearchService {
final List<TripMemory> mergedTrips = [];
for (final trip in tripLocations) {
final tripFirstTime = DateTime.fromMicrosecondsSinceEpoch(
- trip.firstCreationTime,
+ trip.firstCreationTime!,
);
final tripLastTime = DateTime.fromMicrosecondsSinceEpoch(
- trip.lastCreationTime,
+ trip.lastCreationTime!,
);
bool merged = false;
for (int idx = 0; idx < mergedTrips.length; idx++) {
final otherTrip = mergedTrips[idx];
final otherTripFirstTime =
- DateTime.fromMicrosecondsSinceEpoch(otherTrip.firstCreationTime);
+ DateTime.fromMicrosecondsSinceEpoch(otherTrip.firstCreationTime!);
final otherTripLastTime =
- DateTime.fromMicrosecondsSinceEpoch(otherTrip.lastCreationTime);
+ DateTime.fromMicrosecondsSinceEpoch(otherTrip.lastCreationTime!);
if (tripFirstTime
.isBefore(otherTripLastTime.add(const Duration(days: 3))) &&
tripLastTime.isAfter(
@@ -1422,8 +1422,8 @@ class SearchService {
mergedTrips[idx] = TripMemory(
otherTrip.files + trip.files,
otherTrip.location,
- min(otherTrip.firstCreationTime, trip.firstCreationTime),
- max(otherTrip.lastCreationTime, trip.lastCreationTime),
+ firstCreationTime: min(otherTrip.firstCreationTime!, trip.firstCreationTime!),
+ lastCreationTime: max(otherTrip.lastCreationTime!, trip.lastCreationTime!),
);
_logger.finest('Merged two trip locations');
merged = true;
@@ -1435,8 +1435,8 @@ class SearchService {
TripMemory(
trip.files,
trip.location,
- trip.firstCreationTime,
- trip.lastCreationTime,
+ firstCreationTime: trip.firstCreationTime,
+ lastCreationTime: trip.lastCreationTime,
),
);
}
@@ -1445,7 +1445,7 @@ class SearchService {
final List<TripMemory> validTrips = [];
for (final trip in mergedTrips) {
if (trip.files.length >= 20 &&
- trip.averageCreationTime < cutOffTime.microsecondsSinceEpoch) {
+ trip.averageCreationTime() < cutOffTime.microsecondsSinceEpoch) {
validTrips.add(trip);
}
}
@@ -1482,7 +1482,7 @@ class SearchService {
final Map<int, Map<int, List<TripMemory>>> tripsByMonthYear = {};
for (final trip in validTrips) {
final tripDate =
- DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime);
+ DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime());
tripsByMonthYear
.putIfAbsent(tripDate.month, () => {})
.putIfAbsent(tripDate.year, () => [])
@@ -1502,12 +1502,12 @@ class SearchService {
// If there are past trips this month, show the one or two most recent ones.
if (currentMonthTrips.isNotEmpty) {
currentMonthTrips.sort(
- (a, b) => b.averageCreationTime.compareTo(a.averageCreationTime),
+ (a, b) => b.averageCreationTime().compareTo(a.averageCreationTime()),
);
final tripsToShow = currentMonthTrips.take(2);
for (final trip in tripsToShow) {
final year =
- DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime).year;
+ DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime()).year;
final String? locationName = await _tryFindLocationName(trip.files);
String name = "Trip in $year";
if (locationName != null) {
@@ -1553,11 +1553,11 @@ class SearchService {
if (thatMonthTrips.length >= 3) {
// take and use the third earliest trip
thatMonthTrips.sort(
- (a, b) => a.averageCreationTime.compareTo(b.averageCreationTime),
+ (a, b) => a.averageCreationTime().compareTo(b.averageCreationTime()),
);
final trip = thatMonthTrips[2];
final year =
- DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime)
+ DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime())
.year;
final String? locationName = await _tryFindLocationName(trip.files);
String name = "Trip in $year";
diff --git a/mobile/lib/services/smart_memories_service.dart b/mobile/lib/services/smart_memories_service.dart
index 3da40f8d34..8b13789179 100644
--- a/mobile/lib/services/smart_memories_service.dart
+++ b/mobile/lib/services/smart_memories_service.dart
@@ -1,6 +1 @@
-enum MemoryType {
- people,
- trips,
- time,
- filler,
-}
+