Create management portal in Flutter #39
This commit is contained in:
parent
9d083a1d93
commit
45ea5bab8f
@ -1,8 +1,9 @@
|
|||||||
const String addMountTitle = 'New Mount Settings';
|
const addMountTitle = 'New Mount Settings';
|
||||||
const String appTitle = 'Repertory Management Portal';
|
const appTitle = 'Repertory Management Portal';
|
||||||
const databaseTypeList = ['rocksdb', 'sqlite'];
|
const databaseTypeList = ['rocksdb', 'sqlite'];
|
||||||
const downloadTypeList = ['default', 'direct', 'ring_buffer'];
|
const downloadTypeList = ['default', 'direct', 'ring_buffer'];
|
||||||
const eventLevelList = ['critical', 'error', 'warn', 'info', 'debug', 'trace'];
|
const eventLevelList = ['critical', 'error', 'warn', 'info', 'debug', 'trace'];
|
||||||
|
const padding = 15.0;
|
||||||
const protocolTypeList = ['http', 'https'];
|
const protocolTypeList = ['http', 'https'];
|
||||||
const providerTypeList = ['Encrypt', 'Remote', 'S3', 'Sia'];
|
const providerTypeList = ['Encrypt', 'Remote', 'S3', 'Sia'];
|
||||||
const ringBufferSizeList = ['128', '256', '512', '1024', '2048'];
|
const ringBufferSizeList = ['128', '256', '512', '1024', '2048'];
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:repertory/constants.dart' as constants;
|
||||||
|
|
||||||
typedef Validator = bool Function(String);
|
typedef Validator = bool Function(String);
|
||||||
|
|
||||||
bool containsRestrictedChar(String value) {
|
// ignore: prefer_function_declarations_over_variables
|
||||||
const invalidChars = [
|
final Validator noRestrictedChars = (value) {
|
||||||
|
return [
|
||||||
'!',
|
'!',
|
||||||
'"',
|
'"',
|
||||||
'\$',
|
'\$',
|
||||||
'&',
|
'&',
|
||||||
'\'',
|
"'",
|
||||||
'(',
|
'(',
|
||||||
')',
|
')',
|
||||||
'*',
|
'*',
|
||||||
@ -23,10 +25,26 @@ bool containsRestrictedChar(String value) {
|
|||||||
'{',
|
'{',
|
||||||
'}',
|
'}',
|
||||||
'|',
|
'|',
|
||||||
];
|
].firstWhereOrNull((char) => value.contains(char)) ==
|
||||||
return invalidChars.firstWhereOrNull((char) => value.contains(char)) != null;
|
null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ignore: prefer_function_declarations_over_variables
|
||||||
|
final Validator notEmptyValidator = (value) => value.isNotEmpty;
|
||||||
|
|
||||||
|
// ignore: prefer_function_declarations_over_variables
|
||||||
|
final Validator trimNotEmptyValidator = (value) => value.trim().isNotEmpty;
|
||||||
|
|
||||||
|
createUriValidator<Validator>({host, port}) {
|
||||||
|
return (value) =>
|
||||||
|
Uri.tryParse('http://${host ?? value}:${port ?? value}/') != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createHostNameOrIpValidators() => <Validator>[
|
||||||
|
trimNotEmptyValidator,
|
||||||
|
createUriValidator(port: 9000),
|
||||||
|
];
|
||||||
|
|
||||||
Map<String, dynamic> createDefaultSettings(String mountType) {
|
Map<String, dynamic> createDefaultSettings(String mountType) {
|
||||||
switch (mountType) {
|
switch (mountType) {
|
||||||
case 'Encrypt':
|
case 'Encrypt':
|
||||||
@ -80,16 +98,19 @@ String getBaseUri() {
|
|||||||
List<Validator> getSettingValidators(String settingPath) {
|
List<Validator> getSettingValidators(String settingPath) {
|
||||||
switch (settingPath) {
|
switch (settingPath) {
|
||||||
case 'ApiAuth':
|
case 'ApiAuth':
|
||||||
return [(value) => value.isNotEmpty];
|
return [notEmptyValidator];
|
||||||
|
case 'DatabaseType':
|
||||||
|
return [(value) => constants.databaseTypeList.contains(value)];
|
||||||
|
case 'PreferredDownloadType':
|
||||||
|
return [(value) => constants.downloadTypeList.contains(value)];
|
||||||
|
case 'EventLevel':
|
||||||
|
return [(value) => constants.eventLevelList.contains(value)];
|
||||||
case 'EncryptConfig.EncryptionToken':
|
case 'EncryptConfig.EncryptionToken':
|
||||||
return [(value) => value.isNotEmpty];
|
return [notEmptyValidator];
|
||||||
case 'EncryptConfig.Path':
|
case 'EncryptConfig.Path':
|
||||||
return [
|
return [trimNotEmptyValidator, noRestrictedChars];
|
||||||
(value) => value.trim().isNotEmpty,
|
|
||||||
(value) => !containsRestrictedChar(value),
|
|
||||||
];
|
|
||||||
case 'HostConfig.ApiPassword':
|
case 'HostConfig.ApiPassword':
|
||||||
return [(value) => value.isNotEmpty];
|
return [notEmptyValidator];
|
||||||
case 'HostConfig.ApiPort':
|
case 'HostConfig.ApiPort':
|
||||||
return [
|
return [
|
||||||
(value) {
|
(value) {
|
||||||
@ -100,29 +121,32 @@ List<Validator> getSettingValidators(String settingPath) {
|
|||||||
|
|
||||||
return (intValue > 0 && intValue < 65536);
|
return (intValue > 0 && intValue < 65536);
|
||||||
},
|
},
|
||||||
(value) => Uri.tryParse('http://localhost:$value/') != null,
|
createUriValidator(host: 'localhost'),
|
||||||
];
|
];
|
||||||
case 'HostConfig.HostNameOrIp':
|
case 'HostConfig.HostNameOrIp':
|
||||||
return [
|
return createHostNameOrIpValidators();
|
||||||
(value) => value.trim().isNotEmpty,
|
|
||||||
(value) => Uri.tryParse('http://$value:9000/') != null,
|
|
||||||
];
|
|
||||||
case 'HostConfig.Protocol':
|
case 'HostConfig.Protocol':
|
||||||
return [(value) => value == "http" || value == "https"];
|
return [(value) => constants.protocolTypeList.contains(value)];
|
||||||
case 'RemoteConfig.EncryptionToken':
|
case 'RemoteConfig.EncryptionToken':
|
||||||
return [(value) => value.isNotEmpty];
|
return [notEmptyValidator];
|
||||||
|
case 'RemoteConfig.HostNameOrIp':
|
||||||
|
return createHostNameOrIpValidators();
|
||||||
case 'RemoteMount.EncryptionToken':
|
case 'RemoteMount.EncryptionToken':
|
||||||
return [(value) => value.isNotEmpty];
|
return [notEmptyValidator];
|
||||||
|
case 'RemoteMount.HostNameOrIp':
|
||||||
|
return createHostNameOrIpValidators();
|
||||||
|
case 'RingBufferFileSize':
|
||||||
|
return [(value) => constants.ringBufferSizeList.contains(value)];
|
||||||
case 'S3Config.AccessKey':
|
case 'S3Config.AccessKey':
|
||||||
return [(value) => value.trim().isNotEmpty];
|
return [trimNotEmptyValidator];
|
||||||
case 'S3Config.Bucket':
|
case 'S3Config.Bucket':
|
||||||
return [(value) => value.trim().isNotEmpty];
|
return [trimNotEmptyValidator];
|
||||||
case 'S3Config.SecretKey':
|
case 'S3Config.SecretKey':
|
||||||
return [(value) => value.trim().isNotEmpty];
|
return [trimNotEmptyValidator];
|
||||||
case 'S3Config.URL':
|
case 'S3Config.URL':
|
||||||
return [(value) => Uri.tryParse(value) != null];
|
return [trimNotEmptyValidator, (value) => Uri.tryParse(value) != null];
|
||||||
case 'SiaConfig.Bucket':
|
case 'SiaConfig.Bucket':
|
||||||
return [(value) => value.trim().isNotEmpty];
|
return [trimNotEmptyValidator];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
@ -146,18 +170,19 @@ bool validateSettings(
|
|||||||
String? rootKey,
|
String? rootKey,
|
||||||
}) {
|
}) {
|
||||||
settings.forEach((key, value) {
|
settings.forEach((key, value) {
|
||||||
final checkKey = rootKey == null ? key : '$rootKey.$key';
|
final settingKey = rootKey == null ? key : '$rootKey.$key';
|
||||||
if (value is Map) {
|
if (value is Map) {
|
||||||
validateSettings(
|
validateSettings(
|
||||||
value as Map<String, dynamic>,
|
value as Map<String, dynamic>,
|
||||||
failed,
|
failed,
|
||||||
rootKey: checkKey,
|
rootKey: settingKey,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
for (var validator in getSettingValidators(checkKey)) {
|
for (var validator in getSettingValidators(settingKey)) {
|
||||||
if (!validator(value.toString())) {
|
if (validator(value.toString())) {
|
||||||
failed.add(checkKey);
|
continue;
|
||||||
}
|
}
|
||||||
|
failed.add(settingKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/material.dart';
|
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';
|
import 'package:repertory/constants.dart' as constants;
|
||||||
import 'package:repertory/helpers.dart';
|
import 'package:repertory/helpers.dart';
|
||||||
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';
|
||||||
@ -17,8 +18,6 @@ class AddMountScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AddMountScreenState extends State<AddMountScreen> {
|
class _AddMountScreenState extends State<AddMountScreen> {
|
||||||
static const _padding = 15.0;
|
|
||||||
|
|
||||||
Mount? _mount;
|
Mount? _mount;
|
||||||
final _mountNameController = TextEditingController();
|
final _mountNameController = TextEditingController();
|
||||||
String _mountType = "";
|
String _mountType = "";
|
||||||
@ -50,7 +49,7 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(_padding),
|
padding: const EdgeInsets.all(constants.padding),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
@ -58,43 +57,44 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Card(
|
Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(_padding),
|
padding: const EdgeInsets.all(constants.padding),
|
||||||
child: Row(
|
child: Row(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
const Text('Provider Type'),
|
const Text('Provider Type'),
|
||||||
const SizedBox(width: _padding),
|
const SizedBox(width: constants.padding),
|
||||||
DropdownButton<String>(
|
DropdownButton<String>(
|
||||||
value: _mountType,
|
value: _mountType,
|
||||||
onChanged: (mountType) => _handleChange(mountType ?? ''),
|
onChanged: (mountType) => _handleChange(mountType ?? ''),
|
||||||
items:
|
items:
|
||||||
providerTypeList.map<DropdownMenuItem<String>>((
|
constants.providerTypeList
|
||||||
item,
|
.map<DropdownMenuItem<String>>((item) {
|
||||||
) {
|
|
||||||
return DropdownMenuItem<String>(
|
return DropdownMenuItem<String>(
|
||||||
value: item,
|
value: item,
|
||||||
child: Text(item),
|
child: Text(item),
|
||||||
);
|
);
|
||||||
}).toList(),
|
})
|
||||||
|
.toList(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_mountType.isNotEmpty) const SizedBox(height: _padding),
|
if (_mountType.isNotEmpty)
|
||||||
|
const SizedBox(height: constants.padding),
|
||||||
if (_mountType.isNotEmpty)
|
if (_mountType.isNotEmpty)
|
||||||
Card(
|
Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(_padding),
|
padding: const EdgeInsets.all(constants.padding),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
const Text('Configuration Name'),
|
const Text('Configuration Name'),
|
||||||
const SizedBox(width: _padding),
|
const SizedBox(width: constants.padding),
|
||||||
TextField(
|
TextField(
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
controller: _mountNameController,
|
controller: _mountNameController,
|
||||||
@ -112,7 +112,7 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: Card(
|
child: Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(_padding),
|
padding: const EdgeInsets.all(constants.padding),
|
||||||
child: MountSettingsWidget(
|
child: MountSettingsWidget(
|
||||||
isAdd: true,
|
isAdd: true,
|
||||||
mount: _mount!,
|
mount: _mount!,
|
||||||
@ -123,8 +123,15 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_mount != null)
|
if (_mount != null)
|
||||||
ElevatedButton.icon(
|
Builder(
|
||||||
|
builder: (context) {
|
||||||
|
return ElevatedButton.icon(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
final mountList = Provider.of<MountList>(
|
||||||
|
context,
|
||||||
|
listen: false,
|
||||||
|
);
|
||||||
|
|
||||||
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) {
|
||||||
@ -140,7 +147,25 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Provider.of<MountList>(context, listen: false).add(
|
final existingMount = mountList.items.firstWhereOrNull(
|
||||||
|
(item) =>
|
||||||
|
item.name.toLowerCase() ==
|
||||||
|
_mountNameController.text.toLowerCase(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingMount != null) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"'${_mountNameController.text}' already exists",
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mountList.add(
|
||||||
_mountType,
|
_mountType,
|
||||||
_mountNameController.text,
|
_mountNameController.text,
|
||||||
_settings[_mountType]!,
|
_settings[_mountType]!,
|
||||||
@ -150,6 +175,8 @@ class _AddMountScreenState extends State<AddMountScreen> {
|
|||||||
},
|
},
|
||||||
label: const Text('Add'),
|
label: const Text('Add'),
|
||||||
icon: Icon(Icons.add),
|
icon: Icon(Icons.add),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:repertory/constants.dart';
|
import 'package:repertory/constants.dart' as constants;
|
||||||
import 'package:repertory/helpers.dart' show Validator, getSettingValidators;
|
import 'package:repertory/helpers.dart' show Validator, getSettingValidators;
|
||||||
import 'package:repertory/models/mount.dart';
|
import 'package:repertory/models/mount.dart';
|
||||||
import 'package:settings_ui/settings_ui.dart';
|
import 'package:settings_ui/settings_ui.dart';
|
||||||
@ -26,8 +26,6 @@ class MountSettingsWidget extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
||||||
static const _padding = 15.0;
|
|
||||||
|
|
||||||
void _addBooleanSetting(list, root, key, value, isAdvanced) {
|
void _addBooleanSetting(list, root, key, value, isAdvanced) {
|
||||||
if (!isAdvanced || widget.showAdvanced) {
|
if (!isAdvanced || widget.showAdvanced) {
|
||||||
list.add(
|
list.add(
|
||||||
@ -264,7 +262,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
obscuringCharacter: '*',
|
obscuringCharacter: '*',
|
||||||
onChanged: (value) => updatedValue1 = value,
|
onChanged: (value) => updatedValue1 = value,
|
||||||
),
|
),
|
||||||
const SizedBox(height: _padding),
|
const SizedBox(height: constants.padding),
|
||||||
TextField(
|
TextField(
|
||||||
autofocus: false,
|
autofocus: false,
|
||||||
controller: TextEditingController(text: updatedValue2),
|
controller: TextEditingController(text: updatedValue2),
|
||||||
@ -409,7 +407,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
widget.settings,
|
widget.settings,
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
databaseTypeList,
|
constants.databaseTypeList,
|
||||||
Icons.dataset,
|
Icons.dataset,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
@ -456,7 +454,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
widget.settings,
|
widget.settings,
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
eventLevelList,
|
constants.eventLevelList,
|
||||||
Icons.event,
|
Icons.event,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
@ -528,7 +526,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
widget.settings,
|
widget.settings,
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
downloadTypeList,
|
constants.downloadTypeList,
|
||||||
Icons.download,
|
Icons.download,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
@ -553,7 +551,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
widget.settings,
|
widget.settings,
|
||||||
key,
|
key,
|
||||||
value,
|
value,
|
||||||
ringBufferSizeList,
|
constants.ringBufferSizeList,
|
||||||
512,
|
512,
|
||||||
Icons.animation,
|
Icons.animation,
|
||||||
false,
|
false,
|
||||||
@ -733,7 +731,7 @@ class _MountSettingsWidgetState extends State<MountSettingsWidget> {
|
|||||||
widget.settings[key],
|
widget.settings[key],
|
||||||
subKey,
|
subKey,
|
||||||
subValue,
|
subValue,
|
||||||
protocolTypeList,
|
constants.protocolTypeList,
|
||||||
Icons.http,
|
Icons.http,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user