Browse Source

Merge pull request #389 from Leptopoda/refactor/option

Refactor/option
pull/394/head
Nikolas Rimikis 1 year ago committed by GitHub
parent
commit
bba5c2b575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      packages/neon/neon/lib/src/utils/account_options.dart
  2. 26
      packages/neon/neon/lib/src/utils/global_options.dart
  3. 14
      packages/neon/neon_files/lib/options.dart
  4. 22
      packages/neon/neon_news/lib/options.dart
  5. 12
      packages/neon/neon_notes/lib/options.dart
  6. 16
      packages/settings/lib/src/options/option.dart
  7. 2
      packages/settings/lib/src/options/select_option.dart
  8. 2
      packages/settings/lib/src/options/toggle_option.dart
  9. 6
      packages/settings/lib/src/widgets/option_builder.dart

2
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,
);
}

26
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<String?>(
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) ...{

14
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<int>(
@ -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),
);
}

22
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<FilterType>(
@ -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),
);
}

12
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),
);
}

16
packages/settings/lib/src/options/option.dart

@ -10,20 +10,14 @@ abstract class Option<T> {
required this.defaultValue,
this.category,
final BehaviorSubject<bool>? enabled,
}) : assert(defaultValue.hasValue, 'When using defaultValue the stream has to be seeded') {
if (enabled != null) {
this.enabled = enabled;
} else {
this.enabled = BehaviorSubject<bool>.seeded(true);
}
}
}) : enabled = enabled ?? BehaviorSubject<bool>.seeded(true);
final SettingsStorage storage;
final String key;
final LabelBuilder label;
final BehaviorSubject<T> defaultValue;
final T defaultValue;
final OptionsCategory? category;
late final BehaviorSubject<bool> enabled;
final BehaviorSubject<bool> enabled;
late BehaviorSubject<T> stream;
T get value {
@ -31,7 +25,7 @@ abstract class Option<T> {
return stream.value;
}
return defaultValue.value;
return defaultValue;
}
bool get hasValue {
@ -43,7 +37,7 @@ abstract class Option<T> {
}
Future reset() async {
await set(defaultValue.value);
await set(defaultValue);
}
void dispose() {

2
packages/settings/lib/src/options/select_option.dart

@ -19,7 +19,7 @@ class SelectOption<T> extends Option<T> {
if (valueStr != null) {
initialValue = _fromString(vs, valueStr);
}
stream.add(initialValue ?? await defaultValue.first);
stream.add(initialValue ?? defaultValue);
}),
);
}

2
packages/settings/lib/src/options/toggle_option.dart

@ -9,7 +9,7 @@ class ToggleOption extends Option<bool> {
super.category,
super.enabled,
}) {
stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue.value);
stream = BehaviorSubject.seeded(storage.getBool(key) ?? defaultValue);
}
@override

6
packages/settings/lib/src/widgets/option_builder.dart

@ -12,13 +12,11 @@ class OptionBuilder<T> extends StatelessWidget {
@override
Widget build(final BuildContext context) => StreamBuilder<T?>(
stream: option.defaultValue,
builder: (final context, final defaultValueSnapshot) => StreamBuilder<T?>(
stream: option.stream,
initialData: option.defaultValue,
builder: (final context, final valueSnapshot) => builder(
context,
valueSnapshot.data ?? defaultValueSnapshot.data,
),
valueSnapshot.data,
),
);
}

Loading…
Cancel
Save