nc-photos/app/lib/entity/collection/util.dart

70 lines
1.7 KiB
Dart
Raw Normal View History

import 'package:collection/collection.dart';
2023-05-01 19:05:33 +02:00
import 'package:equatable/equatable.dart';
import 'package:nc_photos/entity/collection.dart';
2023-08-25 18:37:17 +02:00
import 'package:np_string/np_string.dart';
2023-05-01 19:05:33 +02:00
import 'package:to_string/to_string.dart';
2023-05-01 19:05:33 +02:00
part 'util.g.dart';
enum CollectionSort {
dateDescending,
dateAscending,
nameAscending,
nameDescending;
bool isAscending() {
return this == CollectionSort.dateAscending ||
this == CollectionSort.nameAscending;
}
}
2023-05-01 19:05:33 +02:00
@toString
class CollectionShare with EquatableMixin {
const CollectionShare({
required this.userId,
required this.username,
});
@override
String toString() => _$toString();
@override
List<Object?> get props => [userId, username];
final CiString userId;
final String username;
}
enum CollectionShareResult {
ok,
partial,
}
extension CollectionListExtension on Iterable<Collection> {
List<Collection> sortedBy(CollectionSort by) {
2024-06-30 21:00:11 +02:00
return map<({Comparable comparable, Collection collection})>((e) {
switch (by) {
case CollectionSort.nameAscending:
case CollectionSort.nameDescending:
2024-06-30 21:00:11 +02:00
return (comparable: e.name.toLowerCase(), collection: e);
case CollectionSort.dateAscending:
case CollectionSort.dateDescending:
2024-06-30 21:00:11 +02:00
return (comparable: e.contentProvider.lastModified, collection: e);
}
})
.sorted((a, b) {
final x = by.isAscending() ? a : b;
final y = by.isAscending() ? b : a;
2024-06-30 21:00:11 +02:00
final tmp = x.comparable.compareTo(y.comparable);
if (tmp != 0) {
return tmp;
} else {
2024-06-30 21:00:11 +02:00
return x.collection.name.compareTo(y.collection.name);
}
})
2024-06-30 21:00:11 +02:00
.map((e) => e.collection)
.toList();
}
}