2023-05-01 21:34:00 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-09-28 15:47:43 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-05-01 21:34:00 +02:00
|
|
|
import 'package:flutter_split_view/flutter_split_view.dart';
|
2022-09-28 15:47:43 +02:00
|
|
|
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-25 21:03:20 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/search_clients.dart';
|
2023-05-01 21:34:00 +02:00
|
|
|
import 'package:adguard_home_manager/screens/clients/logs_list_client.dart';
|
|
|
|
import 'package:adguard_home_manager/screens/clients/clients_desktop_view.dart';
|
2022-10-07 01:12:48 +02:00
|
|
|
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';
|
2023-04-06 18:10:23 +02:00
|
|
|
import 'package:adguard_home_manager/constants/enums.dart';
|
2022-09-28 15:47:43 +02:00
|
|
|
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;
|
2023-04-06 18:10:23 +02:00
|
|
|
final void Function(LoadStatus, bool) setLoadingStatus;
|
2022-09-28 15:47:43 +02:00
|
|
|
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-08 22:16:35 +02:00
|
|
|
final ScrollController scrollController = ScrollController();
|
2022-09-29 23:12:24 +02:00
|
|
|
|
2023-05-01 22:31:36 +02:00
|
|
|
bool searchMode = false;
|
|
|
|
final TextEditingController searchController = TextEditingController();
|
|
|
|
|
2022-10-02 18:31:47 +02:00
|
|
|
Future fetchClients() async {
|
2023-04-06 18:10:23 +02:00
|
|
|
widget.setLoadingStatus(LoadStatus.loading, false);
|
2022-09-28 15:47:43 +02:00
|
|
|
final result = await getClients(widget.server);
|
2022-10-06 00:23:19 +02:00
|
|
|
if (mounted) {
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
widget.setClientsData(result['data']);
|
2023-04-06 18:10:23 +02:00
|
|
|
widget.setLoadingStatus(LoadStatus.loaded, true);
|
2022-10-06 00:23:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget.addLog(result['log']);
|
2023-04-06 18:10:23 +02:00
|
|
|
widget.setLoadingStatus(LoadStatus.error, true);
|
2022-10-06 00:23:19 +02:00
|
|
|
}
|
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,
|
2022-10-09 01:13:55 +02:00
|
|
|
length: 2,
|
2022-09-29 23:12:24 +02:00
|
|
|
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);
|
2023-05-01 21:34:00 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
|
|
|
final width = MediaQuery.of(context).size.width;
|
2022-09-28 15:47:43 +02:00
|
|
|
|
2023-05-01 21:34:00 +02:00
|
|
|
PreferredSizeWidget tabBar() {
|
|
|
|
return TabBar(
|
|
|
|
controller: tabController,
|
|
|
|
unselectedLabelColor: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
tabs: [
|
|
|
|
Tab(
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.devices),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
Text(AppLocalizations.of(context)!.activeClients)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Tab(
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.add_rounded),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
Text(AppLocalizations.of(context)!.added)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget tabBarView() {
|
|
|
|
return TabBarView(
|
|
|
|
controller: tabController,
|
|
|
|
children: [
|
|
|
|
ClientsList(
|
|
|
|
scrollController: scrollController,
|
|
|
|
loadStatus: serversProvider.clients.loadStatus,
|
|
|
|
data: serversProvider.clients.loadStatus == LoadStatus.loaded
|
2023-05-01 22:31:36 +02:00
|
|
|
? serversProvider.filteredActiveClients : [],
|
2023-05-01 21:34:00 +02:00
|
|
|
fetchClients: fetchClients,
|
|
|
|
onClientSelected: (client) => Navigator.push(context, MaterialPageRoute(
|
|
|
|
builder: (context) => LogsListClient(
|
|
|
|
ip: client.ip,
|
|
|
|
serversProvider: serversProvider,
|
|
|
|
appConfigProvider: appConfigProvider
|
|
|
|
)
|
|
|
|
)),
|
|
|
|
splitView: false,
|
|
|
|
),
|
|
|
|
AddedList(
|
|
|
|
scrollController: scrollController,
|
|
|
|
loadStatus: serversProvider.clients.loadStatus,
|
|
|
|
data: serversProvider.clients.loadStatus == LoadStatus.loaded
|
2023-05-01 22:31:36 +02:00
|
|
|
? serversProvider.filteredAddedClients : [],
|
2023-05-01 21:34:00 +02:00
|
|
|
fetchClients: fetchClients,
|
|
|
|
onClientSelected: (client) => Navigator.push(context, MaterialPageRoute(
|
|
|
|
builder: (context) => LogsListClient(
|
|
|
|
ip: client.ids[0],
|
|
|
|
serversProvider: serversProvider,
|
|
|
|
appConfigProvider: appConfigProvider
|
2022-10-08 15:06:40 +02:00
|
|
|
)
|
2023-05-01 21:34:00 +02:00
|
|
|
)),
|
|
|
|
splitView: false,
|
2022-10-08 15:06:40 +02:00
|
|
|
),
|
2023-05-01 21:34:00 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(Platform.isAndroid || Platform.isIOS)) {
|
|
|
|
if (width > 900) {
|
|
|
|
return SplitView.material(
|
|
|
|
breakpoint: 900,
|
|
|
|
hideDivider: true,
|
|
|
|
child: ClientsDesktopView(
|
|
|
|
serversProvider: serversProvider,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
fetchClients: fetchClients,
|
2022-10-08 15:06:40 +02:00
|
|
|
)
|
2023-05-01 21:34:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return DefaultTabController(
|
|
|
|
length: 2,
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.clients),
|
|
|
|
centerTitle: false,
|
|
|
|
actions: [
|
|
|
|
if (serversProvider.clients.loadStatus == LoadStatus.loaded) ...[
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => {
|
|
|
|
Navigator.push(context, MaterialPageRoute(
|
|
|
|
builder: (context) => const SearchClients()
|
|
|
|
))
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
tooltip: AppLocalizations.of(context)!.searchClients,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
bottom: tabBar()
|
|
|
|
),
|
|
|
|
body: tabBarView(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return DefaultTabController(
|
|
|
|
length: 2,
|
|
|
|
child: NestedScrollView(
|
|
|
|
controller: scrollController,
|
|
|
|
headerSliverBuilder: ((context, innerBoxIsScrolled) {
|
|
|
|
return [
|
|
|
|
SliverOverlapAbsorber(
|
|
|
|
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
|
|
|
|
sliver: SliverAppBar(
|
2023-05-01 22:31:36 +02:00
|
|
|
title: searchMode == true
|
|
|
|
? Row(
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
searchMode = false;
|
|
|
|
searchController.text = "";
|
|
|
|
serversProvider.setSearchTermClients(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_back_rounded)
|
|
|
|
),
|
|
|
|
const SizedBox(width: 16),
|
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
|
|
|
controller: searchController,
|
|
|
|
onChanged: (value) => serversProvider.setSearchTermClients(value),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
searchController.text = "";
|
|
|
|
serversProvider.setSearchTermClients(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.clear_rounded)
|
|
|
|
),
|
|
|
|
hintText: AppLocalizations.of(context)!.search,
|
|
|
|
hintStyle: const TextStyle(
|
|
|
|
fontWeight: FontWeight.normal,
|
|
|
|
fontSize: 18
|
|
|
|
),
|
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.normal,
|
|
|
|
fontSize: 18
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
: Text(AppLocalizations.of(context)!.clients),
|
2023-05-01 21:34:00 +02:00
|
|
|
pinned: true,
|
|
|
|
floating: true,
|
|
|
|
centerTitle: false,
|
|
|
|
forceElevated: innerBoxIsScrolled,
|
|
|
|
actions: [
|
2023-05-01 22:31:36 +02:00
|
|
|
if (serversProvider.clients.loadStatus == LoadStatus.loaded && searchMode == false) ...[
|
2023-05-01 21:34:00 +02:00
|
|
|
IconButton(
|
2023-05-01 22:31:36 +02:00
|
|
|
onPressed: () => setState(() => searchMode = true),
|
2023-05-01 21:34:00 +02:00
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
tooltip: AppLocalizations.of(context)!.searchClients,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
bottom: tabBar()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}),
|
|
|
|
body: tabBarView()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
|
|
|
}
|