Improvements and fixes

This commit is contained in:
Juan Gilsanz Polo 2023-10-07 21:51:29 +02:00
parent 2389e34571
commit 36bd7acfed
2 changed files with 59 additions and 77 deletions

View file

@ -68,7 +68,7 @@ String? validateAddress({
}) { }) {
if (value != null && value != '') { if (value != null && value != '') {
RegExp ipAddress = RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$'); RegExp ipAddress = RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$');
RegExp domain = RegExp(r'^(([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+)|((\w|-)+)$'); RegExp domain = RegExp(r'^(([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+)$');
if (ipAddress.hasMatch(value) == true || domain.hasMatch(value) == true) { if (ipAddress.hasMatch(value) == true || domain.hasMatch(value) == true) {
return null; return null;
} }

View file

@ -6,6 +6,8 @@ import 'package:uuid/uuid.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/widgets/add_server/form_text_field.dart'; import 'package:adguard_home_manager/widgets/add_server/form_text_field.dart';
import 'package:adguard_home_manager/widgets/section_label.dart';
import 'package:adguard_home_manager/widgets/custom_switch_list_tile.dart';
import 'package:adguard_home_manager/widgets/add_server/add_server_functions.dart'; import 'package:adguard_home_manager/widgets/add_server/add_server_functions.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart'; import 'package:adguard_home_manager/providers/app_config_provider.dart';
@ -67,23 +69,6 @@ class _AddServerModalState extends State<AddServerModal> {
bool isConnecting = false; bool isConnecting = false;
Widget sectionLabel(String label) {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 24
),
child: Text(
label,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.primary
),
),
);
}
@override @override
void initState() { void initState() {
if (widget.server != null) { if (widget.server != null) {
@ -166,12 +151,13 @@ class _AddServerModalState extends State<AddServerModal> {
cancelConnecting(); cancelConnecting();
appConfigProvider.addLog(result['log']); appConfigProvider.addLog(result['log']);
if (mounted) { if (mounted) {
return showSnacbkar( showSnacbkar(
appConfigProvider: appConfigProvider, appConfigProvider: appConfigProvider,
label: getErrorMessage(result['result']), label: getErrorMessage(result['result']),
color: Colors.red color: Colors.red
); );
} }
return;
} }
if (serverObj.user != null && serverObj.password != null) { if (serverObj.user != null && serverObj.password != null) {
@ -181,7 +167,7 @@ class _AddServerModalState extends State<AddServerModal> {
final serverCreated = await serversProvider.createServer(serverObj); final serverCreated = await serversProvider.createServer(serverObj);
// If something goes wrong when saving the connection on the db // If something goes wrong when saving the connection on the db
if (serverCreated == null) { if (serverCreated != null) {
if (mounted) setState(() => isConnecting = false); if (mounted) setState(() => isConnecting = false);
appConfigProvider.addLog( appConfigProvider.addLog(
AppLog( AppLog(
@ -197,6 +183,7 @@ class _AddServerModalState extends State<AddServerModal> {
color: Colors.red color: Colors.red
); );
} }
return;
} }
statusProvider.setServerStatusLoad(LoadStatus.loading); statusProvider.setServerStatusLoad(LoadStatus.loading);
@ -208,6 +195,7 @@ class _AddServerModalState extends State<AddServerModal> {
appConfigProvider.addLog(serverStatus['log']); appConfigProvider.addLog(serverStatus['log']);
statusProvider.setServerStatusLoad(LoadStatus.error); statusProvider.setServerStatusLoad(LoadStatus.error);
Navigator.pop(context); Navigator.pop(context);
return;
} }
// If everything is successful // If everything is successful
@ -223,6 +211,7 @@ class _AddServerModalState extends State<AddServerModal> {
else { else {
Navigator.pop(context); Navigator.pop(context);
} }
return;
} }
void edit() async { void edit() async {
@ -250,12 +239,13 @@ class _AddServerModalState extends State<AddServerModal> {
cancelConnecting(); cancelConnecting();
appConfigProvider.addLog(result['log']); appConfigProvider.addLog(result['log']);
if (mounted) { if (mounted) {
return showSnacbkar( showSnacbkar(
appConfigProvider: appConfigProvider, appConfigProvider: appConfigProvider,
label: getErrorMessage(result['result']), label: getErrorMessage(result['result']),
color: Colors.red color: Colors.red
); );
} }
return;
} }
if (serverObj.user != null && serverObj.password != null) { if (serverObj.user != null && serverObj.password != null) {
@ -264,7 +254,7 @@ class _AddServerModalState extends State<AddServerModal> {
final serverSaved = await serversProvider.editServer(serverObj); final serverSaved = await serversProvider.editServer(serverObj);
// If something goes wrong when saving the connection on the db // If something goes wrong when saving the connection on the db
if (serverSaved == null) { if (serverSaved != null) {
if (mounted) setState(() => isConnecting = false); if (mounted) setState(() => isConnecting = false);
appConfigProvider.addLog( appConfigProvider.addLog(
AppLog( AppLog(
@ -280,6 +270,7 @@ class _AddServerModalState extends State<AddServerModal> {
color: Colors.red color: Colors.red
); );
} }
return;
} }
// If everything is successful // If everything is successful
@ -294,7 +285,8 @@ class _AddServerModalState extends State<AddServerModal> {
} }
else { else {
Navigator.pop(context); Navigator.pop(context);
} }
return;
} }
Widget actions() { Widget actions() {
@ -314,7 +306,11 @@ class _AddServerModalState extends State<AddServerModal> {
: () => edit() : () => edit()
: null, : null,
icon: isConnecting icon: isConnecting
? const CircularProgressIndicator() ? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator()
)
: Icon( : Icon(
widget.server == null widget.server == null
? Icons.login_rounded ? Icons.login_rounded
@ -350,7 +346,10 @@ class _AddServerModalState extends State<AddServerModal> {
), ),
), ),
), ),
sectionLabel(AppLocalizations.of(context)!.general), SectionLabel(
label: AppLocalizations.of(context)!.general,
padding: const EdgeInsets.all(24),
),
FormTextField( FormTextField(
label: AppLocalizations.of(context)!.name, label: AppLocalizations.of(context)!.name,
controller: nameController, controller: nameController,
@ -367,7 +366,10 @@ class _AddServerModalState extends State<AddServerModal> {
}, },
isConnecting: isConnecting, isConnecting: isConnecting,
), ),
sectionLabel(AppLocalizations.of(context)!.connection), SectionLabel(
label: AppLocalizations.of(context)!.connection,
padding: const EdgeInsets.all(24),
),
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 24), padding: const EdgeInsets.symmetric(horizontal: 24),
child: SegmentedButton<ConnectionType>( child: SegmentedButton<ConnectionType>(
@ -425,7 +427,10 @@ class _AddServerModalState extends State<AddServerModal> {
}, },
isConnecting: isConnecting, isConnecting: isConnecting,
), ),
sectionLabel(AppLocalizations.of(context)!.authentication), SectionLabel(
label: AppLocalizations.of(context)!.authentication,
padding: const EdgeInsets.all(24),
),
FormTextField( FormTextField(
label: AppLocalizations.of(context)!.username, label: AppLocalizations.of(context)!.username,
controller: userController, controller: userController,
@ -441,58 +446,32 @@ class _AddServerModalState extends State<AddServerModal> {
obscureText: true, obscureText: true,
isConnecting: isConnecting, isConnecting: isConnecting,
), ),
sectionLabel(AppLocalizations.of(context)!.other), SectionLabel(
Material( label: AppLocalizations.of(context)!.other,
color: Colors.transparent, padding: const EdgeInsets.only(
child: InkWell( top: 32,
onTap: widget.server == null left: 24,
? () => setState(() => defaultServer = !defaultServer) bottom: 12
: null,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
AppLocalizations.of(context)!.defaultServer,
style: const TextStyle(
fontSize: 15,
),
),
Switch(
value: defaultServer,
onChanged: widget.server == null
? (value) => setState(() => defaultServer = value)
: null,
)
],
),
),
), ),
), ),
const SizedBox(height: 20), CustomSwitchListTile(
Material( value: defaultServer,
color: Colors.transparent, onChanged: (value) => setState(() => defaultServer = value),
child: InkWell( title: AppLocalizations.of(context)!.defaultServer,
onTap: () => setState(() => homeAssistant = !homeAssistant), disabled: widget.server != null || isConnecting,
child: Padding( padding: const EdgeInsets.symmetric(
padding: const EdgeInsets.symmetric(horizontal: 24), horizontal: 24,
child: Row( vertical: 4
mainAxisAlignment: MainAxisAlignment.spaceBetween, ),
children: [ ),
Text( CustomSwitchListTile(
AppLocalizations.of(context)!.runningHomeAssistant, value: homeAssistant,
style: const TextStyle( onChanged: (value) => setState(() => homeAssistant = value),
fontSize: 15, title: AppLocalizations.of(context)!.runningHomeAssistant,
), disabled: widget.server != null || isConnecting,
), padding: const EdgeInsets.symmetric(
Switch( horizontal: 24,
value: homeAssistant, vertical: 4
onChanged: (value) => setState(() => homeAssistant = value),
)
],
),
),
), ),
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
@ -503,6 +482,9 @@ class _AddServerModalState extends State<AddServerModal> {
return Dialog.fullscreen( return Dialog.fullscreen(
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
leading: CloseButton(
onPressed: () => Navigator.pop(context),
),
title: widget.server != null title: widget.server != null
? Text(AppLocalizations.of(context)!.createConnection) ? Text(AppLocalizations.of(context)!.createConnection)
: Text(AppLocalizations.of(context)!.editConnection), : Text(AppLocalizations.of(context)!.editConnection),