diff --git a/lib/main.dart b/lib/main.dart index d853447..61c4019 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -57,6 +57,7 @@ List _providers = [ ChangeNotifierProvider( create: (context) => DataProvider( playlistProvider: context.read(), + songProvider: context.read(), ), ), ChangeNotifierProvider( diff --git a/lib/providers/data_provider.dart b/lib/providers/data_provider.dart index f213910..f0f9aca 100644 --- a/lib/providers/data_provider.dart +++ b/lib/providers/data_provider.dart @@ -7,9 +7,11 @@ import 'package:version/version.dart'; class DataProvider with ChangeNotifier { final PlaylistProvider _playlistProvider; + final SongProvider _songProvider; - DataProvider({required playlistProvider}) - : _playlistProvider = playlistProvider; + DataProvider({required playlistProvider, required songProvider}) + : _playlistProvider = playlistProvider, + _songProvider = songProvider; Future init() async { final Map data = await get('data'); @@ -28,7 +30,7 @@ class DataProvider with ChangeNotifier { if (data.containsKey('queue_state')) { AppState.set( ['app', 'queueState'], - QueueState.parse(data['queue_state']), + QueueState.parse(data['queue_state'], _songProvider), ); } else { AppState.set(['app', 'queueState'], QueueState.empty()); diff --git a/lib/providers/overview_provider.dart b/lib/providers/overview_provider.dart index afa1b7d..aac1b22 100644 --- a/lib/providers/overview_provider.dart +++ b/lib/providers/overview_provider.dart @@ -46,15 +46,15 @@ class OverviewProvider with ChangeNotifier, StreamSubscriber { mostPlayedSongs ..clear() - ..addAll(_parseSongsFromJson(response['most_played_songs'])); + ..addAll(_songProvider.parseFromJson(response['most_played_songs'])); recentlyAddedSongs ..clear() - ..addAll(_parseSongsFromJson(response['recently_added_songs'])); + ..addAll(_songProvider.parseFromJson(response['recently_added_songs'])); recentlyPlayedSongs ..clear() - ..addAll(_parseSongsFromJson(response['recently_played_songs'])); + ..addAll(_songProvider.parseFromJson(response['recently_played_songs'])); final _mostPlayedAlbums = response['most_played_albums'] .map((j) => Album.fromJson(j)) @@ -74,9 +74,4 @@ class OverviewProvider with ChangeNotifier, StreamSubscriber { notifyListeners(); } - - List _parseSongsFromJson(dynamic json) { - return _songProvider - .syncWithVault(json.map((j) => Song.fromJson(j)).toList()); - } } diff --git a/lib/providers/recently_played_provider.dart b/lib/providers/recently_played_provider.dart index 6bef2af..7cddbe7 100644 --- a/lib/providers/recently_played_provider.dart +++ b/lib/providers/recently_played_provider.dart @@ -21,7 +21,14 @@ class RecentlyPlayedProvider with ChangeNotifier, StreamSubscriber { Future> fetch() async { final res = await get('songs/recently-played'); - final items = res.map((j) => Song.fromJson(j)).toList(); + final items = []; + + res.forEach((json) { + if (json['type'] == 'songs') { + items.add(Song.fromJson(json)); + } + }); + songs = _songProvider.syncWithVault(items); _loaded = true; notifyListeners(); diff --git a/lib/providers/song_provider.dart b/lib/providers/song_provider.dart index 26f61ed..9c9b9b9 100644 --- a/lib/providers/song_provider.dart +++ b/lib/providers/song_provider.dart @@ -104,12 +104,7 @@ class SongProvider with ChangeNotifier, StreamSubscriber { Future> _stateAwareFetch(String url, Object cacheKey) async { if (AppState.has(cacheKey)) return AppState.get(cacheKey); - - final res = await get(url); - final items = res.map((json) => Song.fromJson(json)).toList(); - AppState.set(cacheKey, items); - - return syncWithVault(items); + return AppState.set(cacheKey, parseFromJson(await get(url))); } Future> fetchRandom({int limit = 500}) async { @@ -129,6 +124,18 @@ class SongProvider with ChangeNotifier, StreamSubscriber { final items = res.map((json) => Song.fromJson(json)).toList(); return syncWithVault(items); } + + List parseFromJson(dynamic json) { + final songs = []; + + json.forEach((j) { + if (j['type'] == 'songs') { + songs.add(Song.fromJson(j)); + } + }); + + return syncWithVault(songs).toList(); + } } class SongPaginationConfig { diff --git a/lib/values/queue_state.dart b/lib/values/queue_state.dart index 5da0ab5..4f3b0c0 100644 --- a/lib/values/queue_state.dart +++ b/lib/values/queue_state.dart @@ -1,4 +1,5 @@ import 'package:app/models/models.dart'; +import 'package:app/providers/providers.dart'; class QueueState { List songs; @@ -11,14 +12,17 @@ class QueueState { this.playbackPosition = 0, }); - static parse(Map json) { + static parse(Map json, SongProvider songProvider) { + final songs = songProvider.parseFromJson(json['songs']); + + var currentSong = + json['current_song'] != null && json['current_song']['type'] == 'songs' + ? Song.fromJson(json['current_song']) + : null; + return QueueState( - songs: (json['songs'] as List) - .map((song) => Song.fromJson(song)) - .toList(), - currentSong: json['current_song'] != null - ? Song.fromJson(json['current_song']) - : null, + songs: songs, + currentSong: currentSong, playbackPosition: json['playback_position'], ); }