frosty/lib/core/settings/settings_store.dart

33 lines
1 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
2021-10-07 23:12:28 -04:00
import 'package:mobx/mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';
2021-10-07 23:12:28 -04:00
part 'settings_store.g.dart';
class SettingsStore = _SettingsStoreBase with _$SettingsStore;
abstract class _SettingsStoreBase with Store {
2021-11-23 04:15:13 -05:00
@observable
bool videoEnabled = true;
@observable
double messageLimit = 200;
Future<void> init() async {
// Retrieve the instance that will allow us to store and persist settings.
final prefs = await SharedPreferences.getInstance();
// Initialize settings from stored preferences if any.
videoEnabled = prefs.getBool('video_enabled') ?? videoEnabled;
2021-11-23 04:15:13 -05:00
messageLimit = prefs.getDouble('message_limit') ?? messageLimit;
// Set up autorun to store setting anytime they're changed.
// The ReactionDisposer will not be needed since settings will always exist.
autorun((_) {
2021-11-23 04:15:13 -05:00
debugPrint('settings changed');
prefs.setBool('video_enabled', videoEnabled);
2021-11-23 04:15:13 -05:00
prefs.setDouble('message_limit', messageLimit);
});
}
2021-10-07 23:12:28 -04:00
}