nc-photos/app/lib/entity/album_util.dart

42 lines
1.1 KiB
Dart
Raw Normal View History

2022-07-25 07:51:52 +02:00
import 'package:collection/collection.dart';
2021-12-10 19:44:56 +01:00
import 'package:nc_photos/entity/album.dart';
import 'package:tuple/tuple.dart';
enum AlbumSort {
dateDescending,
dateAscending,
nameAscending,
nameDescending,
}
List<Album> sorted(List<Album> albums, AlbumSort by) {
final isAscending = _isSortAscending(by);
return albums
.map<Tuple2<dynamic, Album>>((e) {
switch (by) {
case AlbumSort.nameAscending:
case AlbumSort.nameDescending:
return Tuple2(e.name, e);
case AlbumSort.dateAscending:
case AlbumSort.dateDescending:
return Tuple2(e.provider.latestItemTime ?? e.lastUpdated, e);
}
})
.sorted((a, b) {
final x = isAscending ? a : b;
final y = isAscending ? b : a;
final tmp = x.item1.compareTo(y.item1);
if (tmp != 0) {
return tmp;
} else {
return x.item2.name.compareTo(y.item2.name);
}
})
.map((e) => e.item2)
.toList();
}
bool _isSortAscending(AlbumSort sort) =>
sort == AlbumSort.dateAscending || sort == AlbumSort.nameAscending;