2020-06-23 19:23:12 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-10-09 18:52:45 +00:00
|
|
|
import 'package:freezer/api/cache.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/download.dart';
|
|
|
|
import 'package:freezer/ui/details_screens.dart';
|
|
|
|
import 'package:freezer/ui/error.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2020-10-14 19:09:16 +00:00
|
|
|
import 'package:share/share.dart';
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
import '../api/definitions.dart';
|
|
|
|
import 'cached_image.dart';
|
|
|
|
|
|
|
|
class MenuSheet {
|
|
|
|
|
|
|
|
BuildContext context;
|
|
|
|
|
|
|
|
MenuSheet(this.context);
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// DEFAULT
|
|
|
|
//===================
|
|
|
|
|
|
|
|
void show(List<Widget> options) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
isScrollControlled: true,
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
maxHeight: (MediaQuery.of(context).orientation == Orientation.landscape)?220:350,
|
|
|
|
),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: options
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// TRACK
|
|
|
|
//===================
|
|
|
|
|
|
|
|
void showWithTrack(Track track, List<Widget> options) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
isScrollControlled: true,
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(height: 16.0,),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: <Widget>[
|
|
|
|
CachedImage(
|
|
|
|
url: track.albumArt.full,
|
|
|
|
height: 128,
|
|
|
|
width: 128,
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 240.0,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
track.title,
|
|
|
|
maxLines: 1,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22.0,
|
|
|
|
fontWeight: FontWeight.bold
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
track.artistString,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 20.0
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 8.0,),
|
|
|
|
Text(
|
|
|
|
track.album.title,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
track.durationString
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Container(height: 16.0,),
|
|
|
|
ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
maxHeight: (MediaQuery.of(context).orientation == Orientation.landscape)?220:350,
|
|
|
|
),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: options
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Default track options
|
|
|
|
void defaultTrackMenu(Track track, {List<Widget> options = const [], Function onRemove}) {
|
|
|
|
showWithTrack(track, [
|
|
|
|
addToQueueNext(track),
|
|
|
|
addToQueue(track),
|
2020-10-09 18:52:45 +00:00
|
|
|
(cache.checkTrackFavorite(track))?removeFavoriteTrack(track, onUpdate: onRemove):addTrackFavorite(track),
|
2020-06-23 19:23:12 +00:00
|
|
|
addToPlaylist(track),
|
|
|
|
downloadTrack(track),
|
2020-10-14 19:09:16 +00:00
|
|
|
shareTile('track', track.id),
|
2020-06-23 19:23:12 +00:00
|
|
|
showAlbum(track.album),
|
|
|
|
...List.generate(track.artists.length, (i) => showArtist(track.artists[i])),
|
|
|
|
...options
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// TRACK OPTIONS
|
|
|
|
//===================
|
|
|
|
|
|
|
|
Widget addToQueueNext(Track t) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Play next'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.playlist_play),
|
|
|
|
onTap: () async {
|
2020-08-13 17:39:22 +00:00
|
|
|
//-1 = next
|
|
|
|
await AudioService.addQueueItemAt(t.toMediaItem(), -1);
|
2020-06-23 19:23:12 +00:00
|
|
|
_close();
|
|
|
|
});
|
|
|
|
|
|
|
|
Widget addToQueue(Track t) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add to queue'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.playlist_add),
|
|
|
|
onTap: () async {
|
|
|
|
await AudioService.addQueueItem(t.toMediaItem());
|
|
|
|
_close();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget addTrackFavorite(Track t) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add track to favorites'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.favorite),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.addFavoriteTrack(t.id);
|
|
|
|
//Make track offline, if favorites are offline
|
|
|
|
Playlist p = Playlist(id: deezerAPI.favoritesPlaylistId);
|
|
|
|
if (await downloadManager.checkOffline(playlist: p)) {
|
|
|
|
downloadManager.addOfflinePlaylist(p);
|
|
|
|
}
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Added to library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
toastLength: Toast.LENGTH_SHORT
|
|
|
|
);
|
2020-10-09 18:52:45 +00:00
|
|
|
//Add to cache
|
|
|
|
if (cache.libraryTracks == null)
|
|
|
|
cache.libraryTracks = [];
|
|
|
|
cache.libraryTracks.add(t.id);
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
_close();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget downloadTrack(Track t) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Download'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.file_download),
|
|
|
|
onTap: () async {
|
|
|
|
await downloadManager.addOfflineTrack(t, private: false);
|
|
|
|
_close();
|
2020-10-09 18:52:45 +00:00
|
|
|
showDownloadStartedToast();
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget addToPlaylist(Track t) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add to playlist'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.playlist_add),
|
|
|
|
onTap: () async {
|
|
|
|
//Show dialog to pick playlist
|
|
|
|
await showDialog(
|
2020-10-09 18:52:45 +00:00
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return SelectPlaylistDialog(track: t, callback: (Playlist p) async {
|
|
|
|
await deezerAPI.addToPlaylist(t.id, p.id);
|
|
|
|
//Update the playlist if offline
|
|
|
|
if (await downloadManager.checkOffline(playlist: p)) {
|
|
|
|
downloadManager.addOfflinePlaylist(p);
|
|
|
|
}
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: "Track added to".i18n + " ${p.title}",
|
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
2020-06-23 19:23:12 +00:00
|
|
|
);
|
2020-10-09 18:52:45 +00:00
|
|
|
});
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
2020-10-09 18:52:45 +00:00
|
|
|
);
|
2020-06-23 19:23:12 +00:00
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget removeFromPlaylist(Track t, Playlist p) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Remove from playlist'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.delete),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.removeFromPlaylist(t.id, p.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Track removed from'.i18n + ' ${p.title}',
|
2020-06-23 19:23:12 +00:00
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget removeFavoriteTrack(Track t, {onUpdate}) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Remove favorite'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.delete),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.removeFavorite(t.id);
|
|
|
|
//Check if favorites playlist is offline, update it
|
|
|
|
Playlist p = Playlist(id: deezerAPI.favoritesPlaylistId);
|
|
|
|
if (await downloadManager.checkOffline(playlist: p)) {
|
|
|
|
await downloadManager.addOfflinePlaylist(p);
|
|
|
|
}
|
2020-10-09 18:52:45 +00:00
|
|
|
//Remove from cache
|
|
|
|
if (cache.libraryTracks != null)
|
|
|
|
cache.libraryTracks.removeWhere((i) => i == t.id);
|
2020-06-23 19:23:12 +00:00
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Track removed from library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
2020-10-09 18:52:45 +00:00
|
|
|
if (onUpdate != null)
|
|
|
|
onUpdate();
|
2020-06-23 19:23:12 +00:00
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//Redirect to artist page (ie from track)
|
|
|
|
Widget showArtist(Artist a) => ListTile(
|
|
|
|
title: Text(
|
2020-09-18 17:36:41 +00:00
|
|
|
'Go to'.i18n + ' ${a.name}',
|
2020-06-23 19:23:12 +00:00
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
leading: Icon(Icons.recent_actors),
|
|
|
|
onTap: () {
|
|
|
|
_close();
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => ArtistDetails(a))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget showAlbum(Album a) => ListTile(
|
|
|
|
title: Text(
|
2020-09-18 17:36:41 +00:00
|
|
|
'Go to'.i18n + ' ${a.title}',
|
2020-06-23 19:23:12 +00:00
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
leading: Icon(Icons.album),
|
|
|
|
onTap: () {
|
|
|
|
_close();
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => AlbumDetails(a))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// ALBUM
|
|
|
|
//===================
|
|
|
|
|
|
|
|
//Default album options
|
|
|
|
void defaultAlbumMenu(Album album, {List<Widget> options = const [], Function onRemove}) {
|
|
|
|
show([
|
|
|
|
album.library?removeAlbum(album, onRemove: onRemove):libraryAlbum(album),
|
|
|
|
downloadAlbum(album),
|
|
|
|
offlineAlbum(album),
|
2020-10-14 19:09:16 +00:00
|
|
|
shareTile('album', album.id),
|
2020-06-23 19:23:12 +00:00
|
|
|
...options
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// ALBUM OPTIONS
|
|
|
|
//===================
|
|
|
|
|
|
|
|
Widget downloadAlbum(Album a) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Download'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.file_download),
|
|
|
|
onTap: () async {
|
|
|
|
_close();
|
2020-10-09 18:52:45 +00:00
|
|
|
await downloadManager.addOfflineAlbum(a, private: false);
|
|
|
|
showDownloadStartedToast();
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget offlineAlbum(Album a) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Make offline'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.offline_pin),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.addFavoriteAlbum(a.id);
|
|
|
|
await downloadManager.addOfflineAlbum(a, private: true);
|
|
|
|
_close();
|
2020-10-09 18:52:45 +00:00
|
|
|
showDownloadStartedToast();
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget libraryAlbum(Album a) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add to library'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.library_music),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.addFavoriteAlbum(a.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Added to library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//Remove album from favorites
|
|
|
|
Widget removeAlbum(Album a, {Function onRemove}) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Remove album'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.delete),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.removeAlbum(a.id);
|
|
|
|
await downloadManager.removeOfflineAlbum(a.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Album removed'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
if (onRemove != null) onRemove();
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// ARTIST
|
|
|
|
//===================
|
|
|
|
|
|
|
|
void defaultArtistMenu(Artist artist, {List<Widget> options = const [], Function onRemove}) {
|
|
|
|
show([
|
|
|
|
artist.library?removeArtist(artist, onRemove: onRemove):favoriteArtist(artist),
|
2020-10-14 19:09:16 +00:00
|
|
|
shareTile('artist', artist.id),
|
2020-06-23 19:23:12 +00:00
|
|
|
...options
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// ARTIST OPTIONS
|
|
|
|
//===================
|
|
|
|
|
|
|
|
Widget removeArtist(Artist a, {Function onRemove}) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Remove from favorites'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.delete),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.removeArtist(a.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Artist removed from library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
|
|
|
if (onRemove != null) onRemove();
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget favoriteArtist(Artist a) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add to favorites'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.favorite),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.addFavoriteArtist(a.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Added to library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// PLAYLIST
|
|
|
|
//===================
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
void defaultPlaylistMenu(Playlist playlist, {List<Widget> options = const [], Function onRemove, Function onUpdate}) {
|
2020-06-23 19:23:12 +00:00
|
|
|
show([
|
|
|
|
playlist.library?removePlaylistLibrary(playlist, onRemove: onRemove):addPlaylistLibrary(playlist),
|
|
|
|
addPlaylistOffline(playlist),
|
|
|
|
downloadPlaylist(playlist),
|
2020-10-14 19:09:16 +00:00
|
|
|
shareTile('playlist', playlist.id),
|
2020-10-09 18:52:45 +00:00
|
|
|
if (playlist.user.id == deezerAPI.userId)
|
|
|
|
editPlaylist(playlist, onUpdate: onUpdate),
|
2020-06-23 19:23:12 +00:00
|
|
|
...options
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// PLAYLIST OPTIONS
|
|
|
|
//===================
|
|
|
|
|
|
|
|
Widget removePlaylistLibrary(Playlist p, {Function onRemove}) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Remove from library'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.delete),
|
|
|
|
onTap: () async {
|
|
|
|
if (p.user.id.trim() == deezerAPI.userId) {
|
|
|
|
//Delete playlist if own
|
|
|
|
await deezerAPI.deletePlaylist(p.id);
|
|
|
|
} else {
|
|
|
|
//Just remove from library
|
|
|
|
await deezerAPI.removePlaylist(p.id);
|
|
|
|
}
|
|
|
|
downloadManager.removeOfflinePlaylist(p.id);
|
|
|
|
if (onRemove != null) onRemove();
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget addPlaylistLibrary(Playlist p) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Add playlist to library'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.favorite),
|
|
|
|
onTap: () async {
|
|
|
|
await deezerAPI.addPlaylist(p.id);
|
|
|
|
Fluttertoast.showToast(
|
2020-09-18 17:36:41 +00:00
|
|
|
msg: 'Added playlist to library'.i18n,
|
2020-06-23 19:23:12 +00:00
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
|
|
|
_close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget addPlaylistOffline(Playlist p) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Make playlist offline'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.offline_pin),
|
|
|
|
onTap: () async {
|
|
|
|
//Add to library
|
|
|
|
await deezerAPI.addPlaylist(p.id);
|
|
|
|
downloadManager.addOfflinePlaylist(p, private: true);
|
|
|
|
_close();
|
2020-10-09 18:52:45 +00:00
|
|
|
showDownloadStartedToast();
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget downloadPlaylist(Playlist p) => ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Download playlist'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
leading: Icon(Icons.file_download),
|
|
|
|
onTap: () async {
|
|
|
|
downloadManager.addOfflinePlaylist(p, private: false);
|
|
|
|
_close();
|
2020-10-09 18:52:45 +00:00
|
|
|
showDownloadStartedToast();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget editPlaylist(Playlist p, {Function onUpdate}) => ListTile(
|
|
|
|
title: Text('Edit playlist'.i18n),
|
|
|
|
leading: Icon(Icons.edit),
|
|
|
|
onTap: () async {
|
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => CreatePlaylistDialog(playlist: p)
|
|
|
|
);
|
|
|
|
_close();
|
|
|
|
if (onUpdate != null)
|
|
|
|
onUpdate();
|
2020-06-23 19:23:12 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
//===================
|
|
|
|
// OTHER
|
|
|
|
//===================
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
showDownloadStartedToast() {
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Downloads added!'.i18n,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
toastLength: Toast.LENGTH_SHORT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
//Create playlist
|
2020-10-09 18:52:45 +00:00
|
|
|
Future createPlaylist() async {
|
|
|
|
await showDialog(
|
2020-06-23 19:23:12 +00:00
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return CreatePlaylistDialog();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:09:16 +00:00
|
|
|
Widget shareTile(String type, String id) => ListTile(
|
|
|
|
title: Text('Share'.i18n),
|
|
|
|
leading: Icon(Icons.share),
|
|
|
|
onTap: () async {
|
|
|
|
Share.share('https://deezer.com/$type/$id');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
void _close() => Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
class SelectPlaylistDialog extends StatefulWidget {
|
|
|
|
|
|
|
|
final Track track;
|
|
|
|
final Function callback;
|
|
|
|
SelectPlaylistDialog({this.track, this.callback, Key key}): super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_SelectPlaylistDialogState createState() => _SelectPlaylistDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SelectPlaylistDialogState extends State<SelectPlaylistDialog> {
|
|
|
|
|
|
|
|
bool createNew = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
|
|
//Create new playlist
|
|
|
|
if (createNew) {
|
|
|
|
if (widget.track == null) {
|
|
|
|
return CreatePlaylistDialog();
|
|
|
|
}
|
|
|
|
return CreatePlaylistDialog(tracks: [widget.track]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text('Select playlist'.i18n),
|
|
|
|
content: FutureBuilder(
|
|
|
|
future: deezerAPI.getPlaylists(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
|
|
|
if (snapshot.hasError) SizedBox(
|
|
|
|
height: 100,
|
|
|
|
child: ErrorScreen(),
|
|
|
|
);
|
|
|
|
if (snapshot.connectionState != ConnectionState.done) return SizedBox(
|
|
|
|
height: 100,
|
|
|
|
child: Center(child: CircularProgressIndicator(),),
|
|
|
|
);
|
|
|
|
|
|
|
|
List<Playlist> playlists = snapshot.data;
|
|
|
|
return SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
...List.generate(playlists.length, (i) => ListTile(
|
|
|
|
title: Text(playlists[i].title),
|
|
|
|
leading: CachedImage(
|
|
|
|
url: playlists[i].image.thumb,
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
if (widget.callback != null) {
|
|
|
|
widget.callback(playlists[i]);
|
|
|
|
}
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Create new playlist'.i18n),
|
|
|
|
leading: Icon(Icons.add),
|
|
|
|
onTap: () async {
|
|
|
|
setState(() {
|
|
|
|
createNew = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
class CreatePlaylistDialog extends StatefulWidget {
|
|
|
|
|
|
|
|
final List<Track> tracks;
|
2020-10-09 18:52:45 +00:00
|
|
|
//If playlist not null, update
|
|
|
|
final Playlist playlist;
|
|
|
|
CreatePlaylistDialog({this.tracks, this.playlist, Key key}): super(key: key);
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_CreatePlaylistDialogState createState() => _CreatePlaylistDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CreatePlaylistDialogState extends State<CreatePlaylistDialog> {
|
|
|
|
|
|
|
|
int _playlistType = 1;
|
|
|
|
String _title = '';
|
|
|
|
String _description = '';
|
2020-10-09 18:52:45 +00:00
|
|
|
TextEditingController _titleController;
|
|
|
|
TextEditingController _descController;
|
|
|
|
|
|
|
|
//Create or edit mode
|
|
|
|
bool get edit => widget.playlist != null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
|
|
|
|
//Edit playlist mode
|
|
|
|
if (edit) {
|
|
|
|
_titleController = TextEditingController(text: widget.playlist.title);
|
|
|
|
_descController = TextEditingController(text: widget.playlist.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
2020-06-23 19:23:12 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
2020-10-09 18:52:45 +00:00
|
|
|
title: Text(edit ? 'Edit playlist'.i18n : 'Create playlist'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
TextField(
|
|
|
|
decoration: InputDecoration(
|
2020-09-18 17:36:41 +00:00
|
|
|
labelText: 'Title'.i18n
|
2020-06-23 19:23:12 +00:00
|
|
|
),
|
2020-10-09 18:52:45 +00:00
|
|
|
controller: _titleController ?? TextEditingController(),
|
2020-06-23 19:23:12 +00:00
|
|
|
onChanged: (String s) => _title = s,
|
|
|
|
),
|
|
|
|
TextField(
|
|
|
|
onChanged: (String s) => _description = s,
|
2020-10-09 18:52:45 +00:00
|
|
|
controller: _descController ?? TextEditingController(),
|
2020-06-23 19:23:12 +00:00
|
|
|
decoration: InputDecoration(
|
2020-09-18 17:36:41 +00:00
|
|
|
labelText: 'Description'.i18n
|
2020-06-23 19:23:12 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 4.0,),
|
|
|
|
DropdownButton<int>(
|
|
|
|
value: _playlistType,
|
|
|
|
onChanged: (int v) {
|
|
|
|
setState(() => _playlistType = v);
|
|
|
|
},
|
|
|
|
items: [
|
|
|
|
DropdownMenuItem<int>(
|
|
|
|
value: 1,
|
2020-09-18 17:36:41 +00:00
|
|
|
child: Text('Private'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
),
|
|
|
|
DropdownMenuItem<int>(
|
|
|
|
value: 2,
|
2020-09-18 17:36:41 +00:00
|
|
|
child: Text('Collaborative'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2020-09-18 17:36:41 +00:00
|
|
|
child: Text('Cancel'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
),
|
|
|
|
FlatButton(
|
2020-10-09 18:52:45 +00:00
|
|
|
child: Text(edit ? 'Update'.i18n : 'Create'.i18n),
|
2020-06-23 19:23:12 +00:00
|
|
|
onPressed: () async {
|
2020-10-09 18:52:45 +00:00
|
|
|
if (edit) {
|
|
|
|
//Update
|
|
|
|
await deezerAPI.updatePlaylist(
|
|
|
|
widget.playlist.id,
|
|
|
|
_titleController.value.text,
|
|
|
|
_descController.value.text,
|
|
|
|
status: _playlistType
|
|
|
|
);
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Playlist updated!'.i18n,
|
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
List<String> tracks = [];
|
|
|
|
if (widget.tracks != null) {
|
|
|
|
tracks = widget.tracks.map<String>((t) => t.id).toList();
|
|
|
|
}
|
|
|
|
await deezerAPI.createPlaylist(
|
|
|
|
_title,
|
|
|
|
status: _playlistType,
|
|
|
|
description: _description,
|
|
|
|
trackIds: tracks
|
|
|
|
);
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Playlist created!'.i18n,
|
|
|
|
gravity: ToastGravity.BOTTOM
|
|
|
|
);
|
2020-06-23 19:23:12 +00:00
|
|
|
}
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|