Skip to content

Commit

Permalink
Screen does not stay on #781 (#858)
Browse files Browse the repository at this point in the history
* Added: Ability to keep screen on while using app

* Modified:  Moved battery state determination into KeepScreenOnHelper class.

* Used events where possible

* Revert lyrics_screen and music_player_background_task to redesign branch

* Small changes and adding generated files

* Use wakelock_plus instead of KeepScreenOn package

* Converted KeepScreenOnHelper to singleton rather than static class

* Reverted to calling KeepScreenOnHelper from build and dispose of LyicsScreen

* Removed code that extends Navigator Observer.

* Modified: Use NavigatorObserver to listen for lyrics screen show and hide events.

Modified: Changed default settings to "While Lyrics are Showing"

---------

Co-authored-by: Rich T <[email protected]>
  • Loading branch information
Sp4rky001 and Rich T authored Sep 6, 2024
1 parent bc2f006 commit 049185c
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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({super.key});

@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Box<FinampSettings>>(
valueListenable: FinampSettingsHelper.finampSettingsListener,
builder: (_, box, __) {
return ListTile(
title: Text(AppLocalizations.of(context)!.keepScreenOn),
subtitle: Text(AppLocalizations.of(context)!.keepScreenOnSubtitle),
trailing: DropdownButton<KeepScreenOnOption>(
value: box.get("FinampSettings")?.keepScreenOnOption,
items: KeepScreenOnOption.values
.map((e) => DropdownMenuItem<KeepScreenOnOption>(
value: e,
child: Text(e.toLocalisedString(context)),
))
.toList(),
onChanged: (value) {
if (value != null) {
FinampSettingsHelper.setKeepScreenOnOption(value);
}
},
),
);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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({super.key});

@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Box<FinampSettings>>(
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);
},
);
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -132,7 +133,9 @@ Widget buildPlayerSplitScreenScaffold(BuildContext context, Widget? widget) {
.pushNamed(x.name!,
arguments: x.arguments);
return EmptyRoute();
}),
},
observers: [KeepScreenOnObserver()]
),
)),
)
]);
Expand Down
12 changes: 11 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"@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 setting if device is unplugged"
}
16 changes: 13 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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';
Expand Down Expand Up @@ -86,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);
Expand Down Expand Up @@ -115,7 +117,7 @@ void main() async {
: LocaleHelper.locale.toString())
: "en_US";
await initializeDateFormatting(localeString, null);

runApp(const Finamp());
}
}
Expand Down Expand Up @@ -151,6 +153,10 @@ Future<void> _setupDownloadsHelper() async {
await downloadsService.startQueues();
}

Future<void> _setupKeepScreenOnHelper() async {
GetIt.instance.registerSingleton(KeepScreenOnHelper());
}

Future<void> setupHive() async {
await Hive.initFlutter();
Hive.registerAdapter(BaseItemDtoAdapter());
Expand Down Expand Up @@ -214,6 +220,7 @@ Future<void> setupHive() async {
Hive.registerAdapter(LyricDtoAdapter());
Hive.registerAdapter(LyricsAlignmentAdapter());
Hive.registerAdapter(LyricsFontSizeAdapter());
Hive.registerAdapter(KeepScreenOnOptionAdapter());

final dir = (Platform.isAndroid || Platform.isIOS)
? await getApplicationDocumentsDirectory()
Expand Down Expand Up @@ -397,7 +404,7 @@ Future<void> _setupFinampUserHelper() async {
}

class Finamp extends ConsumerStatefulWidget {
const Finamp({Key? key}) : super(key: key);
const Finamp({super.key});

@override
ConsumerState<Finamp> createState() => _FinampState();
Expand Down Expand Up @@ -510,7 +517,10 @@ class _FinampState extends ConsumerState<Finamp> with WindowListener {
const LanguageSelectionScreen(),
},
initialRoute: SplashScreen.routeName,
navigatorObservers: [SplitScreenNavigatorObserver()],
navigatorObservers: [
SplitScreenNavigatorObserver(),
KeepScreenOnObserver()
],
builder: buildPlayerSplitScreenScaffold,
theme: ThemeData(
brightness: Brightness.light,
Expand Down
53 changes: 53 additions & 0 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const _lyricsFontSizeDefault = LyricsFontSize.medium;
const _showLyricsScreenAlbumPreludeDefault = true;
const _showStopButtonOnMediaNotificationDefault = false;
const _showSeekControlsOnMediaNotificationDefault = true;
const _keepScreenOnOption = KeepScreenOnOption.whileLyrics;
const _keepScreenOnWhilePluggedIn = true;

@HiveType(typeId: 28)
class FinampSettings {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<FinampSettings> create() async {
final downloadLocation = await DownloadLocation.create(
name: "Internal Storage",
Expand Down Expand Up @@ -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;
}
}
}
57 changes: 55 additions & 2 deletions lib/models/finamp_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/screens/interaction_settings_screen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,6 +29,8 @@ class InteractionSettingsScreen extends StatelessWidget {
FastScrollSelector(),
DisableGestureSelector(),
DisableVibrationSelector(),
KeepScreenOnDropdownListTile(),
KeepScreenOnWhilePluggedInSelector()
],
),
);
Expand Down
14 changes: 14 additions & 0 deletions lib/services/finamp_settings_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,18 @@ class FinampSettingsHelper {
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setKeepScreenOnOption(KeepScreenOnOption keepScreenOnOption) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.keepScreenOnOption = keepScreenOnOption;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setKeepScreenOnWhileCharging(bool keepScreenOnWhileCharging) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.keepScreenOnWhilePluggedIn = keepScreenOnWhileCharging;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}
}
Loading

0 comments on commit 049185c

Please sign in to comment.