From afc8e4117115c023ec7e2c3c37715634e87639fe Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Mon, 19 Dec 2022 19:48:37 -0500 Subject: [PATCH] Made defaultvallue part of formitem --- lib/app_sources/github.dart | 14 ++--- lib/components/generated_form.dart | 12 ++-- lib/components/generated_form_modal.dart | 3 +- lib/pages/add_app.dart | 72 +++++++++++------------- lib/pages/app.dart | 5 +- lib/pages/settings.dart | 7 +-- lib/providers/source_provider.dart | 20 ++++--- 7 files changed, 61 insertions(+), 72 deletions(-) diff --git a/lib/app_sources/github.dart b/lib/app_sources/github.dart index ed8f007..a77a2fa 100644 --- a/lib/app_sources/github.dart +++ b/lib/app_sources/github.dart @@ -49,20 +49,18 @@ class GitHub extends AppSource { ]) ]; - additionalSourceAppSpecificDefaults = { - 'includePrereleases': 'true', - 'fallbackToOlderReleases': 'true', - 'filterReleaseTitlesByRegEx': '' - }; - additionalSourceAppSpecificFormItems = [ [ GeneratedFormItem('includePrereleases', - label: tr('includePrereleases'), type: FormItemType.bool) + label: tr('includePrereleases'), + type: FormItemType.bool, + defaultValue: 'true') ], [ GeneratedFormItem('fallbackToOlderReleases', - label: tr('fallbackToOlderReleases'), type: FormItemType.bool) + label: tr('fallbackToOlderReleases'), + type: FormItemType.bool, + defaultValue: 'true') ], [ GeneratedFormItem('filterReleaseTitlesByRegEx', diff --git a/lib/components/generated_form.dart b/lib/components/generated_form.dart index 9bf76ed..d927371 100644 --- a/lib/components/generated_form.dart +++ b/lib/components/generated_form.dart @@ -16,6 +16,7 @@ class GeneratedFormItem { late List belowWidgets; late String? hint; late List>? opts; + late String? defaultValue; GeneratedFormItem(this.key, {this.label = 'Input', @@ -25,7 +26,8 @@ class GeneratedFormItem { this.additionalValidators = const [], this.belowWidgets = const [], this.hint, - this.opts}) { + this.opts, + this.defaultValue}) { if (type != FormItemType.string) { required = false; } @@ -34,14 +36,10 @@ class GeneratedFormItem { class GeneratedForm extends StatefulWidget { const GeneratedForm( - {super.key, - required this.items, - required this.onValueChanges, - required this.defaultValues}); + {super.key, required this.items, required this.onValueChanges}); final List> items; final OnValueChanges onValueChanges; - final Map defaultValues; @override State createState() => _GeneratedFormState(); @@ -82,7 +80,7 @@ class _GeneratedFormState extends State { int j = 0; for (var row in widget.items) { for (var e in row) { - values[e.key] = widget.defaultValues[e.key] ?? e.opts?.first.key ?? ''; + values[e.key] = e.defaultValue ?? e.opts?.first.key ?? ''; } } diff --git a/lib/components/generated_form_modal.dart b/lib/components/generated_form_modal.dart index 66afdd8..772eb59 100644 --- a/lib/components/generated_form_modal.dart +++ b/lib/components/generated_form_modal.dart @@ -59,8 +59,7 @@ class _GeneratedFormModalState extends State { this.valid = valid; }); } - }, - defaultValues: widget.defaultValues) + }) ]), actions: [ TextButton( diff --git a/lib/pages/add_app.dart b/lib/pages/add_app.dart index a3659d4..4e2809e 100644 --- a/lib/pages/add_app.dart +++ b/lib/pages/add_app.dart @@ -43,8 +43,10 @@ class _AddAppPageState extends State { var source = valid ? sourceProvider.getSource(userInput) : null; if (pickedSource.runtimeType != source.runtimeType) { pickedSource = source; - sourceSpecificAdditionalData = - source != null ? source.additionalSourceAppSpecificDefaults : {}; + sourceSpecificAdditionalData = source != null + ? getDefaultValuesFromFormItems( + source.additionalSourceAppSpecificFormItems) + : {}; sourceSpecificDataIsValid = source != null ? !sourceProvider.ifSourceAppsRequireAdditionalData(source) : true; @@ -170,34 +172,33 @@ class _AddAppPageState extends State { Expanded( child: GeneratedForm( items: [ - [ - GeneratedFormItem('appSourceURL', - label: tr('appSourceURL'), - additionalValidators: [ - (value) { - try { - sourceProvider - .getSource(value ?? '') - .standardizeURL( - preStandardizeUrl( - value ?? '')); - } catch (e) { - return e is String - ? e - : e is ObtainiumError - ? e.toString() - : tr('error'); - } - return null; - } - ]) - ] - ], + [ + GeneratedFormItem('appSourceURL', + label: tr('appSourceURL'), + additionalValidators: [ + (value) { + try { + sourceProvider + .getSource(value ?? '') + .standardizeURL( + preStandardizeUrl( + value ?? '')); + } catch (e) { + return e is String + ? e + : e is ObtainiumError + ? e.toString() + : tr('error'); + } + return null; + } + ]) + ] + ], onValueChanges: (values, valid, isBuilding) { changeUserInput(values['appSourceURL']!, valid, isBuilding); - }, - defaultValues: const {'appSourceURL': ''})), + })), const SizedBox( width: 16, ), @@ -211,7 +212,7 @@ class _AddAppPageState extends State { .isNotEmpty && !sourceSpecificDataIsValid) || (pickedSource! - .additionalAppSpecificSourceAgnosticDefaults + .additionalAppSpecificSourceAgnosticFormItems .isNotEmpty && !otherAdditionalDataIsValid) ? null @@ -250,9 +251,6 @@ class _AddAppPageState extends State { values['searchSomeSources']!.trim(); }); } - }, - defaultValues: const { - 'searchSomeSources': '' }), ), const SizedBox( @@ -309,7 +307,7 @@ class _AddAppPageState extends State { ], ), if (pickedSource != null && - (pickedSource!.additionalSourceAppSpecificDefaults + (pickedSource!.additionalSourceAppSpecificFormItems .isNotEmpty || pickedSource! .additionalAppSpecificSourceAgnosticFormItems @@ -346,11 +344,9 @@ class _AddAppPageState extends State { sourceSpecificDataIsValid = valid; }); } - }, - defaultValues: pickedSource! - .additionalSourceAppSpecificDefaults), + }), if (pickedSource! - .additionalAppSpecificSourceAgnosticDefaults + .additionalAppSpecificSourceAgnosticFormItems .isNotEmpty) const SizedBox( height: 8, @@ -370,9 +366,7 @@ class _AddAppPageState extends State { otherAdditionalDataIsValid = valid; }); } - }, - defaultValues: pickedSource! - .additionalAppSpecificSourceAgnosticDefaults), + }), ], ) else diff --git a/lib/pages/app.dart b/lib/pages/app.dart index f5581c2..3d4aae1 100644 --- a/lib/pages/app.dart +++ b/lib/pages/app.dart @@ -217,8 +217,9 @@ class _AppPageState extends State { .additionalSourceAppSpecificFormItems, defaultValues: app != null ? app.app.additionalData - : source - .additionalSourceAppSpecificDefaults); + : getDefaultValuesFromFormItems( + source + .additionalSourceAppSpecificFormItems)); }).then((values) { if (app != null && values != null) { var changedApp = app.app; diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index cffb56c..279f6f9 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -147,12 +147,7 @@ class _SettingsPageState extends State { settingsProvider.setSettingString(key, value); }); } - }, - defaultValues: Map.fromEntries( - e.additionalSourceSpecificSettingFormItems.map((e) { - return MapEntry( - e.key, settingsProvider.getSettingString(e.key) ?? ''); - }))); + }); } else { return Container(); } diff --git a/lib/providers/source_provider.dart b/lib/providers/source_provider.dart index f6be78e..8d123ea 100644 --- a/lib/providers/source_provider.dart +++ b/lib/providers/source_provider.dart @@ -83,9 +83,9 @@ class App { : List.from(jsonDecode(json['apkUrls'])), json['preferredApkIndex'] == null ? 0 : json['preferredApkIndex'] as int, json['additionalData'] == null - ? SourceProvider() + ? getDefaultValuesFromFormItems(SourceProvider() .getSource(json['url']) - .additionalSourceAppSpecificDefaults + .additionalSourceAppSpecificFormItems) : Map.from(jsonDecode(json['additionalData'])), json['lastUpdateCheck'] == null ? null @@ -141,6 +141,12 @@ List getLinksFromParsedHTML( .map((e) => '$prependToLinks${e.attributes['href']!}') .toList(); +getDefaultValuesFromFormItems(List> items) { + Map.fromEntries(items + .map((row) => row.map((el) => MapEntry(el.key, el.defaultValue ?? ''))) + .reduce((value, element) => [...value, ...element])); +} + class AppSource { String? host; late String name; @@ -162,7 +168,6 @@ class AppSource { // Different Sources may need different kinds of additional data for Apps List> additionalSourceAppSpecificFormItems = []; - Map additionalSourceAppSpecificDefaults = {}; // Some additional data may be needed for Apps regardless of Source final List additionalAppSpecificSourceAgnosticFormItems = [ @@ -175,10 +180,6 @@ class AppSource { label: 'Do not attempt version detection', // TODO type: FormItemType.bool) ]; - final Map additionalAppSpecificSourceAgnosticDefaults = { - 'trackOnlyFormItemKey': '', - 'noVersionDetectionKey': '' - }; // Some Sources may have additional settings at the Source level (not specific to Apps) - these use SettingsProvider List additionalSourceSpecificSettingFormItems = []; @@ -332,7 +333,10 @@ class SourceProvider { try { var source = getSource(url); apps.add(await getApp( - source, url, source.additionalSourceAppSpecificDefaults)); + source, + url, + getDefaultValuesFromFormItems( + source.additionalSourceAppSpecificFormItems))); } catch (e) { errors.addAll({url: e}); }