2022-10-07 01:12:48 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
import 'package:animations/animations.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-08 22:16:35 +02:00
|
|
|
import 'package:flutter/rendering.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/remove_domain_modal.dart';
|
2022-10-07 01:22:18 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/fab.dart';
|
2022-10-07 18:16:22 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/options_modal.dart';
|
2022-10-07 17:08:23 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/client_modal.dart';
|
2022-10-07 01:22:18 +02:00
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:adguard_home_manager/models/clients.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
class AddedList extends StatefulWidget {
|
|
|
|
final ScrollController scrollController;
|
2022-10-08 15:06:40 +02:00
|
|
|
final int loadStatus;
|
2022-10-07 01:12:48 +02:00
|
|
|
final List<Client> data;
|
|
|
|
final Future Function() fetchClients;
|
|
|
|
|
|
|
|
const AddedList({
|
|
|
|
Key? key,
|
2022-10-08 22:16:35 +02:00
|
|
|
required this.scrollController,
|
2022-10-08 15:06:40 +02:00
|
|
|
required this.loadStatus,
|
2022-10-07 01:12:48 +02:00
|
|
|
required this.data,
|
|
|
|
required this.fetchClients
|
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
@override
|
|
|
|
State<AddedList> createState() => _AddedListState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddedListState extends State<AddedList> {
|
|
|
|
late bool isVisible;
|
|
|
|
|
|
|
|
@override
|
|
|
|
initState(){
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
isVisible = true;
|
|
|
|
widget.scrollController.addListener(() {
|
|
|
|
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-07 01:12:48 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
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-08 22:16:35 +02:00
|
|
|
appConfigProvider.setShowingSnackbar();
|
2022-10-07 17:08:23 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.clientUpdatedSuccessfully),
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-10-08 22:16:35 +02:00
|
|
|
appConfigProvider.setShowingSnackbar();
|
2022-10-07 17:08:23 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.clientNotUpdated),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
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-08 22:16:35 +02:00
|
|
|
appConfigProvider.setShowingSnackbar();
|
2022-10-07 18:16:22 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.clientDeletedSuccessfully),
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-10-08 22:16:35 +02:00
|
|
|
appConfigProvider.setShowingSnackbar();
|
2022-10-07 18:16:22 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.clientNotDeleted),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
void openClientModal(Client client) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (ctx) => ClientModal(
|
|
|
|
client: client,
|
2022-10-07 18:16:22 +02:00
|
|
|
onConfirm: confirmEditClient,
|
|
|
|
onDelete: deleteClient,
|
2022-10-07 17:08:23 +02:00
|
|
|
),
|
|
|
|
isScrollControlled: true,
|
|
|
|
backgroundColor: Colors.transparent
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
void openDeleteModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
|
|
|
builder: (ctx) => RemoveDomainModal(
|
|
|
|
onConfirm: () => deleteClient(client)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void openOptionsModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
|
|
|
builder: (ctx) => OptionsModal(
|
|
|
|
onDelete: () => openDeleteModal(client),
|
|
|
|
onEdit: () => openClientModal(client),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-08 22:16:35 +02:00
|
|
|
switch (widget.loadStatus) {
|
2022-10-08 15:06:40 +02:00
|
|
|
case 0:
|
|
|
|
return SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
height: MediaQuery.of(context).size.height-171,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.loadingStatus,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return Stack(
|
|
|
|
children: [
|
2022-10-08 22:16:35 +02:00
|
|
|
if (widget.data.isNotEmpty) ListView.builder(
|
2022-10-08 15:06:40 +02:00
|
|
|
padding: const EdgeInsets.only(top: 0),
|
2022-10-08 22:16:35 +02:00
|
|
|
itemCount: widget.data.length,
|
2022-10-08 15:06:40 +02:00
|
|
|
itemBuilder: (context, index) => ListTile(
|
|
|
|
isThreeLine: true,
|
2022-10-08 22:16:35 +02:00
|
|
|
onLongPress: () => openOptionsModal(widget.data[index]),
|
|
|
|
onTap: () => openClientModal(widget.data[index]),
|
2022-10-08 15:06:40 +02:00
|
|
|
title: Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 5),
|
2022-10-08 22:16:35 +02:00
|
|
|
child: Text(widget.data[index].name),
|
2022-10-08 15:06:40 +02:00
|
|
|
),
|
|
|
|
subtitle: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2022-10-08 22:16:35 +02:00
|
|
|
Text(widget.data[index].ids.toString().replaceAll(RegExp(r'^\[|\]$'), '')),
|
2022-10-08 15:06:40 +02:00
|
|
|
const SizedBox(height: 7),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.filter_list_rounded,
|
|
|
|
size: 19,
|
2022-10-08 22:16:35 +02:00
|
|
|
color: widget.data[index].filteringEnabled == true
|
2022-10-08 15:06:40 +02:00
|
|
|
? Colors.green
|
|
|
|
: Colors.red,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.vpn_lock_rounded,
|
|
|
|
size: 18,
|
2022-10-08 22:16:35 +02:00
|
|
|
color: widget.data[index].safebrowsingEnabled == true
|
2022-10-08 15:06:40 +02:00
|
|
|
? Colors.green
|
|
|
|
: Colors.red,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.block,
|
|
|
|
size: 18,
|
2022-10-08 22:16:35 +02:00
|
|
|
color: widget.data[index].parentalEnabled == true
|
2022-10-08 15:06:40 +02:00
|
|
|
? Colors.green
|
|
|
|
: Colors.red,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.search_rounded,
|
|
|
|
size: 19,
|
2022-10-08 22:16:35 +02:00
|
|
|
color: widget.data[index].safesearchEnabled == true
|
2022-10-08 15:06:40 +02:00
|
|
|
? Colors.green
|
|
|
|
: Colors.red,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
2022-10-08 22:16:35 +02:00
|
|
|
if (widget.data.isEmpty) SizedBox(
|
2022-10-08 15:06:40 +02:00
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2022-10-07 01:22:18 +02:00
|
|
|
children: [
|
2022-10-08 15:06:40 +02:00
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.noClientsList,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Colors.grey
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
TextButton.icon(
|
2022-10-08 22:16:35 +02:00
|
|
|
onPressed: widget.fetchClients,
|
2022-10-08 15:06:40 +02:00
|
|
|
icon: const Icon(Icons.refresh_rounded),
|
|
|
|
label: Text(AppLocalizations.of(context)!.refresh),
|
2022-10-07 01:22:18 +02:00
|
|
|
)
|
|
|
|
],
|
2022-10-07 01:12:48 +02:00
|
|
|
),
|
2022-10-08 15:06:40 +02:00
|
|
|
),
|
2022-10-08 22:16:35 +02:00
|
|
|
AnimatedPositioned(
|
|
|
|
duration: const Duration(milliseconds: 100),
|
|
|
|
curve: Curves.easeInOut,
|
|
|
|
bottom: isVisible ?
|
|
|
|
appConfigProvider.showingSnackbar
|
|
|
|
? 70 : 20
|
|
|
|
: -70,
|
2022-10-08 15:06:40 +02:00
|
|
|
right: 20,
|
2022-10-08 22:16:35 +02:00
|
|
|
child: const ClientsFab(tab: 1),
|
|
|
|
)
|
2022-10-08 15:06:40 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return SizedBox(
|
2022-10-07 01:22:18 +02:00
|
|
|
width: double.maxFinite,
|
2022-10-08 15:06:40 +02:00
|
|
|
height: MediaQuery.of(context).size.height-171,
|
2022-10-07 01:22:18 +02:00
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2022-10-08 15:06:40 +02:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2022-10-07 01:22:18 +02:00
|
|
|
children: [
|
2022-10-08 15:06:40 +02:00
|
|
|
const Icon(
|
|
|
|
Icons.error,
|
|
|
|
color: Colors.red,
|
|
|
|
size: 50,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
2022-10-07 01:22:18 +02:00
|
|
|
Text(
|
2022-10-08 15:06:40 +02:00
|
|
|
AppLocalizations.of(context)!.errorLoadServerStatus,
|
2022-10-07 01:22:18 +02:00
|
|
|
style: const TextStyle(
|
2022-10-08 15:06:40 +02:00
|
|
|
fontSize: 22,
|
|
|
|
color: Colors.grey,
|
2022-10-07 01:22:18 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-10-08 15:06:40 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
2022-10-07 01:12:48 +02:00
|
|
|
}
|
|
|
|
}
|