refactor: reorganize settings sections for clarity and improve restore dialog functionality

This commit is contained in:
Dr-Blank 2024-10-05 09:49:56 -04:00
parent 8806185f32
commit 51bfe357c3
No known key found for this signature in database
GPG key ID: 7452CC63F210A266

View file

@ -27,30 +27,6 @@ class AppSettingsPage extends HookConsumerWidget {
return SimpleSettingsPage( return SimpleSettingsPage(
title: const Text('App Settings'), title: const Text('App Settings'),
sections: [ sections: [
// Appearance section
SettingsSection(
margin: const EdgeInsetsDirectional.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
title: Text(
'Appearance',
style: Theme.of(context).textTheme.titleLarge,
),
tiles: [
SettingsTile.navigation(
leading: const Icon(Icons.color_lens),
title: const Text('Theme Settings'),
description: const Text(
'Customize the app theme',
),
onPressed: (context) {
context.pushNamed(Routes.themeSettings.name);
},
),
],
),
// General section // General section
SettingsSection( SettingsSection(
margin: const EdgeInsetsDirectional.symmetric( margin: const EdgeInsetsDirectional.symmetric(
@ -62,6 +38,16 @@ class AppSettingsPage extends HookConsumerWidget {
style: Theme.of(context).textTheme.titleLarge, style: Theme.of(context).textTheme.titleLarge,
), ),
tiles: [ tiles: [
SettingsTile(
title: const Text('Player Settings'),
leading: const Icon(Icons.play_arrow),
description: const Text(
'Customize the player settings',
),
onPressed: (context) {
context.pushNamed(Routes.playerSettings.name);
},
),
NavigationWithSwitchTile( NavigationWithSwitchTile(
title: const Text('Auto Turn On Sleep Timer'), title: const Text('Auto Turn On Sleep Timer'),
description: const Text( description: const Text(
@ -82,26 +68,6 @@ class AppSettingsPage extends HookConsumerWidget {
); );
}, },
), ),
SettingsTile(
title: const Text('Notification Media Player'),
leading: const Icon(Icons.play_lesson),
description: const Text(
'Customize the media player in notifications',
),
onPressed: (context) {
context.pushNamed(Routes.notificationSettings.name);
},
),
SettingsTile(
title: const Text('Player Settings'),
leading: const Icon(Icons.play_arrow),
description: const Text(
'Customize the player settings',
),
onPressed: (context) {
context.pushNamed(Routes.playerSettings.name);
},
),
NavigationWithSwitchTile( NavigationWithSwitchTile(
title: const Text('Shake Detector'), title: const Text('Shake Detector'),
leading: const Icon(Icons.vibration), leading: const Icon(Icons.vibration),
@ -122,6 +88,41 @@ class AppSettingsPage extends HookConsumerWidget {
), ),
], ],
), ),
// Appearance section
SettingsSection(
margin: const EdgeInsetsDirectional.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
title: Text(
'Appearance',
style: Theme.of(context).textTheme.titleLarge,
),
tiles: [
SettingsTile.navigation(
leading: const Icon(Icons.color_lens),
title: const Text('Theme Settings'),
description: const Text(
'Customize the app theme',
),
onPressed: (context) {
context.pushNamed(Routes.themeSettings.name);
},
),
SettingsTile(
title: const Text('Notification Media Player'),
leading: const Icon(Icons.play_lesson),
description: const Text(
'Customize the media player in notifications',
),
onPressed: (context) {
context.pushNamed(Routes.notificationSettings.name);
},
),
],
),
// Backup and Restore section // Backup and Restore section
SettingsSection( SettingsSection(
margin: const EdgeInsetsDirectional.symmetric( margin: const EdgeInsetsDirectional.symmetric(
@ -226,7 +227,8 @@ class RestoreDialogue extends HookConsumerWidget {
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final formKey = GlobalKey<FormState>(); final formKey = useMemoized(() => GlobalKey<FormState>());
final settings = useState<model.AppSettings?>(null);
final settingsInputController = useTextEditingController(); final settingsInputController = useTextEditingController();
return AlertDialog( return AlertDialog(
@ -234,12 +236,12 @@ class RestoreDialogue extends HookConsumerWidget {
content: Form( content: Form(
key: formKey, key: formKey,
child: TextFormField( child: TextFormField(
controller: settingsInputController, autofocus: true,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Backup', labelText: 'Backup',
hintText: 'Paste the backup here', hintText: 'Paste the backup here',
// clear button // clear button
suffix: IconButton( suffixIcon: IconButton(
icon: Icon(Icons.clear), icon: Icon(Icons.clear),
onPressed: () { onPressed: () {
settingsInputController.clear(); settingsInputController.clear();
@ -252,7 +254,7 @@ class RestoreDialogue extends HookConsumerWidget {
} }
try { try {
// try to decode the backup // try to decode the backup
model.AppSettings.fromJson( settings.value = model.AppSettings.fromJson(
jsonDecode(value), jsonDecode(value),
); );
} catch (e) { } catch (e) {
@ -263,15 +265,19 @@ class RestoreDialogue extends HookConsumerWidget {
), ),
), ),
actions: [ actions: [
CancelButton(),
TextButton( TextButton(
onPressed: () { onPressed: () {
if (formKey.currentState!.validate()) { if (formKey.currentState!.validate()) {
final backup = settingsInputController.text; if (settings.value == null) {
final newSettings = model.AppSettings.fromJson( ScaffoldMessenger.of(context).showSnackBar(
// decode the backup as json const SnackBar(
jsonDecode(backup), content: Text('Invalid backup'),
); ),
ref.read(appSettingsProvider.notifier).update(newSettings); );
return;
}
ref.read(appSettingsProvider.notifier).update(settings.value!);
settingsInputController.clear(); settingsInputController.clear();
Navigator.of(context).pop(); Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
@ -289,7 +295,6 @@ class RestoreDialogue extends HookConsumerWidget {
}, },
child: const Text('Restore'), child: const Text('Restore'),
), ),
CancelButton(),
], ],
); );
} }