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

45 lines
1.1 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: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
2020-08-31 16:17:39 +02:00
_saveReactionDisposer = reaction((_) => [theme], (_) {
2020-08-30 22:43:16 +02:00
save();
});
}
void dispose() {
_saveReactionDisposer();
}
void load() async {
var prefs = await SharedPreferences.getInstance();
// set saved settings or create defaults
theme = _themeModeFromString(prefs.getString('theme') ?? 'system');
2020-08-30 22:43:16 +02:00
}
void save() async {
var prefs = await SharedPreferences.getInstance();
await prefs.setString('theme', describeEnum(theme));
}
@observable
ThemeMode theme;
2020-08-30 22:43:16 +02:00
@observable
MaterialColor accentColor;
2020-08-30 22:43:16 +02:00
}
ThemeMode _themeModeFromString(String theme) =>
ThemeMode.values.firstWhere((e) => describeEnum(e) == theme);