Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow playlist modification date to be nullable #169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/logic/models/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Playlist extends PersistentQueue with DuplicatingSongOriginMixin implement
ContentType get type => ContentType.playlist;
final String? filesystemPath;
final int dateAdded;
final int dateModified;
final int? dateModified;
final String name;
@override
final List<int> songIds;
Expand Down Expand Up @@ -108,12 +108,11 @@ class Playlist extends PersistentQueue with DuplicatingSongOriginMixin implement
}

factory Playlist.fromMap(Map<String, dynamic> map) {
int dateAdded = map['dateAdded'] as int;
return Playlist(
id: map['id'] as int,
filesystemPath: map['filesystemPath'] as String?,
dateAdded: dateAdded,
dateModified: map['dateModified'] as int? ?? dateAdded,
dateAdded: map['dateAdded'] as int,
dateModified: map['dateModified'] as int?,
name: map['name'] as String,
songIds: (map['songIds'] as List?)?.cast<int>().toList() ?? [],
);
Expand All @@ -136,7 +135,7 @@ abstract class PlaylistCopyWith {
int id,
String? fileSystemPath,
int dateAdded,
int dateModified,
int? dateModified,
String name,
List<int> songIds,
});
Expand All @@ -157,15 +156,15 @@ class _PlaylistCopyWith extends PlaylistCopyWith {
Object id = _undefined,
Object? fileSystemPath = _undefined,
Object dateAdded = _undefined,
Object dateModified = _undefined,
Object? dateModified = _undefined,
Object name = _undefined,
Object songIds = _undefined,
}) {
return Playlist(
id: id == _undefined ? value.id : id as int,
filesystemPath: fileSystemPath == _undefined ? value.filesystemPath : fileSystemPath as String?,
dateAdded: dateAdded == _undefined ? value.dateAdded : dateAdded as int,
dateModified: dateModified == _undefined ? value.dateModified : dateModified as int,
dateModified: dateModified == _undefined ? value.dateModified : dateModified as int?,
name: name == _undefined ? value.name : name as String,
songIds: songIds == _undefined ? value.songIds : songIds as List<int>,
);
Expand Down
5 changes: 3 additions & 2 deletions lib/logic/models/sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class PlaylistSort extends Sort<Playlist> {
}

int _fallbackDateModified(Playlist a, Playlist b) {
return a.dateModified.compareTo(b.dateModified);
return a.dateModified.compareToNullable(b.dateModified, nullCompareResult: order == SortOrder.descending ? 1 : -1);
}

@override
Expand All @@ -372,7 +372,8 @@ class PlaylistSort extends Sort<Playlist> {
switch (feature) {
case PlaylistSortFeature.dateModified:
c = (a, b) {
final compare = a.dateModified.compareTo(b.dateModified);
final compare = a.dateModified
.compareToNullable(b.dateModified, nullCompareResult: order == SortOrder.descending ? 1 : -1);
if (compare == 0) {
return _fallbackName(a, b);
}
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/compare.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extension NullableCompare<T extends Comparable<Comparable<T>>> on Comparable<T>? {
int compareToNullable(T? other, {nullCompareResult = -1}) {
final self = this;
if (self != null) {
return self.compareToNullable(other, nullCompareResult: nullCompareResult);
}
if (other != null) {
return -other.compareToNullable(self, nullCompareResult: nullCompareResult);
}
return 0;
}
}

extension CompareNullable<T> on Comparable<T> {
int compareToNullable(T? other, {nullCompareResult = -1}) {
if (other != null) {
return compareTo(other);
}
return nullCompareResult;
}
}
1 change: 1 addition & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export 'color.dart';
export 'context.dart';
export 'compare.dart';
export 'line_height.dart';
export 'measure.dart';
export 'memoize.dart';
Expand Down
56 changes: 29 additions & 27 deletions test/logic/models/sort_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,8 @@ void main() {
for (final MapEntry(key: feature, value: expectedContentList) in featureTestCases.entries) {
test('Sorts songs by ${feature.name} ${sortOrder.name}', () async {
expect(
(songs.toList()..sort(SongSort(feature: feature, order: sortOrder).comparator))
.map((song) => song.toMap())
.toList(),
expectedContentList.map((song) => song.toMap()).toList(),
(songs.toList()..sort(SongSort(feature: feature, order: sortOrder).comparator)).map((song) => song.toMap()),
expectedContentList.map((song) => song.toMap()),
);
});
}
Expand Down Expand Up @@ -624,9 +622,8 @@ void main() {
test('Sorts albums by ${feature.name} ${sortOrder.name}', () async {
expect(
(albums.toList()..sort(AlbumSort(feature: feature, order: sortOrder).comparator))
.map((album) => album.toMap())
.toList(),
expectedContentList.map((album) => album.toMap()).toList(),
.map((album) => album.toMap()),
expectedContentList.map((album) => album.toMap()),
);
});
}
Expand All @@ -648,17 +645,18 @@ void main() {
playlistWith(id: 8, name: 'я (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
];
});
final Map<SortOrder, Map<PlaylistSortFeature, List<Playlist>>> testCases = {
SortOrder.ascending: {
PlaylistSortFeature.dateModified: [
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 1, name: 'A', dateModified: 0, dateAdded: 0),
playlistWith(id: 3, name: 'a', dateModified: 0, dateAdded: 0),
playlistWith(id: 2, name: 'AA', dateModified: 0, dateAdded: 0),
Expand All @@ -668,12 +666,14 @@ void main() {
playlistWith(id: 6, name: 'АА (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 8, name: 'я (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
],
PlaylistSortFeature.dateAdded: [
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
playlistWith(id: 1, name: 'A', dateModified: 0, dateAdded: 0),
playlistWith(id: 3, name: 'a', dateModified: 0, dateAdded: 0),
playlistWith(id: 2, name: 'AA', dateModified: 0, dateAdded: 0),
Expand All @@ -682,14 +682,15 @@ void main() {
playlistWith(id: 7, name: 'а (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 6, name: 'АА (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 8, name: 'я (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
],
PlaylistSortFeature.name: [
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
playlistWith(id: 1, name: 'A', dateModified: 0, dateAdded: 0),
playlistWith(id: 3, name: 'a', dateModified: 0, dateAdded: 0),
playlistWith(id: 2, name: 'AA', dateModified: 0, dateAdded: 0),
Expand All @@ -712,12 +713,13 @@ void main() {
playlistWith(id: 1, name: 'A', dateModified: 0, dateAdded: 0),
playlistWith(id: 3, name: 'a', dateModified: 0, dateAdded: 0),
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
],
PlaylistSortFeature.dateAdded: [
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 8, name: 'я (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 6, name: 'АА (кириллица)', dateModified: 0, dateAdded: 0),
playlistWith(id: 5, name: 'А (кириллица)', dateModified: 0, dateAdded: 0),
Expand All @@ -729,7 +731,8 @@ void main() {
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
],
PlaylistSortFeature.name: [
playlistWith(id: 8, name: 'я (кириллица)', dateModified: 0, dateAdded: 0),
Expand All @@ -742,9 +745,10 @@ void main() {
playlistWith(id: 3, name: 'a', dateModified: 0, dateAdded: 0),
playlistWith(id: 9, name: '', dateModified: 1, dateAdded: 0),
playlistWith(id: 0, name: '', dateModified: 0, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 12, name: '', dateModified: 0, dateAdded: 1),
playlistWith(id: 13, name: '', dateModified: 0, dateAdded: -1),
playlistWith(id: 10, name: '', dateModified: -1, dateAdded: 0),
playlistWith(id: 11, name: '', dateModified: null, dateAdded: 0),
],
},
};
Expand All @@ -753,9 +757,8 @@ void main() {
test('Sorts playlists by ${feature.name} ${sortOrder.name}', () async {
expect(
(playlists.toList()..sort(PlaylistSort(feature: feature, order: sortOrder).comparator))
.map((playlist) => playlist.toMap())
.toList(),
expectedContentList.map((playlist) => playlist.toMap()).toList(),
.map((playlist) => playlist.toMap()),
expectedContentList.map((playlist) => playlist.toMap()),
);
});
}
Expand Down Expand Up @@ -882,9 +885,8 @@ void main() {
test('Sorts artists by ${feature.name} ${sortOrder.name}', () async {
expect(
(artists.toList()..sort(ArtistSort(feature: feature, order: sortOrder).comparator))
.map((artist) => artist.toMap())
.toList(),
expectedContentList.map((artist) => artist.toMap()).toList(),
.map((artist) => artist.toMap()),
expectedContentList.map((artist) => artist.toMap()),
);
});
}
Expand Down