Skip to content

Commit

Permalink
fix: cached currently playing track infinite loading
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 4, 2023
1 parent 957c085 commit 9401718
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
59 changes: 34 additions & 25 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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/services/youtube.dart';
import 'package:spotube/utils/persisted_state_notifier.dart';
import 'package:spotube/utils/type_conversion_utils.dart';

Expand Down Expand Up @@ -215,29 +216,35 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
int initialIndex = 0,
bool autoPlay = false,
}) async {
tracks = blacklist.filter(tracks).toList() as List<Track>;
final addableTrack = await SpotubeTrack.fetchFromTrack(
tracks.elementAt(initialIndex),
preferences,
);
try {
tracks = blacklist.filter(tracks).toList() as List<Track>;
final addableTrack = await SpotubeTrack.fetchFromTrack(
tracks.elementAt(initialIndex),
preferences,
);

state = state.copyWith(
tracks: mergeTracks([addableTrack], tracks),
active: initialIndex,
);
print('addableTrack: $addableTrack');

await notificationService.addTrack(addableTrack);
state = state.copyWith(
tracks: mergeTracks([addableTrack], tracks),
active: initialIndex,
);

await audioPlayer.openPlaylist(
state.tracks.map(makeAppropriateSource).toList(),
initialIndex: initialIndex,
autoPlay: autoPlay,
);
await notificationService.addTrack(addableTrack);

await storeTrack(
tracks.elementAt(initialIndex),
addableTrack,
);
await audioPlayer.openPlaylist(
state.tracks.map(makeAppropriateSource).toList(),
initialIndex: initialIndex,
autoPlay: autoPlay,
);

await storeTrack(
tracks.elementAt(initialIndex),
addableTrack,
);
} catch (e) {
print('Error: $e');
}
}

Future<void> jumpTo(int index) async {
Expand Down Expand Up @@ -404,14 +411,16 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
}

@override
onInit() {
onInit() async {
if (state.tracks.isEmpty) return null;

return load(
state.tracks,
initialIndex: state.active ?? 0,
autoPlay: false,
);
if (await PipedSpotube.initialized) {
await load(
state.tracks,
initialIndex: state.active ?? 0,
autoPlay: false,
);
}
}

@override
Expand Down
6 changes: 6 additions & 0 deletions lib/services/youtube.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:async';

import 'package:piped_client/piped_client.dart';

PipedClient _defaultClient = PipedClient();

class PipedSpotube {
static final Completer<bool> _initialized = Completer();
static Future<bool> get initialized => _initialized.future;

/// Checks for a working instance of piped.video
///
/// To distribute the load, in each startup it randomizes public instances
Expand All @@ -15,6 +20,7 @@ class PipedSpotube {
try {
await client.streams("dQw4w9WgXcQ");
_defaultClient = client;
_initialized.complete(true);
break;
} catch (e) {
continue;
Expand Down

0 comments on commit 9401718

Please sign in to comment.