From 6ca67ea39f079a5cb7ce296497b33de548b6e735 Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Fri, 7 Jan 2022 11:06:15 -0500 Subject: [PATCH] Fix existing settings wiped when adding new ones --- lib/main.dart | 14 +++-------- .../settings/stores/settings_store.dart | 12 +++++++++ .../settings/stores/settings_store.g.dart | 25 ++++++++++--------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index b722908a..085a313f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -22,18 +22,12 @@ Future 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))); diff --git a/lib/screens/settings/stores/settings_store.dart b/lib/screens/settings/stores/settings_store.dart index 8943626f..f26b2e0e 100644 --- a/lib/screens/settings/stores/settings_store.dart +++ b/lib/screens/settings/stores/settings_store.dart @@ -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; diff --git a/lib/screens/settings/stores/settings_store.g.dart b/lib/screens/settings/stores/settings_store.g.dart index 0aac1108..25ae98a4 100644 --- a/lib/screens/settings/stores/settings_store.g.dart +++ b/lib/screens/settings/stores/settings_store.g.dart @@ -8,18 +8,19 @@ part of 'settings_store.dart'; SettingsStore _$SettingsStoreFromJson(Map 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 _$SettingsStoreToJson(SettingsStore instance) => {