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