lemmur-app-android/lib/stores/config_store.dart

45 lines
1.3 KiB
Dart
Raw Normal View History

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';
import 'shared_pref_keys.dart';
2020-10-26 00:39:57 +01:00
/// Store managing user-level configuration such as theme or language
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
}
bool _amoledDarkMode;
bool get amoledDarkMode => _amoledDarkMode;
set amoledDarkMode(bool amoledDarkMode) {
_amoledDarkMode = amoledDarkMode;
notifyListeners();
save();
2020-08-30 22:43:16 +02: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
theme =
_themeModeFromString(prefs.getString(SharedPrefKeys.theme) ?? 'system');
amoledDarkMode = prefs.getBool(SharedPrefKeys.amoledDarkMode) ?? false;
notifyListeners();
2020-08-30 22:43:16 +02: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
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);