2020-08-30 22:43:16 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
part 'config_store.g.dart';
|
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// Store managing user-level configuration such as theme or language
|
2020-08-30 22:43:16 +02:00
|
|
|
class ConfigStore extends _ConfigStore with _$ConfigStore {}
|
|
|
|
|
|
|
|
abstract class _ConfigStore with Store {
|
|
|
|
ReactionDisposer _saveReactionDisposer;
|
|
|
|
|
|
|
|
_ConfigStore() {
|
|
|
|
// persitently save settings each time they are changed
|
2020-09-08 10:23:08 +02:00
|
|
|
_saveReactionDisposer = reaction((_) => [theme, amoledDarkMode], (_) {
|
2020-08-30 22:43:16 +02:00
|
|
|
save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
_saveReactionDisposer();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-31 17:52:16 +02:00
|
|
|
theme = _themeModeFromString(prefs.getString('theme') ?? 'system');
|
2020-09-08 10:23:08 +02:00
|
|
|
amoledDarkMode = prefs.getBool('amoledDarkMode') ?? false;
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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('theme', describeEnum(theme));
|
2020-09-08 10:23:08 +02:00
|
|
|
await prefs.setBool('amoledDarkMode', amoledDarkMode);
|
2020-08-30 22:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@observable
|
2020-08-31 17:52:16 +02:00
|
|
|
ThemeMode theme;
|
2020-08-30 22:43:16 +02:00
|
|
|
|
2020-09-08 10:23:08 +02:00
|
|
|
@observable
|
|
|
|
bool 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);
|