2020-08-30 22:43:16 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
2020-10-26 00:49:24 +01:00
|
|
|
import 'shared_pref_keys.dart';
|
|
|
|
|
2020-10-26 00:39:57 +01:00
|
|
|
/// Store managing user-level configuration such as theme or language
|
2020-10-25 22:33:44 +01:00
|
|
|
class ConfigStore extends ChangeNotifier {
|
|
|
|
ThemeMode _theme;
|
|
|
|
ThemeMode get theme => _theme;
|
|
|
|
set theme(ThemeMode theme) {
|
|
|
|
_theme = theme;
|
|
|
|
notifyListeners();
|
|
|
|
save();
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:33:44 +01:00
|
|
|
bool _amoledDarkMode;
|
|
|
|
bool get amoledDarkMode => _amoledDarkMode;
|
|
|
|
set amoledDarkMode(bool amoledDarkMode) {
|
|
|
|
_amoledDarkMode = amoledDarkMode;
|
|
|
|
notifyListeners();
|
|
|
|
save();
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:33:44 +01:00
|
|
|
Future<void> load() async {
|
2020-09-16 23:22:04 +02:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2020-09-01 13:22:37 +02:00
|
|
|
// load saved settings or create defaults
|
2020-10-26 00:49:24 +01:00
|
|
|
theme =
|
|
|
|
_themeModeFromString(prefs.getString(SharedPrefKeys.theme) ?? 'system');
|
|
|
|
amoledDarkMode = prefs.getBool(SharedPrefKeys.amoledDarkMode) ?? false;
|
2020-10-25 22:33:44 +01:00
|
|
|
notifyListeners();
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:33:44 +01:00
|
|
|
Future<void> save() async {
|
2020-09-16 23:22:04 +02:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2020-08-30 22:43:16 +02:00
|
|
|
|
2020-10-26 00:49:24 +01:00
|
|
|
await prefs.setString(SharedPrefKeys.theme, describeEnum(theme));
|
|
|
|
await prefs.setBool(SharedPrefKeys.amoledDarkMode, amoledDarkMode);
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// converts string to ThemeMode
|
2020-08-30 22:43:16 +02:00
|
|
|
ThemeMode _themeModeFromString(String theme) =>
|
|
|
|
ThemeMode.values.firstWhere((e) => describeEnum(e) == theme);
|