Skip to content

Commit

Permalink
feat: kill player after app dismiss option
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Oct 25, 2023
1 parent e033629 commit 800965c
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 45 deletions.
4 changes: 4 additions & 0 deletions lib/controller/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ class Player {
await _audioHandler.pause();
}

Future<void> dispose() async {
await _audioHandler.onDispose();
}

Future<void> pauseRaw() async {
await _audioHandler.onPauseRaw();
}
Expand Down
7 changes: 7 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class SettingsController {
final onNotificationTapAction = NotificationTapAction.openApp.obs;
final onYoutubeLinkOpen = OnYoutubeLinkOpenAction.alwaysAsk.obs;
final performanceMode = PerformanceMode.balanced.obs;
final killPlayerAfterDismissingAppMode = KillAppMode.never.obs;

final RxMap<TrackTilePosition, TrackTileItem> trackItem = {
TrackTilePosition.row1Item1: TrackTileItem.title,
Expand Down Expand Up @@ -420,6 +421,7 @@ class SettingsController {
onNotificationTapAction.value = NotificationTapAction.values.getEnum(json['onNotificationTapAction']) ?? onNotificationTapAction.value;
onYoutubeLinkOpen.value = OnYoutubeLinkOpenAction.values.getEnum(json['onYoutubeLinkOpen']) ?? onYoutubeLinkOpen.value;
performanceMode.value = PerformanceMode.values.getEnum(json['performanceMode']) ?? performanceMode.value;
killPlayerAfterDismissingAppMode.value = KillAppMode.values.getEnum(json['killPlayerAfterDismissingAppMode']) ?? killPlayerAfterDismissingAppMode.value;

trackItem.value = _getEnumMap(
json['trackItem'],
Expand Down Expand Up @@ -594,6 +596,7 @@ class SettingsController {
'onNotificationTapAction': onNotificationTapAction.value.convertToString,
'onYoutubeLinkOpen': onYoutubeLinkOpen.value.convertToString,
'performanceMode': performanceMode.value.convertToString,
'killPlayerAfterDismissingAppMode': killPlayerAfterDismissingAppMode.value.convertToString,
'mostPlayedCustomDateRange': mostPlayedCustomDateRange.value.toJson(),
'mostPlayedCustomisStartOfDay': mostPlayedCustomisStartOfDay.value,

Expand Down Expand Up @@ -760,6 +763,7 @@ class SettingsController {
NotificationTapAction? onNotificationTapAction,
OnYoutubeLinkOpenAction? onYoutubeLinkOpen,
PerformanceMode? performanceMode,
KillAppMode? killPlayerAfterDismissingAppMode,
DateRange? mostPlayedCustomDateRange,
bool? mostPlayedCustomisStartOfDay,
bool? didSupportNamida,
Expand Down Expand Up @@ -1206,6 +1210,9 @@ class SettingsController {
if (performanceMode != null) {
this.performanceMode.value = performanceMode;
}
if (killPlayerAfterDismissingAppMode != null) {
this.killPlayerAfterDismissingAppMode.value = killPlayerAfterDismissingAppMode;
}
if (mostPlayedCustomDateRange != null) {
this.mostPlayedCustomDateRange.value = mostPlayedCustomDateRange;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/core/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,9 @@ enum PerformanceMode {
goodLooking,
custom,
}

enum KillAppMode {
always,
ifNotPlaying,
never,
}
9 changes: 9 additions & 0 deletions lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ extension PlayerRepeatModeUtils on RepeatMode {
IconData toIcon() => _NamidaConverters.inst.getIcon(this);
}

extension KillAppModeutils on KillAppMode {
String toText() => _NamidaConverters.inst.getTitle(this);
}

extension OnYoutubeLinkOpenActionUtils on OnYoutubeLinkOpenAction {
String toText() => _NamidaConverters.inst.getTitle(this);
IconData toIcon() => _NamidaConverters.inst.getIcon(this);
Expand Down Expand Up @@ -1276,6 +1280,11 @@ class _NamidaConverters {
PerformanceMode.balanced: lang.BALANCED,
PerformanceMode.goodLooking: lang.GOOD_LOOKING,
PerformanceMode.custom: lang.CUSTOM,
},
KillAppMode: {
KillAppMode.never: lang.NEVER,
KillAppMode.ifNotPlaying: lang.IF_NOT_PLAYING,
KillAppMode.always: lang.ALWAYS,
}
};

Expand Down
4 changes: 4 additions & 0 deletions lib/core/translations/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class LanguageKeys {
late String ALBUMS_COUNT;
late String ALBUMS;
late String ALL_TIME;
late String ALWAYS;
late String ALWAYS_ASK;
late String ALWAYS_RESTORE;
late String ANIMATING_THUMBNAIL_INTENSITY;
Expand Down Expand Up @@ -241,6 +242,7 @@ class LanguageKeys {
late String HISTORY_LISTENS_REPLACE_WARNING;
late String HOME;
late String HOUR_FORMAT_12;
late String IF_NOT_PLAYING;
late String IGNORES;
late String IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE;
late String IMPORT_ALL;
Expand Down Expand Up @@ -268,6 +270,7 @@ class LanguageKeys {
late String KEEP_SCREEN_AWAKE_MINIPLAYER_EXPANDED;
late String KEEP_SCREEN_AWAKE_NONE;
late String KEEP_SCREEN_AWAKE_WHEN;
late String KILL_PLAYER_AFTER_DISMISSING_APP;
late String LANGUAGE;
late String LIBRARY_TABS_REORDER;
late String LIBRARY_TABS;
Expand Down Expand Up @@ -318,6 +321,7 @@ class LanguageKeys {
late String MULTIPLE_TRACKS_TAGS_EDIT_NOTE;
late String NAME_CONTAINS_BAD_CHARACTER;
late String NAME;
late String NEVER;
late String NEW_DIRECTORY;
late String NEW_TRACKS_ADD;
late String NEW_TRACKS_MOODS_SUBTITLE;
Expand Down
8 changes: 8 additions & 0 deletions lib/core/translations/language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Language extends LanguageKeys {
ALBUMS_COUNT = getKey("ALBUMS_COUNT");
ALBUMS = getKey("ALBUMS");
ALL_TIME = getKey("ALL_TIME");
ALWAYS = getKey("ALWAYS");
ALWAYS_ASK = getKey("ALWAYS_ASK");
ALWAYS_RESTORE = getKey("ALWAYS_RESTORE");
ANIMATING_THUMBNAIL_INTENSITY = getKey("ANIMATING_THUMBNAIL_INTENSITY");
Expand Down Expand Up @@ -308,6 +309,7 @@ class Language extends LanguageKeys {
HISTORY_LISTENS_REPLACE_WARNING = getKey("HISTORY_LISTENS_REPLACE_WARNING");
HOME = getKey("HOME");
HOUR_FORMAT_12 = getKey("HOUR_FORMAT_12");
IF_NOT_PLAYING = getKey("IF_NOT_PLAYING");
IGNORES = getKey("IGNORES");
IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE = getKey("IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE");
IMPORT_ALL = getKey("IMPORT_ALL");
Expand Down Expand Up @@ -335,6 +337,7 @@ class Language extends LanguageKeys {
KEEP_SCREEN_AWAKE_MINIPLAYER_EXPANDED = getKey("KEEP_SCREEN_AWAKE_MINIPLAYER_EXPANDED");
KEEP_SCREEN_AWAKE_NONE = getKey("KEEP_SCREEN_AWAKE_NONE");
KEEP_SCREEN_AWAKE_WHEN = getKey("KEEP_SCREEN_AWAKE_WHEN");
KILL_PLAYER_AFTER_DISMISSING_APP = getKey("KILL_PLAYER_AFTER_DISMISSING_APP");
LANGUAGE = getKey("LANGUAGE");
LIBRARY_TABS_REORDER = getKey("LIBRARY_TABS_REORDER");
LIBRARY_TABS = getKey("LIBRARY_TABS");
Expand Down Expand Up @@ -385,6 +388,7 @@ class Language extends LanguageKeys {
MULTIPLE_TRACKS_TAGS_EDIT_NOTE = getKey("MULTIPLE_TRACKS_TAGS_EDIT_NOTE");
NAME_CONTAINS_BAD_CHARACTER = getKey("NAME_CONTAINS_BAD_CHARACTER");
NAME = getKey("NAME");
NEVER = getKey("NEVER");
NEW_DIRECTORY = getKey("NEW_DIRECTORY");
NEW_TRACKS_ADD = getKey("NEW_TRACKS_ADD");
NEW_TRACKS_MOODS_SUBTITLE = getKey("NEW_TRACKS_MOODS_SUBTITLE");
Expand Down Expand Up @@ -745,6 +749,10 @@ class Language extends LanguageKeys {










Expand Down
55 changes: 32 additions & 23 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,30 +323,39 @@ class Namida extends StatelessWidget {
return Stack(
alignment: Alignment.bottomLeft,
children: [
Obx(
() {
final locale = settings.selectedLanguage.value.code.split('_');
return GetMaterialApp(
key: Key(locale.join()),
themeAnimationDuration: const Duration(milliseconds: kThemeAnimationDurationMS),
debugShowCheckedModeBanner: false,
title: 'Namida',
// restorationScopeId: 'Namida',
theme: AppThemes.inst.getAppTheme(CurrentColor.inst.currentColorScheme, true),
darkTheme: AppThemes.inst.getAppTheme(CurrentColor.inst.currentColorScheme, false),
themeMode: settings.themeMode.value,
builder: (context, widget) {
return ScrollConfiguration(behavior: const ScrollBehaviorModified(), child: widget!);
},
home: MainPageWrapper(
shouldShowOnBoarding: shouldShowOnBoarding,
onContextAvailable: (ctx) {
_initialContext = ctx;
_waitForFirstBuildContext.isCompleted ? null : _waitForFirstBuildContext.complete(true);
},
),
);
NamidaLifeCycleWrapper(
onDetach: () async {
final mode = settings.killPlayerAfterDismissingAppMode.value;
if (mode == KillAppMode.always || (mode == KillAppMode.ifNotPlaying && !Player.inst.isPlaying)) {
await Player.inst.pause();
await Player.inst.dispose();
}
},
child: Obx(
() {
final locale = settings.selectedLanguage.value.code.split('_');
return GetMaterialApp(
key: Key(locale.join()),
themeAnimationDuration: const Duration(milliseconds: kThemeAnimationDurationMS),
debugShowCheckedModeBanner: false,
title: 'Namida',
// restorationScopeId: 'Namida',
theme: AppThemes.inst.getAppTheme(CurrentColor.inst.currentColorScheme, true),
darkTheme: AppThemes.inst.getAppTheme(CurrentColor.inst.currentColorScheme, false),
themeMode: settings.themeMode.value,
builder: (context, widget) {
return ScrollConfiguration(behavior: const ScrollBehaviorModified(), child: widget!);
},
home: MainPageWrapper(
shouldShowOnBoarding: shouldShowOnBoarding,
onContextAvailable: (ctx) {
_initialContext = ctx;
_waitForFirstBuildContext.isCompleted ? null : _waitForFirstBuildContext.complete(true);
},
),
);
},
),
),

// prevent accidental opening for drawer when performing back gesture
Expand Down
37 changes: 17 additions & 20 deletions lib/ui/widgets/custom_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2712,53 +2712,50 @@ class NamidaLifeCycleWrapper extends StatefulWidget {
final Widget child;
final Future<void> Function()? onResume;
final Future<void> Function()? onSuspending;
final Future<void> Function()? onDetach;

const NamidaLifeCycleWrapper({
super.key,
required this.child,
this.onResume,
this.onSuspending,
this.onDetach,
});

@override
State<NamidaLifeCycleWrapper> createState() => _NamidaLifeCycleWrapperState();
}

class _NamidaLifeCycleWrapperState extends State<NamidaLifeCycleWrapper> with WidgetsBindingObserver {
class _NamidaLifeCycleWrapperState extends State<NamidaLifeCycleWrapper> {
late final AppLifecycleListener listener;

@override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
void initState() {
super.initState();

listener = AppLifecycleListener(
onResume: () async {
await Future.wait([
SystemChrome.setPreferredOrientations(kDefaultOrientations),
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values),
if (widget.onResume != null) widget.onResume!(),
]);
break;
case AppLifecycleState.inactive:
await widget.onSuspending?.call();
break;
case AppLifecycleState.paused:
case AppLifecycleState.detached:
}
}
},
onInactive: () async => await widget.onSuspending?.call(),
onDetach: () async => await widget.onDetach?.call(),
);

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
WidgetsBinding.instance.addObserver(listener);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
WidgetsBinding.instance.removeObserver(listener);
super.dispose();
}

@override
Widget build(BuildContext context) {
return widget.child;
}
Widget build(BuildContext context) => widget.child;
}

class NamidaAspectRatio extends StatelessWidget {
Expand Down
33 changes: 33 additions & 0 deletions lib/ui/widgets/settings/playback_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,39 @@ class PlaybackSettings extends StatelessWidget {
},
),
),
CustomListTile(
title: lang.KILL_PLAYER_AFTER_DISMISSING_APP,
icon: Broken.forbidden_2,
trailingRaw: NamidaPopupWrapper(
children: [
...KillAppMode.values.map(
(e) => Obx(
() => NamidaInkWell(
margin: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 2.0),
padding: const EdgeInsets.symmetric(horizontal: 4.0, vertical: 6.0),
borderRadius: 6.0,
bgColor: settings.killPlayerAfterDismissingAppMode.value == e ? context.theme.cardColor : null,
child: Text(
e.toText(),
style: context.textTheme.displayMedium?.copyWith(fontSize: 14.0.multipliedFontScale),
),
onTap: () {
settings.save(killPlayerAfterDismissingAppMode: e);
NamidaNavigator.inst.popMenu(handleClosing: false);
Navigator.of(context).pop();
},
),
),
),
],
child: Obx(
() => Text(
settings.killPlayerAfterDismissingAppMode.value.toText(),
style: context.textTheme.displaySmall,
),
),
),
),
Obx(
() => CustomListTile(
title: lang.ON_NOTIFICATION_TAP,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: "none"
version: 1.5.4+231023

environment:
sdk: ">=3.0.5 <4.0.0"
sdk: ">=3.1.4 <4.0.0"

dependency_overrides:
win32: ^3.0.0
Expand Down Expand Up @@ -57,7 +57,7 @@ dependencies:
picture_in_picture:
git:
url: https://github.com/MSOB7YY/picture_in_picture
ref: f0a76aaf72d668db9bcb8e3289357ea9e065e852
ref: 8b1927af4781cb4a3b5e828053d42f404c79b947
flutter_volume_controller: ^1.3.0
flutter_markdown: ^0.6.17+3
flutter_mailer: ^2.1.1
Expand Down

0 comments on commit 800965c

Please sign in to comment.