Skip to content

Commit

Permalink
feat: enable caching of queue
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed May 27, 2023
1 parent e529c79 commit ec11af5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
1 change: 0 additions & 1 deletion lib/models/spotube_track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:spotube/extensions/album_simple.dart';
import 'package:spotube/extensions/artist_simple.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/supabase.dart';
import 'package:spotube/services/youtube.dart';
import 'package:spotube/utils/platform.dart';
import 'package:spotube/utils/service_utils.dart';
Expand Down
6 changes: 3 additions & 3 deletions lib/provider/proxy_playlist/proxy_playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class ProxyPlaylist {
ProxyPlaylist(this.tracks, [this.active]);
factory ProxyPlaylist.fromJson(Map<String, dynamic> json) {
return ProxyPlaylist(
(json['tracks'] as List<Map<String, dynamic>>)
.map(_makeAppropriateTrack)
.toSet(),
List.castFrom<dynamic, Map<String, dynamic>>(
json['tracks'] ?? <Map<String, dynamic>>[],
).map(_makeAppropriateTrack).toSet(),
json['active'] as int,
);
}
Expand Down
41 changes: 31 additions & 10 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:async';
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:palette_generator/palette_generator.dart';
Expand All @@ -13,6 +16,7 @@ import 'package:spotube/provider/proxy_playlist/proxy_playlist.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/services/audio_services/audio_services.dart';
import 'package:spotube/utils/persisted_state_notifier.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

/// Things to implement:
Expand All @@ -24,7 +28,7 @@ import 'package:spotube/utils/type_conversion_utils.dart';
/// * [x] Add track at the beginning
/// * [x] Remove track
/// * [ ] Reorder track
/// * [ ] Caching and loading of cache of tracks
/// * [x] Caching and loading of cache of tracks
/// * [x] Shuffling
/// * [x] loop => playlist, track, none
/// * [ ] Alternative Track Source
Expand All @@ -35,7 +39,7 @@ import 'package:spotube/utils/type_conversion_utils.dart';
/// * It'll not store any sort of player state e.g playing, paused, shuffled etc
/// * For that, use [SpotubeAudioPlayer]
class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
with NextFetcher {
final Ref ref;
late final AudioServices notificationService;
Expand All @@ -52,7 +56,7 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
static AlwaysAliveRefreshable<ProxyPlaylistNotifier> get notifier =>
provider.notifier;

ProxyPlaylistNotifier(this.ref) : super(ProxyPlaylist({})) {
ProxyPlaylistNotifier(this.ref) : super(ProxyPlaylist({}), "playlist") {
() async {
notificationService = await AudioServices.create(ref, this);

Expand Down Expand Up @@ -80,9 +84,6 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
audioPlayer.shuffledStream.listen((event) {
final newlyOrderedTracks = mapSourcesToTracks(audioPlayer.sources);

print(
'newlyOrderedTracks: ${newlyOrderedTracks.map((e) => e.name).toList()}');

final newActiveIndex = newlyOrderedTracks.indexWhere(
(element) => element.id == state.activeTrack?.id,
);
Expand Down Expand Up @@ -207,13 +208,13 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
}

Future<void> load(
List<Track> tracks, {
Iterable<Track> tracks, {
int initialIndex = 0,
bool autoPlay = false,
}) async {
tracks = blacklist.filter(tracks).toList() as List<Track>;
final addableTrack =
await SpotubeTrack.fetchFromTrack(tracks[initialIndex], preferences);
final addableTrack = await SpotubeTrack.fetchFromTrack(
tracks.elementAt(initialIndex), preferences);

state = state.copyWith(
tracks: mergeTracks([addableTrack], tracks),
Expand All @@ -227,7 +228,7 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
);

await storeTrack(
tracks[initialIndex],
tracks.elementAt(initialIndex),
addableTrack,
);
}
Expand Down Expand Up @@ -385,4 +386,24 @@ class ProxyPlaylistNotifier extends StateNotifier<ProxyPlaylist>
ref.read(paletteProvider.notifier).state = null;
}
}

@override
onInit() {
return load(
state.tracks,
initialIndex: state.active ?? 0,
autoPlay: false,
);
}

@override
FutureOr<ProxyPlaylist> fromJson(Map<String, dynamic> json) {
return ProxyPlaylist.fromJson(json);
}

@override
Map<String, dynamic> toJson() {
final json = state.toJson();
return json;
}
}
2 changes: 1 addition & 1 deletion lib/utils/persisted_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
}

void save() async {
box.put(cacheKey, toJson());
await box.put(cacheKey, toJson());
}

FutureOr<T> fromJson(Map<String, dynamic> json);
Expand Down
8 changes: 3 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1306,11 +1306,9 @@ packages:
piped_client:
dependency: "direct main"
description:
path: "."
ref: "2036a78d3414a0fc7fe3b081f1029dd086352fcd"
resolved-ref: "2036a78d3414a0fc7fe3b081f1029dd086352fcd"
url: "https://github.com/KRTirtho/piped_client"
source: git
path: "../piped_client"
relative: true
source: path
version: "0.1.0"
platform:
dependency: transitive
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ dependencies:
piped_client:
git:
url: https://github.com/KRTirtho/piped_client
ref: 2036a78d3414a0fc7fe3b081f1029dd086352fcd
ref: eaade37d0938d31dbfa35bb5152caca4e284bda6
supabase_flutter: ^1.9.1

dev_dependencies:
Expand Down

0 comments on commit ec11af5

Please sign in to comment.