From 65267afd4ec7b4e00e9808e23c9c5480dabe5a23 Mon Sep 17 00:00:00 2001 From: Rich T Date: Sun, 1 Sep 2024 09:09:08 -0700 Subject: [PATCH 01/10] Added: Ability to keep screen on while using app --- .../keep_screen_on_dropdown_list_tile.dart | 39 +++++++++ ...eep_screen_on_while_charging_selector.dart | 29 +++++++ lib/l10n/app_en.arb | 12 ++- lib/main.dart | 28 ++++++- lib/models/finamp_models.dart | 53 +++++++++++++ lib/screens/interaction_settings_screen.dart | 4 + lib/screens/lyrics_screen.dart | 10 ++- lib/services/finamp_settings_helper.dart | 14 ++++ lib/services/keep_screen_on_helper.dart | 79 +++++++++++++++++++ .../music_player_background_task.dart | 7 +- pubspec.yaml | 2 + 11 files changed, 272 insertions(+), 5 deletions(-) create mode 100644 lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart create mode 100644 lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart create mode 100644 lib/services/keep_screen_on_helper.dart diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart new file mode 100644 index 000000000..35d4bab9d --- /dev/null +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart @@ -0,0 +1,39 @@ +import 'package:finamp/services/keep_screen_on_helper.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:hive/hive.dart'; + +import '../../models/finamp_models.dart'; +import '../../services/finamp_settings_helper.dart'; + +class KeepScreenOnDropdownListTile extends StatelessWidget { + const KeepScreenOnDropdownListTile({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return ValueListenableBuilder>( + valueListenable: FinampSettingsHelper.finampSettingsListener, + builder: (_, box, __) { + return ListTile( + title: Text(AppLocalizations.of(context)!.keepScreenOn), + subtitle: Text(AppLocalizations.of(context)!.keepScreenOnSubtitle), + trailing: DropdownButton( + value: box.get("FinampSettings")?.keepScreenOnOption, + items: KeepScreenOnOption.values + .map((e) => DropdownMenuItem( + value: e, + child: Text(e.toLocalisedString(context)), + )) + .toList(), + onChanged: (value) { + if (value != null) { + FinampSettingsHelper.setKeepScreenOnOption(value); + KeepScreenOnHelper.setKeepScreenOn(); + } + }, + ), + ); + }, + ); + } +} diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart new file mode 100644 index 000000000..ac39f01e6 --- /dev/null +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart @@ -0,0 +1,29 @@ +import 'package:finamp/services/keep_screen_on_helper.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:hive/hive.dart'; + +import '../../models/finamp_models.dart'; +import '../../services/finamp_settings_helper.dart'; + +class KeepScreenOnWhilePluggedInSelector extends StatelessWidget { + const KeepScreenOnWhilePluggedInSelector({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return ValueListenableBuilder>( + valueListenable: FinampSettingsHelper.finampSettingsListener, + builder: (_, box, __) { + return SwitchListTile.adaptive( + title: Text(AppLocalizations.of(context)!.keepScreenOnWhilePluggedIn), + subtitle: Text(AppLocalizations.of(context)!.keepScreenOnWhilePluggedInSubtitle), + value: FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn, + onChanged: (value) { + FinampSettingsHelper.setKeepScreenOnWhileCharging(value); + KeepScreenOnHelper.setKeepScreenOn(); + }, + ); + }, + ); + } +} diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index a43a734b7..d9b201add 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -1633,5 +1633,15 @@ "showLyricsScreenAlbumPreludeSubtitle": "Controls if the album cover is shown above the lyrics before being scrolled away.", "@showLyricsScreenAlbumPreludeSubtitle": { "description": "Subtitle for the setting that controls if the album cover is shown before the lyrics in the lyrics view" - } + }, + "keepScreenOn": "Keep Screen On: Main Setting", + "@keepScreenOn": { + "description": "Option to keep the screen on while using the app" + }, + "keepScreenOnSubtitle": "Keep the screen on while playing music or only while showing lyrics", + "keepScreenOnDisabled": "Disabled", + "whilePlaying": "While Playing Music", + "whileLyrics": "While Showing Lyrics", + "keepScreenOnWhilePluggedIn": "Keep Screen On: only while plugged in", + "keepScreenOnWhilePluggedInSubtitle": "Ignore the Keep Screen On: Main Setting if device is unplugged" } diff --git a/lib/main.dart b/lib/main.dart index 46bad0f53..bd0df4ca8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/color_schemes.g.dart'; import 'package:finamp/gen/assets.gen.dart'; import 'package:finamp/screens/downloads_settings_screen.dart'; @@ -16,6 +17,7 @@ import 'package:finamp/services/downloads_service.dart'; import 'package:finamp/services/downloads_service_backend.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:finamp/services/finamp_user_helper.dart'; +import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:finamp/services/offline_listen_helper.dart'; import 'package:finamp/services/playback_history_service.dart'; import 'package:finamp/services/queue_service.dart'; @@ -116,7 +118,7 @@ void main() async { : "en_US"; await initializeDateFormatting(localeString, null); - runApp(const Finamp()); + runApp(Finamp()); } } @@ -214,6 +216,7 @@ Future setupHive() async { Hive.registerAdapter(LyricDtoAdapter()); Hive.registerAdapter(LyricsAlignmentAdapter()); Hive.registerAdapter(LyricsFontSizeAdapter()); + Hive.registerAdapter(KeepScreenOnOptionAdapter()); final dir = (Platform.isAndroid || Platform.isIOS) ? await getApplicationDocumentsDirectory() @@ -397,7 +400,28 @@ Future _setupFinampUserHelper() async { } class Finamp extends ConsumerStatefulWidget { - const Finamp({Key? key}) : super(key: key); + /// Constructor added for KeepScreenOn + Finamp({super.key}) { + var battery = Battery(); + + // Monitor if device battery state changed. + battery.onBatteryStateChanged.listen((BatteryState state) { + debugPrint("KeepScreenOnInMain battery state: $state"); + switch (state) { + case BatteryState.charging: + case BatteryState.connectedNotCharging: + KeepScreenOnHelper.setCondition(isPluggedIn: true); + break; + case BatteryState.discharging: + case BatteryState.unknown: + KeepScreenOnHelper.setCondition(isPluggedIn: false); + break; + default: + // Do nothing + break; + } + }); + } @override ConsumerState createState() => _FinampState(); diff --git a/lib/models/finamp_models.dart b/lib/models/finamp_models.dart index 6262f1b55..9f81b2d05 100644 --- a/lib/models/finamp_models.dart +++ b/lib/models/finamp_models.dart @@ -111,6 +111,8 @@ const _lyricsFontSizeDefault = LyricsFontSize.medium; const _showLyricsScreenAlbumPreludeDefault = true; const _showStopButtonOnMediaNotificationDefault = false; const _showSeekControlsOnMediaNotificationDefault = true; +const _keepScreenOnOption = KeepScreenOnOption.disabled; +const _keepScreenOnWhilePluggedIn = true; @HiveType(typeId: 28) class FinampSettings { @@ -190,6 +192,8 @@ class FinampSettings { this.showLyricsScreenAlbumPrelude = _showLyricsScreenAlbumPreludeDefault, this.showStopButtonOnMediaNotification = _showStopButtonOnMediaNotificationDefault, this.showSeekControlsOnMediaNotification = _showSeekControlsOnMediaNotificationDefault, + this.keepScreenOnOption = _keepScreenOnOption, + this.keepScreenOnWhilePluggedIn = _keepScreenOnWhilePluggedIn }); @HiveField(0, defaultValue: _isOfflineDefault) @@ -421,6 +425,12 @@ class FinampSettings { @HiveField(71, defaultValue: _showLyricsScreenAlbumPreludeDefault) bool showLyricsScreenAlbumPrelude; + @HiveField(72, defaultValue: _keepScreenOnOption) + KeepScreenOnOption keepScreenOnOption; + + @HiveField(73, defaultValue: _keepScreenOnWhilePluggedIn) + bool keepScreenOnWhilePluggedIn; + static Future create() async { final downloadLocation = await DownloadLocation.create( name: "Internal Storage", @@ -2177,3 +2187,46 @@ enum LyricsFontSize { } } } + +@HiveType(typeId: 72) +enum KeepScreenOnOption { + @HiveField(0) + disabled, + @HiveField(1) + whilePlaying, + @HiveField(2) + whileLyrics; + + /// Human-readable version of this enum. I've written longer descriptions on + /// enums like [TabContentType], and I can't be bothered to copy and paste it + /// again. + @override + @Deprecated("Use toLocalisedString when possible") + String toString() => _humanReadableName(this); + + String toLocalisedString(BuildContext context) => + _humanReadableLocalisedName(this, context); + + String _humanReadableName(KeepScreenOnOption keepScreenOnOption) { + switch (keepScreenOnOption) { + case KeepScreenOnOption.disabled: + return "Disabled"; + case KeepScreenOnOption.whilePlaying: + return "While Playing Music"; + case KeepScreenOnOption.whileLyrics: + return "While Showing Lyrics"; + } + } + + String _humanReadableLocalisedName( + KeepScreenOnOption keepScreenOnOption, BuildContext context) { + switch (keepScreenOnOption) { + case KeepScreenOnOption.disabled: + return AppLocalizations.of(context)!.keepScreenOnDisabled; + case KeepScreenOnOption.whilePlaying: + return AppLocalizations.of(context)!.whilePlaying; + case KeepScreenOnOption.whileLyrics: + return AppLocalizations.of(context)!.whileLyrics; + } + } +} \ No newline at end of file diff --git a/lib/screens/interaction_settings_screen.dart b/lib/screens/interaction_settings_screen.dart index b11bd7ca5..11c947a29 100644 --- a/lib/screens/interaction_settings_screen.dart +++ b/lib/screens/interaction_settings_screen.dart @@ -1,3 +1,5 @@ +import 'package:finamp/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart'; +import 'package:finamp/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart'; import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:flutter/material.dart'; @@ -27,6 +29,8 @@ class InteractionSettingsScreen extends StatelessWidget { FastScrollSelector(), DisableGestureSelector(), DisableVibrationSelector(), + KeepScreenOnDropdownListTile(), + KeepScreenOnWhilePluggedInSelector() ], ), ); diff --git a/lib/screens/lyrics_screen.dart b/lib/screens/lyrics_screen.dart index 9f0ac532e..caa15d91a 100644 --- a/lib/screens/lyrics_screen.dart +++ b/lib/screens/lyrics_screen.dart @@ -7,6 +7,7 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart'; import 'package:finamp/services/current_track_metadata_provider.dart'; import 'package:finamp/services/feedback_helper.dart'; +import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; import 'package:finamp/services/progress_state_stream.dart'; import 'package:flutter/material.dart'; @@ -71,8 +72,16 @@ class _LyricsScreenContent extends StatefulWidget { } class _LyricsScreenContentState extends State<_LyricsScreenContent> { + @override + void dispose() { + KeepScreenOnHelper.setCondition(isLyricsShowing: false); + super.dispose(); + } + @override Widget build(BuildContext context) { + KeepScreenOnHelper.setCondition(isLyricsShowing: true); + double toolbarHeight = 53; int maxLines = 2; @@ -234,7 +243,6 @@ class _LyricsViewState extends ConsumerState @override Widget build(BuildContext context) { - final audioHandler = GetIt.instance(); final metadata = ref.watch(currentTrackMetadataProvider).unwrapPrevious(); diff --git a/lib/services/finamp_settings_helper.dart b/lib/services/finamp_settings_helper.dart index a27da8b85..7a3f5fdf2 100644 --- a/lib/services/finamp_settings_helper.dart +++ b/lib/services/finamp_settings_helper.dart @@ -361,4 +361,18 @@ class FinampSettingsHelper { Hive.box("FinampSettings") .put("FinampSettings", finampSettingsTemp); } + + static void setKeepScreenOnOption(KeepScreenOnOption keepScreenOnOption) { + FinampSettings finampSettingsTemp = finampSettings; + finampSettingsTemp.keepScreenOnOption = keepScreenOnOption; + Hive.box("FinampSettings") + .put("FinampSettings", finampSettingsTemp); + } + + static void setKeepScreenOnWhileCharging(bool keepScreenOnWhileCharging) { + FinampSettings finampSettingsTemp = finampSettings; + finampSettingsTemp.keepScreenOnWhilePluggedIn = keepScreenOnWhileCharging; + Hive.box("FinampSettings") + .put("FinampSettings", finampSettingsTemp); + } } diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart new file mode 100644 index 000000000..fce57a7b2 --- /dev/null +++ b/lib/services/keep_screen_on_helper.dart @@ -0,0 +1,79 @@ +import 'package:finamp/models/finamp_models.dart'; +import 'package:finamp/services/finamp_settings_helper.dart'; +import 'package:flutter/foundation.dart'; +import 'package:keep_screen_on/keep_screen_on.dart'; + +/// KeepScreenOn +/// +/// Added dependencies: +/// - keep_screen_on: ^3.0.0 +/// - battery_plus: ^6.0.2 +/// This functionality adds two options in settings under "Interactions": +/// - Keep Screen On: Main Setting +/// - Keep Screen On: Only while plugged in +/// +/// Ties into the following actions: +/// - MusicPlayerBackgroundTask.play() -- Sets _isPlaying to true. +/// - MusicPlayerBackgroundTask.pause() -- Sets _isPlaying to false. +/// - LyricsScreen.draw() -- Sets _isLyricsShowing to true. +/// - LyricsScreen.dispose() -- Sets _isLyricsShowing to false. +/// - FinAmp constructor -- Creates an event listener for battery status change. Sets _isPluggedIn value. +/// - KeepScreenOnOption -- Changes to this option re-evaluates the keepScreenOn status. +/// - KeepScreenOnWhilePluggedIn -- Changes to this option re-evaluates the keepsScreenOn status. +class KeepScreenOnHelper { + static bool keepingScreenOn = false; + + static bool _isPlaying = false; + static bool _isLyricsShowing = false; + static bool _isPluggedIn = false; + + static void setKeepScreenOn() { + if (FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn && !_isPluggedIn) { + _turnOff(); + } else { + switch (FinampSettingsHelper.finampSettings.keepScreenOnOption) { + case KeepScreenOnOption.disabled: + if (keepingScreenOn) _turnOff(); + break; + case KeepScreenOnOption.whilePlaying: + if (_isPlaying) { + _turnOn(); + } else { + _turnOff(); + } + break; + case KeepScreenOnOption.whileLyrics: + if (_isPlaying && _isLyricsShowing) { + _turnOn(); + } else { + _turnOff(); + } + break; + } + } + debugPrint( + "KeepScreenOnHelper keepingScreenOn: $keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); + } + + static void setCondition({bool? isPlaying, bool? isLyricsShowing, bool? isPluggedIn}) { + if (isPlaying != null) _isPlaying = isPlaying; + if (isLyricsShowing != null) _isLyricsShowing = isLyricsShowing; + if (isPluggedIn != null) _isPluggedIn = isPluggedIn; + + setKeepScreenOn(); + } + + static void _turnOn() { + if (!keepingScreenOn) { + keepingScreenOn = true; + KeepScreenOn.turnOn(); + } + } + + static void _turnOff() { + if (keepingScreenOn) { + keepingScreenOn = false; + KeepScreenOn.turnOff(); + } + } +} \ No newline at end of file diff --git a/lib/services/music_player_background_task.dart b/lib/services/music_player_background_task.dart index 553c3c161..e032c8766 100644 --- a/lib/services/music_player_background_task.dart +++ b/lib/services/music_player_background_task.dart @@ -9,6 +9,7 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart' as jellyfin_models; import 'package:finamp/services/favorite_provider.dart'; import 'package:finamp/services/jellyfin_api_helper.dart'; +import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:get_it/get_it.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -215,6 +216,7 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { @override Future play() { + KeepScreenOnHelper.setCondition(isPlaying: true); return _player.play(); } @@ -224,7 +226,10 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { } @override - Future pause() => _player.pause(); + Future pause() { + KeepScreenOnHelper.setCondition(isPlaying: false); + return _player.pause(); + } Future togglePlayback() { if (_player.playing) { diff --git a/pubspec.yaml b/pubspec.yaml index 495becbd1..b6ee75473 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -101,6 +101,8 @@ dependencies: scroll_to_index: ^3.0.1 window_manager: ^0.3.8 url_launcher: ^6.2.6 + keep_screen_on: ^3.0.0 + battery_plus: ^6.0.2 dev_dependencies: flutter_test: From 816a858e5ee72ceee2e337603462ff320bd318d6 Mon Sep 17 00:00:00 2001 From: Rich T Date: Sun, 1 Sep 2024 12:18:53 -0700 Subject: [PATCH 02/10] Modified: Moved battery state determination into KeepScreenOnHelper class. --- lib/main.dart | 14 +------------- lib/services/keep_screen_on_helper.dart | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index bd0df4ca8..88802bdb2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -407,19 +407,7 @@ class Finamp extends ConsumerStatefulWidget { // Monitor if device battery state changed. battery.onBatteryStateChanged.listen((BatteryState state) { debugPrint("KeepScreenOnInMain battery state: $state"); - switch (state) { - case BatteryState.charging: - case BatteryState.connectedNotCharging: - KeepScreenOnHelper.setCondition(isPluggedIn: true); - break; - case BatteryState.discharging: - case BatteryState.unknown: - KeepScreenOnHelper.setCondition(isPluggedIn: false); - break; - default: - // Do nothing - break; - } + KeepScreenOnHelper.setCondition(batteryState: state); }); } diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index fce57a7b2..b39640227 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -1,3 +1,4 @@ +import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:flutter/foundation.dart'; @@ -55,10 +56,26 @@ class KeepScreenOnHelper { "KeepScreenOnHelper keepingScreenOn: $keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); } - static void setCondition({bool? isPlaying, bool? isLyricsShowing, bool? isPluggedIn}) { + static void setCondition({bool? isPlaying, bool? isLyricsShowing, BatteryState? batteryState}) { if (isPlaying != null) _isPlaying = isPlaying; if (isLyricsShowing != null) _isLyricsShowing = isLyricsShowing; - if (isPluggedIn != null) _isPluggedIn = isPluggedIn; + if (batteryState != null) { + debugPrint("KeepScreenOnHelper reported battery state: $batteryState"); + switch (batteryState) { + case BatteryState.charging: + case BatteryState.connectedNotCharging: + case BatteryState.full: + _isPluggedIn = true; + break; + case BatteryState.discharging: + case BatteryState.unknown: + _isPluggedIn = false; + break; + default: + // Do nothing + break; + } + } setKeepScreenOn(); } From dacc5f81c192c3752f5d28f1fa12a076316ec43d Mon Sep 17 00:00:00 2001 From: Rich T Date: Mon, 2 Sep 2024 16:28:44 -0700 Subject: [PATCH 03/10] Used events where possible --- .../keep_screen_on_dropdown_list_tile.dart | 3 +- ...eep_screen_on_while_charging_selector.dart | 3 +- lib/main.dart | 18 ++---- lib/screens/lyrics_screen.dart | 4 -- lib/services/keep_screen_on_helper.dart | 60 +++++++++++++++---- .../music_player_background_task.dart | 3 - 6 files changed, 57 insertions(+), 34 deletions(-) diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart index 35d4bab9d..350ee563b 100644 --- a/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart @@ -7,7 +7,7 @@ import '../../models/finamp_models.dart'; import '../../services/finamp_settings_helper.dart'; class KeepScreenOnDropdownListTile extends StatelessWidget { - const KeepScreenOnDropdownListTile({Key? key}) : super(key: key); + const KeepScreenOnDropdownListTile({super.key}); @override Widget build(BuildContext context) { @@ -28,7 +28,6 @@ class KeepScreenOnDropdownListTile extends StatelessWidget { onChanged: (value) { if (value != null) { FinampSettingsHelper.setKeepScreenOnOption(value); - KeepScreenOnHelper.setKeepScreenOn(); } }, ), diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart index ac39f01e6..1e3e82ea5 100644 --- a/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart @@ -7,7 +7,7 @@ import '../../models/finamp_models.dart'; import '../../services/finamp_settings_helper.dart'; class KeepScreenOnWhilePluggedInSelector extends StatelessWidget { - const KeepScreenOnWhilePluggedInSelector({Key? key}) : super(key: key); + const KeepScreenOnWhilePluggedInSelector({super.key}); @override Widget build(BuildContext context) { @@ -20,7 +20,6 @@ class KeepScreenOnWhilePluggedInSelector extends StatelessWidget { value: FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn, onChanged: (value) { FinampSettingsHelper.setKeepScreenOnWhileCharging(value); - KeepScreenOnHelper.setKeepScreenOn(); }, ); }, diff --git a/lib/main.dart b/lib/main.dart index 88802bdb2..a5448179a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,6 @@ import 'dart:async'; import 'dart:io'; -import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/color_schemes.g.dart'; import 'package:finamp/gen/assets.gen.dart'; import 'package:finamp/screens/downloads_settings_screen.dart'; @@ -118,6 +117,7 @@ void main() async { : "en_US"; await initializeDateFormatting(localeString, null); + KeepScreenOnHelper.init(); runApp(Finamp()); } } @@ -400,16 +400,7 @@ Future _setupFinampUserHelper() async { } class Finamp extends ConsumerStatefulWidget { - /// Constructor added for KeepScreenOn - Finamp({super.key}) { - var battery = Battery(); - - // Monitor if device battery state changed. - battery.onBatteryStateChanged.listen((BatteryState state) { - debugPrint("KeepScreenOnInMain battery state: $state"); - KeepScreenOnHelper.setCondition(batteryState: state); - }); - } + Finamp({super.key}); @override ConsumerState createState() => _FinampState(); @@ -522,7 +513,10 @@ class _FinampState extends ConsumerState with WindowListener { const LanguageSelectionScreen(), }, initialRoute: SplashScreen.routeName, - navigatorObservers: [SplitScreenNavigatorObserver()], + navigatorObservers: [ + SplitScreenNavigatorObserver(), + KeepScreenOnObserver() + ], builder: buildPlayerSplitScreenScaffold, theme: ThemeData( brightness: Brightness.light, diff --git a/lib/screens/lyrics_screen.dart b/lib/screens/lyrics_screen.dart index caa15d91a..7e8fdd44b 100644 --- a/lib/screens/lyrics_screen.dart +++ b/lib/screens/lyrics_screen.dart @@ -7,7 +7,6 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart'; import 'package:finamp/services/current_track_metadata_provider.dart'; import 'package:finamp/services/feedback_helper.dart'; -import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; import 'package:finamp/services/progress_state_stream.dart'; import 'package:flutter/material.dart'; @@ -74,14 +73,11 @@ class _LyricsScreenContent extends StatefulWidget { class _LyricsScreenContentState extends State<_LyricsScreenContent> { @override void dispose() { - KeepScreenOnHelper.setCondition(isLyricsShowing: false); super.dispose(); } @override Widget build(BuildContext context) { - KeepScreenOnHelper.setCondition(isLyricsShowing: true); - double toolbarHeight = 53; int maxLines = 2; diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index b39640227..9151eda62 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -1,8 +1,12 @@ import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/models/finamp_models.dart'; +import 'package:finamp/screens/lyrics_screen.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; -import 'package:flutter/foundation.dart'; +import 'package:finamp/services/music_player_background_task.dart'; +import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; import 'package:keep_screen_on/keep_screen_on.dart'; +import 'package:logging/logging.dart'; /// KeepScreenOn /// @@ -14,13 +18,9 @@ import 'package:keep_screen_on/keep_screen_on.dart'; /// - Keep Screen On: Only while plugged in /// /// Ties into the following actions: -/// - MusicPlayerBackgroundTask.play() -- Sets _isPlaying to true. -/// - MusicPlayerBackgroundTask.pause() -- Sets _isPlaying to false. -/// - LyricsScreen.draw() -- Sets _isLyricsShowing to true. -/// - LyricsScreen.dispose() -- Sets _isLyricsShowing to false. -/// - FinAmp constructor -- Creates an event listener for battery status change. Sets _isPluggedIn value. -/// - KeepScreenOnOption -- Changes to this option re-evaluates the keepScreenOn status. -/// - KeepScreenOnWhilePluggedIn -- Changes to this option re-evaluates the keepsScreenOn status. +/// - listens for audioHandler's playbackstate to determine _isPlaying. +/// - Extends NavigatorObserver class to listen for push and pop of LyricScreen to determine _isLyricsShowing +/// - listens for battyer state changes from the battery_plus widget to determine _isPluggedIn class KeepScreenOnHelper { static bool keepingScreenOn = false; @@ -28,6 +28,27 @@ class KeepScreenOnHelper { static bool _isLyricsShowing = false; static bool _isPluggedIn = false; + static final _keepScreenOnLogger = Logger("KeepScreenOnHelper"); + + static void init() { + // Subscribe to audio playback events + final audioHandler = GetIt.instance(); + audioHandler.playbackState.listen((event) async { + KeepScreenOnHelper.setCondition(isPlaying: event.playing); + }); + + // Subscribe to battery state change events + var battery = Battery(); + battery.onBatteryStateChanged.listen((BatteryState state) { + KeepScreenOnHelper.setCondition(batteryState: state); + }); + + FinampSettingsHelper.finampSettingsListener.addListener(() { + // When a settings change occurs, check keepScreenOnState. + setKeepScreenOn(); + }); + } + static void setKeepScreenOn() { if (FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn && !_isPluggedIn) { _turnOff(); @@ -52,15 +73,15 @@ class KeepScreenOnHelper { break; } } - debugPrint( - "KeepScreenOnHelper keepingScreenOn: $keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); + _keepScreenOnLogger.fine( + "keepingScreenOn: $keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); } static void setCondition({bool? isPlaying, bool? isLyricsShowing, BatteryState? batteryState}) { if (isPlaying != null) _isPlaying = isPlaying; if (isLyricsShowing != null) _isLyricsShowing = isLyricsShowing; if (batteryState != null) { - debugPrint("KeepScreenOnHelper reported battery state: $batteryState"); + _keepScreenOnLogger.fine("reported battery state: $batteryState"); switch (batteryState) { case BatteryState.charging: case BatteryState.connectedNotCharging: @@ -93,4 +114,21 @@ class KeepScreenOnHelper { KeepScreenOn.turnOff(); } } +} + +class KeepScreenOnObserver extends NavigatorObserver { + static final _lyricsCheck = ModalRoute.withName(LyricsScreen.routeName); + @override + void didPush(Route route, Route? previousRoute) { + // Just pushed to lyrics? + if (_lyricsCheck(route)) KeepScreenOnHelper.setCondition(isLyricsShowing: true); + super.didPush(route, previousRoute); + } + + @override + void didPop(Route route, Route? previousRoute) { + // Just popped lyrics? + if (_lyricsCheck(route)) KeepScreenOnHelper.setCondition(isLyricsShowing: false); + super.didPop(route, previousRoute); + } } \ No newline at end of file diff --git a/lib/services/music_player_background_task.dart b/lib/services/music_player_background_task.dart index e032c8766..d4e5222d7 100644 --- a/lib/services/music_player_background_task.dart +++ b/lib/services/music_player_background_task.dart @@ -9,7 +9,6 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart' as jellyfin_models; import 'package:finamp/services/favorite_provider.dart'; import 'package:finamp/services/jellyfin_api_helper.dart'; -import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:get_it/get_it.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -216,7 +215,6 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { @override Future play() { - KeepScreenOnHelper.setCondition(isPlaying: true); return _player.play(); } @@ -227,7 +225,6 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { @override Future pause() { - KeepScreenOnHelper.setCondition(isPlaying: false); return _player.pause(); } From 5b6a90f522c0df5e7646045732d94d559d9210b5 Mon Sep 17 00:00:00 2001 From: Rich T Date: Mon, 2 Sep 2024 16:37:56 -0700 Subject: [PATCH 04/10] Revert lyrics_screen and music_player_background_task to redesign branch --- lib/screens/lyrics_screen.dart | 6 +----- lib/services/music_player_background_task.dart | 4 +--- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/screens/lyrics_screen.dart b/lib/screens/lyrics_screen.dart index 7e8fdd44b..9f0ac532e 100644 --- a/lib/screens/lyrics_screen.dart +++ b/lib/screens/lyrics_screen.dart @@ -71,11 +71,6 @@ class _LyricsScreenContent extends StatefulWidget { } class _LyricsScreenContentState extends State<_LyricsScreenContent> { - @override - void dispose() { - super.dispose(); - } - @override Widget build(BuildContext context) { double toolbarHeight = 53; @@ -239,6 +234,7 @@ class _LyricsViewState extends ConsumerState @override Widget build(BuildContext context) { + final audioHandler = GetIt.instance(); final metadata = ref.watch(currentTrackMetadataProvider).unwrapPrevious(); diff --git a/lib/services/music_player_background_task.dart b/lib/services/music_player_background_task.dart index d4e5222d7..553c3c161 100644 --- a/lib/services/music_player_background_task.dart +++ b/lib/services/music_player_background_task.dart @@ -224,9 +224,7 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { } @override - Future pause() { - return _player.pause(); - } + Future pause() => _player.pause(); Future togglePlayback() { if (_player.playing) { From e22c318e9b1afa5d6561ca9d36f25aae38a21384 Mon Sep 17 00:00:00 2001 From: Rich T Date: Tue, 3 Sep 2024 19:19:47 -0700 Subject: [PATCH 05/10] Small changes and adding generated files --- lib/l10n/app_en.arb | 6 +- lib/main.dart | 4 +- lib/models/finamp_models.g.dart | 57 ++++++++++++++++- lib/services/keep_screen_on_helper.dart | 14 +--- pubspec.lock | 64 +++++++++++++++---- .../flutter/generated_plugin_registrant.cc | 3 + windows/flutter/generated_plugins.cmake | 1 + 7 files changed, 117 insertions(+), 32 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index d9b201add..9373fe308 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -1634,7 +1634,7 @@ "@showLyricsScreenAlbumPreludeSubtitle": { "description": "Subtitle for the setting that controls if the album cover is shown before the lyrics in the lyrics view" }, - "keepScreenOn": "Keep Screen On: Main Setting", + "keepScreenOn": "Keep Screen On", "@keepScreenOn": { "description": "Option to keep the screen on while using the app" }, @@ -1642,6 +1642,6 @@ "keepScreenOnDisabled": "Disabled", "whilePlaying": "While Playing Music", "whileLyrics": "While Showing Lyrics", - "keepScreenOnWhilePluggedIn": "Keep Screen On: only while plugged in", - "keepScreenOnWhilePluggedInSubtitle": "Ignore the Keep Screen On: Main Setting if device is unplugged" + "keepScreenOnWhilePluggedIn": "Keep Screen On only while plugged in", + "keepScreenOnWhilePluggedInSubtitle": "Ignore the Keep Screen On setting if device is unplugged" } diff --git a/lib/main.dart b/lib/main.dart index a5448179a..b9ea57497 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -118,7 +118,7 @@ void main() async { await initializeDateFormatting(localeString, null); KeepScreenOnHelper.init(); - runApp(Finamp()); + runApp(const Finamp()); } } @@ -400,7 +400,7 @@ Future _setupFinampUserHelper() async { } class Finamp extends ConsumerStatefulWidget { - Finamp({super.key}); + const Finamp({super.key}); @override ConsumerState createState() => _FinampState(); diff --git a/lib/models/finamp_models.g.dart b/lib/models/finamp_models.g.dart index 4cff2a9dd..58148935a 100644 --- a/lib/models/finamp_models.g.dart +++ b/lib/models/finamp_models.g.dart @@ -180,6 +180,11 @@ class FinampSettingsAdapter extends TypeAdapter { fields[68] == null ? false : fields[68] as bool, showSeekControlsOnMediaNotification: fields[69] == null ? true : fields[69] as bool, + keepScreenOnOption: fields[72] == null + ? KeepScreenOnOption.disabled + : fields[72] as KeepScreenOnOption, + keepScreenOnWhilePluggedIn: + fields[73] == null ? true : fields[73] as bool, ) ..disableGesture = fields[19] == null ? false : fields[19] as bool ..showFastScroller = fields[25] == null ? true : fields[25] as bool @@ -189,7 +194,7 @@ class FinampSettingsAdapter extends TypeAdapter { @override void write(BinaryWriter writer, FinampSettings obj) { writer - ..writeByte(70) + ..writeByte(72) ..writeByte(0) ..write(obj.isOffline) ..writeByte(1) @@ -329,7 +334,11 @@ class FinampSettingsAdapter extends TypeAdapter { ..writeByte(70) ..write(obj.lyricsFontSize) ..writeByte(71) - ..write(obj.showLyricsScreenAlbumPrelude); + ..write(obj.showLyricsScreenAlbumPrelude) + ..writeByte(72) + ..write(obj.keepScreenOnOption) + ..writeByte(73) + ..write(obj.keepScreenOnWhilePluggedIn); } @override @@ -1820,6 +1829,50 @@ class LyricsFontSizeAdapter extends TypeAdapter { typeId == other.typeId; } +class KeepScreenOnOptionAdapter extends TypeAdapter { + @override + final int typeId = 72; + + @override + KeepScreenOnOption read(BinaryReader reader) { + switch (reader.readByte()) { + case 0: + return KeepScreenOnOption.disabled; + case 1: + return KeepScreenOnOption.whilePlaying; + case 2: + return KeepScreenOnOption.whileLyrics; + default: + return KeepScreenOnOption.disabled; + } + } + + @override + void write(BinaryWriter writer, KeepScreenOnOption obj) { + switch (obj) { + case KeepScreenOnOption.disabled: + writer.writeByte(0); + break; + case KeepScreenOnOption.whilePlaying: + writer.writeByte(1); + break; + case KeepScreenOnOption.whileLyrics: + writer.writeByte(2); + break; + } + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is KeepScreenOnOptionAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // IsarCollectionGenerator // ************************************************************************** diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index 9151eda62..b6a133b50 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -8,19 +8,7 @@ import 'package:get_it/get_it.dart'; import 'package:keep_screen_on/keep_screen_on.dart'; import 'package:logging/logging.dart'; -/// KeepScreenOn -/// -/// Added dependencies: -/// - keep_screen_on: ^3.0.0 -/// - battery_plus: ^6.0.2 -/// This functionality adds two options in settings under "Interactions": -/// - Keep Screen On: Main Setting -/// - Keep Screen On: Only while plugged in -/// -/// Ties into the following actions: -/// - listens for audioHandler's playbackstate to determine _isPlaying. -/// - Extends NavigatorObserver class to listen for push and pop of LyricScreen to determine _isLyricsShowing -/// - listens for battyer state changes from the battery_plus widget to determine _isPluggedIn +/// Implements ability to keep screen on according to various conditions class KeepScreenOnHelper { static bool keepingScreenOn = false; diff --git a/pubspec.lock b/pubspec.lock index 493878bbf..1236190c4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -114,6 +114,22 @@ packages: url: "https://github.com/Komodo5197/flutter-balanced-text.git" source: git version: "0.0.3" + battery_plus: + dependency: "direct main" + description: + name: battery_plus + sha256: ccc1322fee1153a0f89e663e0eac2f64d659da506454cf24dcad75eb08ae138b + url: "https://pub.dev" + source: hosted + version: "6.0.2" + battery_plus_platform_interface: + dependency: transitive + description: + name: battery_plus_platform_interface + sha256: e8342c0f32de4b1dfd0223114b6785e48e579bfc398da9471c9179b907fa4910 + url: "https://pub.dev" + source: hosted + version: "2.0.1" boolean_selector: dependency: transitive description: @@ -799,22 +815,38 @@ packages: url: "https://pub.dev" source: hosted version: "0.4.11" + keep_screen_on: + dependency: "direct main" + description: + name: keep_screen_on + sha256: "374405358a3229b0e1041b6e390ff4c74e73fbd21075298042044e0c84b5574c" + url: "https://pub.dev" + source: hosted + version: "3.0.0" + keep_screen_on_platform_interface: + dependency: transitive + description: + name: keep_screen_on_platform_interface + sha256: "065a0811407a970027c7530f9b8f36d11c89f36aab85b4b5acdacfe2cf3a8568" + url: "https://pub.dev" + source: hosted + version: "3.0.0" leak_tracker: dependency: transitive description: name: leak_tracker - sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted - version: "10.0.4" + version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.5" leak_tracker_testing: dependency: transitive description: @@ -867,10 +899,10 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.8.0" + version: "0.11.1" media_kit: dependency: transitive description: @@ -900,10 +932,10 @@ packages: dependency: transitive description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" mime: dependency: transitive description: @@ -1419,10 +1451,10 @@ packages: dependency: transitive description: name: test_api - sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" + sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted - version: "0.7.0" + version: "0.7.2" time: dependency: transitive description: @@ -1463,6 +1495,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + upower: + dependency: transitive + description: + name: upower + sha256: cf042403154751180affa1d15614db7fa50234bc2373cd21c3db666c38543ebf + url: "https://pub.dev" + source: hosted + version: "0.7.0" uri_parser: dependency: transitive description: @@ -1579,10 +1619,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.2.1" + version: "14.2.5" watcher: dependency: transitive description: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index d9dcef48d..8c09f956d 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,7 @@ #include "generated_plugin_registrant.h" +#include #include #include #include @@ -15,6 +16,8 @@ #include void RegisterPlugins(flutter::PluginRegistry* registry) { + BatteryPlusWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("BatteryPlusWindowsPlugin")); IsarFlutterLibsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("IsarFlutterLibsPlugin")); MediaKitLibsWindowsAudioPluginCApiRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index c8ae9a5a8..41be227b4 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + battery_plus isar_flutter_libs media_kit_libs_windows_audio permission_handler_windows From a79b71d2d8677c6dfa1758e7f2c5e0c0ad48120b Mon Sep 17 00:00:00 2001 From: Rich T Date: Tue, 3 Sep 2024 19:40:08 -0700 Subject: [PATCH 06/10] Use wakelock_plus instead of KeepScreenOn package --- .../keep_screen_on_dropdown_list_tile.dart | 1 - lib/models/finamp_models.dart | 4 +-- lib/services/keep_screen_on_helper.dart | 6 ++-- pubspec.lock | 32 +++++++++---------- pubspec.yaml | 2 +- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart index 350ee563b..0f4883c9c 100644 --- a/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_dropdown_list_tile.dart @@ -1,4 +1,3 @@ -import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:hive/hive.dart'; diff --git a/lib/models/finamp_models.dart b/lib/models/finamp_models.dart index 9f81b2d05..fc0de9221 100644 --- a/lib/models/finamp_models.dart +++ b/lib/models/finamp_models.dart @@ -2191,11 +2191,11 @@ enum LyricsFontSize { @HiveType(typeId: 72) enum KeepScreenOnOption { @HiveField(0) - disabled, + whileLyrics, @HiveField(1) whilePlaying, @HiveField(2) - whileLyrics; + disabled; /// Human-readable version of this enum. I've written longer descriptions on /// enums like [TabContentType], and I can't be bothered to copy and paste it diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index b6a133b50..d51bf16c1 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -5,8 +5,8 @@ import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; -import 'package:keep_screen_on/keep_screen_on.dart'; import 'package:logging/logging.dart'; +import 'package:wakelock_plus/wakelock_plus.dart'; /// Implements ability to keep screen on according to various conditions class KeepScreenOnHelper { @@ -92,14 +92,14 @@ class KeepScreenOnHelper { static void _turnOn() { if (!keepingScreenOn) { keepingScreenOn = true; - KeepScreenOn.turnOn(); + WakelockPlus.enable(); } } static void _turnOff() { if (keepingScreenOn) { keepingScreenOn = false; - KeepScreenOn.turnOff(); + WakelockPlus.disable(); } } } diff --git a/pubspec.lock b/pubspec.lock index 1236190c4..2f39e2bd4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -815,22 +815,6 @@ packages: url: "https://pub.dev" source: hosted version: "0.4.11" - keep_screen_on: - dependency: "direct main" - description: - name: keep_screen_on - sha256: "374405358a3229b0e1041b6e390ff4c74e73fbd21075298042044e0c84b5574c" - url: "https://pub.dev" - source: hosted - version: "3.0.0" - keep_screen_on_platform_interface: - dependency: transitive - description: - name: keep_screen_on_platform_interface - sha256: "065a0811407a970027c7530f9b8f36d11c89f36aab85b4b5acdacfe2cf3a8568" - url: "https://pub.dev" - source: hosted - version: "3.0.0" leak_tracker: dependency: transitive description: @@ -1623,6 +1607,22 @@ packages: url: "https://pub.dev" source: hosted version: "14.2.5" + wakelock_plus: + dependency: "direct main" + description: + name: wakelock_plus + sha256: bf4ee6f17a2fa373ed3753ad0e602b7603f8c75af006d5b9bdade263928c0484 + url: "https://pub.dev" + source: hosted + version: "1.2.8" + wakelock_plus_platform_interface: + dependency: transitive + description: + name: wakelock_plus_platform_interface + sha256: "422d1cdbb448079a8a62a5a770b69baa489f8f7ca21aef47800c726d404f9d16" + url: "https://pub.dev" + source: hosted + version: "1.2.1" watcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b6ee75473..61dea73ad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -101,7 +101,7 @@ dependencies: scroll_to_index: ^3.0.1 window_manager: ^0.3.8 url_launcher: ^6.2.6 - keep_screen_on: ^3.0.0 + wakelock_plus: ^1.2.8 battery_plus: ^6.0.2 dev_dependencies: From 4487674e1f803c8e1c6535d30a95c772e96e614d Mon Sep 17 00:00:00 2001 From: Rich T Date: Tue, 3 Sep 2024 20:04:07 -0700 Subject: [PATCH 07/10] Converted KeepScreenOnHelper to singleton rather than static class --- ...eep_screen_on_while_charging_selector.dart | 1 - lib/main.dart | 8 +++- lib/services/keep_screen_on_helper.dart | 45 ++++++++++--------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart index 1e3e82ea5..e89969c15 100644 --- a/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart +++ b/lib/components/InteractionSettingsScreen/keep_screen_on_while_charging_selector.dart @@ -1,4 +1,3 @@ -import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:hive/hive.dart'; diff --git a/lib/main.dart b/lib/main.dart index b9ea57497..e76a12faf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -87,6 +87,7 @@ void main() async { await _setupDownloadsHelper(); await _setupOSIntegration(); await _setupPlaybackServices(); + await _setupKeepScreenOnHelper(); } catch (error, trace) { hasFailed = true; Logger("ErrorApp").severe(error, null, trace); @@ -116,8 +117,7 @@ void main() async { : LocaleHelper.locale.toString()) : "en_US"; await initializeDateFormatting(localeString, null); - - KeepScreenOnHelper.init(); + runApp(const Finamp()); } } @@ -153,6 +153,10 @@ Future _setupDownloadsHelper() async { await downloadsService.startQueues(); } +Future _setupKeepScreenOnHelper() async { + GetIt.instance.registerSingleton(KeepScreenOnHelper()); +} + Future setupHive() async { await Hive.initFlutter(); Hive.registerAdapter(BaseItemDtoAdapter()); diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index d51bf16c1..8415e88f2 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -10,25 +10,29 @@ import 'package:wakelock_plus/wakelock_plus.dart'; /// Implements ability to keep screen on according to various conditions class KeepScreenOnHelper { - static bool keepingScreenOn = false; + bool _keepingScreenOn = false; - static bool _isPlaying = false; - static bool _isLyricsShowing = false; - static bool _isPluggedIn = false; + bool _isPlaying = false; + bool _isLyricsShowing = false; + bool _isPluggedIn = false; - static final _keepScreenOnLogger = Logger("KeepScreenOnHelper"); + final _keepScreenOnLogger = Logger("KeepScreenOnHelper"); + + KeepScreenOnHelper() { + _attachEvents(); + } - static void init() { + void _attachEvents() { // Subscribe to audio playback events final audioHandler = GetIt.instance(); audioHandler.playbackState.listen((event) async { - KeepScreenOnHelper.setCondition(isPlaying: event.playing); + setCondition(isPlaying: event.playing); }); // Subscribe to battery state change events var battery = Battery(); battery.onBatteryStateChanged.listen((BatteryState state) { - KeepScreenOnHelper.setCondition(batteryState: state); + setCondition(batteryState: state); }); FinampSettingsHelper.finampSettingsListener.addListener(() { @@ -37,13 +41,13 @@ class KeepScreenOnHelper { }); } - static void setKeepScreenOn() { + void setKeepScreenOn() { if (FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn && !_isPluggedIn) { _turnOff(); } else { switch (FinampSettingsHelper.finampSettings.keepScreenOnOption) { case KeepScreenOnOption.disabled: - if (keepingScreenOn) _turnOff(); + if (_keepingScreenOn) _turnOff(); break; case KeepScreenOnOption.whilePlaying: if (_isPlaying) { @@ -62,10 +66,10 @@ class KeepScreenOnHelper { } } _keepScreenOnLogger.fine( - "keepingScreenOn: $keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); + "keepingScreenOn: $_keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); } - static void setCondition({bool? isPlaying, bool? isLyricsShowing, BatteryState? batteryState}) { + void setCondition({bool? isPlaying, bool? isLyricsShowing, BatteryState? batteryState}) { if (isPlaying != null) _isPlaying = isPlaying; if (isLyricsShowing != null) _isLyricsShowing = isLyricsShowing; if (batteryState != null) { @@ -89,34 +93,35 @@ class KeepScreenOnHelper { setKeepScreenOn(); } - static void _turnOn() { - if (!keepingScreenOn) { - keepingScreenOn = true; + void _turnOn() { + if (!_keepingScreenOn) { + _keepingScreenOn = true; WakelockPlus.enable(); } } - static void _turnOff() { - if (keepingScreenOn) { - keepingScreenOn = false; + void _turnOff() { + if (_keepingScreenOn) { + _keepingScreenOn = false; WakelockPlus.disable(); } } } class KeepScreenOnObserver extends NavigatorObserver { + final keepScreenOnHelper = GetIt.instance(); static final _lyricsCheck = ModalRoute.withName(LyricsScreen.routeName); @override void didPush(Route route, Route? previousRoute) { // Just pushed to lyrics? - if (_lyricsCheck(route)) KeepScreenOnHelper.setCondition(isLyricsShowing: true); + if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: true); super.didPush(route, previousRoute); } @override void didPop(Route route, Route? previousRoute) { // Just popped lyrics? - if (_lyricsCheck(route)) KeepScreenOnHelper.setCondition(isLyricsShowing: false); + if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: false); super.didPop(route, previousRoute); } } \ No newline at end of file From a190247e4b30bdc1b96a12ede8c82507e80f5442 Mon Sep 17 00:00:00 2001 From: Rich T Date: Wed, 4 Sep 2024 14:29:57 -0700 Subject: [PATCH 08/10] Reverted to calling KeepScreenOnHelper from build and dispose of LyicsScreen --- lib/main.dart | 5 +---- lib/models/finamp_models.dart | 4 ++-- lib/screens/lyrics_screen.dart | 9 +++++++++ lib/services/keep_screen_on_helper.dart | 8 ++++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index b9ea57497..997ff93b3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -513,10 +513,7 @@ class _FinampState extends ConsumerState with WindowListener { const LanguageSelectionScreen(), }, initialRoute: SplashScreen.routeName, - navigatorObservers: [ - SplitScreenNavigatorObserver(), - KeepScreenOnObserver() - ], + navigatorObservers: [SplitScreenNavigatorObserver()], builder: buildPlayerSplitScreenScaffold, theme: ThemeData( brightness: Brightness.light, diff --git a/lib/models/finamp_models.dart b/lib/models/finamp_models.dart index fc0de9221..9f81b2d05 100644 --- a/lib/models/finamp_models.dart +++ b/lib/models/finamp_models.dart @@ -2191,11 +2191,11 @@ enum LyricsFontSize { @HiveType(typeId: 72) enum KeepScreenOnOption { @HiveField(0) - whileLyrics, + disabled, @HiveField(1) whilePlaying, @HiveField(2) - disabled; + whileLyrics; /// Human-readable version of this enum. I've written longer descriptions on /// enums like [TabContentType], and I can't be bothered to copy and paste it diff --git a/lib/screens/lyrics_screen.dart b/lib/screens/lyrics_screen.dart index 9f0ac532e..afa4fe979 100644 --- a/lib/screens/lyrics_screen.dart +++ b/lib/screens/lyrics_screen.dart @@ -7,6 +7,7 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart'; import 'package:finamp/services/current_track_metadata_provider.dart'; import 'package:finamp/services/feedback_helper.dart'; +import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; import 'package:finamp/services/progress_state_stream.dart'; import 'package:flutter/material.dart'; @@ -71,11 +72,19 @@ class _LyricsScreenContent extends StatefulWidget { } class _LyricsScreenContentState extends State<_LyricsScreenContent> { + @override + void dispose() { + KeepScreenOnHelper.setCondition(isLyricsShowing: false); + super.dispose(); + } + @override Widget build(BuildContext context) { double toolbarHeight = 53; int maxLines = 2; + KeepScreenOnHelper.setCondition(isLyricsShowing: true); + var controller = PlayerHideableController(); return Scaffold( diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index d51bf16c1..152fc444f 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -15,6 +15,7 @@ class KeepScreenOnHelper { static bool _isPlaying = false; static bool _isLyricsShowing = false; static bool _isPluggedIn = false; + static BatteryState _prevBattState = BatteryState.unknown; static final _keepScreenOnLogger = Logger("KeepScreenOnHelper"); @@ -28,7 +29,10 @@ class KeepScreenOnHelper { // Subscribe to battery state change events var battery = Battery(); battery.onBatteryStateChanged.listen((BatteryState state) { - KeepScreenOnHelper.setCondition(batteryState: state); + if (_prevBattState != state) { + _prevBattState = state; + KeepScreenOnHelper.setCondition(batteryState: state); + } }); FinampSettingsHelper.finampSettingsListener.addListener(() { @@ -104,7 +108,7 @@ class KeepScreenOnHelper { } } -class KeepScreenOnObserver extends NavigatorObserver { +class KeepScreenOnObserver extends RouteObserver> { static final _lyricsCheck = ModalRoute.withName(LyricsScreen.routeName); @override void didPush(Route route, Route? previousRoute) { From 69574ed18b45772d7aee6dafb26beac3866885ea Mon Sep 17 00:00:00 2001 From: Rich T Date: Thu, 5 Sep 2024 17:24:07 -0700 Subject: [PATCH 09/10] Removed code that extends Navigator Observer. --- lib/services/keep_screen_on_helper.dart | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index 1d4d79d3e..69886d9cd 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -1,9 +1,7 @@ import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/models/finamp_models.dart'; -import 'package:finamp/screens/lyrics_screen.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; -import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:logging/logging.dart'; import 'package:wakelock_plus/wakelock_plus.dart'; @@ -111,22 +109,4 @@ class KeepScreenOnHelper { WakelockPlus.disable(); } } -} - -class KeepScreenOnObserver extends NavigatorObserver { - final keepScreenOnHelper = GetIt.instance(); - static final _lyricsCheck = ModalRoute.withName(LyricsScreen.routeName); - @override - void didPush(Route route, Route? previousRoute) { - // Just pushed to lyrics? - if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: true); - super.didPush(route, previousRoute); - } - - @override - void didPop(Route route, Route? previousRoute) { - // Just popped lyrics? - if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: false); - super.didPop(route, previousRoute); - } } \ No newline at end of file From d7525029577daa8191a742b82bb9bf45c04496d3 Mon Sep 17 00:00:00 2001 From: Rich T Date: Fri, 6 Sep 2024 07:58:58 -0700 Subject: [PATCH 10/10] Modified: Use NavigatorObserver to listen for lyrics screen show and hide events. Modified: Changed default settings to "While Lyrics are Showing" --- .../player_split_screen_scaffold.dart | 5 +++- lib/main.dart | 5 +++- lib/models/finamp_models.dart | 2 +- lib/screens/lyrics_screen.dart | 11 --------- lib/services/keep_screen_on_helper.dart | 24 +++++++++++++++++-- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/components/PlayerScreen/player_split_screen_scaffold.dart b/lib/components/PlayerScreen/player_split_screen_scaffold.dart index 24851d87d..2251683d6 100644 --- a/lib/components/PlayerScreen/player_split_screen_scaffold.dart +++ b/lib/components/PlayerScreen/player_split_screen_scaffold.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:finamp/components/global_snackbar.dart'; import 'package:finamp/screens/lyrics_screen.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; +import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:get_it/get_it.dart'; @@ -132,7 +133,9 @@ Widget buildPlayerSplitScreenScaffold(BuildContext context, Widget? widget) { .pushNamed(x.name!, arguments: x.arguments); return EmptyRoute(); - }), + }, + observers: [KeepScreenOnObserver()] + ), )), ) ]); diff --git a/lib/main.dart b/lib/main.dart index 0658f01f8..e76a12faf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -517,7 +517,10 @@ class _FinampState extends ConsumerState with WindowListener { const LanguageSelectionScreen(), }, initialRoute: SplashScreen.routeName, - navigatorObservers: [SplitScreenNavigatorObserver()], + navigatorObservers: [ + SplitScreenNavigatorObserver(), + KeepScreenOnObserver() + ], builder: buildPlayerSplitScreenScaffold, theme: ThemeData( brightness: Brightness.light, diff --git a/lib/models/finamp_models.dart b/lib/models/finamp_models.dart index 9f81b2d05..b8a3e1c81 100644 --- a/lib/models/finamp_models.dart +++ b/lib/models/finamp_models.dart @@ -111,7 +111,7 @@ const _lyricsFontSizeDefault = LyricsFontSize.medium; const _showLyricsScreenAlbumPreludeDefault = true; const _showStopButtonOnMediaNotificationDefault = false; const _showSeekControlsOnMediaNotificationDefault = true; -const _keepScreenOnOption = KeepScreenOnOption.disabled; +const _keepScreenOnOption = KeepScreenOnOption.whileLyrics; const _keepScreenOnWhilePluggedIn = true; @HiveType(typeId: 28) diff --git a/lib/screens/lyrics_screen.dart b/lib/screens/lyrics_screen.dart index b8a4bc1ba..9f0ac532e 100644 --- a/lib/screens/lyrics_screen.dart +++ b/lib/screens/lyrics_screen.dart @@ -7,7 +7,6 @@ import 'package:finamp/models/finamp_models.dart'; import 'package:finamp/models/jellyfin_models.dart'; import 'package:finamp/services/current_track_metadata_provider.dart'; import 'package:finamp/services/feedback_helper.dart'; -import 'package:finamp/services/keep_screen_on_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; import 'package:finamp/services/progress_state_stream.dart'; import 'package:flutter/material.dart'; @@ -72,21 +71,11 @@ class _LyricsScreenContent extends StatefulWidget { } class _LyricsScreenContentState extends State<_LyricsScreenContent> { - final keepScreenOnHelper = GetIt.instance(); - - @override - void dispose() { - keepScreenOnHelper.setCondition(isLyricsShowing: false); - super.dispose(); - } - @override Widget build(BuildContext context) { double toolbarHeight = 53; int maxLines = 2; - keepScreenOnHelper.setCondition(isLyricsShowing: true); - var controller = PlayerHideableController(); return Scaffold( diff --git a/lib/services/keep_screen_on_helper.dart b/lib/services/keep_screen_on_helper.dart index 69886d9cd..5142fb5c8 100644 --- a/lib/services/keep_screen_on_helper.dart +++ b/lib/services/keep_screen_on_helper.dart @@ -1,7 +1,9 @@ import 'package:battery_plus/battery_plus.dart'; import 'package:finamp/models/finamp_models.dart'; +import 'package:finamp/screens/lyrics_screen.dart'; import 'package:finamp/services/finamp_settings_helper.dart'; import 'package:finamp/services/music_player_background_task.dart'; +import 'package:flutter/widgets.dart'; import 'package:get_it/get_it.dart'; import 'package:logging/logging.dart'; import 'package:wakelock_plus/wakelock_plus.dart'; @@ -68,8 +70,7 @@ class KeepScreenOnHelper { break; } } - _keepScreenOnLogger.fine( - "keepingScreenOn: $_keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); + _keepScreenOnLogger.fine("keepingScreenOn: $_keepingScreenOn | mainSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnOption} | whilePluggedInSetting: ${FinampSettingsHelper.finampSettings.keepScreenOnWhilePluggedIn} | isPlaying: $_isPlaying | lyricsShowing: $_isLyricsShowing | isPluggedIn: $_isPluggedIn"); } void setCondition({bool? isPlaying, bool? isLyricsShowing, BatteryState? batteryState}) { @@ -109,4 +110,23 @@ class KeepScreenOnHelper { WakelockPlus.disable(); } } +} + +class KeepScreenOnObserver extends NavigatorObserver { + final KeepScreenOnHelper keepScreenOnHelper = GetIt.instance(); + + static final _lyricsCheck = ModalRoute.withName(LyricsScreen.routeName); + @override + void didPush(Route route, Route? previousRoute) { + // Just pushed to lyrics? + if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: true); + super.didPush(route, previousRoute); + } + + @override + void didPop(Route route, Route? previousRoute) { + // Just popped lyrics? + if (_lyricsCheck(route)) keepScreenOnHelper.setCondition(isLyricsShowing: false); + super.didPop(route, previousRoute); + } } \ No newline at end of file