From 411878c0aac9e7094cb404e7eb8907d5d85630a6 Mon Sep 17 00:00:00 2001 From: Alessandro Piccin <117726828+alessandrv@users.noreply.github.com> Date: Wed, 4 Dec 2024 20:53:55 +0100 Subject: [PATCH] fix(mobile): album most recent sorting on mobile (#13766) * Fix album most recent sorting on mobile * fix: format * fix: format --------- Co-authored-by: Alex --- .../album/album_sort_by_options.provider.dart | 19 +++++--- mobile/test/fixtures/album.stub.dart | 45 +++++++++++++++++++ .../album_sort_by_options_provider_test.dart | 40 ++++++++++++----- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/mobile/lib/providers/album/album_sort_by_options.provider.dart b/mobile/lib/providers/album/album_sort_by_options.provider.dart index 216688ee15..cafde37253 100644 --- a/mobile/lib/providers/album/album_sort_by_options.provider.dart +++ b/mobile/lib/providers/album/album_sort_by_options.provider.dart @@ -39,12 +39,21 @@ class _AlbumSortHandlers { static const AlbumSortFn mostRecent = _sortByMostRecent; static List _sortByMostRecent(List albums, bool isReverse) { final sorted = albums.sorted((a, b) { - if (a.endDate != null && b.endDate != null) { - return a.endDate!.compareTo(b.endDate!); + if (a.endDate == null && b.endDate == null) { + return 0; } - if (a.endDate == null) return 1; - if (b.endDate == null) return -1; - return 0; + + if (a.endDate == null) { + // Put nulls at the end for recent sorting + return 1; + } + + if (b.endDate == null) { + return -1; + } + + // Sort by descending recent date + return b.endDate!.compareTo(a.endDate!); }); return (isReverse ? sorted.reversed : sorted).toList(); } diff --git a/mobile/test/fixtures/album.stub.dart b/mobile/test/fixtures/album.stub.dart index 4fa0dac1d2..e820f193d5 100644 --- a/mobile/test/fixtures/album.stub.dart +++ b/mobile/test/fixtures/album.stub.dart @@ -54,4 +54,49 @@ final class AlbumStub { ..assets.addAll([AssetStub.image1, AssetStub.image2]) ..activityEnabled = true ..owner.value = UserStub.admin; + + static final create2020end2020Album = Album( + name: "create2020update2020Album", + localId: "create2020update2020Album-local", + remoteId: "create2020update2020Album-remote", + createdAt: DateTime(2020), + modifiedAt: DateTime(2020), + shared: false, + activityEnabled: false, + startDate: DateTime(2020), + endDate: DateTime(2020), + ); + static final create2020end2022Album = Album( + name: "create2020update2021Album", + localId: "create2020update2021Album-local", + remoteId: "create2020update2021Album-remote", + createdAt: DateTime(2020), + modifiedAt: DateTime(2022), + shared: false, + activityEnabled: false, + startDate: DateTime(2020), + endDate: DateTime(2022), + ); + static final create2020end2024Album = Album( + name: "create2020update2022Album", + localId: "create2020update2022Album-local", + remoteId: "create2020update2022Album-remote", + createdAt: DateTime(2020), + modifiedAt: DateTime(2024), + shared: false, + activityEnabled: false, + startDate: DateTime(2020), + endDate: DateTime(2024), + ); + static final create2020end2026Album = Album( + name: "create2020update2023Album", + localId: "create2020update2023Album-local", + remoteId: "create2020update2023Album-remote", + createdAt: DateTime(2020), + modifiedAt: DateTime(2026), + shared: false, + activityEnabled: false, + startDate: DateTime(2020), + endDate: DateTime(2026), + ); } diff --git a/mobile/test/modules/album/album_sort_by_options_provider_test.dart b/mobile/test/modules/album/album_sort_by_options_provider_test.dart index 84a7e6e9b8..bfb61ef402 100644 --- a/mobile/test/modules/album/album_sort_by_options_provider_test.dart +++ b/mobile/test/modules/album/album_sort_by_options_provider_test.dart @@ -147,24 +147,40 @@ void main() { group("Album sort - Most Recent", () { const mostRecent = AlbumSortMode.mostRecent; - test("Most Recent - ASC", () { - final sorted = mostRecent.sortFn(albums, false); + test("Most Recent - DESC", () { + final sorted = mostRecent.sortFn( + [ + AlbumStub.create2020end2020Album, + AlbumStub.create2020end2022Album, + AlbumStub.create2020end2024Album, + AlbumStub.create2020end2026Album, + ], + false, + ); final sortedList = [ - AlbumStub.sharedWithUser, - AlbumStub.twoAsset, - AlbumStub.oneAsset, - AlbumStub.emptyAlbum, + AlbumStub.create2020end2026Album, + AlbumStub.create2020end2024Album, + AlbumStub.create2020end2022Album, + AlbumStub.create2020end2020Album, ]; expect(sorted, orderedEquals(sortedList)); }); - test("Most Recent - DESC", () { - final sorted = mostRecent.sortFn(albums, true); + test("Most Recent - ASC", () { + final sorted = mostRecent.sortFn( + [ + AlbumStub.create2020end2020Album, + AlbumStub.create2020end2022Album, + AlbumStub.create2020end2024Album, + AlbumStub.create2020end2026Album, + ], + true, + ); final sortedList = [ - AlbumStub.emptyAlbum, - AlbumStub.oneAsset, - AlbumStub.twoAsset, - AlbumStub.sharedWithUser, + AlbumStub.create2020end2020Album, + AlbumStub.create2020end2022Album, + AlbumStub.create2020end2024Album, + AlbumStub.create2020end2026Album, ]; expect(sorted, orderedEquals(sortedList)); });