[ui] Implement provider test button #49
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

This commit is contained in:
Scott E. Graves 2025-04-24 10:48:13 -05:00
parent 3c8e1d5986
commit 45a46650a2
2 changed files with 70 additions and 53 deletions

View File

@ -21,6 +21,8 @@ class _AddMountScreenState extends State<AddMountScreen> {
Mount? _mount; Mount? _mount;
final _mountNameController = TextEditingController(); final _mountNameController = TextEditingController();
String _mountType = ""; String _mountType = "";
bool _allowAdd = false;
final Map<String, Map<String, dynamic>> _settings = { final Map<String, Map<String, dynamic>> _settings = {
"": {}, "": {},
"Encrypt": createDefaultSettings("Encrypt"), "Encrypt": createDefaultSettings("Encrypt"),
@ -123,6 +125,7 @@ class _AddMountScreenState extends State<AddMountScreen> {
child: MountSettingsWidget( child: MountSettingsWidget(
isAdd: true, isAdd: true,
mount: _mount!, mount: _mount!,
onChanged: () => setState(() => _allowAdd = false),
settings: _settings[_mountType]!, settings: _settings[_mountType]!,
showAdvanced: false, showAdvanced: false,
), ),
@ -132,68 +135,70 @@ class _AddMountScreenState extends State<AddMountScreen> {
const SizedBox(height: constants.padding), const SizedBox(height: constants.padding),
Row( Row(
children: [ children: [
ElevatedButton.icon( if (_allowAdd) ...[
label: const Text('Add'), ElevatedButton.icon(
icon: const Icon(Icons.add), label: const Text('Add'),
onPressed: () async { icon: const Icon(Icons.add),
final mountList = Provider.of<MountList>(context); onPressed: () async {
final mountList = Provider.of<MountList>(context);
List<String> failed = []; List<String> failed = [];
if (!validateSettings( if (!validateSettings(
_settings[_mountType]!, _settings[_mountType]!,
failed, failed,
)) { )) {
for (var key in failed) { for (var key in failed) {
displayErrorMessage( displayErrorMessage(
context, context,
"Setting '$key' is not valid", "Setting '$key' is not valid",
); );
}
return;
} }
return;
}
if (mountList.hasConfigName( if (mountList.hasConfigName(
_mountNameController.text, _mountNameController.text,
)) { )) {
return displayErrorMessage(
context,
"Configuration name '${_mountNameController.text}' already exists",
);
}
if (_mountType == "Sia" || _mountType == "S3") {
final bucket =
_settings[_mountType]!["${_mountType}Config"]["Bucket"]
as String;
if (mountList.hasBucketName(_mountType, bucket)) {
return displayErrorMessage( return displayErrorMessage(
context, context,
"Bucket '$bucket' already exists", "Configuration name '${_mountNameController.text}' already exists",
); );
} }
}
final success = await mountList.add( if (_mountType == "Sia" || _mountType == "S3") {
_mountType, final bucket =
_mountType == 'Remote' _settings[_mountType]!["${_mountType}Config"]["Bucket"]
? '${_settings[_mountType]!['RemoteConfig']['HostNameOrIp']}_${_settings[_mountType]!['RemoteConfig']['ApiPort']}' as String;
: _mountNameController.text, if (mountList.hasBucketName(_mountType, bucket)) {
_settings[_mountType]!, return displayErrorMessage(
); context,
"Bucket '$bucket' already exists",
);
}
}
if (!success || !context.mounted) { final success = await mountList.add(
return; _mountType,
} _mountType == 'Remote'
? '${_settings[_mountType]!['RemoteConfig']['HostNameOrIp']}_${_settings[_mountType]!['RemoteConfig']['ApiPort']}'
: _mountNameController.text,
_settings[_mountType]!,
);
Navigator.pop(context); if (!success || !context.mounted) {
}, return;
), }
const SizedBox(width: constants.padding),
ElevatedButton.icon( Navigator.pop(context);
label: const Text('Test'), },
icon: const Icon(Icons.check), ),
onPressed: _handleProviderTest, ],
), if (!_allowAdd)
ElevatedButton.icon(
label: const Text('Test'),
icon: const Icon(Icons.check),
onPressed: _handleProviderTest,
),
], ],
), ),
], ],
@ -237,14 +242,20 @@ class _AddMountScreenState extends State<AddMountScreen> {
return; return;
} }
if (_allowAdd) {
return;
}
final success = await _mount!.test(); final success = await _mount!.test();
if (!mounted) { if (!mounted) {
return; return;
} }
setState(() => _allowAdd = success);
displayErrorMessage( displayErrorMessage(
context, context,
success ? "Success!" : "Provider settings are invalid!", success ? "Success" : "Provider settings are invalid!",
); );
} }

View File

@ -18,9 +18,11 @@ class MountSettingsWidget extends StatefulWidget {
final bool showAdvanced; final bool showAdvanced;
final Mount mount; final Mount mount;
final Map<String, dynamic> settings; final Map<String, dynamic> settings;
final Function? onChanged;
const MountSettingsWidget({ const MountSettingsWidget({
super.key, super.key,
this.isAdd = false, this.isAdd = false,
this.onChanged,
required this.mount, required this.mount,
required this.settings, required this.settings,
required this.showAdvanced, required this.showAdvanced,
@ -1020,6 +1022,10 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
return; return;
} }
if (widget.onChanged != null) {
widget.onChanged!();
}
super.setState(fn); super.setState(fn);
} }
} }