2022-10-07 01:12:48 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-10-09 17:45:18 +02:00
|
|
|
import 'package:animations/animations.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-11-04 01:15:45 +01:00
|
|
|
import 'package:adguard_home_manager/screens/clients/client_screen.dart';
|
2022-10-08 23:04:28 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/remove_client_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';
|
2023-04-06 18:10:23 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/tab_content_list.dart';
|
2022-10-07 01:22:18 +02:00
|
|
|
|
2022-10-09 20:03:17 +02:00
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
2023-04-06 18:10:23 +02:00
|
|
|
import 'package:adguard_home_manager/constants/enums.dart';
|
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';
|
2023-04-15 01:22:44 +02:00
|
|
|
import 'package:adguard_home_manager/functions/compare_versions.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;
|
2023-04-06 18:10:23 +02:00
|
|
|
final LoadStatus 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-09 20:03:17 +02:00
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientUpdatedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-07 17:08:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-10-09 20:03:17 +02:00
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientNotUpdated,
|
|
|
|
color: Colors.red
|
2022-10-07 17:08:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09 20:03:17 +02:00
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientDeletedSuccessfully,
|
|
|
|
color: Colors.green
|
2022-10-07 18:16:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-10-09 20:03:17 +02:00
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.clientNotDeleted,
|
|
|
|
color: Colors.red
|
2022-10-07 18:16:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 17:08:23 +02:00
|
|
|
void openClientModal(Client client) {
|
2022-11-04 01:15:45 +01:00
|
|
|
Navigator.push(context, MaterialPageRoute(
|
|
|
|
fullscreenDialog: true,
|
|
|
|
builder: (BuildContext context) => ClientScreen(
|
2022-10-07 18:16:22 +02:00
|
|
|
onConfirm: confirmEditClient,
|
2023-04-15 01:22:44 +02:00
|
|
|
serverVersion: serversProvider.serverStatus.data!.serverVersion,
|
2022-10-07 18:16:22 +02:00
|
|
|
onDelete: deleteClient,
|
2022-11-04 01:15:45 +01:00
|
|
|
client: client,
|
|
|
|
)
|
|
|
|
));
|
2022-10-07 17:08:23 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:16:22 +02:00
|
|
|
void openDeleteModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
2022-10-08 23:04:28 +02:00
|
|
|
builder: (ctx) => RemoveClientModal(
|
2022-10-07 18:16:22 +02:00
|
|
|
onConfirm: () => deleteClient(client)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void openOptionsModal(Client client) {
|
|
|
|
showModal(
|
|
|
|
context: context,
|
|
|
|
builder: (ctx) => OptionsModal(
|
|
|
|
onDelete: () => openDeleteModal(client),
|
|
|
|
onEdit: () => openClientModal(client),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:29:46 +02:00
|
|
|
return CustomTabContentList(
|
|
|
|
loadingGenerator: () => 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,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
itemsCount: widget.data.length,
|
|
|
|
contentWidget: (index) => ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
|
|
|
isThreeLine: true,
|
|
|
|
onLongPress: () => openOptionsModal(widget.data[index]),
|
|
|
|
onTap: () => openClientModal(widget.data[index]),
|
|
|
|
title: Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 5),
|
|
|
|
child: Text(
|
|
|
|
widget.data[index].name,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18,
|
|
|
|
fontWeight: FontWeight.normal,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
subtitle: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
widget.data[index].ids.toString().replaceAll(RegExp(r'^\[|\]$'), ''),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).listTileTheme.textColor
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(height: 7),
|
|
|
|
Row(
|
2023-04-06 18:10:23 +02:00
|
|
|
children: [
|
2023-04-06 18:29:46 +02:00
|
|
|
Icon(
|
|
|
|
Icons.filter_list_rounded,
|
|
|
|
size: 19,
|
|
|
|
color: widget.data[index].filteringEnabled == true
|
|
|
|
? appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.green
|
|
|
|
: appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Colors.grey
|
|
|
|
: Colors.red,
|
2022-10-08 15:06:40 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.vpn_lock_rounded,
|
|
|
|
size: 18,
|
|
|
|
color: widget.data[index].safebrowsingEnabled == true
|
|
|
|
? appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.green
|
|
|
|
: appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Colors.grey
|
|
|
|
: Colors.red,
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.block,
|
|
|
|
size: 18,
|
|
|
|
color: widget.data[index].parentalEnabled == true
|
|
|
|
? appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.green
|
|
|
|
: appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Colors.grey
|
|
|
|
: Colors.red,
|
2022-10-07 01:22:18 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
Icon(
|
|
|
|
Icons.search_rounded,
|
|
|
|
size: 19,
|
2023-04-15 01:22:44 +02:00
|
|
|
color: versionIsGreater(
|
|
|
|
currentVersion: serversProvider.serverStatus.data!.serverVersion,
|
|
|
|
referenceVersion: 'v0.107.28',
|
|
|
|
referenceVersionBeta: 'v0.108.0-b.33'
|
|
|
|
) == true
|
|
|
|
? widget.data[index].safeSearch!.enabled == true
|
|
|
|
? appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.green
|
|
|
|
: appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Colors.grey
|
|
|
|
: Colors.red
|
|
|
|
: widget.data[index].safesearchEnabled == true
|
|
|
|
? appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Colors.green
|
|
|
|
: appConfigProvider.useThemeColorForStatus == true
|
|
|
|
? Colors.grey
|
|
|
|
: Colors.red,
|
2023-04-06 18:10:23 +02:00
|
|
|
)
|
|
|
|
],
|
2023-04-06 18:29:46 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
noData: SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.noClientsList,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
const SizedBox(height: 30),
|
|
|
|
TextButton.icon(
|
|
|
|
onPressed: widget.fetchClients,
|
|
|
|
icon: const Icon(Icons.refresh_rounded),
|
|
|
|
label: Text(AppLocalizations.of(context)!.refresh),
|
|
|
|
)
|
|
|
|
],
|
2023-04-06 18:10:23 +02:00
|
|
|
),
|
2023-04-06 18:29:46 +02:00
|
|
|
),
|
|
|
|
errorGenerator: () => SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
height: MediaQuery.of(context).size.height-171,
|
|
|
|
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)!.errorLoadServerStatus,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
loadStatus: widget.loadStatus,
|
|
|
|
onRefresh: widget.fetchClients,
|
|
|
|
fab: const ClientsFab(),
|
|
|
|
fabVisible: isVisible,
|
2023-04-06 18:10:23 +02:00
|
|
|
);
|
2022-10-07 01:12:48 +02:00
|
|
|
}
|
|
|
|
}
|