Fix existing settings wiped when adding new ones

This commit is contained in:
Tommy Chow 2022-01-07 11:06:15 -05:00
parent b3753cad81
commit 6ca67ea39f
3 changed files with 29 additions and 22 deletions

View file

@ -22,18 +22,12 @@ Future<void> main() async {
await authStore.init();
// Get the shared pereferences instance and obtain the existing user settings if it exists.
// If default settings don't exist, use an empty JSON string to use the default values.
final preferences = await SharedPreferences.getInstance();
final userSettings = preferences.getString('settings');
final userSettings = preferences.getString('settings') ?? '{}';
SettingsStore settingsStore;
try {
// Initialize a settings store from existing settings.
settingsStore = SettingsStore.fromJson(jsonDecode(userSettings!));
} catch (e) {
// If existing settings don't exist or if the settings could not be decoded properly, create a new one.
// This will also occur when new settings are added to the app.
settingsStore = SettingsStore();
}
// Initialize a settings store from the settings JSON string.
final settingsStore = SettingsStore.fromJson(jsonDecode(userSettings));
// Create a MobX reaction that will save the settings on disk everytime they are changed.
autorun((_) => preferences.setString('settings', jsonEncode(settingsStore)));

View file

@ -14,42 +14,54 @@ class SettingsStore extends _SettingsStoreBase with _$SettingsStore {
abstract class _SettingsStoreBase with Store {
//General Settings
@JsonKey(defaultValue: false)
@observable
var useOledTheme = false;
@JsonKey(defaultValue: true)
@observable
var showThumbnailUptime = true;
// Video Settings
@JsonKey(defaultValue: true)
@observable
var showVideo = true;
@JsonKey(defaultValue: true)
@observable
var showOverlay = true;
@JsonKey(defaultValue: false)
@observable
var pictureInPicture = false;
// Chat Settings
@JsonKey(defaultValue: false)
@observable
var showDeletedMessages = false;
@JsonKey(defaultValue: false)
@observable
var showZeroWidth = false;
@JsonKey(defaultValue: false)
@observable
var showTimestamps = false;
@JsonKey(defaultValue: false)
@observable
var useTwelveHourTimestamps = false;
@JsonKey(defaultValue: true)
@observable
var useReadableColors = true;
// Global configs
@JsonKey(defaultValue: false)
@observable
var fullScreen = false;
@JsonKey(defaultValue: true)
@observable
var expandInfo = true;

View file

@ -8,18 +8,19 @@ part of 'settings_store.dart';
SettingsStore _$SettingsStoreFromJson(Map<String, dynamic> json) =>
SettingsStore()
..useOledTheme = json['useOledTheme'] as bool
..showThumbnailUptime = json['showThumbnailUptime'] as bool
..showVideo = json['showVideo'] as bool
..showOverlay = json['showOverlay'] as bool
..pictureInPicture = json['pictureInPicture'] as bool
..showDeletedMessages = json['showDeletedMessages'] as bool
..showZeroWidth = json['showZeroWidth'] as bool
..showTimestamps = json['showTimestamps'] as bool
..useTwelveHourTimestamps = json['useTwelveHourTimestamps'] as bool
..useReadableColors = json['useReadableColors'] as bool
..fullScreen = json['fullScreen'] as bool
..expandInfo = json['expandInfo'] as bool;
..useOledTheme = json['useOledTheme'] as bool? ?? false
..showThumbnailUptime = json['showThumbnailUptime'] as bool? ?? true
..showVideo = json['showVideo'] as bool? ?? true
..showOverlay = json['showOverlay'] as bool? ?? true
..pictureInPicture = json['pictureInPicture'] as bool? ?? false
..showDeletedMessages = json['showDeletedMessages'] as bool? ?? false
..showZeroWidth = json['showZeroWidth'] as bool? ?? false
..showTimestamps = json['showTimestamps'] as bool? ?? false
..useTwelveHourTimestamps =
json['useTwelveHourTimestamps'] as bool? ?? false
..useReadableColors = json['useReadableColors'] as bool? ?? true
..fullScreen = json['fullScreen'] as bool? ?? false
..expandInfo = json['expandInfo'] as bool? ?? true;
Map<String, dynamic> _$SettingsStoreToJson(SettingsStore instance) =>
<String, dynamic>{