1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-16 18:39:21 +01:00
lemmur-app-android/lib/stores/config_store.dart
2020-09-08 10:23:08 +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 {
var prefs = await SharedPreferences.getInstance();
// load saved settings or create defaults
theme = _themeModeFromString(prefs.getString('theme') ?? 'system');
amoledDarkMode = prefs.getBool('amoledDarkMode') ?? false;
}
void save() async {
var 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);