Skip to content

Commit

Permalink
Merge pull request #1382 from TheLastFlame/main
Browse files Browse the repository at this point in the history
Remembering window closing action
  • Loading branch information
hiddify-com authored Oct 3, 2024
2 parents 0ff4a04 + e3cc918 commit e31dc29
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release


/data
13 changes: 10 additions & 3 deletions assets/translations/strings_en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@
"ignoreBatteryOptimizationsMsg": "Remove Restrictions For Optimal VPN Performance",
"dynamicNotification": "Display Speed in Notification",
"hapticFeedback": "Haptic Feedback",
"autoIpCheck": "Automatically Check Connection IP"
"autoIpCheck": "Automatically Check Connection IP",
"actionAtClosing": "Action at closing",
"actionsAtClosing": {
"askEachTime": "Ask each time",
"hide": "Hide",
"exit": "Exit"
}
},
"advanced": {
"sectionTitle": "Advanced",
Expand Down Expand Up @@ -425,6 +431,7 @@
"window": {
"hide": "Hide",
"close": "Exit",
"alertMessage": "Hide or Exit the application?"
"alertMessage": "Hide or Exit the application?",
"remember": "Remember my choice"
}
}
}
13 changes: 10 additions & 3 deletions assets/translations/strings_ru.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@
"ignoreBatteryOptimizationsMsg": "Отключение ограничений для оптимальной производительности VPN",
"dynamicNotification": "Отображение скорости в уведомлении",
"hapticFeedback": "Тактильная обратная связь",
"autoIpCheck": "Автоматически проверять IP-адрес соединения"
"autoIpCheck": "Автоматически проверять IP-адрес соединения",
"actionAtClosing": "Действие при закрытии",
"actionsAtClosing": {
"askEachTime": "Каждый раз спрашивать",
"hide": "Скрыть",
"exit": "Выйти"
}
},
"advanced": {
"sectionTitle": "Расширенные",
Expand Down Expand Up @@ -425,6 +431,7 @@
"window": {
"hide": "Скрыть",
"close": "Закрыть",
"alertMessage": "Скрыть или выйти из приложения?"
"alertMessage": "Скрыть приложение или выйти?",
"remember": "Запомнить выбор"
}
}
}
13 changes: 13 additions & 0 deletions lib/core/preferences/actions_at_closing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:hiddify/gen/translations.g.dart';

enum ActionsAtClosing {
ask,
hide,
exit;

String present(TranslationsEn t) => switch (this) {
ask => t.settings.general.actionsAtClosing.askEachTime,
hide => t.settings.general.actionsAtClosing.hide,
exit => t.settings.general.actionsAtClosing.exit,
};
}
8 changes: 8 additions & 0 deletions lib/core/preferences/general_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:hiddify/core/app_info/app_info_provider.dart';
import 'package:hiddify/core/model/environment.dart';
import 'package:hiddify/core/preferences/actions_at_closing.dart';
// import 'package:hiddify/core/model/region.dart';
import 'package:hiddify/core/preferences/preferences_provider.dart';
import 'package:hiddify/core/utils/preferences_utils.dart';
Expand Down Expand Up @@ -61,6 +62,13 @@ abstract class Preferences {
"store_reviewed_by_user",
false,
);

static final actionAtClose = PreferencesNotifier.create<ActionsAtClosing, String>(
"action_at_close",
ActionsAtClosing.ask,
mapFrom: ActionsAtClosing.values.byName,
mapTo: (value) => value.name,
);
}

@Riverpod(keepAlive: true)
Expand Down
90 changes: 86 additions & 4 deletions lib/features/common/general_pref_tiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import 'package:hiddify/core/localization/locale_extensions.dart';
import 'package:hiddify/core/localization/locale_preferences.dart';
import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/model/region.dart';
import 'package:hiddify/core/preferences/actions_at_closing.dart';
import 'package:hiddify/core/preferences/general_preferences.dart';
import 'package:hiddify/core/theme/app_theme_mode.dart';
import 'package:hiddify/core/theme/theme_preferences.dart';
import 'package:hiddify/features/config_option/data/config_option_repository.dart';
import 'package:hiddify/features/config_option/notifier/config_option_notifier.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class LocalePrefTile extends HookConsumerWidget {
class LocalePrefTile extends ConsumerWidget {
const LocalePrefTile({super.key});

@override
Expand Down Expand Up @@ -50,7 +52,7 @@ class LocalePrefTile extends HookConsumerWidget {
}
}

class RegionPrefTile extends HookConsumerWidget {
class RegionPrefTile extends ConsumerWidget {
const RegionPrefTile({super.key});

@override
Expand Down Expand Up @@ -102,7 +104,7 @@ class RegionPrefTile extends HookConsumerWidget {
}
}

class EnableAnalyticsPrefTile extends HookConsumerWidget {
class EnableAnalyticsPrefTile extends ConsumerWidget {
const EnableAnalyticsPrefTile({
super.key,
this.onChanged,
Expand Down Expand Up @@ -137,3 +139,83 @@ class EnableAnalyticsPrefTile extends HookConsumerWidget {
);
}
}

class ThemeModePrefTile extends ConsumerWidget {
const ThemeModePrefTile({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);

final themeMode = ref.watch(themePreferencesProvider);

return ListTile(
title: Text(t.settings.general.themeMode),
subtitle: Text(themeMode.present(t)),
leading: const Icon(FluentIcons.weather_moon_20_regular),
onTap: () async {
final selectedThemeMode = await showDialog<AppThemeMode>(
context: context,
builder: (context) {
return SimpleDialog(
title: Text(t.settings.general.themeMode),
children: AppThemeMode.values
.map(
(e) => RadioListTile(
title: Text(e.present(t)),
value: e,
groupValue: themeMode,
onChanged: Navigator.of(context).maybePop,
),
)
.toList(),
);
},
);
if (selectedThemeMode != null) {
await ref.read(themePreferencesProvider.notifier).changeThemeMode(selectedThemeMode);
}
},
);
}
}

class ClosingPrefTile extends ConsumerWidget {
const ClosingPrefTile({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);

final action = ref.watch(Preferences.actionAtClose);

return ListTile(
title: Text(t.settings.general.actionAtClosing),
subtitle: Text(action.present(t)),
leading: const Icon(FluentIcons.arrow_exit_20_regular),
onTap: () async {
final selectedAction = await showDialog<ActionsAtClosing>(
context: context,
builder: (context) {
return SimpleDialog(
title: Text(t.settings.general.actionAtClosing),
children: ActionsAtClosing.values
.map(
(e) => RadioListTile(
title: Text(e.present(t)),
value: e,
groupValue: action,
onChanged: Navigator.of(context).maybePop,
),
)
.toList(),
);
},
);
if (selectedAction != null) {
await ref.read(Preferences.actionAtClose.notifier).update(selectedAction);
}
},
);
}
}
34 changes: 2 additions & 32 deletions lib/features/settings/widgets/general_setting_tiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:flutter/material.dart';
import 'package:hiddify/core/haptic/haptic_service.dart';
import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/preferences/general_preferences.dart';
import 'package:hiddify/core/theme/app_theme_mode.dart';
import 'package:hiddify/core/theme/theme_preferences.dart';
import 'package:hiddify/features/auto_start/notifier/auto_start_notifier.dart';
import 'package:hiddify/features/common/general_pref_tiles.dart';
import 'package:hiddify/utils/utils.dart';
Expand All @@ -19,39 +17,10 @@ class GeneralSettingTiles extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);

final themeMode = ref.watch(themePreferencesProvider);

return Column(
children: [
const LocalePrefTile(),
ListTile(
title: Text(t.settings.general.themeMode),
subtitle: Text(themeMode.present(t)),
leading: const Icon(FluentIcons.weather_moon_20_regular),
onTap: () async {
final selectedThemeMode = await showDialog<AppThemeMode>(
context: context,
builder: (context) {
return SimpleDialog(
title: Text(t.settings.general.themeMode),
children: AppThemeMode.values
.map(
(e) => RadioListTile(
title: Text(e.present(t)),
value: e,
groupValue: themeMode,
onChanged: Navigator.of(context).maybePop,
),
)
.toList(),
);
},
);
if (selectedThemeMode != null) {
await ref.read(themePreferencesProvider.notifier).changeThemeMode(selectedThemeMode);
}
},
),
const ThemeModePrefTile(),
const EnableAnalyticsPrefTile(),
SwitchListTile(
title: Text(t.settings.general.autoIpCheck),
Expand All @@ -76,6 +45,7 @@ class GeneralSettingTiles extends HookConsumerWidget {
),
],
if (PlatformUtils.isDesktop) ...[
const ClosingPrefTile(),
SwitchListTile(
title: Text(t.settings.general.autoStart),
value: ref.watch(autoStartNotifierProvider).asData!.value,
Expand Down
69 changes: 69 additions & 0 deletions lib/features/window/widget/window_closing_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'package:flutter/material.dart';
import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/preferences/actions_at_closing.dart';
import 'package:hiddify/core/preferences/general_preferences.dart';
import 'package:hiddify/features/window/notifier/window_notifier.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class WindowClosingDialog extends ConsumerStatefulWidget {
const WindowClosingDialog({super.key});

@override
ConsumerState<WindowClosingDialog> createState() => _WindowClosingDialogState();
}

class _WindowClosingDialogState extends ConsumerState<WindowClosingDialog> {
bool remember = false;

@override
Widget build(BuildContext context) {
final t = ref.watch(translationsProvider);

return AlertDialog(
title: Text(t.window.alertMessage),
content: GestureDetector(
onTap: () => setState(() {
remember = !remember;
}),
behavior: HitTestBehavior.translucent,
child: Row(
children: [
Checkbox(
value: remember,
onChanged: (v) {
remember = v ?? remember;
setState(() {});
},
),
const SizedBox(width: 16),
Text(
t.window.remember,
style: const TextStyle(fontSize: 16),
),
],
),
),
actions: [
TextButton(
onPressed: () {
if (remember) {
ref.read(Preferences.actionAtClose.notifier).update(ActionsAtClosing.exit);
}
ref.read(windowNotifierProvider.notifier).quit();
},
child: Text(t.window.close),
),
FilledButton(
onPressed: () async {
if (remember) {
ref.read(Preferences.actionAtClose.notifier).update(ActionsAtClosing.hide);
}
Navigator.of(context).maybePop(false);
await ref.read(windowNotifierProvider.notifier).close();
},
child: Text(t.window.hide),
),
],
);
}
}
Loading

0 comments on commit e31dc29

Please sign in to comment.