Vaani/lib/features/settings/view/app_settings_page.dart

339 lines
12 KiB
Dart
Raw Normal View History

2024-08-20 08:36:39 -04:00
import 'dart:convert';
2024-05-08 05:03:49 -04:00
import 'package:flutter/material.dart';
2024-08-20 08:36:39 -04:00
import 'package:flutter/services.dart';
2024-05-08 05:03:49 -04:00
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:go_router/go_router.dart';
2024-05-08 05:03:49 -04:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:vaani/features/settings/app_settings_provider.dart';
import 'package:vaani/features/settings/models/app_settings.dart' as model;
import 'package:vaani/features/settings/view/buttons.dart';
import 'package:vaani/features/settings/view/simple_settings_page.dart';
import 'package:vaani/features/settings/view/widgets/navigation_with_switch_tile.dart';
2026-01-04 17:49:05 +08:00
import 'package:vaani/generated/l10n.dart';
import 'package:vaani/router/router.dart';
import 'package:vaani/shared/widgets/custom_dropdown.dart';
2024-05-08 05:03:49 -04:00
class AppSettingsPage extends HookConsumerWidget {
const AppSettingsPage({
super.key,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final appSettings = ref.watch(appSettingsProvider);
final sleepTimerSettings = appSettings.sleepTimerSettings;
2025-10-22 17:58:29 +08:00
final locales = {'en': 'English', 'zh': '中文'};
return SimpleSettingsPage(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).appSettings),
sections: [
// General section
SettingsSection(
margin: const EdgeInsetsDirectional.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
title: Text(
2025-10-22 17:58:29 +08:00
S.of(context).general,
style: Theme.of(context).textTheme.titleLarge,
),
tiles: [
SettingsTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).language),
2025-12-29 17:56:03 +08:00
leading: const Icon(Icons.language),
2026-01-04 17:49:05 +08:00
trailing: CustomDropdown<String>(
selected: appSettings.language,
items: (f, cs) => S.delegate.supportedLocales.map((locale) {
return locale.languageCode;
2025-10-22 17:58:29 +08:00
}).toList(),
2026-01-04 17:49:05 +08:00
itemAsString: (item) => locales[item] ?? 'unknown',
onChanged: (value) async =>
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith(
language: value!,
),
2025-10-22 17:58:29 +08:00
),
),
2026-01-04 17:49:05 +08:00
// trailing: DropdownButton(
// value: appSettings.language,
// items: S.delegate.supportedLocales.map((locale) {
// return DropdownMenuItem(
// value: locale.languageCode,
// child: Text(locales[locale.languageCode] ?? 'unknown'),
// );
// }).toList(),
// onChanged: (value) {
// ref.read(appSettingsProvider.notifier).update(
// appSettings.copyWith(
// language: value!,
// ),
// );
// },
// ),
2025-10-22 17:58:29 +08:00
description: Text(S.of(context).languageDescription),
),
SettingsTile(
title: Text(S.of(context).playerSettings),
leading: const Icon(Icons.play_arrow),
description: Text(S.of(context).playerSettingsDescription),
onPressed: (context) {
context.pushNamed(Routes.playerSettings.name);
},
),
2025-12-29 17:56:03 +08:00
SettingsTile(
2026-01-04 17:49:05 +08:00
title: Text(S.of(context).downloadSettings),
2025-12-29 17:56:03 +08:00
leading: const Icon(Icons.download),
2026-01-04 17:49:05 +08:00
description: Text(S.of(context).downloadSettingsDescription),
2025-12-29 17:56:03 +08:00
onPressed: (context) {
context.pushNamed(Routes.downloadSettings.name);
},
),
NavigationWithSwitchTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).autoTurnOnSleepTimer),
description: Text(S.of(context).automaticallyDescription),
leading: sleepTimerSettings.autoTurnOnTimer
2025-11-13 17:53:23 +08:00
? const Icon(Icons.timer, fill: 1)
: const Icon(Icons.timer_off, fill: 1),
onPressed: (context) {
context.pushNamed(Routes.autoSleepTimerSettings.name);
},
value: sleepTimerSettings.autoTurnOnTimer,
onToggle: (value) {
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.sleepTimerSettings(
autoTurnOnTimer: value,
),
);
},
),
NavigationWithSwitchTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).shakeDetector),
leading: const Icon(Icons.vibration),
2025-10-22 17:58:29 +08:00
description: Text(
S.of(context).shakeDetectorDescription,
),
value: appSettings.shakeDetectionSettings.isEnabled,
onPressed: (context) {
context.pushNamed(Routes.shakeDetectorSettings.name);
},
onToggle: (value) {
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.shakeDetectionSettings(
isEnabled: value,
),
);
},
2024-08-20 08:36:39 -04:00
),
],
),
// Appearance section
SettingsSection(
margin: const EdgeInsetsDirectional.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
title: Text(
2025-10-22 17:58:29 +08:00
S.of(context).appearance,
style: Theme.of(context).textTheme.titleLarge,
),
tiles: [
SettingsTile.navigation(
leading: const Icon(Icons.color_lens),
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).themeSettings),
description: Text(S.of(context).themeSettingsDescription),
onPressed: (context) {
context.pushNamed(Routes.themeSettings.name);
},
),
SettingsTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).notificationMediaPlayer),
leading: const Icon(Icons.play_lesson),
2025-10-25 10:38:56 +08:00
description:
Text(S.of(context).notificationMediaPlayerDescription),
onPressed: (context) {
context.pushNamed(Routes.notificationSettings.name);
},
),
SettingsTile.navigation(
leading: const Icon(Icons.home_filled),
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).homePageSettings),
description: Text(S.of(context).homePageSettingsDescription),
onPressed: (context) {
context.pushNamed(Routes.homePageSettings.name);
},
),
],
),
// Backup and Restore section
SettingsSection(
margin: const EdgeInsetsDirectional.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
title: Text(
2025-10-22 17:58:29 +08:00
S.of(context).backupAndRestore,
style: Theme.of(context).textTheme.titleLarge,
),
tiles: [
SettingsTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).copyToClipboard),
leading: const Icon(Icons.copy),
2025-10-22 17:58:29 +08:00
description: Text(
S.of(context).copyToClipboardDescription,
),
onPressed: (context) async {
// copy to clipboard
await Clipboard.setData(
ClipboardData(
text: jsonEncode(appSettings.toJson()),
),
);
// show toast
ScaffoldMessenger.of(context).showSnackBar(
2025-10-22 17:58:29 +08:00
SnackBar(
content: Text(S.of(context).copyToClipboardToast),
),
);
},
2024-08-20 08:36:39 -04:00
),
SettingsTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).restore),
leading: const Icon(Icons.restore),
2025-10-22 17:58:29 +08:00
description: Text(S.of(context).restoreDescription),
onPressed: (context) {
// show a dialog to get the backup
showDialog(
context: context,
builder: (context) {
return RestoreDialogue();
},
);
},
),
2024-08-20 08:36:39 -04:00
// a button to reset the app settings
SettingsTile(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).resetAppSettings),
leading: const Icon(Icons.settings_backup_restore),
2025-10-22 17:58:29 +08:00
description: Text(S.of(context).resetAppSettingsDescription),
onPressed: (context) async {
// confirm the reset
final res = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).resetAppSettings),
content: Text(S.of(context).resetAppSettingsDialog),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
2025-10-22 17:58:29 +08:00
child: Text(S.of(context).cancel),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
2025-10-22 17:58:29 +08:00
child: Text(S.of(context).reset),
),
],
);
},
);
// if the user confirms the reset
if (res == true) {
ref.read(appSettingsProvider.notifier).reset();
}
},
),
],
),
],
2024-05-08 05:03:49 -04:00
);
}
}
class RestoreDialogue extends HookConsumerWidget {
const RestoreDialogue({
super.key,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final formKey = useMemoized(() => GlobalKey<FormState>());
final settings = useState<model.AppSettings?>(null);
final settingsInputController = useTextEditingController();
return AlertDialog(
2025-10-22 17:58:29 +08:00
title: Text(S.of(context).restoreBackup),
content: Form(
key: formKey,
child: TextFormField(
autofocus: true,
decoration: InputDecoration(
2025-10-22 17:58:29 +08:00
labelText: S.of(context).backup,
hintText: S.of(context).restoreBackupHint,
// clear button
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () {
settingsInputController.clear();
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
2025-10-22 17:58:29 +08:00
return S.of(context).restoreBackupValidator;
}
try {
// try to decode the backup
settings.value = model.AppSettings.fromJson(
jsonDecode(value),
);
} catch (e) {
2025-10-22 17:58:29 +08:00
return S.of(context).restoreBackupInvalid;
}
return null;
},
),
),
actions: [
CancelButton(),
TextButton(
onPressed: () {
if (formKey.currentState!.validate()) {
if (settings.value == null) {
ScaffoldMessenger.of(context).showSnackBar(
2025-10-22 17:58:29 +08:00
SnackBar(
content: Text(S.of(context).restoreBackupInvalid),
),
);
return;
}
ref.read(appSettingsProvider.notifier).update(settings.value!);
settingsInputController.clear();
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
2025-10-22 17:58:29 +08:00
SnackBar(
content: Text(S.of(context).restoreBackupSuccess),
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
2025-10-22 17:58:29 +08:00
SnackBar(
content: Text(S.of(context).restoreBackupInvalid),
),
);
}
},
2025-10-22 17:58:29 +08:00
child: Text(S.of(context).restore),
),
],
);
}
}