Make screen wacklock toggle menu item text dynamic

- Clearly indicate screen wakelock toggle menu action (and thus current status) by changing menu text depending on wakelock status
This commit is contained in:
DJDoubleD
2024-09-16 00:11:40 +02:00
parent 9138b07783
commit 410b2bdd33
3 changed files with 49 additions and 117 deletions

View File

@@ -428,6 +428,9 @@ const language_en_us = {
// 0.7.14
'Permission denied, download canceled!': 'Permission denied, download canceled!',
'Crowdin': 'Crowdin',
'Help translating this app on Crowdin!': 'Help translating this app on Crowdin!'
'Help translating this app on Crowdin!': 'Help translating this app on Crowdin!',
// 0.7.15
'Allow screen to turn off': 'Allow screen to turn off',
}
};

View File

@@ -23,8 +23,7 @@ class MenuSheet {
Function navigateCallback;
// Use no-op callback if not provided
MenuSheet({Function? navigateCallback})
: navigateCallback = navigateCallback ?? (() {});
MenuSheet({Function? navigateCallback}) : navigateCallback = navigateCallback ?? (() {});
//===================
// DEFAULT
@@ -37,10 +36,7 @@ class MenuSheet {
builder: (BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints(
maxHeight:
(MediaQuery.of(context).orientation == Orientation.landscape)
? 220
: 350,
maxHeight: (MediaQuery.of(context).orientation == Orientation.landscape) ? 220 : 350,
),
child: SingleChildScrollView(
child: Column(children: options),
@@ -87,8 +83,7 @@ class MenuSheet {
maxLines: 1,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 22.0, fontWeight: FontWeight.bold),
style: const TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold),
),
Text(
track.artistString ?? '',
@@ -117,10 +112,7 @@ class MenuSheet {
),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: (MediaQuery.of(context).orientation ==
Orientation.landscape)
? 200
: 350,
maxHeight: (MediaQuery.of(context).orientation == Orientation.landscape) ? 200 : 350,
),
child: SingleChildScrollView(
child: Column(children: options),
@@ -133,9 +125,7 @@ class MenuSheet {
//Default track options
void defaultTrackMenu(Track track,
{required BuildContext context,
List<Widget> options = const [],
Function? onRemove}) {
{required BuildContext context, List<Widget> options = const [], Function? onRemove}) {
showWithTrack(context, track, [
addToQueueNext(track, context),
addToQueue(track, context),
@@ -148,8 +138,7 @@ class MenuSheet {
shareTile('track', track.id ?? ''),
playMix(track, context),
showAlbum(track.album!, context),
...List.generate(track.artists?.length ?? 0,
(i) => showArtist(track.artists![i], context)),
...List.generate(track.artists?.length ?? 0, (i) => showArtist(track.artists![i], context)),
...options
]);
}
@@ -163,8 +152,7 @@ class MenuSheet {
leading: const Icon(Icons.playlist_play),
onTap: () async {
//-1 = next
await GetIt.I<AudioPlayerHandler>()
.insertQueueItem(-1, t.toMediaItem());
await GetIt.I<AudioPlayerHandler>().insertQueueItem(-1, t.toMediaItem());
if (context.mounted) _close(context);
});
@@ -187,9 +175,7 @@ class MenuSheet {
downloadManager.addOfflinePlaylist(p);
}
Fluttertoast.showToast(
msg: 'Added to library'.i18n,
gravity: ToastGravity.BOTTOM,
toastLength: Toast.LENGTH_SHORT);
msg: 'Added to library'.i18n, gravity: ToastGravity.BOTTOM, toastLength: Toast.LENGTH_SHORT);
//Add to cache
cache.libraryTracks ??= [];
cache.libraryTracks?.add(t.id!);
@@ -201,9 +187,7 @@ class MenuSheet {
title: Text('Download'.i18n),
leading: const Icon(Icons.file_download),
onTap: () async {
if (await downloadManager.addOfflineTrack(t,
private: false, isSingleton: true) !=
false) {
if (await downloadManager.addOfflineTrack(t, private: false, isSingleton: true) != false) {
showDownloadStartedToast();
}
if (context.mounted) _close(context);
@@ -237,8 +221,7 @@ class MenuSheet {
},
);
Widget removeFromPlaylist(Track t, Playlist p, BuildContext context) =>
ListTile(
Widget removeFromPlaylist(Track t, Playlist p, BuildContext context) => ListTile(
title: Text('Remove from playlist'.i18n),
leading: const Icon(Icons.delete),
onTap: () async {
@@ -252,8 +235,7 @@ class MenuSheet {
},
);
Widget removeFavoriteTrack(Track t, BuildContext context, {onUpdate}) =>
ListTile(
Widget removeFavoriteTrack(Track t, BuildContext context, {onUpdate}) => ListTile(
title: Text('Remove favorite'.i18n),
leading: const Icon(Icons.delete),
onTap: () async {
@@ -266,9 +248,7 @@ class MenuSheet {
//Remove from cache
cache.libraryTracks?.removeWhere((i) => i == t.id);
Fluttertoast.showToast(
msg: 'Track removed from library'.i18n,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
msg: 'Track removed from library'.i18n, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM);
if (onUpdate != null) onUpdate();
if (context.mounted) _close(context);
},
@@ -284,8 +264,7 @@ class MenuSheet {
leading: const Icon(Icons.recent_actors),
onTap: () {
if (context.mounted) _close(context);
customNavigatorKey.currentState
?.push(MaterialPageRoute(builder: (context) => ArtistDetails(a)));
customNavigatorKey.currentState?.push(MaterialPageRoute(builder: (context) => ArtistDetails(a)));
navigateCallback();
},
@@ -300,8 +279,7 @@ class MenuSheet {
leading: const Icon(Icons.album),
onTap: () {
if (context.mounted) _close(context);
customNavigatorKey.currentState
?.push(MaterialPageRoute(builder: (context) => AlbumDetails(a)));
customNavigatorKey.currentState?.push(MaterialPageRoute(builder: (context) => AlbumDetails(a)));
navigateCallback();
},
@@ -345,9 +323,7 @@ class MenuSheet {
//Default album options
void defaultAlbumMenu(Album album,
{required BuildContext context,
List<Widget> options = const [],
Function? onRemove}) {
{required BuildContext context, List<Widget> options = const [], Function? onRemove}) {
show(context, [
(album.library != null && onRemove != null)
? removeAlbum(album, context, onRemove: onRemove)
@@ -389,16 +365,13 @@ class MenuSheet {
leading: const Icon(Icons.library_music),
onTap: () async {
await deezerAPI.addFavoriteAlbum(a.id!);
Fluttertoast.showToast(
msg: 'Added to library'.i18n, gravity: ToastGravity.BOTTOM);
Fluttertoast.showToast(msg: 'Added to library'.i18n, gravity: ToastGravity.BOTTOM);
if (context.mounted) _close(context);
},
);
//Remove album from favorites
Widget removeAlbum(Album a, BuildContext context,
{required Function onRemove}) =>
ListTile(
Widget removeAlbum(Album a, BuildContext context, {required Function onRemove}) => ListTile(
title: Text('Remove album'.i18n),
leading: const Icon(Icons.delete),
onTap: () async {
@@ -419,13 +392,9 @@ class MenuSheet {
//===================
void defaultArtistMenu(Artist artist,
{required BuildContext context,
List<Widget> options = const [],
Function? onRemove}) {
{required BuildContext context, List<Widget> options = const [], Function? onRemove}) {
show(context, [
(artist.library != null)
? removeArtist(artist, context, onRemove: onRemove)
: favoriteArtist(artist, context),
(artist.library != null) ? removeArtist(artist, context, onRemove: onRemove) : favoriteArtist(artist, context),
shareTile('artist', artist.id!),
...options
]);
@@ -435,16 +404,13 @@ class MenuSheet {
// ARTIST OPTIONS
//===================
Widget removeArtist(Artist a, BuildContext context, {Function? onRemove}) =>
ListTile(
Widget removeArtist(Artist a, BuildContext context, {Function? onRemove}) => ListTile(
title: Text('Remove from favorites'.i18n),
leading: const Icon(Icons.delete),
onTap: () async {
await deezerAPI.removeArtist(a.id!);
Fluttertoast.showToast(
msg: 'Artist removed from library'.i18n,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
msg: 'Artist removed from library'.i18n, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM);
if (onRemove != null) onRemove();
if (context.mounted) _close(context);
},
@@ -456,9 +422,7 @@ class MenuSheet {
onTap: () async {
await deezerAPI.addFavoriteArtist(a.id!);
Fluttertoast.showToast(
msg: 'Added to library'.i18n,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM);
msg: 'Added to library'.i18n, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM);
if (context.mounted) _close(context);
},
);
@@ -468,10 +432,7 @@ class MenuSheet {
//===================
void defaultPlaylistMenu(Playlist playlist,
{required BuildContext context,
List<Widget> options = const [],
Function? onRemove,
Function? onUpdate}) {
{required BuildContext context, List<Widget> options = const [], Function? onRemove, Function? onUpdate}) {
show(context, [
(playlist.library != null)
? removePlaylistLibrary(playlist, context, onRemove: onRemove)
@@ -479,8 +440,7 @@ class MenuSheet {
addPlaylistOffline(playlist, context),
downloadPlaylist(playlist, context),
shareTile('playlist', playlist.id!),
if (playlist.user?.id == deezerAPI.userId)
editPlaylist(playlist, context: context, onUpdate: onUpdate),
if (playlist.user?.id == deezerAPI.userId) editPlaylist(playlist, context: context, onUpdate: onUpdate),
...options
]);
}
@@ -489,9 +449,7 @@ class MenuSheet {
// PLAYLIST OPTIONS
//===================
Widget removePlaylistLibrary(Playlist p, BuildContext context,
{Function? onRemove}) =>
ListTile(
Widget removePlaylistLibrary(Playlist p, BuildContext context, {Function? onRemove}) => ListTile(
title: Text('Remove from library'.i18n),
leading: const Icon(Icons.delete),
onTap: () async {
@@ -513,9 +471,7 @@ class MenuSheet {
leading: const Icon(Icons.favorite),
onTap: () async {
await deezerAPI.addPlaylist(p.id!);
Fluttertoast.showToast(
msg: 'Added playlist to library'.i18n,
gravity: ToastGravity.BOTTOM);
Fluttertoast.showToast(msg: 'Added playlist to library'.i18n, gravity: ToastGravity.BOTTOM);
if (context.mounted) _close(context);
},
);
@@ -537,22 +493,17 @@ class MenuSheet {
leading: const Icon(Icons.file_download),
onTap: () async {
if (context.mounted) _close(context);
if (await downloadManager.addOfflinePlaylist(p, private: false) !=
false) {
if (await downloadManager.addOfflinePlaylist(p, private: false) != false) {
showDownloadStartedToast();
}
},
);
Widget editPlaylist(Playlist p,
{required BuildContext context, Function? onUpdate}) =>
ListTile(
Widget editPlaylist(Playlist p, {required BuildContext context, Function? onUpdate}) => ListTile(
title: Text('Edit playlist'.i18n),
leading: const Icon(Icons.edit),
onTap: () async {
await showDialog(
context: context,
builder: (context) => CreatePlaylistDialog(playlist: p));
await showDialog(context: context, builder: (context) => CreatePlaylistDialog(playlist: p));
if (context.mounted) _close(context);
if (onUpdate != null) onUpdate();
},
@@ -562,14 +513,8 @@ class MenuSheet {
// SHOW/EPISODE
//===================
defaultShowEpisodeMenu(Show s, ShowEpisode e,
{required BuildContext context, List<Widget> options = const []}) {
show(context, [
shareTile('episode', e.id!),
shareShow(s.id!),
downloadExternalEpisode(e),
...options
]);
defaultShowEpisodeMenu(Show s, ShowEpisode e, {required BuildContext context, List<Widget> options = const []}) {
show(context, [shareTile('episode', e.id!), shareShow(s.id!), downloadExternalEpisode(e), ...options]);
}
Widget shareShow(String id) => ListTile(
@@ -594,10 +539,7 @@ class MenuSheet {
//===================
showDownloadStartedToast() {
Fluttertoast.showToast(
msg: 'Downloads added!'.i18n,
gravity: ToastGravity.BOTTOM,
toastLength: Toast.LENGTH_SHORT);
Fluttertoast.showToast(msg: 'Downloads added!'.i18n, gravity: ToastGravity.BOTTOM, toastLength: Toast.LENGTH_SHORT);
}
//Create playlist
@@ -630,22 +572,20 @@ class MenuSheet {
);
Widget wakelock(BuildContext context) => ListTile(
title: Text('Keep the screen on'.i18n),
title: Text(cache.wakelock ? 'Allow screen to turn off'.i18n : 'Keep the screen on'.i18n),
leading: const Icon(Icons.screen_lock_portrait),
onTap: () async {
_close(context);
//Enable
if (!cache.wakelock) {
WakelockPlus.enable();
Fluttertoast.showToast(
msg: 'Wakelock enabled!'.i18n, gravity: ToastGravity.BOTTOM);
Fluttertoast.showToast(msg: 'Wakelock enabled!'.i18n, gravity: ToastGravity.BOTTOM);
cache.wakelock = true;
return;
}
//Disable
WakelockPlus.disable();
Fluttertoast.showToast(
msg: 'Wakelock disabled!'.i18n, gravity: ToastGravity.BOTTOM);
Fluttertoast.showToast(msg: 'Wakelock disabled!'.i18n, gravity: ToastGravity.BOTTOM);
cache.wakelock = false;
},
);
@@ -682,11 +622,7 @@ class _SleepTimerDialogState extends State<SleepTimerDialog> {
mainAxisSize: MainAxisSize.min,
children: [
Text('Hours:'.i18n),
NumberPicker(
value: hours,
minValue: 0,
maxValue: 69,
onChanged: (v) => setState(() => hours = v)),
NumberPicker(value: hours, minValue: 0, maxValue: 69, onChanged: (v) => setState(() => hours = v)),
],
),
Column(
@@ -694,10 +630,7 @@ class _SleepTimerDialogState extends State<SleepTimerDialog> {
children: [
Text('Minutes:'.i18n),
NumberPicker(
value: minutes,
minValue: 0,
maxValue: 60,
onChanged: (v) => setState(() => minutes = v)),
value: minutes, minValue: 0, maxValue: 60, onChanged: (v) => setState(() => minutes = v)),
],
),
],
@@ -733,8 +666,7 @@ class _SleepTimerDialogState extends State<SleepTimerDialog> {
Duration duration = Duration(hours: hours, minutes: minutes);
cache.sleepTimer?.cancel();
//Create timer
cache.sleepTimer =
Stream.fromFuture(Future.delayed(duration)).listen((_) {
cache.sleepTimer = Stream.fromFuture(Future.delayed(duration)).listen((_) {
GetIt.I<AudioPlayerHandler>().pause();
cache.sleepTimer?.cancel();
cache.sleepTimerTime = null;
@@ -905,20 +837,16 @@ class _CreatePlaylistDialogState extends State<CreatePlaylistDialog> {
onPressed: () async {
if (edit) {
//Update
await deezerAPI.updatePlaylist(widget.playlist!.id!,
_titleController!.value.text, _descController!.value.text,
await deezerAPI.updatePlaylist(
widget.playlist!.id!, _titleController!.value.text, _descController!.value.text,
status: _playlistType);
Fluttertoast.showToast(
msg: 'Playlist updated!'.i18n, gravity: ToastGravity.BOTTOM);
Fluttertoast.showToast(msg: 'Playlist updated!'.i18n, gravity: ToastGravity.BOTTOM);
} else {
List<String> tracks = [];
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);
status: _playlistType, description: _description, trackIds: tracks);
Fluttertoast.showToast(msg: 'Playlist created!'.i18n, gravity: ToastGravity.BOTTOM);
}
if (context.mounted) Navigator.of(context).pop();
},

View File

@@ -352,5 +352,6 @@
"Download failed!": "Download failed!",
"Permission denied, download canceled!": "Permission denied, download canceled!",
"Crowdin": "Crowdin",
"Help translating this app on Crowdin!": "Help translating this app on Crowdin!"
"Help translating this app on Crowdin!": "Help translating this app on Crowdin!",
"Allow screen to turn off": "Allow screen to turn off"
}