freezer/lib/ui/menu.dart

884 lines
27 KiB
Dart
Raw Normal View History

2020-10-15 20:10:17 +00:00
import 'dart:async';
2020-12-27 18:33:59 +00:00
import 'package:freezer/main.dart';
import 'package:wakelock/wakelock.dart';
2020-06-23 19:23:12 +00:00
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
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';
2020-10-24 21:00:29 +00:00
import 'package:freezer/api/player.dart';
2020-06-23 19:23:12 +00:00
import 'package:freezer/ui/details_screens.dart';
import 'package:freezer/ui/error.dart';
import 'package:freezer/translations.i18n.dart';
2020-12-27 18:33:59 +00:00
import 'package:freezer/api/definitions.dart';
import 'package:freezer/ui/cached_image.dart';
2020-10-15 20:10:17 +00:00
import 'package:numberpicker/numberpicker.dart';
import 'package:share/share.dart';
2020-11-28 21:32:17 +00:00
import 'package:url_launcher/url_launcher.dart';
2020-06-23 19:23:12 +00:00
class MenuSheet {
BuildContext context;
2021-09-01 12:38:32 +00:00
Function? navigateCallback;
2020-06-23 19:23:12 +00:00
2020-12-27 18:33:59 +00:00
MenuSheet(this.context, {this.navigateCallback});
2020-06-23 19:23:12 +00:00
//===================
// DEFAULT
//===================
void show(List<Widget> options) {
showModalBottomSheet(
2021-08-29 22:25:18 +00:00
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
2021-09-02 20:45:14 +00:00
return SafeArea(
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: (MediaQuery.of(context).orientation ==
Orientation.landscape)
? 220
: 350,
),
child: SingleChildScrollView(
child: Column(children: options),
),
2021-08-29 22:25:18 +00:00
),
);
});
2020-06-23 19:23:12 +00:00
}
//===================
// TRACK
//===================
void showWithTrack(Track track, List<Widget> options) {
showModalBottomSheet(
2021-08-29 22:25:18 +00:00
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
2021-09-02 20:45:14 +00:00
return SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(height: 16.0),
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Semantics(
child: CachedImage(
url: track.albumArt!.full,
height: 128,
width: 128,
),
label: "Album art".i18n,
image: true,
2021-08-29 22:25:18 +00:00
),
2021-09-02 20:45:14 +00:00
SizedBox(
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)
],
),
2021-08-29 22:25:18 +00:00
),
2021-09-02 20:45:14 +00:00
],
2021-08-29 22:25:18 +00:00
),
2021-09-02 20:45:14 +00:00
const SizedBox(height: 16.0),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: (MediaQuery.of(context).orientation ==
Orientation.landscape)
? 220
: 350,
),
child: SingleChildScrollView(
child: Column(children: options),
),
)
],
),
2021-08-29 22:25:18 +00:00
);
});
2020-06-23 19:23:12 +00:00
}
//Default track options
2021-08-29 22:25:18 +00:00
void defaultTrackMenu(Track track,
2021-09-01 12:38:32 +00:00
{List<Widget> options = const [], Function? onRemove}) {
2020-06-23 19:23:12 +00:00
showWithTrack(track, [
addToQueueNext(track),
addToQueue(track),
2021-08-29 22:25:18 +00:00
(cache.checkTrackFavorite(track))
? removeFavoriteTrack(track, onUpdate: onRemove)
: addTrackFavorite(track),
2020-06-23 19:23:12 +00:00
addToPlaylist(track),
downloadTrack(track),
offlineTrack(track),
shareTile('track', track.id),
2020-10-24 21:00:29 +00:00
playMix(track),
2021-09-01 12:38:32 +00:00
showAlbum(track.album!),
2021-08-29 22:25:18 +00:00
...List.generate(
2021-09-01 12:38:32 +00:00
track.artists!.length, (i) => showArtist(track.artists![i])),
2020-06-23 19:23:12 +00:00
...options
]);
}
//===================
// TRACK OPTIONS
//===================
Widget addToQueueNext(Track t) => ListTile(
title: Text('Play next'.i18n),
2020-06-23 19:23:12 +00:00
leading: Icon(Icons.playlist_play),
onTap: () async {
//-1 = next
2021-09-01 12:38:32 +00:00
await audioHandler.insertQueueItem(-1, t.toMediaItem());
2020-06-23 19:23:12 +00:00
_close();
});
Widget addToQueue(Track t) => ListTile(
title: Text('Add to queue'.i18n),
2020-06-23 19:23:12 +00:00
leading: Icon(Icons.playlist_add),
onTap: () async {
2021-09-01 12:38:32 +00:00
await audioHandler.addQueueItem(t.toMediaItem());
2020-06-23 19:23:12 +00:00
_close();
2021-08-29 22:25:18 +00:00
});
2020-06-23 19:23:12 +00:00
Widget addTrackFavorite(Track t) => ListTile(
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);
}
ScaffoldMessenger.of(context).snack('Added to library'.i18n);
//Add to cache
2021-08-29 22:25:18 +00:00
if (cache.libraryTracks == null) cache.libraryTracks = [];
2021-09-01 12:38:32 +00:00
cache.libraryTracks!.add(t.id);
2020-06-23 19:23:12 +00:00
_close();
2021-08-29 22:25:18 +00:00
});
2020-06-23 19:23:12 +00:00
Widget downloadTrack(Track t) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Download'.i18n),
leading: Icon(Icons.file_download),
onTap: () async {
if (await downloadManager.addOfflineTrack(t,
private: false, context: context, isSingleton: true) !=
false) showDownloadStartedToast();
_close();
},
);
2020-06-23 19:23:12 +00:00
Widget addToPlaylist(Track t) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Add to playlist'.i18n),
leading: Icon(Icons.playlist_add),
onTap: () async {
//Show dialog to pick playlist
await showDialog(
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);
}
ScaffoldMessenger.of(context)
.snack("Track added to".i18n + " ${p.title}");
2021-08-29 22:25:18 +00:00
});
});
_close();
},
);
2020-06-23 19:23:12 +00:00
2021-09-01 12:38:32 +00:00
Widget removeFromPlaylist(Track t, Playlist? p) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Remove from playlist'.i18n),
leading: Icon(Icons.delete),
onTap: () async {
2021-09-01 12:38:32 +00:00
await deezerAPI.removeFromPlaylist(t.id, p!.id);
ScaffoldMessenger.of(context)
.snack('Track removed from'.i18n + ' ${p.title}');
2021-08-29 22:25:18 +00:00
_close();
},
2020-06-23 19:23:12 +00:00
);
Widget removeFavoriteTrack(Track t, {onUpdate}) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Remove favorite'.i18n),
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);
}
//Remove from cache
if (cache.libraryTracks != null)
2021-09-01 12:38:32 +00:00
cache.libraryTracks!.removeWhere((i) => i == t.id);
ScaffoldMessenger.of(context)
.snack('Track removed from library'.i18n);
2021-08-29 22:25:18 +00:00
if (onUpdate != null) onUpdate();
_close();
},
2020-06-23 19:23:12 +00:00
);
//Redirect to artist page (ie from track)
Widget showArtist(Artist a) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text(
'Go to'.i18n + ' ${a.name}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
leading: Icon(Icons.recent_actors),
onTap: () {
_close();
2021-09-01 12:38:32 +00:00
navigatorKey.currentState!
2021-08-29 22:25:18 +00:00
.push(MaterialPageRoute(builder: (context) => ArtistDetails(a)));
2020-12-27 18:33:59 +00:00
2021-08-29 22:25:18 +00:00
if (this.navigateCallback != null) {
2021-09-01 12:38:32 +00:00
this.navigateCallback!();
2021-08-29 22:25:18 +00:00
}
},
);
2020-06-23 19:23:12 +00:00
Widget showAlbum(Album a) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text(
'Go to'.i18n + ' ${a.title}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
leading: Icon(Icons.album),
onTap: () {
_close();
2021-09-01 12:38:32 +00:00
navigatorKey.currentState!
2021-08-29 22:25:18 +00:00
.push(MaterialPageRoute(builder: (context) => AlbumDetails(a)));
2020-12-27 18:33:59 +00:00
2021-08-29 22:25:18 +00:00
if (this.navigateCallback != null) {
2021-09-01 12:38:32 +00:00
this.navigateCallback!();
2021-08-29 22:25:18 +00:00
}
},
);
2020-06-23 19:23:12 +00:00
2020-10-24 21:00:29 +00:00
Widget playMix(Track track) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Play mix'.i18n),
leading: Icon(Icons.online_prediction),
2020-12-27 18:33:59 +00:00
onTap: () async {
2021-09-01 12:38:32 +00:00
playerHelper.playMix(track.id, track.title!);
2020-12-27 18:33:59 +00:00
_close();
},
);
2021-08-29 22:25:18 +00:00
Widget offlineTrack(Track track) => FutureBuilder(
future: downloadManager.checkOffline(track: track),
builder: (context, snapshot) {
2021-09-01 12:38:32 +00:00
bool isOffline = (snapshot.data as bool?) ?? (track.offline ?? false);
2021-08-29 22:25:18 +00:00
return ListTile(
title: Text(isOffline ? 'Remove offline'.i18n : 'Offline'.i18n),
leading: Icon(Icons.offline_pin),
onTap: () async {
if (isOffline) {
await downloadManager.removeOfflineTracks([track]);
ScaffoldMessenger.of(context)
.snack("Track removed from offline!".i18n);
2021-08-29 22:25:18 +00:00
} else {
await downloadManager.addOfflineTrack(track,
private: true, context: context);
}
_close();
},
);
},
);
2020-06-23 19:23:12 +00:00
//===================
// ALBUM
//===================
//Default album options
2021-08-29 22:25:18 +00:00
void defaultAlbumMenu(Album album,
2021-09-01 12:38:32 +00:00
{List<Widget> options = const [], Function? onRemove}) {
2020-06-23 19:23:12 +00:00
show([
2021-09-01 12:38:32 +00:00
album.library!
2021-08-29 22:25:18 +00:00
? removeAlbum(album, onRemove: onRemove)
: libraryAlbum(album),
2020-06-23 19:23:12 +00:00
downloadAlbum(album),
offlineAlbum(album),
shareTile('album', album.id),
2020-06-23 19:23:12 +00:00
...options
]);
}
//===================
// ALBUM OPTIONS
//===================
Widget downloadAlbum(Album a) => ListTile(
title: Text('Download'.i18n),
2020-06-23 19:23:12 +00:00
leading: Icon(Icons.file_download),
onTap: () async {
_close();
2021-08-29 22:25:18 +00:00
if (await downloadManager.addOfflineAlbum(a,
private: false, context: context) !=
false) showDownloadStartedToast();
});
2020-06-23 19:23:12 +00:00
Widget offlineAlbum(Album a) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Make offline'.i18n),
leading: Icon(Icons.offline_pin),
onTap: () async {
await deezerAPI.addFavoriteAlbum(a.id);
await downloadManager.addOfflineAlbum(a, private: true);
_close();
showDownloadStartedToast();
},
);
2020-06-23 19:23:12 +00:00
Widget libraryAlbum(Album a) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Add to library'.i18n),
leading: Icon(Icons.library_music),
onTap: () async {
await deezerAPI.addFavoriteAlbum(a.id);
ScaffoldMessenger.of(context).snack('Added to library'.i18n);
2021-08-29 22:25:18 +00:00
_close();
},
2020-06-23 19:23:12 +00:00
);
//Remove album from favorites
2021-09-01 12:38:32 +00:00
Widget removeAlbum(Album a, {Function? onRemove}) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Remove album'.i18n),
leading: Icon(Icons.delete),
onTap: () async {
await deezerAPI.removeAlbum(a.id);
await downloadManager.removeOfflineAlbum(a.id);
ScaffoldMessenger.of(context).snack('Album removed'.i18n);
2021-08-29 22:25:18 +00:00
if (onRemove != null) onRemove();
_close();
},
2020-06-23 19:23:12 +00:00
);
//===================
// ARTIST
//===================
2021-08-29 22:25:18 +00:00
void defaultArtistMenu(Artist artist,
2021-09-01 12:38:32 +00:00
{List<Widget> options = const [], Function? onRemove}) {
2020-06-23 19:23:12 +00:00
show([
2021-09-01 12:38:32 +00:00
artist.library!
2021-08-29 22:25:18 +00:00
? removeArtist(artist, onRemove: onRemove)
: favoriteArtist(artist),
shareTile('artist', artist.id),
2020-06-23 19:23:12 +00:00
...options
]);
}
//===================
// ARTIST OPTIONS
//===================
2021-09-01 12:38:32 +00:00
Widget removeArtist(Artist a, {Function? onRemove}) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Remove from favorites'.i18n),
leading: Icon(Icons.delete),
onTap: () async {
await deezerAPI.removeArtist(a.id);
ScaffoldMessenger.of(context)
.snack('Artist removed from library'.i18n);
2021-08-29 22:25:18 +00:00
if (onRemove != null) onRemove();
_close();
},
2020-06-23 19:23:12 +00:00
);
Widget favoriteArtist(Artist a) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Add to favorites'.i18n),
leading: Icon(Icons.favorite),
onTap: () async {
await deezerAPI.addFavoriteArtist(a.id);
ScaffoldMessenger.of(context).snack('Added to library'.i18n);
2021-08-29 22:25:18 +00:00
_close();
},
2020-06-23 19:23:12 +00:00
);
//===================
// PLAYLIST
//===================
2021-08-29 22:25:18 +00:00
void defaultPlaylistMenu(Playlist playlist,
2021-09-01 12:38:32 +00:00
{List<Widget> options = const [],
Function? onRemove,
Function? onUpdate}) {
2020-06-23 19:23:12 +00:00
show([
2021-09-01 12:38:32 +00:00
playlist.library!
2021-08-29 22:25:18 +00:00
? removePlaylistLibrary(playlist, onRemove: onRemove)
: addPlaylistLibrary(playlist),
2020-06-23 19:23:12 +00:00
addPlaylistOffline(playlist),
downloadPlaylist(playlist),
shareTile('playlist', playlist.id),
2021-09-01 12:38:32 +00:00
if (playlist.user!.id == deezerAPI.userId)
editPlaylist(playlist, onUpdate: onUpdate),
2020-06-23 19:23:12 +00:00
...options
]);
}
//===================
// PLAYLIST OPTIONS
//===================
2021-09-01 12:38:32 +00:00
Widget removePlaylistLibrary(Playlist p, {Function? onRemove}) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Remove from library'.i18n),
leading: Icon(Icons.delete),
onTap: () async {
2021-09-01 12:38:32 +00:00
if (p.user!.id!.trim() == deezerAPI.userId) {
2021-08-29 22:25:18 +00:00
//Delete playlist if own
await deezerAPI.deletePlaylist(p.id);
} else {
//Just remove from library
2021-09-01 12:38:32 +00:00
await deezerAPI.removePlaylist(p.id!);
2021-08-29 22:25:18 +00:00
}
downloadManager.removeOfflinePlaylist(p.id);
if (onRemove != null) onRemove();
_close();
},
);
2020-06-23 19:23:12 +00:00
Widget addPlaylistLibrary(Playlist p) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Add playlist to library'.i18n),
leading: Icon(Icons.favorite),
onTap: () async {
2021-09-01 12:38:32 +00:00
await deezerAPI.addPlaylist(p.id!);
ScaffoldMessenger.of(context).snack('Added playlist to library'.i18n);
2021-08-29 22:25:18 +00:00
_close();
},
2020-06-23 19:23:12 +00:00
);
Widget addPlaylistOffline(Playlist p) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Make playlist offline'.i18n),
leading: Icon(Icons.offline_pin),
onTap: () async {
//Add to library
2021-09-01 12:38:32 +00:00
await deezerAPI.addPlaylist(p.id!);
2021-08-29 22:25:18 +00:00
downloadManager.addOfflinePlaylist(p, private: true);
_close();
showDownloadStartedToast();
},
);
2020-06-23 19:23:12 +00:00
Widget downloadPlaylist(Playlist p) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Download playlist'.i18n),
leading: Icon(Icons.file_download),
onTap: () async {
_close();
if (await downloadManager.addOfflinePlaylist(p,
private: false, context: context) !=
false) showDownloadStartedToast();
},
);
2021-09-01 12:38:32 +00:00
Widget editPlaylist(Playlist p, {Function? onUpdate}) => ListTile(
2021-08-29 22:25:18 +00:00
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
2020-11-28 21:32:17 +00:00
//===================
// SHOW/EPISODE
//===================
2021-08-29 22:25:18 +00:00
defaultShowEpisodeMenu(Show s, ShowEpisode e,
{List<Widget> options = const []}) {
2020-11-28 21:32:17 +00:00
show([
shareTile('episode', e.id),
shareShow(s.id),
downloadExternalEpisode(e),
...options
]);
}
2021-09-01 12:38:32 +00:00
Widget shareShow(String? id) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Share show'.i18n),
leading: Icon(Icons.share),
onTap: () async {
Share.share('https://deezer.com/show/$id');
},
);
2020-11-28 21:32:17 +00:00
//Open direct download link in browser
Widget downloadExternalEpisode(ShowEpisode e) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Download externally'.i18n),
leading: Icon(Icons.file_download),
onTap: () async {
2021-09-01 12:38:32 +00:00
launch(e.url!);
2021-08-29 22:25:18 +00:00
},
);
2020-06-23 19:23:12 +00:00
//===================
// OTHER
//===================
showDownloadStartedToast() {
ScaffoldMessenger.of(context).snack('Downloads added!'.i18n);
}
2020-06-23 19:23:12 +00:00
//Create playlist
Future createPlaylist() async {
await showDialog(
2021-08-29 22:25:18 +00:00
context: context,
builder: (BuildContext context) {
return CreatePlaylistDialog();
});
2020-06-23 19:23:12 +00:00
}
2021-09-01 12:38:32 +00:00
Widget shareTile(String type, String? id) => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Share'.i18n),
leading: Icon(Icons.share),
onTap: () async {
Share.share('https://deezer.com/$type/$id');
},
);
2020-10-15 20:10:17 +00:00
Widget sleepTimer() => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Sleep timer'.i18n),
leading: Icon(Icons.access_time),
onTap: () async {
showDialog(
context: context,
builder: (context) {
return SleepTimerDialog();
});
},
2020-10-15 20:10:17 +00:00
);
2020-06-23 19:23:12 +00:00
Widget wakelock() => ListTile(
2021-08-29 22:25:18 +00:00
title: Text('Keep the screen on'.i18n),
leading: Icon(Icons.screen_lock_portrait),
onTap: () async {
_close();
//Enable
if (!cache.wakelock) {
Wakelock.enable();
ScaffoldMessenger.of(context).snack('Wakelock enabled!'.i18n);
2021-08-29 22:25:18 +00:00
cache.wakelock = true;
return;
}
//Disable
Wakelock.disable();
ScaffoldMessenger.of(context).snack('Wakelock disabled!'.i18n);
2021-08-29 22:25:18 +00:00
cache.wakelock = false;
},
);
2020-06-23 19:23:12 +00:00
void _close() => Navigator.of(context).pop();
}
2020-10-15 20:10:17 +00:00
class SleepTimerDialog extends StatefulWidget {
@override
_SleepTimerDialogState createState() => _SleepTimerDialogState();
}
class _SleepTimerDialogState extends State<SleepTimerDialog> {
int hours = 0;
int minutes = 30;
String _endTime() {
2021-09-01 12:38:32 +00:00
return '${cache.sleepTimerTime!.hour.toString().padLeft(2, '0')}:${cache.sleepTimerTime!.minute.toString().padLeft(2, '0')}';
2020-10-15 20:10:17 +00:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Sleep timer'.i18n),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
2020-10-15 20:10:17 +00:00
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Hours:'.i18n),
2021-08-29 22:25:18 +00:00
NumberPicker(
value: hours,
2020-10-15 20:10:17 +00:00
minValue: 0,
maxValue: 69,
onChanged: (v) => setState(() => hours = v),
),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Minutes:'.i18n),
2021-08-29 22:25:18 +00:00
NumberPicker(
value: minutes,
minValue: 0,
maxValue: 60,
onChanged: (v) => setState(() => minutes = v),
2020-10-15 20:10:17 +00:00
),
],
),
],
),
Container(height: 4.0),
if (cache.sleepTimerTime != null)
Text(
2021-08-29 22:25:18 +00:00
'Current timer ends at'.i18n + ': ' + _endTime(),
2020-10-15 20:10:17 +00:00
textAlign: TextAlign.center,
)
],
),
actions: [
TextButton(
2020-10-15 20:10:17 +00:00
child: Text('Dismiss'.i18n),
onPressed: () {
Navigator.of(context).pop();
},
),
if (cache.sleepTimer != null)
TextButton(
2020-10-15 20:10:17 +00:00
child: Text('Cancel current timer'.i18n),
onPressed: () {
2021-09-01 12:38:32 +00:00
cache.sleepTimer!.cancel();
2020-10-15 20:10:17 +00:00
cache.sleepTimer = null;
cache.sleepTimerTime = null;
Navigator.of(context).pop();
},
),
TextButton(
2020-10-15 20:10:17 +00:00
child: Text('Save'.i18n),
onPressed: () {
Duration duration = Duration(hours: hours, minutes: minutes);
if (cache.sleepTimer != null) {
2021-09-01 12:38:32 +00:00
cache.sleepTimer!.cancel();
2020-10-15 20:10:17 +00:00
}
//Create timer
2021-08-29 22:25:18 +00:00
cache.sleepTimer =
Stream.fromFuture(Future.delayed(duration)).listen((_) {
2021-09-01 12:38:32 +00:00
audioHandler.pause();
cache.sleepTimer!.cancel();
2020-10-15 20:10:17 +00:00
cache.sleepTimerTime = null;
cache.sleepTimer = null;
});
cache.sleepTimerTime = DateTime.now().add(duration);
Navigator.of(context).pop();
},
),
],
);
}
}
class SelectPlaylistDialog extends StatefulWidget {
2021-09-01 12:38:32 +00:00
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) {
2021-08-29 22:25:18 +00:00
if (snapshot.hasError)
SizedBox(
height: 100,
child: ErrorScreen(),
);
if (snapshot.connectionState != ConnectionState.done)
return SizedBox(
height: 100,
child: Center(
child: CircularProgressIndicator(),
),
);
2021-09-01 12:38:32 +00:00
List<Playlist> playlists = snapshot.data! as List<Playlist>;
return SingleChildScrollView(
2021-08-29 22:25:18 +00:00
child: Column(mainAxisSize: MainAxisSize.min, children: [
...List.generate(
playlists.length,
(i) => ListTile(
2021-09-01 12:38:32 +00:00
title: Text(playlists[i].title!),
2021-08-29 22:25:18 +00:00
leading: CachedImage(
2021-09-01 12:38:32 +00:00
url: playlists[i].image!.thumb,
2021-08-29 22:25:18 +00:00
),
onTap: () {
if (widget.callback != null) {
2021-09-01 12:38:32 +00:00
widget.callback!(playlists[i]);
2021-08-29 22:25:18 +00:00
}
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 {
2021-09-01 12:38:32 +00:00
final List<Track?>? tracks;
//If playlist not null, update
2021-09-01 12:38:32 +00:00
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> {
2021-09-01 12:38:32 +00:00
int? _playlistType = 1;
2020-06-23 19:23:12 +00:00
String _title = '';
String _description = '';
2021-09-01 12:38:32 +00:00
TextEditingController? _titleController;
TextEditingController? _descController;
//Create or edit mode
bool get edit => widget.playlist != null;
@override
void initState() {
//Edit playlist mode
if (edit) {
2021-09-01 12:38:32 +00:00
_titleController = TextEditingController(text: widget.playlist!.title);
2021-08-29 22:25:18 +00:00
_descController =
2021-09-01 12:38:32 +00:00
TextEditingController(text: widget.playlist!.description);
}
super.initState();
}
2020-06-23 19:23:12 +00:00
@override
Widget build(BuildContext context) {
return AlertDialog(
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(
2021-08-29 22:25:18 +00:00
decoration: InputDecoration(labelText: 'Title'.i18n),
controller: _titleController ?? TextEditingController(),
2020-06-23 19:23:12 +00:00
onChanged: (String s) => _title = s,
),
TextField(
onChanged: (String s) => _description = s,
controller: _descController ?? TextEditingController(),
2021-08-29 22:25:18 +00:00
decoration: InputDecoration(labelText: 'Description'.i18n),
),
Container(
height: 4.0,
2020-06-23 19:23:12 +00:00
),
DropdownButton<int>(
value: _playlistType,
2021-09-01 12:38:32 +00:00
onChanged: (int? v) {
2020-06-23 19:23:12 +00:00
setState(() => _playlistType = v);
},
items: [
DropdownMenuItem<int>(
value: 1,
child: Text('Private'.i18n),
2020-06-23 19:23:12 +00:00
),
DropdownMenuItem<int>(
value: 2,
child: Text('Collaborative'.i18n),
2020-06-23 19:23:12 +00:00
),
],
),
],
),
actions: <Widget>[
TextButton(
child: Text('Cancel'.i18n),
2020-06-23 19:23:12 +00:00
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: Text(edit ? 'Update'.i18n : 'Create'.i18n),
2020-06-23 19:23:12 +00:00
onPressed: () async {
if (edit) {
//Update
2021-09-01 12:38:32 +00:00
await deezerAPI.updatePlaylist(widget.playlist!.id!,
_titleController!.value.text, _descController!.value.text,
2021-08-29 22:25:18 +00:00
status: _playlistType);
ScaffoldMessenger.of(context).snack('Playlist updated!'.i18n);
} else {
List<String> tracks = [];
if (widget.tracks != null) {
2021-09-01 12:38:32 +00:00
tracks = widget.tracks!.map<String>((t) => t!.id).toList();
}
2021-08-29 22:25:18 +00:00
await deezerAPI.createPlaylist(_title,
status: _playlistType,
description: _description,
2021-08-29 22:25:18 +00:00
trackIds: tracks);
ScaffoldMessenger.of(context).snack('Playlist created!'.i18n);
2020-06-23 19:23:12 +00:00
}
Navigator.of(context).pop();
},
)
],
);
}
}