2022-10-09 01:13:55 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2023-04-30 23:36:02 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-10-09 01:13:55 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/rendering.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2022-10-10 01:26:33 +02:00
|
|
|
import 'package:adguard_home_manager/screens/settings/access_settings/add_client_modal.dart';
|
2023-10-07 23:03:30 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/client/remove_client_modal.dart';
|
2023-04-06 18:50:06 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/tab_content_list.dart';
|
2022-10-09 01:13:55 +02:00
|
|
|
|
2022-10-09 20:03:17 +02:00
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
2022-10-09 01:13:55 +02:00
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
2023-04-06 18:50:06 +02:00
|
|
|
import 'package:adguard_home_manager/constants/enums.dart';
|
2023-05-24 14:16:53 +02:00
|
|
|
import 'package:adguard_home_manager/providers/clients_provider.dart';
|
2022-10-09 01:13:55 +02:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
|
|
|
|
class ClientsList extends StatefulWidget {
|
|
|
|
final String type;
|
|
|
|
final ScrollController scrollController;
|
2023-04-06 18:10:23 +02:00
|
|
|
final LoadStatus loadStatus;
|
2022-10-09 01:13:55 +02:00
|
|
|
final List<String> data;
|
|
|
|
|
|
|
|
const ClientsList({
|
|
|
|
Key? key,
|
|
|
|
required this.type,
|
|
|
|
required this.scrollController,
|
|
|
|
required this.loadStatus,
|
|
|
|
required this.data,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ClientsList> createState() => _ClientsListState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ClientsListState extends State<ClientsList> {
|
|
|
|
late bool isVisible;
|
|
|
|
|
|
|
|
@override
|
|
|
|
initState(){
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
isVisible = true;
|
|
|
|
widget.scrollController.addListener(() {
|
2022-10-09 01:20:38 +02:00
|
|
|
if (mounted) {
|
|
|
|
if (widget.scrollController.position.userScrollDirection == ScrollDirection.reverse) {
|
|
|
|
if (mounted && isVisible == true) {
|
|
|
|
setState(() => isVisible = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (widget.scrollController.position.userScrollDirection == ScrollDirection.forward) {
|
|
|
|
if (mounted && isVisible == false) {
|
|
|
|
setState(() => isVisible = true);
|
|
|
|
}
|
2022-10-09 01:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
2023-05-24 14:16:53 +02:00
|
|
|
final clientsProvider = Provider.of<ClientsProvider>(context);
|
2022-10-09 01:13:55 +02:00
|
|
|
|
2023-04-30 23:36:02 +02:00
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
Future refetchClients() async {
|
|
|
|
final result = await clientsProvider.fetchClients();
|
2023-11-15 18:16:43 +01:00
|
|
|
if (result == false && mounted) {
|
2023-05-25 15:06:21 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientsNotLoaded,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 01:13:55 +02:00
|
|
|
void confirmRemoveItem(String client, String type) async {
|
|
|
|
Map<String, List<String>> body = {
|
2023-05-24 14:16:53 +02:00
|
|
|
"allowed_clients": clientsProvider.clients!.clientsAllowedBlocked?.allowedClients ?? [],
|
|
|
|
"disallowed_clients": clientsProvider.clients!.clientsAllowedBlocked?.disallowedClients ?? [],
|
|
|
|
"blocked_hosts": clientsProvider.clients!.clientsAllowedBlocked?.blockedHosts ?? [],
|
2022-10-09 01:13:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type == 'allowed') {
|
|
|
|
body['allowed_clients'] = body['allowed_clients']!.where((c) => c != client).toList();
|
|
|
|
}
|
|
|
|
else if (type == 'disallowed') {
|
|
|
|
body['disallowed_clients'] = body['disallowed_clients']!.where((c) => c != client).toList();
|
|
|
|
}
|
|
|
|
else if (type == 'domains') {
|
|
|
|
body['blocked_hosts'] = body['blocked_hosts']!.where((c) => c != client).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.removingClient);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await clientsProvider.removeClientList(client, type);
|
2022-10-09 01:13:55 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result['success'] == true) {
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientRemovedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
2023-05-25 15:06:21 +02:00
|
|
|
else if (result['success'] == false && result['error'] == 'client_another_list') {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientAnotherList,
|
|
|
|
color: Colors.red
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
2023-05-25 15:06:21 +02:00
|
|
|
label: type == 'allowed' || type == 'blocked'
|
|
|
|
? AppLocalizations.of(context)!.clientNotRemoved
|
|
|
|
: AppLocalizations.of(context)!.domainNotAdded,
|
2022-10-09 20:03:17 +02:00
|
|
|
color: Colors.red
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void confirmAddItem(String item, String type) async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.removingClient);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await clientsProvider.addClientList(item, type);
|
2022-10-09 01:13:55 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result['success'] == true) {
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientAddedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
2023-05-25 15:06:21 +02:00
|
|
|
else if (result['success'] == false && result['error'] == 'client_another_list') {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientAnotherList,
|
|
|
|
color: Colors.red
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-09 20:03:17 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: type == 'allowed' || type == 'blocked'
|
|
|
|
? AppLocalizations.of(context)!.clientNotRemoved
|
|
|
|
: AppLocalizations.of(context)!.domainNotAdded,
|
|
|
|
color: Colors.red
|
2022-10-09 01:13:55 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String description() {
|
|
|
|
switch (widget.type) {
|
|
|
|
case 'allowed':
|
|
|
|
return AppLocalizations.of(context)!.allowedClientsDescription;
|
|
|
|
|
|
|
|
case 'disallowed':
|
|
|
|
return AppLocalizations.of(context)!.blockedClientsDescription;
|
|
|
|
|
|
|
|
case 'domains':
|
|
|
|
return AppLocalizations.of(context)!.disallowedDomainsDescription;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String noItems() {
|
|
|
|
switch (widget.type) {
|
|
|
|
case 'allowed':
|
|
|
|
return AppLocalizations.of(context)!.noAllowedClients;
|
|
|
|
|
|
|
|
case 'disallowed':
|
|
|
|
return AppLocalizations.of(context)!.noBlockedClients;
|
|
|
|
|
|
|
|
case 'domains':
|
|
|
|
return AppLocalizations.of(context)!.noDisallowedDomains;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:50:06 +02:00
|
|
|
return CustomTabContentList(
|
2023-04-30 23:36:02 +02:00
|
|
|
noSliver: !(Platform.isAndroid || Platform.isIOS) ? true : false,
|
2023-04-06 18:50:06 +02:00
|
|
|
loadingGenerator: () => SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
height: MediaQuery.of(context).size.height-171,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2022-10-09 01:13:55 +02:00
|
|
|
children: [
|
2023-04-06 18:50:06 +02:00
|
|
|
const CircularProgressIndicator(),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.loadingClients,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
itemsCount: widget.data.isNotEmpty
|
|
|
|
? widget.data.length+1
|
|
|
|
: 0,
|
|
|
|
contentWidget: (index) {
|
|
|
|
if (index == 0) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Card(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(20),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.info_rounded,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
2022-10-09 01:20:38 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
const SizedBox(width: 20),
|
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
description(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface,
|
2022-10-09 01:20:38 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-10-09 01:20:38 +02:00
|
|
|
),
|
2022-10-09 01:13:55 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ListTile(
|
|
|
|
title: Text(
|
|
|
|
widget.data[index-1],
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.normal,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
2022-10-09 01:13:55 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
),
|
|
|
|
trailing: IconButton(
|
|
|
|
onPressed: () => {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => RemoveClientModal(
|
|
|
|
onConfirm: () => confirmRemoveItem(widget.data[index-1], widget.type),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.delete_rounded)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
noData: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Card(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(20),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.info_rounded,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 20),
|
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
description(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-10-09 01:13:55 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
noItems(),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
2022-10-09 01:13:55 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
const SizedBox(height: 30),
|
|
|
|
TextButton.icon(
|
2023-05-25 15:06:21 +02:00
|
|
|
onPressed: refetchClients,
|
2023-04-06 18:50:06 +02:00
|
|
|
icon: const Icon(Icons.refresh_rounded),
|
|
|
|
label: Text(AppLocalizations.of(context)!.refresh),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-10-09 01:13:55 +02:00
|
|
|
),
|
2023-04-06 18:50:06 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
errorGenerator: () => SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
height: MediaQuery.of(context).size.height-101,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Icon(
|
|
|
|
Icons.error,
|
|
|
|
color: Colors.red,
|
|
|
|
size: 50,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.clientsNotLoaded,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
loadStatus: widget.loadStatus,
|
2023-05-25 15:06:21 +02:00
|
|
|
onRefresh: refetchClients,
|
2023-04-06 18:50:06 +02:00
|
|
|
refreshIndicatorOffset: 0,
|
|
|
|
fab: FloatingActionButton(
|
|
|
|
onPressed: () {
|
2023-04-30 23:39:56 +02:00
|
|
|
if (width > 900 || !(Platform.isAndroid || Platform.isIOS)) {
|
2023-04-30 23:36:02 +02:00
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AddClientModal(
|
|
|
|
type: widget.type,
|
|
|
|
onConfirm: confirmAddItem,
|
|
|
|
dialog: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
2023-10-29 02:47:14 +01:00
|
|
|
useRootNavigator: true,
|
2023-04-30 23:36:02 +02:00
|
|
|
builder: (context) => AddClientModal(
|
|
|
|
type: widget.type,
|
|
|
|
onConfirm: confirmAddItem,
|
|
|
|
dialog: false,
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true
|
|
|
|
);
|
|
|
|
}
|
2023-04-06 18:50:06 +02:00
|
|
|
},
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
fabVisible: isVisible,
|
|
|
|
);
|
2022-10-09 01:13:55 +02:00
|
|
|
}
|
|
|
|
}
|