Made defaultvallue part of formitem

This commit is contained in:
Imran Remtulla
2022-12-19 19:48:37 -05:00
parent 1fe9e4f91e
commit afc8e41171
7 changed files with 61 additions and 72 deletions

View File

@@ -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',

View File

@@ -16,6 +16,7 @@ class GeneratedFormItem {
late List<Widget> belowWidgets;
late String? hint;
late List<MapEntry<String, String>>? 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<List<GeneratedFormItem>> items;
final OnValueChanges onValueChanges;
final Map<String, String> defaultValues;
@override
State<GeneratedForm> createState() => _GeneratedFormState();
@@ -82,7 +80,7 @@ class _GeneratedFormState extends State<GeneratedForm> {
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 ?? '';
}
}

View File

@@ -59,8 +59,7 @@ class _GeneratedFormModalState extends State<GeneratedFormModal> {
this.valid = valid;
});
}
},
defaultValues: widget.defaultValues)
})
]),
actions: [
TextButton(

View File

@@ -43,8 +43,10 @@ class _AddAppPageState extends State<AddAppPage> {
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<AddAppPage> {
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<AddAppPage> {
.isNotEmpty &&
!sourceSpecificDataIsValid) ||
(pickedSource!
.additionalAppSpecificSourceAgnosticDefaults
.additionalAppSpecificSourceAgnosticFormItems
.isNotEmpty &&
!otherAdditionalDataIsValid)
? null
@@ -250,9 +251,6 @@ class _AddAppPageState extends State<AddAppPage> {
values['searchSomeSources']!.trim();
});
}
},
defaultValues: const {
'searchSomeSources': ''
}),
),
const SizedBox(
@@ -309,7 +307,7 @@ class _AddAppPageState extends State<AddAppPage> {
],
),
if (pickedSource != null &&
(pickedSource!.additionalSourceAppSpecificDefaults
(pickedSource!.additionalSourceAppSpecificFormItems
.isNotEmpty ||
pickedSource!
.additionalAppSpecificSourceAgnosticFormItems
@@ -346,11 +344,9 @@ class _AddAppPageState extends State<AddAppPage> {
sourceSpecificDataIsValid = valid;
});
}
},
defaultValues: pickedSource!
.additionalSourceAppSpecificDefaults),
}),
if (pickedSource!
.additionalAppSpecificSourceAgnosticDefaults
.additionalAppSpecificSourceAgnosticFormItems
.isNotEmpty)
const SizedBox(
height: 8,
@@ -370,9 +366,7 @@ class _AddAppPageState extends State<AddAppPage> {
otherAdditionalDataIsValid = valid;
});
}
},
defaultValues: pickedSource!
.additionalAppSpecificSourceAgnosticDefaults),
}),
],
)
else

View File

@@ -217,8 +217,9 @@ class _AppPageState extends State<AppPage> {
.additionalSourceAppSpecificFormItems,
defaultValues: app != null
? app.app.additionalData
: source
.additionalSourceAppSpecificDefaults);
: getDefaultValuesFromFormItems(
source
.additionalSourceAppSpecificFormItems));
}).then((values) {
if (app != null && values != null) {
var changedApp = app.app;

View File

@@ -147,12 +147,7 @@ class _SettingsPageState extends State<SettingsPage> {
settingsProvider.setSettingString(key, value);
});
}
},
defaultValues: Map.fromEntries(
e.additionalSourceSpecificSettingFormItems.map((e) {
return MapEntry(
e.key, settingsProvider.getSettingString(e.key) ?? '');
})));
});
} else {
return Container();
}

View File

@@ -83,9 +83,9 @@ class App {
: List<String>.from(jsonDecode(json['apkUrls'])),
json['preferredApkIndex'] == null ? 0 : json['preferredApkIndex'] as int,
json['additionalData'] == null
? SourceProvider()
? getDefaultValuesFromFormItems(SourceProvider()
.getSource(json['url'])
.additionalSourceAppSpecificDefaults
.additionalSourceAppSpecificFormItems)
: Map<String, String>.from(jsonDecode(json['additionalData'])),
json['lastUpdateCheck'] == null
? null
@@ -141,6 +141,12 @@ List<String> getLinksFromParsedHTML(
.map((e) => '$prependToLinks${e.attributes['href']!}')
.toList();
getDefaultValuesFromFormItems(List<List<GeneratedFormItem>> 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<List<GeneratedFormItem>> additionalSourceAppSpecificFormItems = [];
Map<String, String> additionalSourceAppSpecificDefaults = {};
// Some additional data may be needed for Apps regardless of Source
final List<GeneratedFormItem> additionalAppSpecificSourceAgnosticFormItems = [
@@ -175,10 +180,6 @@ class AppSource {
label: 'Do not attempt version detection', // TODO
type: FormItemType.bool)
];
final Map<String, String> additionalAppSpecificSourceAgnosticDefaults = {
'trackOnlyFormItemKey': '',
'noVersionDetectionKey': ''
};
// Some Sources may have additional settings at the Source level (not specific to Apps) - these use SettingsProvider
List<GeneratedFormItem> 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(<String, dynamic>{url: e});
}