From 5c9e0e99b9783aac64f71ed574a43ce6a1e795e4 Mon Sep 17 00:00:00 2001 From: Juan Gilsanz Polo Date: Thu, 6 Oct 2022 00:47:35 +0200 Subject: [PATCH] Updated block client modal --- lib/l10n/app_en.arb | 3 +- lib/l10n/app_es.arb | 3 +- lib/screens/clients/add_client_modal.dart | 198 -------------------- lib/screens/clients/block_client_modal.dart | 125 ++++++++++++ lib/screens/clients/fab.dart | 52 ++--- 5 files changed, 156 insertions(+), 225 deletions(-) delete mode 100644 lib/screens/clients/add_client_modal.dart create mode 100644 lib/screens/clients/block_client_modal.dart diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 063bd5b..0c01bdc 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -169,5 +169,6 @@ "hideZeroValuesDescription": "On homescreen, hide blocks with zero value", "webAdminPanel": "Web admin. panel", "visitGooglePlay": "Visit Google Play page", - "gitHub": "App code available on GitHub" + "gitHub": "App code available on GitHub", + "blockClient": "Block client" } \ No newline at end of file diff --git a/lib/l10n/app_es.arb b/lib/l10n/app_es.arb index d48d952..e4b1c53 100644 --- a/lib/l10n/app_es.arb +++ b/lib/l10n/app_es.arb @@ -169,5 +169,6 @@ "hideZeroValuesDescription": "En la pantalla de inicio, oculta bloqueos con valor cero", "webAdminPanel": "Panel de admin. web", "visitGooglePlay": "Visita la página de Google Play", - "gitHub": "Código de la app disponible en GitHub" + "gitHub": "Código de la app disponible en GitHub", + "blockClient": "Bloquear cliente" } \ No newline at end of file diff --git a/lib/screens/clients/add_client_modal.dart b/lib/screens/clients/add_client_modal.dart deleted file mode 100644 index 2a377e9..0000000 --- a/lib/screens/clients/add_client_modal.dart +++ /dev/null @@ -1,198 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_gen/gen_l10n/app_localizations.dart'; - -class AddClientModal extends StatefulWidget { - final String list; - final void Function(String, String) onConfirm; - - const AddClientModal({ - Key? key, - required this.list, - required this.onConfirm - }) : super(key: key); - - @override - State createState() => _AddClientModalState(); -} - -class _AddClientModalState extends State { - String list = ''; - - TextEditingController ipController = TextEditingController(); - String? ipError; - - @override - void initState() { - list = widget.list; - super.initState(); - } - - void validateIp(String value) { - RegExp ipAddress = RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$'); - if (ipAddress.hasMatch(value) == true) { - setState(() => ipError = null); - } - else { - setState(() => ipError = AppLocalizations.of(context)!.ipNotValid); - } - } - - bool checkValidValues() { - if ( - (list == 'allowed' || - list == 'blocked') && - ipController.text != '' && - ipError == null - ) { - return true; - } - else { - return false; - } - } - - @override - Widget build(BuildContext context) { - return AlertDialog( - title: Column( - children: [ - const Icon( - Icons.add, - size: 26, - ), - const SizedBox(height: 20), - Text( - AppLocalizations.of(context)!.addClient, - ) - ], - ), - content: Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const SizedBox(height: 10), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Material( - borderRadius: const BorderRadius.only( - topLeft: Radius.circular(15), - bottomLeft: Radius.circular(15) - ), - color: Colors.transparent, - child: InkWell( - borderRadius: const BorderRadius.only( - topLeft: Radius.circular(15), - bottomLeft: Radius.circular(15) - ), - onTap: () => setState(() => list = 'allowed'), - child: AnimatedContainer( - padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10), - duration: const Duration(milliseconds: 200), - curve: Curves.easeInOut, - decoration: BoxDecoration( - borderRadius: const BorderRadius.only( - topLeft: Radius.circular(15), - bottomLeft: Radius.circular(15) - ), - border: Border.all( - color: Theme.of(context).primaryColor - ), - color: list == 'allowed' - ? Theme.of(context).primaryColor - : Theme.of(context).primaryColor.withOpacity(0.05) - ), - child: Text( - AppLocalizations.of(context)!.allowed, - style: TextStyle( - color: list == 'allowed' - ? Colors.white - : null - ), - ), - ), - ), - ), - Material( - borderRadius: const BorderRadius.only( - topRight: Radius.circular(15), - bottomRight: Radius.circular(15) - ), - color: Colors.transparent, - child: InkWell( - borderRadius: const BorderRadius.only( - topRight: Radius.circular(15), - bottomRight: Radius.circular(15) - ), - onTap: () => setState(() => list = 'blocked'), - child: AnimatedContainer( - padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10), - duration: const Duration(milliseconds: 200), - curve: Curves.easeInOut, - decoration: BoxDecoration( - borderRadius: const BorderRadius.only( - topRight: Radius.circular(15), - bottomRight: Radius.circular(15) - ), - border: Border.all( - color: Theme.of(context).primaryColor - ), - color: list == 'blocked' - ? Theme.of(context).primaryColor - : Theme.of(context).primaryColor.withOpacity(0.05) - ), - child: Text( - AppLocalizations.of(context)!.blocked, - style: TextStyle( - color: list == 'blocked' - ? Colors.white - : null - ), - ), - ), - ), - ) - ], - ), - const SizedBox(height: 30), - TextFormField( - controller: ipController, - onChanged: validateIp, - decoration: InputDecoration( - prefixIcon: const Icon(Icons.link_rounded), - errorText: ipError, - border: const OutlineInputBorder( - borderRadius: BorderRadius.all( - Radius.circular(10) - ) - ), - labelText: AppLocalizations.of(context)!.ipAddress, - ), - ), - ], - ), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text(AppLocalizations.of(context)!.cancel) - ), - TextButton( - onPressed: checkValidValues() == true - ? () { - Navigator.pop(context); - widget.onConfirm(list, ipController.text); - } - : null, - child: Text( - AppLocalizations.of(context)!.confirm, - style: TextStyle( - color: checkValidValues() == true - ? Theme.of(context).primaryColor - : Colors.grey - ), - ) - ), - ], - ); - } -} \ No newline at end of file diff --git a/lib/screens/clients/block_client_modal.dart b/lib/screens/clients/block_client_modal.dart new file mode 100644 index 0000000..0269557 --- /dev/null +++ b/lib/screens/clients/block_client_modal.dart @@ -0,0 +1,125 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +class BlockClientModal extends StatefulWidget { + final void Function(String) onConfirm; + + const BlockClientModal({ + Key? key, + required this.onConfirm + }) : super(key: key); + + @override + State createState() => _BlockClientModalState(); +} + +class _BlockClientModalState extends State { + TextEditingController ipController = TextEditingController(); + String? ipError; + + void validateIp(String value) { + RegExp ipAddress = RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$'); + if (ipAddress.hasMatch(value) == true) { + setState(() => ipError = null); + } + else { + setState(() => ipError = AppLocalizations.of(context)!.ipNotValid); + } + } + + bool checkValidValues() { + if ( + ipController.text != '' && + ipError == null + ) { + return true; + } + else { + return false; + } + } + + @override + Widget build(BuildContext context) { + return Padding( + padding: MediaQuery.of(context).viewInsets, + child: Container( + height: 322, + padding: const EdgeInsets.all(28), + decoration: BoxDecoration( + color: Theme.of(context).dialogBackgroundColor, + borderRadius: const BorderRadius.only( + topLeft: Radius.circular(28), + topRight: Radius.circular(28) + ) + ), + child: Column( + children: [ + const Icon( + Icons.block, + size: 26, + ), + const SizedBox(height: 20), + Text( + AppLocalizations.of(context)!.blockClient, + style: const TextStyle( + fontSize: 24 + ), + ), + const SizedBox(height: 30), + TextFormField( + controller: ipController, + onChanged: validateIp, + decoration: InputDecoration( + prefixIcon: const Icon(Icons.link_rounded), + errorText: ipError, + border: const OutlineInputBorder( + borderRadius: BorderRadius.all( + Radius.circular(10) + ) + ), + labelText: AppLocalizations.of(context)!.ipAddress, + ), + ), + Expanded( + child: Column( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Padding( + padding: const EdgeInsets.only(top: 20), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: Text(AppLocalizations.of(context)!.cancel) + ), + const SizedBox(width: 20), + TextButton( + onPressed: checkValidValues() == true + ? () { + Navigator.pop(context); + widget.onConfirm(ipController.text); + } + : null, + child: Text( + AppLocalizations.of(context)!.confirm, + style: TextStyle( + color: checkValidValues() == true + ? Theme.of(context).primaryColor + : Colors.grey + ), + ) + ), + ], + ), + ), + ], + ), + ) + ], + ), + ), + ); + } +} \ No newline at end of file diff --git a/lib/screens/clients/fab.dart b/lib/screens/clients/fab.dart index ab58f0f..6a283c5 100644 --- a/lib/screens/clients/fab.dart +++ b/lib/screens/clients/fab.dart @@ -4,7 +4,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:adguard_home_manager/screens/clients/add_client_modal.dart'; +import 'package:adguard_home_manager/screens/clients/block_client_modal.dart'; import 'package:adguard_home_manager/services/http_requests.dart'; import 'package:adguard_home_manager/models/clients_allowed_blocked.dart'; @@ -20,25 +20,15 @@ class ClientsFab extends StatelessWidget { final serversProvider = Provider.of(context); final appConfigProvider = Provider.of(context); - void confirmRemoveDomain(String list, String ip) async { + void confirmRemoveDomain(String ip) async { Map> body = {}; - if (list == 'allowed') { - final List clients = [...serversProvider.clients.data!.clientsAllowedBlocked?.allowedClients ?? [], ip]; - body = { - "allowed_clients": clients, - "disallowed_clients": serversProvider.clients.data!.clientsAllowedBlocked?.disallowedClients ?? [], - "blocked_hosts": serversProvider.clients.data!.clientsAllowedBlocked?.blockedHosts ?? [], - }; - } - else if (list == 'blocked') { - final List clients = [...serversProvider.clients.data!.clientsAllowedBlocked?.disallowedClients ?? [], ip]; - body = { - "allowed_clients": serversProvider.clients.data!.clientsAllowedBlocked?.allowedClients ?? [], - "disallowed_clients": clients, - "blocked_hosts": serversProvider.clients.data!.clientsAllowedBlocked?.blockedHosts ?? [], - }; - } + final List clients = [...serversProvider.clients.data!.clientsAllowedBlocked?.disallowedClients ?? [], ip]; + body = { + "allowed_clients": serversProvider.clients.data!.clientsAllowedBlocked?.allowedClients ?? [], + "disallowed_clients": clients, + "blocked_hosts": serversProvider.clients.data!.clientsAllowedBlocked?.blockedHosts ?? [], + }; ProcessModal processModal = ProcessModal(context: context); processModal.open(AppLocalizations.of(context)!.addingClient); @@ -82,25 +72,37 @@ class ClientsFab extends StatelessWidget { } } - void openAddClient(String list) { - showDialog( + void openBlockClient() { + showModalBottomSheet( context: context, - builder: (ctx) => AddClientModal( - list: list, + builder: (ctx) => BlockClientModal( onConfirm: confirmRemoveDomain - ) + ), + isScrollControlled: true, + backgroundColor: Colors.transparent ); } + void openAddClient() { + // showModalBottomSheet( + // context: context, + // builder: (ctx) => BlockClientModal( + // onConfirm: confirmRemoveDomain + // ), + // isScrollControlled: true, + // backgroundColor: Colors.transparent + // ); + } + if (appConfigProvider.selectedClientsTab == 1) { return FloatingActionButton( - onPressed: () => openAddClient('allowed'), + onPressed: () => openAddClient(), child: const Icon(Icons.add), ); } else if (appConfigProvider.selectedClientsTab == 2) { return FloatingActionButton( - onPressed: () => openAddClient('blocked'), + onPressed: () => openBlockClient(), child: const Icon(Icons.add), ); }