refactor
This commit is contained in:
parent
2ff72eebce
commit
ca8de7f87d
@ -1,5 +1,6 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:repertory/constants.dart' as constants;
|
||||
|
||||
typedef Validator = bool Function(String);
|
||||
@ -95,6 +96,16 @@ Map<String, dynamic> createDefaultSettings(String mountType) {
|
||||
return {};
|
||||
}
|
||||
|
||||
void displayErrorMessage(context, text) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(text, textAlign: TextAlign.center)));
|
||||
}
|
||||
|
||||
String formatMountName(String type, String name) {
|
||||
if (type == 'remote') {
|
||||
return name.replaceAll('_', ':');
|
||||
|
@ -137,28 +137,19 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
||||
List<String> failed = [];
|
||||
if (!validateSettings(_settings[_mountType]!, failed)) {
|
||||
for (var key in failed) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Setting '$key' is not valid",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
displayErrorMessage(
|
||||
context,
|
||||
"Setting '$key' is not valid",
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (mountList.hasConfigName(_mountNameController.text)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Configuration name '${_mountNameController.text}' already exists",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Configuration name '${_mountNameController.text}' already exists",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mountType == "Sia" || _mountType == "S3") {
|
||||
@ -166,15 +157,10 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
||||
_settings[_mountType]!["${_mountType}Config"]["Bucket"]
|
||||
as String;
|
||||
if (mountList.hasBucketName(_mountType, bucket)) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Bucket '$bucket' already exists",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Bucket '$bucket' already exists",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:repertory/constants.dart' as constants;
|
||||
import 'package:repertory/helpers.dart' show Validator, getSettingValidators;
|
||||
import 'package:repertory/helpers.dart'
|
||||
show Validator, displayErrorMessage, getSettingValidators;
|
||||
import 'package:repertory/models/mount.dart';
|
||||
import 'package:repertory/models/mount_list.dart';
|
||||
import 'package:settings_ui/settings_ui.dart';
|
||||
@ -83,15 +84,10 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
(validator) => !validator(updatedValue),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Setting '$key' is not valid",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Setting '$key' is not valid",
|
||||
);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
root[key] = int.parse(updatedValue);
|
||||
@ -218,30 +214,20 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
child: const Text('OK'),
|
||||
onPressed: () {
|
||||
if (updatedValue1 != updatedValue2) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Setting '$key' does not match",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Setting '$key' does not match",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final result = validators.firstWhereOrNull(
|
||||
(validator) => !validator(updatedValue1),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Setting '$key' is not valid",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Setting '$key' is not valid",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
@ -317,15 +303,10 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||
(validator) => !validator(updatedValue),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Setting '$key' is not valid",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Setting '$key' is not valid",
|
||||
);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
root[key] = updatedValue;
|
||||
|
@ -92,19 +92,7 @@ class _MountWidgetState extends State<MountWidget> {
|
||||
}
|
||||
|
||||
if (!isMounted && location == null) {
|
||||
if (!context.mounted) {
|
||||
return cleanup();
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text(
|
||||
"Mount location is not set",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
displayErrorMessage(context, "Mount location is not set");
|
||||
return cleanup();
|
||||
}
|
||||
|
||||
@ -117,17 +105,7 @@ class _MountWidgetState extends State<MountWidget> {
|
||||
return cleanup();
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(
|
||||
constants.navigatorKey.currentContext!,
|
||||
).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text(
|
||||
"Mount location not found",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
displayErrorMessage(context, "Mount location not found");
|
||||
return cleanup();
|
||||
}
|
||||
: null;
|
||||
@ -175,15 +153,10 @@ class _MountWidgetState extends State<MountWidget> {
|
||||
(validator) => !validator(currentLocation ?? ''),
|
||||
);
|
||||
if (result != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: const Text(
|
||||
"Mount location is not valid",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
return displayErrorMessage(
|
||||
context,
|
||||
"Mount location is not valid",
|
||||
);
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop(currentLocation);
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user