Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Support sorting device albums

AmanRajSinghMouryaf33b180c1a90
diff --git a/mobile/apps/photos/lib/models/device_collection.dart b/mobile/apps/photos/lib/models/device_collection.dart
index 898d118074..8d94f8a724 100644
--- a/mobile/apps/photos/lib/models/device_collection.dart
+++ b/mobile/apps/photos/lib/models/device_collection.dart
@@ -6,6 +6,7 @@ class DeviceCollection {
final String name;
final int count;
final bool shouldBackup;
+ final int modifiedAt;
UploadStrategy uploadStrategy;
final String? coverId;
int? collectionID;
@@ -21,6 +22,7 @@ class DeviceCollection {
this.coverId,
this.count = 0,
this.collectionID,
+ this.modifiedAt = 0,
this.thumbnail,
this.uploadStrategy = UploadStrategy.ifMissing,
this.shouldBackup = false,
diff --git a/mobile/apps/photos/lib/ui/collections/device/device_folders_vertical_grid_view.dart b/mobile/apps/photos/lib/ui/collections/device/device_folders_vertical_grid_view.dart
index e4937da526..da28362ae8 100644
--- a/mobile/apps/photos/lib/ui/collections/device/device_folders_vertical_grid_view.dart
+++ b/mobile/apps/photos/lib/ui/collections/device/device_folders_vertical_grid_view.dart
@@ -19,6 +19,7 @@ import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/components/searchable_appbar.dart';
import "package:photos/ui/tabs/albums/empty_states/on_device_select_folders_empty_state.dart";
import 'package:photos/ui/viewer/gallery/empty_state.dart';
+import "package:photos/utils/device_collection_sort_util.dart";
import "package:photos/utils/local_settings.dart";
class DeviceFolderVerticalGridView extends StatefulWidget {
@@ -77,6 +78,8 @@ class _DeviceFolderVerticalGridViewState
class DeviceFolderVerticalGridSliver extends StatefulWidget {
final String searchQuery;
final AlbumViewType albumViewType;
+ final AlbumSortKey sortKey;
+ final AlbumSortDirection sortDirection;
final bool showEmptyState;
final double topPadding;
final double bottomPadding;
@@ -86,6 +89,8 @@ class DeviceFolderVerticalGridSliver extends StatefulWidget {
const DeviceFolderVerticalGridSliver({
required this.searchQuery,
this.albumViewType = AlbumViewType.grid,
+ this.sortKey = AlbumSortKey.newestPhoto,
+ this.sortDirection = AlbumSortDirection.ascending,
this.showEmptyState = true,
this.topPadding = 16,
this.bottomPadding = 200,
@@ -103,7 +108,6 @@ class _DeviceFolderVerticalGridViewBodyState
extends State<DeviceFolderVerticalGridSliver> {
StreamSubscription<BackupFoldersUpdatedEvent>? _backupFoldersUpdatedEvent;
StreamSubscription<LocalPhotosUpdatedEvent>? _localFilesSubscription;
- String _loadReason = "init";
late Future<List<DeviceCollection>> _deviceCollectionsFuture;
final logger = Logger((_DeviceFolderVerticalGridViewBodyState).toString());
final _debouncer = Debouncer(
@@ -127,14 +131,12 @@ class _DeviceFolderVerticalGridViewBodyState
_deviceCollectionsFuture = _loadDeviceCollections();
_backupFoldersUpdatedEvent = Bus.instance
.on<BackupFoldersUpdatedEvent>()
- .listen((event) {
- _loadReason = event.reason;
+ .listen((_) {
_refreshDeviceCollections();
});
_localFilesSubscription = Bus.instance.on<LocalPhotosUpdatedEvent>().listen(
- (event) {
+ (_) {
_debouncer.run(() async {
- _loadReason = event.reason;
_refreshDeviceCollections();
});
},
@@ -158,9 +160,6 @@ class _DeviceFolderVerticalGridViewBodyState
@override
Widget build(BuildContext context) {
- debugPrint(
- "${(DeviceFolderVerticalGridSliver).toString()} - $_loadReason",
- );
if (backupPreferenceService.hasSkippedOnboardingPermission) {
return SliverFillRemaining(
hasScrollBody: false,
@@ -176,7 +175,7 @@ class _DeviceFolderVerticalGridViewBodyState
future: _deviceCollectionsFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
- List<DeviceCollection> deviceCollections = snapshot.data!;
+ List<DeviceCollection> deviceCollections = snapshot.data!.toList();
if (widget.searchQuery.isNotEmpty) {
final String query = widget.searchQuery.toLowerCase();
deviceCollections = deviceCollections
@@ -186,6 +185,11 @@ class _DeviceFolderVerticalGridViewBodyState
)
.toList();
}
+ sortDeviceCollections(
+ deviceCollections,
+ widget.sortKey,
+ widget.sortDirection,
+ );
if (deviceCollections.isEmpty) {
if (widget.emptyStateSliver != null) {
diff --git a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart b/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
index 6ad285c385..05b9601136 100644
--- a/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
+++ b/mobile/apps/photos/lib/ui/tabs/albums_tab.dart
@@ -314,6 +314,8 @@ class _AlbumsTabState extends State<AlbumsTab>
key: const ValueKey("album_search_local_device_folders"),
searchQuery: _searchQuery.trim(),
albumViewType: _viewType.value,
+ sortKey: _sortKey.value,
+ sortDirection: _sortDirection.value,
showEmptyState: true,
topPadding: 8,
bottomPadding: 0,
@@ -352,6 +354,8 @@ class _AlbumsTabState extends State<AlbumsTab>
key: const ValueKey("album_search_device_folders"),
searchQuery: _searchQuery.trim(),
albumViewType: _viewType.value,
+ sortKey: _sortKey.value,
+ sortDirection: _sortDirection.value,
showEmptyState: shouldShowDeviceSearchState,
topPadding: 8,
bottomPadding: 0,
@@ -401,8 +405,7 @@ class _AlbumsTabState extends State<AlbumsTab>
final colorScheme = getEnteColorScheme(context);
final strings = AppLocalizations.of(context);
final isListView = _viewType.value == AlbumViewType.list;
- final showSortActions =
- !_hasSearchQuery && _effectiveFilter != _AlbumsFilter.onDevice;
+ final showSortActions = !_hasSearchQuery;
final currentSortKey = _sortKey.value;
final currentSortDirection = _sortDirection.value;
final nameSortDirection = currentSortKey == AlbumSortKey.albumName
@@ -724,6 +727,8 @@ class _AlbumsTabState extends State<AlbumsTab>
_enteCollections,
_sharedCollections,
_viewType,
+ _sortKey,
+ _sortDirection,
],
),
builder: (context, _) {
@@ -747,6 +752,8 @@ class _AlbumsTabState extends State<AlbumsTab>
return DeviceFolderVerticalGridSliver(
searchQuery: _searchQuery.trim(),
albumViewType: _viewType.value,
+ sortKey: _sortKey.value,
+ sortDirection: _sortDirection.value,
);
}
if (collections == null) {
diff --git a/mobile/apps/photos/lib/utils/device_collection_sort_util.dart b/mobile/apps/photos/lib/utils/device_collection_sort_util.dart
new file mode 100644
index 0000000000..2084fb2615
--- /dev/null
+++ b/mobile/apps/photos/lib/utils/device_collection_sort_util.dart
@@ -0,0 +1,49 @@
+import "package:collection/collection.dart";
+import "package:photos/models/device_collection.dart";
+import "package:photos/utils/local_settings.dart";
+
+void sortDeviceCollections(
+ List<DeviceCollection> deviceCollections,
+ AlbumSortKey sortKey,
+ AlbumSortDirection sortDirection,
+) {
+ if (deviceCollections.length < 2) {
+ return;
+ }
+
+ deviceCollections.sort((first, second) {
+ var comparison = switch (sortKey) {
+ AlbumSortKey.albumName => _compareByName(first, second),
+ AlbumSortKey.newestPhoto => _newestPhotoTime(second).compareTo(
+ _newestPhotoTime(first),
+ ),
+ AlbumSortKey.lastUpdated => _updatedTime(second).compareTo(
+ _updatedTime(first),
+ ),
+ };
+ if (comparison == 0) {
+ comparison = _compareByName(first, second);
+ }
+ if (comparison == 0) {
+ comparison = compareAsciiLowerCaseNatural(first.id, second.id);
+ }
+ return sortDirection == AlbumSortDirection.ascending
+ ? comparison
+ : -comparison;
+ });
+}
+
+int _compareByName(DeviceCollection first, DeviceCollection second) {
+ return compareAsciiLowerCaseNatural(first.name, second.name);
+}
+
+int _newestPhotoTime(DeviceCollection deviceCollection) {
+ return deviceCollection.thumbnail?.creationTime ?? -1;
+}
+
+int _updatedTime(DeviceCollection deviceCollection) {
+ if (deviceCollection.modifiedAt > 0) {
+ return deviceCollection.modifiedAt;
+ }
+ return _newestPhotoTime(deviceCollection);
+}