2022-09-28 15:47:43 +02:00
|
|
|
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/clients_list.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/blocked_list.dart';
|
|
|
|
import 'package:adguard_home_manager/screens/clients/added_list.dart';
|
2022-09-28 15:47:43 +02:00
|
|
|
|
2022-10-03 22:41:19 +02:00
|
|
|
import 'package:adguard_home_manager/models/app_log.dart';
|
2022-09-29 23:12:24 +02:00
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
2022-09-28 15:47:43 +02:00
|
|
|
import 'package:adguard_home_manager/models/server.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
|
|
|
import 'package:adguard_home_manager/models/clients.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
|
|
|
|
class Clients extends StatelessWidget {
|
|
|
|
const Clients({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
2022-09-29 23:12:24 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
2022-09-28 15:47:43 +02:00
|
|
|
|
|
|
|
return ClientsWidget(
|
|
|
|
server: serversProvider.selectedServer!,
|
|
|
|
setLoadingStatus: serversProvider.setClientsLoadStatus,
|
|
|
|
setClientsData: serversProvider.setClientsData,
|
2022-09-29 23:12:24 +02:00
|
|
|
setSelectedClientsTab: appConfigProvider.setSelectedClientsTab,
|
2022-10-03 22:41:19 +02:00
|
|
|
addLog: appConfigProvider.addLog,
|
2022-09-28 15:47:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClientsWidget extends StatefulWidget {
|
|
|
|
final Server server;
|
|
|
|
final void Function(int, bool) setLoadingStatus;
|
|
|
|
final void Function(ClientsData) setClientsData;
|
2022-09-29 23:12:24 +02:00
|
|
|
final void Function(int) setSelectedClientsTab;
|
2022-10-03 22:41:19 +02:00
|
|
|
final void Function(AppLog) addLog;
|
2022-09-28 15:47:43 +02:00
|
|
|
|
|
|
|
const ClientsWidget({
|
|
|
|
Key? key,
|
|
|
|
required this.server,
|
|
|
|
required this.setLoadingStatus,
|
|
|
|
required this.setClientsData,
|
2022-10-03 22:41:19 +02:00
|
|
|
required this.setSelectedClientsTab,
|
|
|
|
required this.addLog,
|
2022-09-28 15:47:43 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ClientsWidget> createState() => _ClientsWidgetState();
|
|
|
|
}
|
|
|
|
|
2022-09-29 23:12:24 +02:00
|
|
|
class _ClientsWidgetState extends State<ClientsWidget> with TickerProviderStateMixin {
|
|
|
|
late TabController tabController;
|
|
|
|
|
2022-10-02 18:31:47 +02:00
|
|
|
Future fetchClients() async {
|
2022-09-28 15:47:43 +02:00
|
|
|
widget.setLoadingStatus(0, false);
|
|
|
|
final result = await getClients(widget.server);
|
2022-10-06 00:23:19 +02:00
|
|
|
if (mounted) {
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
widget.setClientsData(result['data']);
|
|
|
|
widget.setLoadingStatus(1, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget.addLog(result['log']);
|
|
|
|
widget.setLoadingStatus(2, true);
|
|
|
|
}
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
fetchClients();
|
|
|
|
super.initState();
|
2022-09-29 23:12:24 +02:00
|
|
|
tabController = TabController(
|
|
|
|
initialIndex: 0,
|
|
|
|
length: 3,
|
|
|
|
vsync: this,
|
|
|
|
);
|
|
|
|
tabController.addListener(() => widget.setSelectedClientsTab(tabController.index));
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 00:13:54 +02:00
|
|
|
List<AutoClient> generateClientsList(List<AutoClient> clients, List<String> ips) {
|
|
|
|
return clients.where((client) => ips.contains(client.ip)).toList();
|
|
|
|
}
|
|
|
|
|
2022-09-28 15:47:43 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
|
|
|
|
switch (serversProvider.clients.loadStatus) {
|
|
|
|
case 0:
|
2022-10-06 00:23:19 +02:00
|
|
|
return Column(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
2022-10-06 00:26:17 +02:00
|
|
|
AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.clients),
|
|
|
|
centerTitle: true,
|
2022-10-06 00:23:19 +02:00
|
|
|
),
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2022-09-28 15:47:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return DefaultTabController(
|
|
|
|
length: 3,
|
|
|
|
child: NestedScrollView(
|
|
|
|
headerSliverBuilder: ((context, innerBoxIsScrolled) {
|
|
|
|
return [
|
|
|
|
SliverAppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.clients),
|
|
|
|
centerTitle: true,
|
|
|
|
pinned: true,
|
|
|
|
floating: true,
|
|
|
|
forceElevated: innerBoxIsScrolled,
|
|
|
|
bottom: TabBar(
|
2022-09-29 23:12:24 +02:00
|
|
|
controller: tabController,
|
2022-09-28 15:47:43 +02:00
|
|
|
tabs: [
|
|
|
|
Tab(
|
|
|
|
icon: const Icon(Icons.devices),
|
2022-09-29 21:26:18 +02:00
|
|
|
text: AppLocalizations.of(context)!.activeClients,
|
2022-09-28 15:47:43 +02:00
|
|
|
),
|
|
|
|
Tab(
|
2022-10-07 01:12:48 +02:00
|
|
|
icon: const Icon(Icons.add),
|
|
|
|
text: AppLocalizations.of(context)!.added,
|
2022-09-28 15:47:43 +02:00
|
|
|
),
|
|
|
|
Tab(
|
|
|
|
icon: const Icon(Icons.block),
|
|
|
|
text: AppLocalizations.of(context)!.blocked,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}),
|
2022-09-29 21:26:18 +02:00
|
|
|
body: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(
|
|
|
|
color: Theme.of(context).brightness == Brightness.light
|
|
|
|
? const Color.fromRGBO(220, 220, 220, 1)
|
|
|
|
: const Color.fromRGBO(50, 50, 50, 1)
|
2022-09-29 00:13:54 +02:00
|
|
|
)
|
2022-09-29 21:26:18 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
child: TabBarView(
|
2022-09-29 23:12:24 +02:00
|
|
|
controller: tabController,
|
2022-09-29 21:26:18 +02:00
|
|
|
children: [
|
2022-10-02 18:31:47 +02:00
|
|
|
RefreshIndicator(
|
|
|
|
onRefresh: fetchClients,
|
|
|
|
child: ClientsList(
|
|
|
|
data: serversProvider.clients.data!.autoClientsData,
|
|
|
|
fetchClients: fetchClients,
|
|
|
|
),
|
2022-09-29 21:26:18 +02:00
|
|
|
),
|
2022-10-02 18:31:47 +02:00
|
|
|
RefreshIndicator(
|
|
|
|
onRefresh: fetchClients,
|
2022-10-07 01:12:48 +02:00
|
|
|
child: AddedList(
|
|
|
|
data: serversProvider.clients.data!.clients,
|
2022-10-02 18:31:47 +02:00
|
|
|
fetchClients: fetchClients,
|
2022-10-07 01:12:48 +02:00
|
|
|
)
|
2022-09-29 21:26:18 +02:00
|
|
|
),
|
2022-10-02 18:31:47 +02:00
|
|
|
RefreshIndicator(
|
|
|
|
onRefresh: fetchClients,
|
2022-10-07 01:12:48 +02:00
|
|
|
child: BlockedList(
|
2022-10-02 18:31:47 +02:00
|
|
|
data: serversProvider.clients.data!.clientsAllowedBlocked!.disallowedClients,
|
|
|
|
fetchClients: fetchClients,
|
|
|
|
),
|
2022-09-29 21:26:18 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-09-28 15:47:43 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
case 2:
|
2022-10-06 00:23:19 +02:00
|
|
|
return Column(
|
|
|
|
children: [
|
2022-10-06 00:26:17 +02:00
|
|
|
AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.clients),
|
|
|
|
centerTitle: true,
|
2022-10-06 00:23:19 +02:00
|
|
|
),
|
|
|
|
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,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2022-09-28 15:47:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|