1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-29 15:11:58 +00:00

fix(mobile): album most recent sorting on mobile (#13766)

* Fix album most recent sorting on mobile

* fix: format

* fix: format

---------

Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
Alessandro Piccin 2024-12-04 20:53:55 +01:00 committed by GitHub
parent ba9b9353bc
commit 411878c0aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 87 additions and 17 deletions

View file

@ -39,12 +39,21 @@ class _AlbumSortHandlers {
static const AlbumSortFn mostRecent = _sortByMostRecent; static const AlbumSortFn mostRecent = _sortByMostRecent;
static List<Album> _sortByMostRecent(List<Album> albums, bool isReverse) { static List<Album> _sortByMostRecent(List<Album> albums, bool isReverse) {
final sorted = albums.sorted((a, b) { final sorted = albums.sorted((a, b) {
if (a.endDate != null && b.endDate != null) { if (a.endDate == null && b.endDate == null) {
return a.endDate!.compareTo(b.endDate!); return 0;
} }
if (a.endDate == null) return 1;
if (b.endDate == null) return -1; if (a.endDate == null) {
return 0; // 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(); return (isReverse ? sorted.reversed : sorted).toList();
} }

View file

@ -54,4 +54,49 @@ final class AlbumStub {
..assets.addAll([AssetStub.image1, AssetStub.image2]) ..assets.addAll([AssetStub.image1, AssetStub.image2])
..activityEnabled = true ..activityEnabled = true
..owner.value = UserStub.admin; ..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),
);
} }

View file

@ -147,24 +147,40 @@ void main() {
group("Album sort - Most Recent", () { group("Album sort - Most Recent", () {
const mostRecent = AlbumSortMode.mostRecent; const mostRecent = AlbumSortMode.mostRecent;
test("Most Recent - ASC", () { test("Most Recent - DESC", () {
final sorted = mostRecent.sortFn(albums, false); final sorted = mostRecent.sortFn(
[
AlbumStub.create2020end2020Album,
AlbumStub.create2020end2022Album,
AlbumStub.create2020end2024Album,
AlbumStub.create2020end2026Album,
],
false,
);
final sortedList = [ final sortedList = [
AlbumStub.sharedWithUser, AlbumStub.create2020end2026Album,
AlbumStub.twoAsset, AlbumStub.create2020end2024Album,
AlbumStub.oneAsset, AlbumStub.create2020end2022Album,
AlbumStub.emptyAlbum, AlbumStub.create2020end2020Album,
]; ];
expect(sorted, orderedEquals(sortedList)); expect(sorted, orderedEquals(sortedList));
}); });
test("Most Recent - DESC", () { test("Most Recent - ASC", () {
final sorted = mostRecent.sortFn(albums, true); final sorted = mostRecent.sortFn(
[
AlbumStub.create2020end2020Album,
AlbumStub.create2020end2022Album,
AlbumStub.create2020end2024Album,
AlbumStub.create2020end2026Album,
],
true,
);
final sortedList = [ final sortedList = [
AlbumStub.emptyAlbum, AlbumStub.create2020end2020Album,
AlbumStub.oneAsset, AlbumStub.create2020end2022Album,
AlbumStub.twoAsset, AlbumStub.create2020end2024Album,
AlbumStub.sharedWithUser, AlbumStub.create2020end2026Album,
]; ];
expect(sorted, orderedEquals(sortedList)); expect(sorted, orderedEquals(sortedList));
}); });