-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add selected tracks to playlists, optimistic playlist remove track
- Loading branch information
Showing
6 changed files
with
176 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'package:fl_query_hooks/fl_query_hooks.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:platform_ui/platform_ui.dart'; | ||
import 'package:spotify/spotify.dart'; | ||
import 'package:spotube/provider/SpotifyDI.dart'; | ||
import 'package:spotube/provider/SpotifyRequests.dart'; | ||
|
||
class AddTracksToPlaylistDialog extends HookConsumerWidget { | ||
final List<Track> tracks; | ||
const AddTracksToPlaylistDialog({ | ||
required this.tracks, | ||
Key? key, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context, ref) { | ||
final spotify = ref.watch(spotifyProvider); | ||
final userPlaylists = useQuery( | ||
job: currentUserPlaylistsQueryJob, | ||
externalData: spotify, | ||
); | ||
final me = useQuery( | ||
job: currentUserQueryJob, | ||
externalData: spotify, | ||
); | ||
final filteredPlaylists = userPlaylists.data?.where( | ||
(playlist) => | ||
playlist.owner?.id != null && playlist.owner!.id == me.data?.id, | ||
); | ||
final playlistsCheck = useState(<String, bool>{}); | ||
|
||
return PlatformAlertDialog( | ||
title: const PlatformText("Add to Playlist"), | ||
secondaryActions: [ | ||
PlatformFilledButton( | ||
isSecondary: true, | ||
child: const PlatformText("Cancel"), | ||
onPressed: () => Navigator.pop(context), | ||
), | ||
], | ||
primaryActions: [ | ||
PlatformFilledButton( | ||
child: const PlatformText("Add"), | ||
onPressed: () async { | ||
final selectedPlaylists = playlistsCheck.value.entries | ||
.where((entry) => entry.value) | ||
.map((entry) => entry.key); | ||
|
||
await Future.wait( | ||
selectedPlaylists.map( | ||
(playlistId) => spotify.playlists.addTracks( | ||
tracks | ||
.map( | ||
(track) => track.uri!, | ||
) | ||
.toList(), | ||
playlistId), | ||
), | ||
).then((_) => Navigator.pop(context)); | ||
}, | ||
) | ||
], | ||
content: SizedBox( | ||
height: 300, | ||
width: 300, | ||
child: !userPlaylists.hasData | ||
? const Center(child: PlatformCircularProgressIndicator()) | ||
: ListView.builder( | ||
shrinkWrap: true, | ||
itemCount: filteredPlaylists!.length, | ||
itemBuilder: (context, index) { | ||
final playlist = filteredPlaylists.elementAt(index); | ||
return PlatformCheckbox( | ||
label: PlatformText(playlist.name!), | ||
value: playlistsCheck.value[playlist.id] ?? false, | ||
onChanged: (val) { | ||
playlistsCheck.value = { | ||
...playlistsCheck.value, | ||
playlist.id!: val == true | ||
}; | ||
}, | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters