From 2a19f7b2fa53c73f3d48bffc6cf878667dc72f83 Mon Sep 17 00:00:00 2001 From: kilowatt Date: Sun, 25 Oct 2020 12:32:23 +0300 Subject: [PATCH] Fix albums/artists sorting by date of adding to favorites --- lib/api/definitions.dart | 22 +++++++++++++++------- lib/api/definitions.g.dart | 2 ++ lib/ui/library.dart | 10 ++++++---- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/api/definitions.dart b/lib/api/definitions.dart index b7e8706..cb295a4 100644 --- a/lib/api/definitions.dart +++ b/lib/api/definitions.dart @@ -195,8 +195,9 @@ class Album { bool library; AlbumType type; String releaseDate; + String favoriteDate; - Album({this.id, this.title, this.art, this.artists, this.tracks, this.fans, this.offline, this.library, this.type, this.releaseDate}); + Album({this.id, this.title, this.art, this.artists, this.tracks, this.fans, this.offline, this.library, this.type, this.releaseDate, this.favoriteDate}); String get artistString => artists.map((art) => art.name).join(', '); Duration get duration => Duration(seconds: tracks.fold(0, (v, t) => v += t.duration.inSeconds)); @@ -218,7 +219,8 @@ class Album { fans: json['NB_FAN'], library: library, type: type, - releaseDate: json['DIGITAL_RELEASE_DATE']??json['PHYSICAL_RELEASE_DATE'] + releaseDate: json['DIGITAL_RELEASE_DATE']??json['PHYSICAL_RELEASE_DATE'], + favoriteDate: json['DATE_FAVORITE'] ); } Map toSQL({off = false}) => { @@ -231,7 +233,8 @@ class Album { 'offline': off?1:0, 'library': (library??false)?1:0, 'type': AlbumType.values.indexOf(type), - 'releaseDate': releaseDate + 'releaseDate': releaseDate, + 'favoriteDate': favoriteDate }; factory Album.fromSQL(Map data) => Album( id: data['id'], @@ -247,7 +250,8 @@ class Album { offline: (data['offline'] == 1) ? true:false, library: (data['library'] == 1) ? true:false, type: AlbumType.values[data['type']], - releaseDate: data['releaseDate'] + releaseDate: data['releaseDate'], + favoriteDate: data['favoriteDate'] ); factory Album.fromJson(Map json) => _$AlbumFromJson(json); @@ -266,8 +270,9 @@ class Artist { bool offline; bool library; bool radio; + String favoriteDate; - Artist({this.id, this.name, this.albums, this.albumCount, this.topTracks, this.picture, this.fans, this.offline, this.library, this.radio}); + Artist({this.id, this.name, this.albums, this.albumCount, this.topTracks, this.picture, this.fans, this.offline, this.library, this.radio, this.favoriteDate}); String get fansString => NumberFormat.compact().format(fans); @@ -292,6 +297,7 @@ class Artist { topTracks: (topJson['data']??[]).map((dynamic data) => Track.fromPrivateJson(data)).toList(), library: library, radio: _radio, + favoriteDate: json['DATE_FAVORITE'] ); } Map toSQL({off = false}) => { @@ -304,7 +310,8 @@ class Artist { 'albumCount': this.albumCount??(this.albums??[]).length, 'offline': off?1:0, 'library': (library??false)?1:0, - 'radio': radio?1:0 + 'radio': radio?1:0, + 'favoriteDate': favoriteDate }; factory Artist.fromSQL(Map data) => Artist( id: data['id'], @@ -320,7 +327,8 @@ class Artist { fans: data['fans'], offline: (data['offline'] == 1)?true:false, library: (data['library'] == 1)?true:false, - radio: (data['radio'] == 1)?true:false + radio: (data['radio'] == 1)?true:false, + favoriteDate: data['favoriteDate'] ); factory Artist.fromJson(Map json) => _$ArtistFromJson(json); diff --git a/lib/api/definitions.g.dart b/lib/api/definitions.g.dart index bbc0d72..c995899 100644 --- a/lib/api/definitions.g.dart +++ b/lib/api/definitions.g.dart @@ -71,6 +71,7 @@ Album _$AlbumFromJson(Map json) { library: json['library'] as bool, type: _$enumDecodeNullable(_$AlbumTypeEnumMap, json['type']), releaseDate: json['releaseDate'] as String, + favoriteDate: json['favoriteDate'] as String ); } @@ -85,6 +86,7 @@ Map _$AlbumToJson(Album instance) => { 'library': instance.library, 'type': _$AlbumTypeEnumMap[instance.type], 'releaseDate': instance.releaseDate, + 'favoriteDate': instance.favoriteDate }; T _$enumDecode( diff --git a/lib/ui/library.dart b/lib/ui/library.dart index 645448a..83855f4 100644 --- a/lib/ui/library.dart +++ b/lib/ui/library.dart @@ -512,11 +512,12 @@ class _LibraryAlbumsState extends State { List get _sorted { List albums = List.from(_albums); + albums.sort((a, b) => a.favoriteDate.compareTo(b.favoriteDate)); switch (_sort) { case AlbumSortType.DEFAULT: - return _albums; + return albums; case AlbumSortType.REVERSE: - return _albums.reversed.toList(); + return albums.reversed.toList(); case AlbumSortType.ALPHABETIC: albums.sort((a, b) => a.title.toLowerCase().compareTo(b.title.toLowerCase())); return albums; @@ -677,11 +678,12 @@ class _LibraryArtistsState extends State { List get _sorted { List artists = List.from(_artists); + artists.sort((a, b) => a.favoriteDate.compareTo(b.favoriteDate)); switch (_sort) { case ArtistSortType.DEFAULT: - return _artists; + return artists; case ArtistSortType.REVERSE: - return _artists.reversed.toList(); + return artists.reversed.toList(); case ArtistSortType.POPULARITY: artists.sort((a, b) => b.fans - a.fans); return artists;