1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-17 10:59:29 +01:00
lemmur-app-android/lib/stores/config_store.dart
2020-09-16 23:22:04 +02:00

47 lines
1.2 KiB
Dart

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';
class ConfigStore extends _ConfigStore with _$ConfigStore {}
abstract class _ConfigStore with Store {
ReactionDisposer _saveReactionDisposer;
_ConfigStore() {
// persitently save settings each time they are changed
_saveReactionDisposer = reaction((_) => [theme, amoledDarkMode], (_) {
save();
});
}
void dispose() {
_saveReactionDisposer();
}
void load() async {
final prefs = await SharedPreferences.getInstance();
// load saved settings or create defaults
theme = _themeModeFromString(prefs.getString('theme') ?? 'system');
amoledDarkMode = prefs.getBool('amoledDarkMode') ?? false;
}
void save() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('theme', describeEnum(theme));
await prefs.setBool('amoledDarkMode', amoledDarkMode);
}
@observable
ThemeMode theme;
@observable
bool amoledDarkMode;
}
ThemeMode _themeModeFromString(String theme) =>
ThemeMode.values.firstWhere((e) => describeEnum(e) == theme);