Jisho-Study-Tool/lib/view/screens/settings.dart

168 lines
5.1 KiB
Dart
Raw Normal View History

2021-08-03 22:04:29 +02:00
import 'package:confirm_dialog/confirm_dialog.dart';
import 'package:flutter/material.dart';
import 'package:settings_ui/settings_ui.dart';
2021-08-08 23:16:54 +02:00
import 'package:shared_preferences/shared_preferences.dart';
2021-12-01 23:09:53 +01:00
import '../../bloc/database/database_bloc.dart';
import '../../bloc/theme/theme_bloc.dart';
import '../../models/history/search.dart';
import '../../models/themes/theme.dart';
import '../../objectbox.g.dart';
2021-08-08 23:16:54 +02:00
class SettingsView extends StatefulWidget {
2021-12-01 23:09:53 +01:00
const SettingsView({Key? key}) : super(key: key);
2021-08-08 23:16:54 +02:00
@override
_SettingsViewState createState() => _SettingsViewState();
}
class _SettingsViewState extends State<SettingsView> {
late final SharedPreferences prefs;
bool darkThemeEnabled = false;
bool autoThemeEnabled = false;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs) {
this.prefs = prefs;
_getPrefs();
});
}
/// Get stored preferences and set setting page state accordingly
void _getPrefs() {
setState(() {
darkThemeEnabled = prefs.getBool('darkThemeEnabled') ?? darkThemeEnabled;
autoThemeEnabled = prefs.getBool('autoThemeEnabled') ?? autoThemeEnabled;
});
}
/// Update stored preferences with values from setting page state
2021-12-01 23:09:53 +01:00
void _updatePrefs() {
prefs.setBool('darkThemeEnabled', darkThemeEnabled);
prefs.setBool('autoThemeEnabled', autoThemeEnabled);
2021-08-08 23:16:54 +02:00
}
2021-08-03 22:04:29 +02:00
2021-12-01 23:09:53 +01:00
@override
2021-08-03 22:04:29 +02:00
Widget build(BuildContext context) {
2021-08-08 23:16:54 +02:00
final TextStyle _titleTextStyle = TextStyle(
2021-12-01 23:09:53 +01:00
color: BlocProvider.of<ThemeBloc>(context).state is DarkThemeState
? AppTheme.jishoGreen.background
: null,
);
2021-08-08 23:16:54 +02:00
2021-08-03 22:04:29 +02:00
return SettingsList(
2021-08-08 23:16:54 +02:00
backgroundColor: Colors.transparent,
2021-12-01 23:09:53 +01:00
contentPadding: const EdgeInsets.symmetric(vertical: 10),
sections: <SettingsSection>[
2021-08-08 23:16:54 +02:00
SettingsSection(
title: 'Theme',
titleTextStyle: _titleTextStyle,
2021-12-01 23:09:53 +01:00
tiles: <SettingsTile>[
2021-08-08 23:16:54 +02:00
SettingsTile.switchTile(
title: 'Automatically determine theme',
onToggle: (b) {
setState(() {
autoThemeEnabled = b;
});
_updatePrefs();
},
switchValue: autoThemeEnabled,
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
),
SettingsTile.switchTile(
title: 'Dark Theme',
onToggle: (b) {
2021-12-01 23:09:53 +01:00
BlocProvider.of<ThemeBloc>(context)
.add(SetTheme(themeIsDark: b));
2021-08-08 23:16:54 +02:00
setState(() {
darkThemeEnabled = b;
});
_updatePrefs();
},
switchValue: darkThemeEnabled,
enabled: !autoThemeEnabled,
switchActiveColor: AppTheme.jishoGreen.background,
),
],
),
2021-08-03 22:04:29 +02:00
SettingsSection(
title: 'Cache',
2021-08-08 23:16:54 +02:00
titleTextStyle: _titleTextStyle,
2021-12-01 23:09:53 +01:00
tiles: <SettingsTile>[
2021-08-03 22:04:29 +02:00
SettingsTile.switchTile(
2021-08-08 23:16:54 +02:00
title: 'Cache grade 1-7 kanji',
2021-08-03 22:04:29 +02:00
switchValue: false,
onToggle: (v) {},
2021-08-08 23:16:54 +02:00
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
2021-08-03 22:04:29 +02:00
),
SettingsTile.switchTile(
2021-08-08 23:16:54 +02:00
title: 'Cache grade standard kanji',
2021-08-03 22:04:29 +02:00
switchValue: false,
onToggle: (v) {},
2021-08-08 23:16:54 +02:00
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
2021-08-03 22:04:29 +02:00
),
SettingsTile.switchTile(
2021-08-08 23:16:54 +02:00
title: 'Cache all favourites',
2021-08-03 22:04:29 +02:00
switchValue: false,
onToggle: (v) {},
2021-08-08 23:16:54 +02:00
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
2021-08-03 22:04:29 +02:00
),
SettingsTile.switchTile(
2021-08-08 23:16:54 +02:00
title: 'Cache all searches',
2021-08-03 22:04:29 +02:00
switchValue: false,
onToggle: (v) {},
2021-08-08 23:16:54 +02:00
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
2021-08-03 22:04:29 +02:00
),
],
),
SettingsSection(
title: 'Data',
2021-08-08 23:16:54 +02:00
titleTextStyle: _titleTextStyle,
2021-12-01 23:09:53 +01:00
tiles: <SettingsTile>[
const SettingsTile(
2021-08-03 22:04:29 +02:00
leading: Icon(Icons.file_download),
2021-08-08 23:16:54 +02:00
title: 'Export Data',
enabled: false,
2021-08-03 22:04:29 +02:00
),
2021-12-01 23:09:53 +01:00
const SettingsTile(
2021-08-03 22:04:29 +02:00
leading: Icon(Icons.delete),
2021-08-08 23:16:54 +02:00
title: 'Clear History',
2021-08-03 22:04:29 +02:00
onPressed: _clearHistory,
titleTextStyle: TextStyle(color: Colors.red),
2021-08-08 23:16:54 +02:00
enabled: false,
2021-08-03 22:04:29 +02:00
),
SettingsTile(
2021-12-01 23:09:53 +01:00
leading: const Icon(Icons.delete),
2021-08-08 23:16:54 +02:00
title: 'Clear Favourites',
2021-08-03 22:04:29 +02:00
onPressed: (c) {},
2021-12-01 23:09:53 +01:00
titleTextStyle: const TextStyle(color: Colors.red),
2021-08-08 23:16:54 +02:00
enabled: false,
2021-08-03 22:04:29 +02:00
)
],
2021-08-08 23:16:54 +02:00
),
2021-08-03 22:04:29 +02:00
],
);
}
}
2021-12-01 23:09:53 +01:00
void _clearHistory(context) {
confirm(context).then((userIsSure) {
if (userIsSure) {
final Store db =
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
.database;
// db.box<Search>().query().build().find()
db.box<Search>().removeAll();
}
});
2021-08-03 22:04:29 +02:00
}