adguard-home-manager/lib/screens/clients/search_clients.dart

398 lines
14 KiB
Dart
Raw Normal View History

2022-10-25 21:03:20 +02:00
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/screens/clients/remove_client_modal.dart';
2022-11-04 01:15:45 +01:00
import 'package:adguard_home_manager/screens/clients/client_screen.dart';
2022-10-25 21:03:20 +02:00
import 'package:adguard_home_manager/screens/clients/options_modal.dart';
import 'package:adguard_home_manager/widgets/custom_list_tile.dart';
import 'package:adguard_home_manager/services/http_requests.dart';
import 'package:adguard_home_manager/classes/process_modal.dart';
import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
import 'package:adguard_home_manager/models/clients.dart';
2022-10-25 21:12:00 +02:00
import 'package:adguard_home_manager/widgets/section_label.dart';
2022-10-25 21:03:20 +02:00
import 'package:adguard_home_manager/providers/servers_provider.dart';
class SearchClients extends StatelessWidget {
const SearchClients({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final serversProvider = Provider.of<ServersProvider>(context);
return SearchClientsWidget(
serversProvider: serversProvider,
);
}
}
class SearchClientsWidget extends StatefulWidget {
final ServersProvider serversProvider;
const SearchClientsWidget({
Key? key,
required this.serversProvider,
}) : super(key: key);
@override
State<SearchClientsWidget> createState() => _SearchClientsWidgetState();
}
class _SearchClientsWidgetState extends State<SearchClientsWidget> {
late ScrollController scrollController;
2022-10-25 21:03:20 +02:00
final TextEditingController searchController = TextEditingController();
List<Client> clients = [];
List<AutoClient> autoClients = [];
List<Client> clientsScreen = [];
List<AutoClient> autoClientsScreen = [];
bool showDivider = true;
2022-10-25 21:03:20 +02:00
void search(String value) {
2022-11-04 01:18:54 +01:00
if (value == '') {
setState(() {
clientsScreen = [];
autoClientsScreen = [];
});
}
else {
setState(() {
clientsScreen = clients.where((client) => client.name.contains(value) || client.ids.where((e) => e.contains(value)).isNotEmpty).toList();
autoClientsScreen = autoClients.where((client) => (client.name != null ? client.name!.contains(value) : true) || client.ip.contains(value)).toList();
});
}
2022-10-25 21:03:20 +02:00
}
void scrollListener() {
if (scrollController.position.pixels > 0) {
setState(() => showDivider = false);
}
else {
setState(() => showDivider = true);
}
}
2022-10-25 21:03:20 +02:00
@override
void initState() {
scrollController = ScrollController()..addListener(scrollListener);
2022-10-25 21:03:20 +02:00
setState(() {
clients = widget.serversProvider.clients.data!.clients;
autoClients = widget.serversProvider.clients.data!.autoClientsData;
});
super.initState();
}
@override
Widget build(BuildContext context) {
final serversProvider = Provider.of<ServersProvider>(context);
final appConfigProvider = Provider.of<AppConfigProvider>(context);
void deleteClient(Client client) async {
ProcessModal processModal = ProcessModal(context: context);
processModal.open(AppLocalizations.of(context)!.removingClient);
final result = await postDeleteClient(server: serversProvider.selectedServer!, name: client.name);
processModal.close();
if (result['result'] == 'success') {
ClientsData clientsData = serversProvider.clients.data!;
clientsData.clients = clientsData.clients.where((c) => c.name != client.name).toList();
serversProvider.setClientsData(clientsData);
2022-10-25 21:15:37 +02:00
setState(() {
clients = clientsData.clients;
});
search(searchController.text);
2022-10-25 21:03:20 +02:00
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientDeletedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotDeleted,
color: Colors.red
);
}
}
void confirmEditClient(Client client) async {
ProcessModal processModal = ProcessModal(context: context);
processModal.open(AppLocalizations.of(context)!.addingClient);
final result = await postUpdateClient(server: serversProvider.selectedServer!, data: {
'name': client.name,
'data': client.toJson()
});
processModal.close();
if (result['result'] == 'success') {
ClientsData clientsData = serversProvider.clients.data!;
clientsData.clients = clientsData.clients.map((e) {
if (e.name == client.name) {
return client;
}
else {
return e;
}
}).toList();
serversProvider.setClientsData(clientsData);
2022-10-25 21:15:37 +02:00
setState(() {
clients = clientsData.clients;
});
search(searchController.text);
2022-10-25 21:03:20 +02:00
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientUpdatedSuccessfully,
color: Colors.green
);
}
else {
appConfigProvider.addLog(result['log']);
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotUpdated,
color: Colors.red
);
}
}
void openClientModal(Client client) {
2022-11-04 01:15:45 +01:00
Navigator.push(context, MaterialPageRoute(
fullscreenDialog: true,
builder: (BuildContext context) => ClientScreen(
2022-10-25 21:03:20 +02:00
onConfirm: confirmEditClient,
onDelete: deleteClient,
2022-11-04 01:15:45 +01:00
client: client,
)
));
2022-10-25 21:03:20 +02:00
}
void openDeleteModal(Client client) {
showModal(
context: context,
builder: (ctx) => RemoveClientModal(
onConfirm: () => deleteClient(client)
)
);
}
void openOptionsModal(Client client) {
showModal(
context: context,
builder: (ctx) => OptionsModal(
onDelete: () => openDeleteModal(client),
onEdit: () => openClientModal(client),
)
);
}
return Scaffold(
appBar: AppBar(
toolbarHeight: 60,
title: Padding(
padding: const EdgeInsets.only(bottom: 3),
child: TextFormField(
controller: searchController,
onChanged: search,
decoration: InputDecoration(
hintText: AppLocalizations.of(context)!.search,
hintStyle: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 18
),
border: InputBorder.none,
),
style: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 18
),
),
),
actions: [
IconButton(
onPressed: searchController.text != ''
? () => setState(() {
searchController.text = '';
autoClientsScreen = autoClients;
clientsScreen = clients;
})
: null,
icon: const Icon(Icons.clear_rounded),
tooltip: AppLocalizations.of(context)!.clearSearch,
),
const SizedBox(width: 10)
],
bottom: PreferredSize(
preferredSize: const Size(double.maxFinite, 1),
child: Container(
width: double.maxFinite,
height: 1,
decoration: BoxDecoration(
color: showDivider == true
2023-01-29 21:52:37 +01:00
? Theme.of(context).colorScheme.surfaceVariant
: Colors.transparent
2022-10-25 21:03:20 +02:00
),
),
),
),
body: clientsScreen.isNotEmpty || autoClientsScreen.isNotEmpty
? ListView(
controller: scrollController,
2022-10-25 21:03:20 +02:00
children: [
if (clientsScreen.isNotEmpty) ...[
2022-10-25 21:12:00 +02:00
SectionLabel(
label: AppLocalizations.of(context)!.added,
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 25),
),
2022-10-25 21:03:20 +02:00
ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: clientsScreen.length,
padding: const EdgeInsets.only(bottom: 0),
itemBuilder: (context, index) => ListTile(
2022-10-25 21:12:00 +02:00
contentPadding: index == 0
2022-10-26 12:28:01 +02:00
? const EdgeInsets.only(left: 20, right: 20, bottom: 15)
2022-10-25 21:12:00 +02:00
: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
2022-10-25 21:03:20 +02:00
isThreeLine: true,
onLongPress: () => openOptionsModal(clientsScreen[index]),
onTap: () => openClientModal(clientsScreen[index]),
title: Padding(
padding: const EdgeInsets.only(bottom: 5),
child: Text(
clientsScreen[index].name,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.normal
),
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(clientsScreen[index].ids.toString().replaceAll(RegExp(r'^\[|\]$'), '')),
const SizedBox(height: 7),
Row(
children: [
Icon(
Icons.filter_list_rounded,
size: 19,
color: clientsScreen[index].filteringEnabled == true
? appConfigProvider.useThemeColorForStatus == true
2023-01-25 20:51:23 +01:00
? Theme.of(context).colorScheme.primary
: Colors.green
: appConfigProvider.useThemeColorForStatus == true
? Colors.grey
: Colors.red,
2022-10-25 21:03:20 +02:00
),
const SizedBox(width: 10),
Icon(
Icons.vpn_lock_rounded,
size: 18,
color: clientsScreen[index].safebrowsingEnabled == true
? appConfigProvider.useThemeColorForStatus == true
2023-01-25 20:51:23 +01:00
? Theme.of(context).colorScheme.primary
: Colors.green
: appConfigProvider.useThemeColorForStatus == true
? Colors.grey
: Colors.red,
2022-10-25 21:03:20 +02:00
),
const SizedBox(width: 10),
Icon(
Icons.block,
size: 18,
color: clientsScreen[index].parentalEnabled == true
? appConfigProvider.useThemeColorForStatus == true
2023-01-25 20:51:23 +01:00
? Theme.of(context).colorScheme.primary
: Colors.green
: appConfigProvider.useThemeColorForStatus == true
? Colors.grey
: Colors.red,
2022-10-25 21:03:20 +02:00
),
const SizedBox(width: 10),
Icon(
Icons.search_rounded,
size: 19,
color: clientsScreen[index].safesearchEnabled == true
? appConfigProvider.useThemeColorForStatus == true
2023-01-25 20:51:23 +01:00
? Theme.of(context).colorScheme.primary
: Colors.green
: appConfigProvider.useThemeColorForStatus == true
? Colors.grey
: Colors.red,
2022-10-25 21:03:20 +02:00
)
],
)
],
),
)
)
],
if (autoClientsScreen.isNotEmpty) ...[
2022-10-25 21:12:00 +02:00
SectionLabel(
label: AppLocalizations.of(context)!.activeClients,
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 25),
),
2022-10-25 21:03:20 +02:00
ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: autoClientsScreen.length,
padding: const EdgeInsets.only(bottom: 0),
itemBuilder: (context, index) => CustomListTile(
title: autoClientsScreen[index].name != ''
? autoClientsScreen[index].name!
: autoClientsScreen[index].ip,
subtitle: autoClientsScreen[index].name != ''
? autoClientsScreen[index].ip
: null,
trailing: Text(autoClientsScreen[index].source),
2022-10-25 21:12:00 +02:00
padding: index == 0
2022-10-26 12:28:01 +02:00
? const EdgeInsets.only(left: 20, right: 20, bottom: 15)
2022-10-25 21:12:00 +02:00
: null,
2022-10-25 21:03:20 +02:00
)
)
]
],
)
: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(
2022-11-04 01:18:54 +01:00
searchController.text == ''
? AppLocalizations.of(context)!.inputSearchTerm
: AppLocalizations.of(context)!.noClientsSearch,
2022-10-25 21:03:20 +02:00
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.grey,
fontSize: 22
),
),
),
)
);
}
}