From 68b440920c1c74b70d1f3090f4626f1840e3f5b7 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Tue, 19 Jul 2022 12:49:47 +0600 Subject: [PATCH] support for presearch next track lowers the track change latency --- lib/hooks/useUpdateChecker.dart | 4 ++-- lib/provider/Playback.dart | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/hooks/useUpdateChecker.dart b/lib/hooks/useUpdateChecker.dart index 1efac132..672bcb4b 100644 --- a/lib/hooks/useUpdateChecker.dart +++ b/lib/hooks/useUpdateChecker.dart @@ -54,8 +54,8 @@ void useUpdateChecker(WidgetRef ref) { showDialog( context: context, builder: (context) { - final url = - "https://github.com/KRTirtho/spotube/releases/tag/v${value.last}"; + const url = + "https://spotube.netlify.app/other-downloads/stable-downloads"; return AlertDialog( title: const Text("Spotube has an update"), actions: [ diff --git a/lib/provider/Playback.dart b/lib/provider/Playback.dart index 0cd7e205..e6431933 100644 --- a/lib/provider/Playback.dart +++ b/lib/provider/Playback.dart @@ -58,6 +58,8 @@ class Playback extends PersistedChangeNotifier { // internal stuff final List _subscriptions; final _logger = getLogger(Playback); + // state of preSearch used inside [onPositionChanged] + bool _isPreSearching = false; PlaybackStatus status; @@ -110,6 +112,26 @@ class Playback extends PersistedChangeNotifier { currentDuration = await player.getDuration() ?? Duration.zero; notifyListeners(); } + + final currentTrackIndex = + playlist!.tracks.indexWhere((t) => t.id == track?.id); + + // when the track progress is above 80%, track isn't the last + // and is not already fetch and nothing is fetching currently + if (pos.inSeconds > currentDuration.inSeconds * .8 && + playlist != null && + currentTrackIndex != playlist!.tracks.length - 1 && + playlist!.tracks.elementAt(currentTrackIndex + 1) + is! SpotubeTrack && + !_isPreSearching) { + _isPreSearching = true; + playlist!.tracks[currentTrackIndex + 1] = await toSpotubeTrack( + playlist!.tracks[currentTrackIndex + 1], + ).then((v) { + _isPreSearching = false; + return v; + }); + } }), ]); }()); @@ -389,7 +411,9 @@ class Playback extends PersistedChangeNotifier { (playlist!.trackIds.indexOf(track!.id!) + 1).toInt(); // checking if there's any track available forward if (nextTrackIndex > (playlist?.tracks.length ?? 0) - 1) return; - await play(playlist!.tracks.elementAt(nextTrackIndex)); + await play(playlist!.tracks.elementAt(nextTrackIndex)).then((_) { + playlist!.tracks[nextTrackIndex] = track!; + }); } Future seekBackward() async { @@ -398,7 +422,9 @@ class Playback extends PersistedChangeNotifier { (playlist!.trackIds.indexOf(track!.id!) - 1).toInt(); // checking if there's any track available behind if (prevTrackIndex < 0) return; - await play(playlist!.tracks.elementAt(prevTrackIndex)); + await play(playlist!.tracks.elementAt(prevTrackIndex)).then((_) { + playlist!.tracks[prevTrackIndex] = track!; + }); } @override