From 0e563cec189f9c835112a8fe4d05e06cc4a4ecca Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 29 Aug 2023 17:13:34 +0200 Subject: [PATCH] refactor(neon,neon_files,neon_news,neon_notes): make Option.key unique Signed-off-by: Nikolas Rimikis --- packages/neon/neon/lib/settings.dart | 1 + .../neon/lib/src/settings/models/option.dart | 2 +- .../src/settings/models/select_option.dart | 8 ++-- .../neon/lib/src/settings/models/storage.dart | 7 ++- .../src/settings/models/toggle_option.dart | 8 ++-- .../neon/lib/src/utils/account_options.dart | 11 ++++- .../neon/lib/src/utils/global_options.dart | 44 ++++++++++++++----- .../lib/src/utils/settings_export_helper.dart | 2 +- packages/neon/neon/test/option_test.dart | 31 ++++++++----- packages/neon/neon_files/lib/options.dart | 29 +++++++++--- packages/neon/neon_news/lib/options.dart | 41 ++++++++++++----- packages/neon/neon_notes/lib/options.dart | 26 ++++++++--- 12 files changed, 151 insertions(+), 59 deletions(-) diff --git a/packages/neon/neon/lib/settings.dart b/packages/neon/neon/lib/settings.dart index 8a1e4d8e..13193417 100644 --- a/packages/neon/neon/lib/settings.dart +++ b/packages/neon/neon/lib/settings.dart @@ -2,5 +2,6 @@ export 'package:neon/src/models/label_builder.dart'; export 'package:neon/src/settings/models/nextcloud_app_options.dart'; export 'package:neon/src/settings/models/options_category.dart'; export 'package:neon/src/settings/models/select_option.dart'; +export 'package:neon/src/settings/models/storage.dart' show Storable; export 'package:neon/src/settings/models/toggle_option.dart'; export 'package:neon/src/settings/widgets/settings_list.dart'; diff --git a/packages/neon/neon/lib/src/settings/models/option.dart b/packages/neon/neon/lib/src/settings/models/option.dart index 4d33bfb3..ac954b4d 100644 --- a/packages/neon/neon/lib/src/settings/models/option.dart +++ b/packages/neon/neon/lib/src/settings/models/option.dart @@ -38,7 +38,7 @@ abstract class Option extends ChangeNotifier implements ValueListenable { } final SettingsStorage storage; - final String key; + final Storable key; final LabelBuilder label; final T defaultValue; final OptionsCategory? category; diff --git a/packages/neon/neon/lib/src/settings/models/select_option.dart b/packages/neon/neon/lib/src/settings/models/select_option.dart index 0cc0b9e2..6139cbe5 100644 --- a/packages/neon/neon/lib/src/settings/models/select_option.dart +++ b/packages/neon/neon/lib/src/settings/models/select_option.dart @@ -21,7 +21,7 @@ class SelectOption extends Option { super.category, super.enabled, }) : _values = values, - super(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue)); + super(initialValue: loadValue(values, storage.getString(key.value), forceLoad: forceLoadValue)); /// Creates a SelectOption depending on the State of another [Option]. SelectOption.depend({ @@ -39,7 +39,7 @@ class SelectOption extends Option { final bool forceLoadValue = true, super.category, }) : _values = values, - super.depend(initialValue: loadValue(values, storage.getString(key), forceLoad: forceLoadValue)); + super.depend(initialValue: loadValue(values, storage.getString(key.value), forceLoad: forceLoadValue)); static T? loadValue(final Map vs, final String? stored, {final bool forceLoad = true}) { if (forceLoad && vs.isEmpty && stored is T) { @@ -51,7 +51,7 @@ class SelectOption extends Option { @override void reset() { - unawaited(storage.remove(key)); + unawaited(storage.remove(key.value)); super.reset(); } @@ -63,7 +63,7 @@ class SelectOption extends Option { super.value = value; if (value != null) { - unawaited(storage.setString(key, serialize()!)); + unawaited(storage.setString(key.value, serialize()!)); } } diff --git a/packages/neon/neon/lib/src/settings/models/storage.dart b/packages/neon/neon/lib/src/settings/models/storage.dart index 76575fc8..74a52862 100644 --- a/packages/neon/neon/lib/src/settings/models/storage.dart +++ b/packages/neon/neon/lib/src/settings/models/storage.dart @@ -16,8 +16,12 @@ abstract interface class SettingsStorage { Future remove(final String key); } +abstract interface class Storable { + String get value; +} + @internal -enum StorageKeys { +enum StorageKeys implements Storable { apps._('app'), accounts._('accounts'), global._('global'), @@ -28,6 +32,7 @@ enum StorageKeys { const StorageKeys._(this.value); + @override final String value; } diff --git a/packages/neon/neon/lib/src/settings/models/toggle_option.dart b/packages/neon/neon/lib/src/settings/models/toggle_option.dart index e3ef5397..9b3ae94b 100644 --- a/packages/neon/neon/lib/src/settings/models/toggle_option.dart +++ b/packages/neon/neon/lib/src/settings/models/toggle_option.dart @@ -11,7 +11,7 @@ class ToggleOption extends Option { required final bool defaultValue, super.category, super.enabled, - }) : super(defaultValue: storage.getBool(key) ?? defaultValue); + }) : super(defaultValue: storage.getBool(key.value) ?? defaultValue); /// Creates a ToggleOption depending on the State of another [Option]. ToggleOption.depend({ @@ -22,12 +22,12 @@ class ToggleOption extends Option { required super.enabled, super.category, }) : super.depend( - defaultValue: storage.getBool(key) ?? defaultValue, + defaultValue: storage.getBool(key.value) ?? defaultValue, ); @override void reset() { - unawaited(storage.remove(key)); + unawaited(storage.remove(key.value)); super.reset(); } @@ -36,7 +36,7 @@ class ToggleOption extends Option { set value(final bool value) { super.value = value; - unawaited(storage.setBool(key, serialize())); + unawaited(storage.setBool(key.value, serialize())); } @override diff --git a/packages/neon/neon/lib/src/utils/account_options.dart b/packages/neon/neon/lib/src/utils/account_options.dart index 6125009d..61c2c5cc 100644 --- a/packages/neon/neon/lib/src/utils/account_options.dart +++ b/packages/neon/neon/lib/src/utils/account_options.dart @@ -47,9 +47,18 @@ class AccountSpecificOptions { late final initialApp = SelectOption( storage: _storage, - key: 'initial-app', + key: AccountOptionKeys.initialApp, label: (final context) => AppLocalizations.of(context).accountOptionsInitialApp, defaultValue: null, values: {}, ); } + +enum AccountOptionKeys implements Storable { + initialApp._('initial-app'); + + const AccountOptionKeys._(this.value); + + @override + final String value; +} diff --git a/packages/neon/neon/lib/src/utils/global_options.dart b/packages/neon/neon/lib/src/utils/global_options.dart index ca31d8c5..e7081fef 100644 --- a/packages/neon/neon/lib/src/utils/global_options.dart +++ b/packages/neon/neon/lib/src/utils/global_options.dart @@ -125,7 +125,7 @@ class GlobalOptions { late final themeMode = SelectOption( storage: _storage, - key: 'theme-mode', + key: GlobalOptionKeys.themeMode, label: (final context) => AppLocalizations.of(context).globalOptionsThemeMode, defaultValue: ThemeMode.system, values: { @@ -137,28 +137,28 @@ class GlobalOptions { late final themeOLEDAsDark = ToggleOption( storage: _storage, - key: 'theme-oled-as-dark', + key: GlobalOptionKeys.themeOledAsDark, label: (final context) => AppLocalizations.of(context).globalOptionsThemeOLEDAsDark, defaultValue: false, ); late final themeKeepOriginalAccentColor = ToggleOption( storage: _storage, - key: 'theme-keep-original-accent-color', + key: GlobalOptionKeys.themeKeepOriginalAccentColor, label: (final context) => AppLocalizations.of(context).globalOptionsThemeKeepOriginalAccentColor, defaultValue: false, ); late final pushNotificationsEnabled = ToggleOption( storage: _storage, - key: 'push-notifications-enabled', + key: GlobalOptionKeys.pushNotificationsEnabled, label: (final context) => AppLocalizations.of(context).globalOptionsPushNotificationsEnabled, defaultValue: false, ); late final pushNotificationsDistributor = SelectOption.depend( storage: _storage, - key: 'push-notifications-distributor', + key: GlobalOptionKeys.pushNotificationsDistributor, label: (final context) => AppLocalizations.of(context).globalOptionsPushNotificationsDistributor, defaultValue: null, values: {}, @@ -167,14 +167,14 @@ class GlobalOptions { late final startupMinimized = ToggleOption( storage: _storage, - key: 'startup-minimized', + key: GlobalOptionKeys.startupMinimized, label: (final context) => AppLocalizations.of(context).globalOptionsStartupMinimized, defaultValue: false, ); late final startupMinimizeInsteadOfExit = ToggleOption( storage: _storage, - key: 'startup-minimize-instead-of-exit', + key: GlobalOptionKeys.startupMinimizeInsteadOfExit, label: (final context) => AppLocalizations.of(context).globalOptionsStartupMinimizeInsteadOfExit, defaultValue: false, ); @@ -183,14 +183,14 @@ class GlobalOptions { late final systemTrayEnabled = ToggleOption( storage: _storage, - key: 'systemtray-enabled', + key: GlobalOptionKeys.systemtrayEnabled, label: (final context) => AppLocalizations.of(context).globalOptionsSystemTrayEnabled, defaultValue: false, ); late final systemTrayHideToTrayWhenMinimized = ToggleOption.depend( storage: _storage, - key: 'systemtray-hide-to-tray-when-minimized', + key: GlobalOptionKeys.systemtrayHideToTrayWhenMinimized, label: (final context) => AppLocalizations.of(context).globalOptionsSystemTrayHideToTrayWhenMinimized, defaultValue: true, enabled: systemTrayEnabled, @@ -198,14 +198,14 @@ class GlobalOptions { late final rememberLastUsedAccount = ToggleOption( storage: _storage, - key: 'remember-last-used-account', + key: GlobalOptionKeys.rememberLastUsedAccount, label: (final context) => AppLocalizations.of(context).globalOptionsAccountsRememberLastUsedAccount, defaultValue: true, ); late final initialAccount = SelectOption( storage: _storage, - key: 'initial-account', + key: GlobalOptionKeys.initialAccount, label: (final context) => AppLocalizations.of(context).globalOptionsAccountsInitialAccount, defaultValue: null, values: {}, @@ -213,7 +213,7 @@ class GlobalOptions { late final navigationMode = SelectOption( storage: _storage, - key: 'navigation-mode', + key: GlobalOptionKeys.navigationMode, label: (final context) => AppLocalizations.of(context).globalOptionsNavigationMode, defaultValue: Platform.isAndroid || Platform.isIOS ? NavigationMode.drawer : NavigationMode.drawerAlwaysVisible, values: { @@ -228,6 +228,26 @@ class GlobalOptions { ); } +enum GlobalOptionKeys implements Storable { + themeMode._('theme-mode'), + themeOledAsDark._('theme-oled-as-dark'), + themeKeepOriginalAccentColor._('theme-keep-original-accent-color'), + pushNotificationsEnabled._('push-notifications-enabled'), + pushNotificationsDistributor._('push-notifications-distributor'), + startupMinimized._('startup-minimized'), + startupMinimizeInsteadOfExit._('startup-minimize-instead-of-exit'), + systemtrayEnabled._('systemtray-enabled'), + systemtrayHideToTrayWhenMinimized._('systemtray-hide-to-tray-when-minimized'), + rememberLastUsedAccount._('remember-last-used-account'), + initialAccount._('initial-account'), + navigationMode._('navigation-mode'); + + const GlobalOptionKeys._(this.value); + + @override + final String value; +} + enum NavigationMode { drawer, drawerAlwaysVisible, diff --git a/packages/neon/neon/lib/src/utils/settings_export_helper.dart b/packages/neon/neon/lib/src/utils/settings_export_helper.dart index 3fe86dfa..d402eb5b 100644 --- a/packages/neon/neon/lib/src/utils/settings_export_helper.dart +++ b/packages/neon/neon/lib/src/utils/settings_export_helper.dart @@ -53,7 +53,7 @@ class SettingsExportHelper { Future _applyOptionsMapToOptions(final List