Browse Source

Update logic to create collectionName for links

Neeraj Gupta 2 years ago
parent
commit
d5e8630bd0

+ 10 - 1
lib/ui/actions/collection/collection_sharing_actions.dart

@@ -74,7 +74,16 @@ class CollectionActions {
       // create album with emptyName, use collectionCreationTime on UI to
       // show name
       logger.finest("creating album for sharing files");
-      final String dummyName = " ${getDateAndMonthAndYear(DateTime.now())} ";
+      final File fileWithMinCreationTime = files.reduce(
+        (a, b) => (a.creationTime ?? 0) < (b.creationTime ?? 0) ? a : b,
+      );
+      final File fileWithMaxCreationTime = files.reduce(
+        (a, b) => (a.creationTime ?? 0) > (b.creationTime ?? 0) ? a : b,
+      );
+      final String dummyName = getNameForDateRange(
+        fileWithMinCreationTime.creationTime!,
+        fileWithMaxCreationTime.creationTime!,
+      );
       final collection = await collectionsService.createAlbum(dummyName);
       logger.finest("adding files to share to new album");
       await collectionsService.addToCollection(collection.id, files);

+ 26 - 0
lib/utils/date_time_util.dart

@@ -86,6 +86,32 @@ String getDateAndMonthAndYear(DateTime dateTime) {
       dateTime.year.toString();
 }
 
+// Create link default names:
+// Same day: "Dec 19, 2022"
+// Same month: "Dec 19 - 22, 2022"
+// Base case: "Dec 19, 2022 - Jan 7, 2023"
+String getNameForDateRange(int firstCreationTime, int secondCreationTime) {
+  final startTime = DateTime.fromMicrosecondsSinceEpoch(firstCreationTime);
+  final endTime = DateTime.fromMicrosecondsSinceEpoch(secondCreationTime);
+  // different year
+  if (startTime.year != endTime.year) {
+    return "${_months[startTime.month]!} ${startTime.day}, ${startTime.year} - "
+        "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
+  }
+  // same year, diff month
+  if (startTime.month != endTime.month) {
+    return "${_months[startTime.month]!} ${startTime.day} - "
+        "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
+  }
+  // same month and year, diff day
+  if (startTime.day != endTime.day) {
+    return "${_months[startTime.month]!} ${startTime.day} - "
+        "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
+  }
+  // same day
+  return "${_months[endTime.month]!} ${endTime.day}, ${endTime.year}";
+}
+
 String getDay(DateTime dateTime) {
   return _days[dateTime.weekday]!;
 }