mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
feat: play initially available tracks of playlist/album immediately and fetch rest in background #670
This commit is contained in:
parent
c4023aa09d
commit
02acbd9327
@ -36,12 +36,23 @@ class PlaylistCard extends HookConsumerWidget {
|
|||||||
final updating = useState(false);
|
final updating = useState(false);
|
||||||
final me = ref.watch(meProvider);
|
final me = ref.watch(meProvider);
|
||||||
|
|
||||||
Future<List<Track>> fetchAllTracks() async {
|
Future<List<Track>> fetchInitialTracks() async {
|
||||||
if (playlist.id == 'user-liked-tracks') {
|
if (playlist.id == 'user-liked-tracks') {
|
||||||
return await ref.read(likedTracksProvider.future);
|
return await ref.read(likedTracksProvider.future);
|
||||||
}
|
}
|
||||||
|
|
||||||
await ref.read(playlistTracksProvider(playlist.id!).future);
|
final result =
|
||||||
|
await ref.read(playlistTracksProvider(playlist.id!).future);
|
||||||
|
|
||||||
|
return result.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Track>> fetchAllTracks() async {
|
||||||
|
final initialTracks = await fetchInitialTracks();
|
||||||
|
|
||||||
|
if (playlist.id == 'user-liked-tracks') {
|
||||||
|
return initialTracks;
|
||||||
|
}
|
||||||
|
|
||||||
return ref.read(playlistTracksProvider(playlist.id!).notifier).fetchAll();
|
return ref.read(playlistTracksProvider(playlist.id!).notifier).fetchAll();
|
||||||
}
|
}
|
||||||
@ -77,23 +88,29 @@ class PlaylistCard extends HookConsumerWidget {
|
|||||||
return audioPlayer.resume();
|
return audioPlayer.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Track> fetchedTracks = await fetchAllTracks();
|
final fetchedInitialTracks = await fetchInitialTracks();
|
||||||
|
|
||||||
if (fetchedTracks.isEmpty || !context.mounted) return;
|
if (fetchedInitialTracks.isEmpty || !context.mounted) return;
|
||||||
|
|
||||||
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
||||||
if (isRemoteDevice) {
|
if (isRemoteDevice) {
|
||||||
final remotePlayback = ref.read(connectProvider.notifier);
|
final remotePlayback = ref.read(connectProvider.notifier);
|
||||||
|
final allTracks = await fetchAllTracks();
|
||||||
await remotePlayback.load(
|
await remotePlayback.load(
|
||||||
WebSocketLoadEventData.playlist(
|
WebSocketLoadEventData.playlist(
|
||||||
tracks: fetchedTracks,
|
tracks: allTracks,
|
||||||
collection: playlist,
|
collection: playlist,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
await playlistNotifier.load(fetchedTracks, autoPlay: true);
|
await playlistNotifier.load(fetchedInitialTracks, autoPlay: true);
|
||||||
playlistNotifier.addCollection(playlist.id!);
|
playlistNotifier.addCollection(playlist.id!);
|
||||||
historyNotifier.addPlaylists([playlist]);
|
historyNotifier.addPlaylists([playlist]);
|
||||||
|
|
||||||
|
final allTracks = await fetchAllTracks();
|
||||||
|
|
||||||
|
await playlistNotifier
|
||||||
|
.addTracks(allTracks.sublist(fetchedInitialTracks.length));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
@ -106,21 +123,22 @@ class PlaylistCard extends HookConsumerWidget {
|
|||||||
try {
|
try {
|
||||||
if (isPlaylistPlaying) return;
|
if (isPlaylistPlaying) return;
|
||||||
|
|
||||||
final fetchedTracks = await fetchAllTracks();
|
final fetchedInitialTracks = await fetchAllTracks();
|
||||||
|
|
||||||
if (fetchedTracks.isEmpty) return;
|
if (fetchedInitialTracks.isEmpty) return;
|
||||||
|
|
||||||
playlistNotifier.addTracks(fetchedTracks);
|
playlistNotifier.addTracks(fetchedInitialTracks);
|
||||||
playlistNotifier.addCollection(playlist.id!);
|
playlistNotifier.addCollection(playlist.id!);
|
||||||
historyNotifier.addPlaylists([playlist]);
|
historyNotifier.addPlaylists([playlist]);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
final snackbar = SnackBar(
|
final snackbar = SnackBar(
|
||||||
content: Text("Added ${fetchedTracks.length} tracks to queue"),
|
content:
|
||||||
|
Text("Added ${fetchedInitialTracks.length} tracks to queue"),
|
||||||
action: SnackBarAction(
|
action: SnackBarAction(
|
||||||
label: "Undo",
|
label: "Undo",
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
playlistNotifier
|
playlistNotifier
|
||||||
.removeTracks(fetchedTracks.map((e) => e.id!));
|
.removeTracks(fetchedInitialTracks.map((e) => e.id!));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -47,12 +47,12 @@ class TrackViewHeaderButtons extends HookConsumerWidget {
|
|||||||
try {
|
try {
|
||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
|
|
||||||
final allTracks = await props.pagination.onFetchAll();
|
final initialTracks = props.tracks;
|
||||||
|
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
|
|
||||||
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
||||||
if (isRemoteDevice) {
|
if (isRemoteDevice) {
|
||||||
|
final allTracks = await props.pagination.onFetchAll();
|
||||||
final remotePlayback = ref.read(connectProvider.notifier);
|
final remotePlayback = ref.read(connectProvider.notifier);
|
||||||
await remotePlayback.load(
|
await remotePlayback.load(
|
||||||
props.collection is AlbumSimple
|
props.collection is AlbumSimple
|
||||||
@ -69,9 +69,9 @@ class TrackViewHeaderButtons extends HookConsumerWidget {
|
|||||||
await remotePlayback.setShuffle(true);
|
await remotePlayback.setShuffle(true);
|
||||||
} else {
|
} else {
|
||||||
await playlistNotifier.load(
|
await playlistNotifier.load(
|
||||||
allTracks,
|
initialTracks,
|
||||||
autoPlay: true,
|
autoPlay: true,
|
||||||
initialIndex: Random().nextInt(allTracks.length),
|
initialIndex: Random().nextInt(initialTracks.length),
|
||||||
);
|
);
|
||||||
await audioPlayer.setShuffle(true);
|
await audioPlayer.setShuffle(true);
|
||||||
playlistNotifier.addCollection(props.collectionId);
|
playlistNotifier.addCollection(props.collectionId);
|
||||||
@ -80,6 +80,12 @@ class TrackViewHeaderButtons extends HookConsumerWidget {
|
|||||||
} else {
|
} else {
|
||||||
historyNotifier.addPlaylists([props.collection as PlaylistSimple]);
|
historyNotifier.addPlaylists([props.collection as PlaylistSimple]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final allTracks = await props.pagination.onFetchAll();
|
||||||
|
|
||||||
|
await playlistNotifier.addTracks(
|
||||||
|
allTracks.sublist(initialTracks.length),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isLoading.value = false;
|
isLoading.value = false;
|
||||||
@ -90,12 +96,13 @@ class TrackViewHeaderButtons extends HookConsumerWidget {
|
|||||||
try {
|
try {
|
||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
|
|
||||||
final allTracks = await props.pagination.onFetchAll();
|
final initialTracks = props.tracks;
|
||||||
|
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
|
|
||||||
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
final isRemoteDevice = await showSelectDeviceDialog(context, ref);
|
||||||
if (isRemoteDevice) {
|
if (isRemoteDevice) {
|
||||||
|
final allTracks = await props.pagination.onFetchAll();
|
||||||
final remotePlayback = ref.read(connectProvider.notifier);
|
final remotePlayback = ref.read(connectProvider.notifier);
|
||||||
await remotePlayback.load(
|
await remotePlayback.load(
|
||||||
props.collection is AlbumSimple
|
props.collection is AlbumSimple
|
||||||
@ -109,13 +116,19 @@ class TrackViewHeaderButtons extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
await playlistNotifier.load(allTracks, autoPlay: true);
|
await playlistNotifier.load(initialTracks, autoPlay: true);
|
||||||
playlistNotifier.addCollection(props.collectionId);
|
playlistNotifier.addCollection(props.collectionId);
|
||||||
if (props.collection is AlbumSimple) {
|
if (props.collection is AlbumSimple) {
|
||||||
historyNotifier.addAlbums([props.collection as AlbumSimple]);
|
historyNotifier.addAlbums([props.collection as AlbumSimple]);
|
||||||
} else {
|
} else {
|
||||||
historyNotifier.addPlaylists([props.collection as PlaylistSimple]);
|
historyNotifier.addPlaylists([props.collection as PlaylistSimple]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final allTracks = await props.pagination.onFetchAll();
|
||||||
|
|
||||||
|
await playlistNotifier.addTracks(
|
||||||
|
allTracks.sublist(initialTracks.length),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isLoading.value = false;
|
isLoading.value = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user