From 6be1442816739223ef3acafd65a5e3149398358c Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Thu, 15 Jun 2023 14:32:49 +0200 Subject: [PATCH 1/2] settings: cleanup option --- packages/settings/lib/src/options/option.dart | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/settings/lib/src/options/option.dart b/packages/settings/lib/src/options/option.dart index 8f47635c..182ea9ca 100644 --- a/packages/settings/lib/src/options/option.dart +++ b/packages/settings/lib/src/options/option.dart @@ -10,20 +10,15 @@ abstract class Option { required this.defaultValue, this.category, final BehaviorSubject? enabled, - }) : assert(defaultValue.hasValue, 'When using defaultValue the stream has to be seeded') { - if (enabled != null) { - this.enabled = enabled; - } else { - this.enabled = BehaviorSubject.seeded(true); - } - } + }) : enabled = enabled ?? BehaviorSubject.seeded(true), + assert(defaultValue.hasValue, 'When using defaultValue the stream has to be seeded'); final SettingsStorage storage; final String key; final LabelBuilder label; final BehaviorSubject defaultValue; final OptionsCategory? category; - late final BehaviorSubject enabled; + final BehaviorSubject enabled; late BehaviorSubject stream; T get value { From 7557cd5963787fb0210ece344fae069b3f7e1748 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Thu, 15 Jun 2023 14:39:36 +0200 Subject: [PATCH 2/2] neon, settings: make option.defaultValue a value instead of a stream --- .../neon/lib/src/utils/account_options.dart | 2 +- .../neon/lib/src/utils/global_options.dart | 26 +++++++++---------- packages/neon/neon_files/lib/options.dart | 14 +++++----- packages/neon/neon_news/lib/options.dart | 22 ++++++++-------- packages/neon/neon_notes/lib/options.dart | 12 ++++----- packages/settings/lib/src/options/option.dart | 9 +++---- .../lib/src/options/select_option.dart | 2 +- .../lib/src/options/toggle_option.dart | 2 +- .../lib/src/widgets/option_builder.dart | 12 ++++----- 9 files changed, 48 insertions(+), 53 deletions(-) diff --git a/packages/neon/neon/lib/src/utils/account_options.dart b/packages/neon/neon/lib/src/utils/account_options.dart index 1b90a1fd..d14d4eca 100644 --- a/packages/neon/neon/lib/src/utils/account_options.dart +++ b/packages/neon/neon/lib/src/utils/account_options.dart @@ -42,7 +42,7 @@ class AccountSpecificOptions { storage: _storage, key: 'initial-app', label: (final context) => AppLocalizations.of(context).accountOptionsInitialApp, - defaultValue: BehaviorSubject.seeded(null), + defaultValue: null, values: _appIDsSubject, ); } diff --git a/packages/neon/neon/lib/src/utils/global_options.dart b/packages/neon/neon/lib/src/utils/global_options.dart index 8a8cb43b..2a43bdfc 100644 --- a/packages/neon/neon/lib/src/utils/global_options.dart +++ b/packages/neon/neon/lib/src/utils/global_options.dart @@ -124,7 +124,7 @@ class GlobalOptions { storage: _storage, key: 'theme-mode', label: (final context) => AppLocalizations.of(context).globalOptionsThemeMode, - defaultValue: BehaviorSubject.seeded(ThemeMode.system), + defaultValue: ThemeMode.system, values: BehaviorSubject.seeded({ ThemeMode.light: (final context) => AppLocalizations.of(context).globalOptionsThemeModeLight, ThemeMode.dark: (final context) => AppLocalizations.of(context).globalOptionsThemeModeDark, @@ -136,7 +136,7 @@ class GlobalOptions { storage: _storage, key: 'theme-oled-as-dark', label: (final context) => AppLocalizations.of(context).globalOptionsThemeOLEDAsDark, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, enabled: _themeOLEDAsDarkEnabledSubject, ); @@ -144,14 +144,14 @@ class GlobalOptions { storage: _storage, key: 'theme-keep-original-accent-color', label: (final context) => AppLocalizations.of(context).globalOptionsThemeKeepOriginalAccentColor, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, ); late final pushNotificationsEnabled = ToggleOption( storage: _storage, key: 'push-notifications-enabled', label: (final context) => AppLocalizations.of(context).globalOptionsPushNotificationsEnabled, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, enabled: _pushNotificationsEnabledEnabledSubject, ); @@ -159,7 +159,7 @@ class GlobalOptions { storage: _storage, key: 'push-notifications-distributor', label: (final context) => AppLocalizations.of(context).globalOptionsPushNotificationsDistributor, - defaultValue: BehaviorSubject.seeded(null), + defaultValue: null, values: _pushNotificationsDistributorsSubject, enabled: pushNotificationsEnabled.stream, ); @@ -168,14 +168,14 @@ class GlobalOptions { storage: _storage, key: 'startup-minimized', label: (final context) => AppLocalizations.of(context).globalOptionsStartupMinimized, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, ); late final startupMinimizeInsteadOfExit = ToggleOption( storage: _storage, key: 'startup-minimize-instead-of-exit', label: (final context) => AppLocalizations.of(context).globalOptionsStartupMinimizeInsteadOfExit, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, ); // TODO: Autostart option @@ -184,14 +184,14 @@ class GlobalOptions { storage: _storage, key: 'systemtray-enabled', label: (final context) => AppLocalizations.of(context).globalOptionsSystemTrayEnabled, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, ); late final systemTrayHideToTrayWhenMinimized = ToggleOption( storage: _storage, key: 'systemtray-hide-to-tray-when-minimized', label: (final context) => AppLocalizations.of(context).globalOptionsSystemTrayHideToTrayWhenMinimized, - defaultValue: BehaviorSubject.seeded(true), + defaultValue: true, enabled: systemTrayEnabled.stream, ); @@ -199,14 +199,14 @@ class GlobalOptions { storage: _storage, key: 'remember-last-used-account', label: (final context) => AppLocalizations.of(context).globalOptionsAccountsRememberLastUsedAccount, - defaultValue: BehaviorSubject.seeded(true), + defaultValue: true, ); late final initialAccount = SelectOption( storage: _storage, key: 'initial-account', label: (final context) => AppLocalizations.of(context).globalOptionsAccountsInitialAccount, - defaultValue: BehaviorSubject.seeded(null), + defaultValue: null, values: _accountsIDsSubject, enabled: _initialAccountEnabledSubject, ); @@ -215,9 +215,7 @@ class GlobalOptions { storage: _storage, key: 'navigation-mode', label: (final context) => AppLocalizations.of(context).globalOptionsNavigationMode, - defaultValue: BehaviorSubject.seeded( - Platform.isAndroid || Platform.isIOS ? NavigationMode.drawer : NavigationMode.drawerAlwaysVisible, - ), + defaultValue: Platform.isAndroid || Platform.isIOS ? NavigationMode.drawer : NavigationMode.drawerAlwaysVisible, values: BehaviorSubject.seeded({ NavigationMode.drawer: (final context) => AppLocalizations.of(context).globalOptionsNavigationModeDrawer, if (!Platform.isAndroid && !Platform.isIOS) ...{ diff --git a/packages/neon/neon_files/lib/options.dart b/packages/neon/neon_files/lib/options.dart index 1c39b61d..efd49f89 100644 --- a/packages/neon/neon_files/lib/options.dart +++ b/packages/neon/neon_files/lib/options.dart @@ -25,7 +25,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'files-sort-property', label: (final context) => AppLocalizations.of(context).optionsFilesSortProperty, - defaultValue: BehaviorSubject.seeded(FilesSortProperty.name), + defaultValue: FilesSortProperty.name, values: BehaviorSubject.seeded({ FilesSortProperty.name: (final context) => AppLocalizations.of(context).optionsFilesSortPropertyName, FilesSortProperty.modifiedDate: (final context) => @@ -39,7 +39,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'files-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsFilesSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.ascending), + defaultValue: SortBoxOrder.ascending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); @@ -48,7 +48,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'show-previews', label: (final context) => AppLocalizations.of(context).optionsShowPreviews, - defaultValue: BehaviorSubject.seeded(true), + defaultValue: true, ); late final uploadQueueParallelism = SelectOption( @@ -56,7 +56,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'upload-queue-parallelism', label: (final context) => AppLocalizations.of(context).optionsUploadQueueParallelism, - defaultValue: BehaviorSubject.seeded(4), + defaultValue: 4, values: BehaviorSubject.seeded({ for (var i = 1; i <= 16; i = i * 2) ...{ i: (final _) => i.toString(), @@ -69,7 +69,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'download-queue-parallelism', label: (final context) => AppLocalizations.of(context).optionsDownloadQueueParallelism, - defaultValue: BehaviorSubject.seeded(4), + defaultValue: 4, values: BehaviorSubject.seeded({ for (var i = 1; i <= 16; i = i * 2) ...{ i: (final _) => i.toString(), @@ -99,7 +99,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'upload-size-warning', label: (final context) => AppLocalizations.of(context).optionsUploadSizeWarning, - defaultValue: BehaviorSubject.seeded(_mb(10)), + defaultValue: _mb(10), values: BehaviorSubject.seeded(_sizeWarningValues), ); @@ -108,7 +108,7 @@ class FilesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'download-size-warning', label: (final context) => AppLocalizations.of(context).optionsDownloadSizeWarning, - defaultValue: BehaviorSubject.seeded(_mb(10)), + defaultValue: _mb(10), values: BehaviorSubject.seeded(_sizeWarningValues), ); } diff --git a/packages/neon/neon_news/lib/options.dart b/packages/neon/neon_news/lib/options.dart index c760012a..c619d2ce 100644 --- a/packages/neon/neon_news/lib/options.dart +++ b/packages/neon/neon_news/lib/options.dart @@ -56,7 +56,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'default-category', label: (final context) => AppLocalizations.of(context).optionsDefaultCategory, - defaultValue: BehaviorSubject.seeded(DefaultCategory.articles), + defaultValue: DefaultCategory.articles, values: BehaviorSubject.seeded({ DefaultCategory.articles: (final context) => AppLocalizations.of(context).articles, DefaultCategory.folders: (final context) => AppLocalizations.of(context).folders, @@ -69,7 +69,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: articlesCategory, key: 'article-view-type', label: (final context) => AppLocalizations.of(context).optionsArticleViewType, - defaultValue: BehaviorSubject.seeded(ArticleViewType.direct), + defaultValue: ArticleViewType.direct, values: _articleViewTypeValuesSubject, ); @@ -78,7 +78,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: articlesCategory, key: 'article-disable-mark-as-read-timeout', label: (final context) => AppLocalizations.of(context).optionsArticleDisableMarkAsReadTimeout, - defaultValue: BehaviorSubject.seeded(false), + defaultValue: false, ); late final defaultArticlesFilterOption = SelectOption( @@ -86,7 +86,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: articlesCategory, key: 'default-articles-filter', label: (final context) => AppLocalizations.of(context).optionsDefaultArticlesFilter, - defaultValue: BehaviorSubject.seeded(FilterType.unread), + defaultValue: FilterType.unread, values: BehaviorSubject.seeded({ FilterType.all: (final context) => AppLocalizations.of(context).articlesFilterAll, FilterType.unread: (final context) => AppLocalizations.of(context).articlesFilterUnread, @@ -99,7 +99,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: articlesCategory, key: 'articles-sort-property', label: (final context) => AppLocalizations.of(context).optionsArticlesSortProperty, - defaultValue: BehaviorSubject.seeded(ArticlesSortProperty.publishDate), + defaultValue: ArticlesSortProperty.publishDate, values: BehaviorSubject.seeded({ ArticlesSortProperty.publishDate: (final context) => AppLocalizations.of(context).optionsArticlesSortPropertyPublishDate, @@ -114,7 +114,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: articlesCategory, key: 'articles-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsArticlesSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.descending), + defaultValue: SortBoxOrder.descending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); @@ -123,7 +123,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: foldersCategory, key: 'folders-sort-property', label: (final context) => AppLocalizations.of(context).optionsFoldersSortProperty, - defaultValue: BehaviorSubject.seeded(FoldersSortProperty.alphabetical), + defaultValue: FoldersSortProperty.alphabetical, values: BehaviorSubject.seeded({ FoldersSortProperty.alphabetical: (final context) => AppLocalizations.of(context).optionsFoldersSortPropertyAlphabetical, @@ -137,7 +137,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: foldersCategory, key: 'folders-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsFoldersSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.ascending), + defaultValue: SortBoxOrder.ascending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); @@ -146,7 +146,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: foldersCategory, key: 'default-folder-view-type', label: (final context) => AppLocalizations.of(context).optionsDefaultFolderViewType, - defaultValue: BehaviorSubject.seeded(DefaultFolderViewType.articles), + defaultValue: DefaultFolderViewType.articles, values: BehaviorSubject.seeded({ DefaultFolderViewType.articles: (final context) => AppLocalizations.of(context).articles, DefaultFolderViewType.feeds: (final context) => AppLocalizations.of(context).feeds, @@ -158,7 +158,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: feedsCategory, key: 'feeds-sort-property', label: (final context) => AppLocalizations.of(context).optionsFeedsSortProperty, - defaultValue: BehaviorSubject.seeded(FeedsSortProperty.alphabetical), + defaultValue: FeedsSortProperty.alphabetical, values: BehaviorSubject.seeded({ FeedsSortProperty.alphabetical: (final context) => AppLocalizations.of(context).optionsFeedsSortPropertyAlphabetical, @@ -172,7 +172,7 @@ class NewsAppSpecificOptions extends NextcloudAppSpecificOptions { category: feedsCategory, key: 'feeds-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsFeedsSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.ascending), + defaultValue: SortBoxOrder.ascending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); } diff --git a/packages/neon/neon_notes/lib/options.dart b/packages/neon/neon_notes/lib/options.dart index 47c780f3..b3c2fefa 100644 --- a/packages/neon/neon_notes/lib/options.dart +++ b/packages/neon/neon_notes/lib/options.dart @@ -34,7 +34,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'default-category', label: (final context) => AppLocalizations.of(context).optionsDefaultCategory, - defaultValue: BehaviorSubject.seeded(DefaultCategory.notes), + defaultValue: DefaultCategory.notes, values: BehaviorSubject.seeded({ DefaultCategory.notes: (final context) => AppLocalizations.of(context).notes, DefaultCategory.categories: (final context) => AppLocalizations.of(context).categories, @@ -46,7 +46,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: generalCategory, key: 'default-note-view-type', label: (final context) => AppLocalizations.of(context).optionsDefaultNoteViewType, - defaultValue: BehaviorSubject.seeded(DefaultNoteViewType.preview), + defaultValue: DefaultNoteViewType.preview, values: BehaviorSubject.seeded({ DefaultNoteViewType.preview: (final context) => AppLocalizations.of(context).optionsDefaultNoteViewTypePreview, DefaultNoteViewType.edit: (final context) => AppLocalizations.of(context).optionsDefaultNoteViewTypeEdit, @@ -58,7 +58,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: notesCategory, key: 'notes-sort-property', label: (final context) => AppLocalizations.of(context).optionsNotesSortProperty, - defaultValue: BehaviorSubject.seeded(NotesSortProperty.lastModified), + defaultValue: NotesSortProperty.lastModified, values: BehaviorSubject.seeded({ NotesSortProperty.lastModified: (final context) => AppLocalizations.of(context).optionsNotesSortPropertyLastModified, @@ -72,7 +72,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: notesCategory, key: 'notes-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsNotesSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.descending), + defaultValue: SortBoxOrder.descending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); @@ -81,7 +81,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: categoriesCategory, key: 'categories-sort-property', label: (final context) => AppLocalizations.of(context).optionsCategoriesSortProperty, - defaultValue: BehaviorSubject.seeded(CategoriesSortProperty.alphabetical), + defaultValue: CategoriesSortProperty.alphabetical, values: BehaviorSubject.seeded({ CategoriesSortProperty.alphabetical: (final context) => AppLocalizations.of(context).optionsCategoriesSortPropertyAlphabetical, @@ -95,7 +95,7 @@ class NotesAppSpecificOptions extends NextcloudAppSpecificOptions { category: categoriesCategory, key: 'categories-sort-box-order', label: (final context) => AppLocalizations.of(context).optionsCategoriesSortOrder, - defaultValue: BehaviorSubject.seeded(SortBoxOrder.ascending), + defaultValue: SortBoxOrder.ascending, values: BehaviorSubject.seeded(sortBoxOrderOptionValues), ); } diff --git a/packages/settings/lib/src/options/option.dart b/packages/settings/lib/src/options/option.dart index 182ea9ca..14023de1 100644 --- a/packages/settings/lib/src/options/option.dart +++ b/packages/settings/lib/src/options/option.dart @@ -10,13 +10,12 @@ abstract class Option { required this.defaultValue, this.category, final BehaviorSubject? enabled, - }) : enabled = enabled ?? BehaviorSubject.seeded(true), - assert(defaultValue.hasValue, 'When using defaultValue the stream has to be seeded'); + }) : enabled = enabled ?? BehaviorSubject.seeded(true); final SettingsStorage storage; final String key; final LabelBuilder label; - final BehaviorSubject defaultValue; + final T defaultValue; final OptionsCategory? category; final BehaviorSubject enabled; @@ -26,7 +25,7 @@ abstract class Option { return stream.value; } - return defaultValue.value; + return defaultValue; } bool get hasValue { @@ -38,7 +37,7 @@ abstract class Option { } Future reset() async { - await set(defaultValue.value); + await set(defaultValue); } void dispose() { diff --git a/packages/settings/lib/src/options/select_option.dart b/packages/settings/lib/src/options/select_option.dart index 5917e672..be06156a 100644 --- a/packages/settings/lib/src/options/select_option.dart +++ b/packages/settings/lib/src/options/select_option.dart @@ -19,7 +19,7 @@ class SelectOption extends Option { if (valueStr != null) { initialValue = _fromString(vs, valueStr); } - stream.add(initialValue ?? await defaultValue.first); + stream.add(initialValue ?? defaultValue); }), ); } diff --git a/packages/settings/lib/src/options/toggle_option.dart b/packages/settings/lib/src/options/toggle_option.dart index 268a3a6d..c1c6fa6e 100644 --- a/packages/settings/lib/src/options/toggle_option.dart +++ b/packages/settings/lib/src/options/toggle_option.dart @@ -9,7 +9,7 @@ class ToggleOption extends Option { super.category, super.enabled, }) { - stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue.value); + stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue); } @override diff --git a/packages/settings/lib/src/widgets/option_builder.dart b/packages/settings/lib/src/widgets/option_builder.dart index 6334c8b1..605b6563 100644 --- a/packages/settings/lib/src/widgets/option_builder.dart +++ b/packages/settings/lib/src/widgets/option_builder.dart @@ -12,13 +12,11 @@ class OptionBuilder extends StatelessWidget { @override Widget build(final BuildContext context) => StreamBuilder( - stream: option.defaultValue, - builder: (final context, final defaultValueSnapshot) => StreamBuilder( - stream: option.stream, - builder: (final context, final valueSnapshot) => builder( - context, - valueSnapshot.data ?? defaultValueSnapshot.data, - ), + stream: option.stream, + initialData: option.defaultValue, + builder: (final context, final valueSnapshot) => builder( + context, + valueSnapshot.data, ), ); }