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';
|
2024-06-13 18:10:10 -04:00
|
|
|
import 'package:go_router/go_router.dart';
|
2024-05-08 05:03:49 -04:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-10-02 09:18:06 -04:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2024-08-23 04:21:46 -04:00
|
|
|
import 'package:vaani/router/router.dart';
|
|
|
|
|
import 'package:vaani/settings/app_settings_provider.dart';
|
|
|
|
|
import 'package:vaani/settings/models/app_settings.dart' as model;
|
2024-10-05 10:01:08 -04:00
|
|
|
import 'package:vaani/settings/view/buttons.dart';
|
2024-09-25 03:13:42 -04:00
|
|
|
import 'package:vaani/settings/view/simple_settings_page.dart';
|
2024-09-28 01:27:56 -04:00
|
|
|
import 'package:vaani/settings/view/widgets/navigation_with_switch_tile.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);
|
2024-10-02 09:18:06 -04:00
|
|
|
final sleepTimerSettings = appSettings.sleepTimerSettings;
|
2024-05-08 05:03:49 -04:00
|
|
|
|
2024-09-25 03:13:42 -04:00
|
|
|
return SimpleSettingsPage(
|
|
|
|
|
title: const Text('App Settings'),
|
|
|
|
|
sections: [
|
2024-10-05 10:01:08 -04:00
|
|
|
// General section
|
2024-09-25 03:13:42 -04:00
|
|
|
SettingsSection(
|
|
|
|
|
margin: const EdgeInsetsDirectional.symmetric(
|
|
|
|
|
horizontal: 16.0,
|
|
|
|
|
vertical: 8.0,
|
|
|
|
|
),
|
|
|
|
|
title: Text(
|
2024-10-05 10:01:08 -04:00
|
|
|
'General',
|
2024-09-25 03:13:42 -04:00
|
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
|
|
|
),
|
|
|
|
|
tiles: [
|
2024-10-05 10:01:08 -04:00
|
|
|
SettingsTile(
|
|
|
|
|
title: const Text('Player Settings'),
|
|
|
|
|
leading: const Icon(Icons.play_arrow),
|
2024-09-25 03:13:42 -04:00
|
|
|
description: const Text(
|
2024-10-05 10:01:08 -04:00
|
|
|
'Customize the player settings',
|
2024-09-25 03:13:42 -04:00
|
|
|
),
|
2024-10-05 10:01:08 -04:00
|
|
|
onPressed: (context) {
|
|
|
|
|
context.pushNamed(Routes.playerSettings.name);
|
2024-09-25 03:13:42 -04:00
|
|
|
},
|
|
|
|
|
),
|
2024-09-28 01:27:56 -04:00
|
|
|
NavigationWithSwitchTile(
|
|
|
|
|
title: const Text('Auto Turn On Sleep Timer'),
|
2024-09-25 03:13:42 -04:00
|
|
|
description: const Text(
|
|
|
|
|
'Automatically turn on the sleep timer based on the time of day',
|
|
|
|
|
),
|
|
|
|
|
leading: sleepTimerSettings.autoTurnOnTimer
|
2024-10-02 09:18:06 -04:00
|
|
|
? const Icon(Symbols.time_auto, fill: 1)
|
|
|
|
|
: const Icon(Symbols.timer_off, fill: 1),
|
2024-09-25 03:13:42 -04:00
|
|
|
onPressed: (context) {
|
|
|
|
|
context.pushNamed(Routes.autoSleepTimerSettings.name);
|
|
|
|
|
},
|
2024-09-28 01:27:56 -04:00
|
|
|
value: sleepTimerSettings.autoTurnOnTimer,
|
|
|
|
|
onToggle: (value) {
|
|
|
|
|
ref.read(appSettingsProvider.notifier).update(
|
2024-10-02 09:18:06 -04:00
|
|
|
appSettings.copyWith.sleepTimerSettings(
|
2024-09-28 01:27:56 -04:00
|
|
|
autoTurnOnTimer: value,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
NavigationWithSwitchTile(
|
|
|
|
|
title: const Text('Shake Detector'),
|
|
|
|
|
leading: const Icon(Icons.vibration),
|
|
|
|
|
description: const Text(
|
|
|
|
|
'Customize the shake detector settings',
|
|
|
|
|
),
|
|
|
|
|
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
|
|
|
),
|
2024-09-25 03:13:42 -04:00
|
|
|
],
|
|
|
|
|
),
|
2024-10-05 10:01:08 -04:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
},
|
|
|
|
|
),
|
2025-05-22 08:24:59 +05:30
|
|
|
SettingsTile.navigation(
|
|
|
|
|
leading: const Icon(Icons.home_filled),
|
|
|
|
|
title: const Text('Home Page Settings'),
|
|
|
|
|
description: const Text(
|
|
|
|
|
'Customize the home page',
|
|
|
|
|
),
|
|
|
|
|
onPressed: (context) {
|
|
|
|
|
context.pushNamed(Routes.homePageSettings.name);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-10-05 10:01:08 -04:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
|
2024-09-25 03:13:42 -04:00
|
|
|
// Backup and Restore section
|
|
|
|
|
SettingsSection(
|
|
|
|
|
margin: const EdgeInsetsDirectional.symmetric(
|
|
|
|
|
horizontal: 16.0,
|
|
|
|
|
vertical: 8.0,
|
|
|
|
|
),
|
|
|
|
|
title: Text(
|
|
|
|
|
'Backup and Restore',
|
|
|
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
|
|
|
),
|
|
|
|
|
tiles: [
|
|
|
|
|
SettingsTile(
|
|
|
|
|
title: const Text('Copy to Clipboard'),
|
|
|
|
|
leading: const Icon(Icons.copy),
|
|
|
|
|
description: const Text(
|
|
|
|
|
'Copy the app settings to the clipboard',
|
|
|
|
|
),
|
|
|
|
|
onPressed: (context) async {
|
|
|
|
|
// copy to clipboard
|
|
|
|
|
await Clipboard.setData(
|
|
|
|
|
ClipboardData(
|
|
|
|
|
text: jsonEncode(appSettings.toJson()),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
// show toast
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
content: Text('Settings copied to clipboard'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-08-20 08:36:39 -04:00
|
|
|
),
|
2024-09-25 03:13:42 -04:00
|
|
|
SettingsTile(
|
|
|
|
|
title: const Text('Restore'),
|
|
|
|
|
leading: const Icon(Icons.restore),
|
|
|
|
|
description: const Text(
|
|
|
|
|
'Restore the app settings from the backup',
|
2024-08-20 08:36:39 -04:00
|
|
|
),
|
2024-09-25 03:13:42 -04:00
|
|
|
onPressed: (context) {
|
|
|
|
|
// show a dialog to get the backup
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) {
|
2024-10-05 10:01:08 -04:00
|
|
|
return RestoreDialogue();
|
2024-09-25 03:13:42 -04:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-08-20 08:36:39 -04:00
|
|
|
|
2024-09-25 03:13:42 -04:00
|
|
|
// a button to reset the app settings
|
|
|
|
|
SettingsTile(
|
|
|
|
|
title: const Text('Reset App Settings'),
|
|
|
|
|
leading: const Icon(Icons.settings_backup_restore),
|
|
|
|
|
description: const Text(
|
|
|
|
|
'Reset the app settings to the default values',
|
2024-08-20 08:36:39 -04:00
|
|
|
),
|
2024-09-25 03:13:42 -04:00
|
|
|
onPressed: (context) async {
|
|
|
|
|
// confirm the reset
|
|
|
|
|
final res = await showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: const Text('Reset App Settings'),
|
|
|
|
|
content: const Text(
|
|
|
|
|
'Are you sure you want to reset the app settings?',
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(context).pop(false);
|
|
|
|
|
},
|
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
|
},
|
|
|
|
|
child: const Text('Reset'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// if the user confirms the reset
|
|
|
|
|
if (res == true) {
|
|
|
|
|
ref.read(appSettingsProvider.notifier).reset();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-05-08 05:03:49 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-05 10:01:08 -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(
|
|
|
|
|
title: const Text('Restore Backup'),
|
|
|
|
|
content: Form(
|
|
|
|
|
key: formKey,
|
|
|
|
|
child: TextFormField(
|
|
|
|
|
autofocus: true,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: 'Backup',
|
|
|
|
|
hintText: 'Paste the backup here',
|
|
|
|
|
// clear button
|
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
|
icon: Icon(Icons.clear),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
settingsInputController.clear();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return 'Please paste the backup here';
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// try to decode the backup
|
|
|
|
|
settings.value = model.AppSettings.fromJson(
|
|
|
|
|
jsonDecode(value),
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return 'Invalid backup';
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
CancelButton(),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
if (formKey.currentState!.validate()) {
|
|
|
|
|
if (settings.value == null) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
content: Text('Invalid backup'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ref.read(appSettingsProvider.notifier).update(settings.value!);
|
|
|
|
|
settingsInputController.clear();
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
content: Text('Settings restored'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
content: Text('Invalid backup'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
child: const Text('Restore'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|