2022-09-28 13:59:09 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-10-25 20:02:48 +02:00
|
|
|
import 'package:percent_indicator/percent_indicator.dart';
|
2022-09-28 13:59:09 +02:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2022-12-31 17:23:19 +01:00
|
|
|
import 'package:adguard_home_manager/screens/home/top_items_options_modal.dart';
|
2022-10-22 03:16:08 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/custom_list_tile.dart';
|
|
|
|
|
2022-12-31 17:33:46 +01:00
|
|
|
import 'package:adguard_home_manager/models/applied_filters.dart';
|
2023-01-04 14:08:25 +01:00
|
|
|
import 'package:adguard_home_manager/functions/copy_clipboard.dart';
|
2022-12-31 17:33:46 +01:00
|
|
|
import 'package:adguard_home_manager/providers/logs_provider.dart';
|
2022-12-31 17:23:19 +01:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/models/filtering_status.dart';
|
2022-09-28 13:59:09 +02:00
|
|
|
import 'package:adguard_home_manager/functions/number_format.dart';
|
2022-10-03 22:41:19 +02:00
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
2022-09-28 13:59:09 +02:00
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
class TopItemsScreen extends StatefulWidget {
|
2022-09-28 13:59:09 +02:00
|
|
|
final String type;
|
|
|
|
final String title;
|
2022-10-22 03:16:08 +02:00
|
|
|
final bool? isClient;
|
2022-10-25 19:41:49 +02:00
|
|
|
final List<Map<String, dynamic>> data;
|
2022-09-28 13:59:09 +02:00
|
|
|
|
|
|
|
const TopItemsScreen({
|
|
|
|
Key? key,
|
|
|
|
required this.type,
|
|
|
|
required this.title,
|
2022-10-22 03:16:08 +02:00
|
|
|
this.isClient,
|
2022-10-25 19:41:49 +02:00
|
|
|
required this.data,
|
2022-09-28 13:59:09 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2022-10-25 19:41:49 +02:00
|
|
|
State<TopItemsScreen> createState() => _TopItemsScreenState();
|
|
|
|
}
|
2022-09-28 13:59:09 +02:00
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
class _TopItemsScreenState extends State<TopItemsScreen> {
|
|
|
|
bool searchActive = false;
|
|
|
|
final TextEditingController searchController = TextEditingController();
|
2022-09-28 13:59:09 +02:00
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
List<Map<String, dynamic>> data = [];
|
|
|
|
List<Map<String, dynamic>> screenData = [];
|
2022-09-28 13:59:09 +02:00
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
void search(String value) {
|
|
|
|
List<Map<String, dynamic>> newValues = widget.data.where((item) => item.keys.toList()[0].contains(value)).toList();
|
|
|
|
setState(() => screenData = newValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
data = widget.data;
|
|
|
|
screenData = widget.data;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
2022-12-31 17:33:46 +01:00
|
|
|
final logsProvider = Provider.of<LogsProvider>(context);
|
2022-09-28 13:59:09 +02:00
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
for (var element in data) {
|
|
|
|
total = total + int.parse(element.values.toList()[0].toString());
|
|
|
|
}
|
|
|
|
|
2022-12-31 17:23:19 +01:00
|
|
|
void blockUnblock(String domain, String newStatus) async {
|
|
|
|
final ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.savingUserFilters);
|
|
|
|
|
|
|
|
final rules = await getFilteringRules(server: serversProvider.selectedServer!);
|
|
|
|
|
|
|
|
if (rules['result'] == 'success') {
|
|
|
|
FilteringStatus oldStatus = serversProvider.serverStatus.data!.filteringStatus;
|
|
|
|
|
|
|
|
List<String> newRules = rules['data'].userRules.where((d) => !d.contains(domain)).toList();
|
|
|
|
if (newStatus == 'block') {
|
|
|
|
newRules.add("||$domain^");
|
|
|
|
}
|
|
|
|
else if (newStatus == 'unblock') {
|
|
|
|
newRules.add("@@||$domain^");
|
|
|
|
}
|
|
|
|
FilteringStatus newObj = serversProvider.serverStatus.data!.filteringStatus;
|
|
|
|
newObj.userRules = newRules;
|
|
|
|
serversProvider.setFilteringStatus(newObj);
|
|
|
|
|
|
|
|
final result = await postFilteringRules(server: serversProvider.selectedServer!, data: {'rules': newRules});
|
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.userFilteringRulesUpdated),
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
serversProvider.setFilteringStatus(oldStatus);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(rules['log']);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool? getIsBlocked() {
|
|
|
|
if (widget.type == 'topBlockedDomains') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (widget.type == 'topQueriedDomains') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void openOptionsModal(String domain) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => TopItemsOptionsModal(
|
|
|
|
isBlocked: getIsBlocked(),
|
|
|
|
changeStatus: (String status) => blockUnblock(domain, status),
|
2023-01-04 14:08:25 +01:00
|
|
|
copyToClipboard: () => copyToClipboard(
|
|
|
|
context: context,
|
|
|
|
value: domain,
|
|
|
|
successMessage: AppLocalizations.of(context)!.domainCopiedClipboard
|
|
|
|
)
|
2022-12-31 17:23:19 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-28 13:59:09 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2022-10-25 19:41:49 +02:00
|
|
|
title: searchActive == true
|
2022-10-25 21:17:05 +02:00
|
|
|
? 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(
|
2022-10-25 19:41:49 +02:00
|
|
|
fontWeight: FontWeight.normal,
|
|
|
|
fontSize: 18
|
|
|
|
),
|
|
|
|
),
|
2022-10-25 21:17:05 +02:00
|
|
|
)
|
2022-10-25 19:41:49 +02:00
|
|
|
: Text(widget.title),
|
|
|
|
leading: searchActive == true ?
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => setState(() {
|
|
|
|
searchActive = false;
|
|
|
|
searchController.text = '';
|
|
|
|
screenData = data;
|
|
|
|
}),
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
tooltip: AppLocalizations.of(context)!.exitSearch,
|
|
|
|
) : null,
|
|
|
|
actions: [
|
|
|
|
if (searchActive == false) IconButton(
|
|
|
|
onPressed: () => setState(() => searchActive = true),
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
tooltip: AppLocalizations.of(context)!.search,
|
|
|
|
),
|
|
|
|
if (searchActive == true) IconButton(
|
|
|
|
onPressed: () => setState(() {
|
|
|
|
searchController.text = '';
|
|
|
|
screenData = data;
|
|
|
|
}),
|
|
|
|
icon: const Icon(Icons.clear_rounded),
|
|
|
|
tooltip: AppLocalizations.of(context)!.clearSearch,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10)
|
|
|
|
],
|
2022-10-26 12:30:15 +02:00
|
|
|
bottom: PreferredSize(
|
|
|
|
preferredSize: const Size(double.maxFinite, 1),
|
|
|
|
child: Container(
|
|
|
|
width: double.maxFinite,
|
|
|
|
height: 1,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: searchActive == true
|
|
|
|
? Colors.grey.withOpacity(0.5)
|
|
|
|
: Colors.transparent
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-09-28 13:59:09 +02:00
|
|
|
),
|
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: () async {
|
|
|
|
final result = await getServerStatus(serversProvider.selectedServer!);
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
serversProvider.setServerStatusData(result['data']);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 22:41:19 +02:00
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-09-28 13:59:09 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.serverStatusNotRefreshed),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2022-10-25 19:41:49 +02:00
|
|
|
child: screenData.isNotEmpty
|
|
|
|
? ListView.builder(
|
|
|
|
itemCount: screenData.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
String? name;
|
|
|
|
if (widget.isClient != null && widget.isClient == true) {
|
|
|
|
try {
|
|
|
|
name = serversProvider.serverStatus.data!.clients.firstWhere((c) => c.ids.contains(screenData[index].keys.toList()[0])).name;
|
|
|
|
} catch (e) {
|
|
|
|
// ---- //
|
|
|
|
}
|
|
|
|
}
|
2022-10-22 03:16:08 +02:00
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
return CustomListTile(
|
2022-12-31 17:33:46 +01:00
|
|
|
onTap: widget.type == 'topQueriedDomains' || widget.type == 'topBlockedDomains'
|
|
|
|
? () {
|
|
|
|
logsProvider.setSearchText(screenData[index].keys.toList()[0]);
|
|
|
|
logsProvider.setAppliedFilters(
|
|
|
|
AppliedFiters(
|
|
|
|
selectedResultStatus: 'all',
|
|
|
|
searchText: screenData[index].keys.toList()[0]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
Navigator.pop(context);
|
|
|
|
appConfigProvider.setSelectedScreen(2);
|
|
|
|
}
|
|
|
|
: null,
|
2022-12-31 17:23:19 +01:00
|
|
|
onLongPress: widget.type == 'topQueriedDomains' || widget.type == 'topBlockedDomains'
|
|
|
|
? () => openOptionsModal(screenData[index].keys.toList()[0])
|
|
|
|
: null,
|
2022-10-25 19:41:49 +02:00
|
|
|
title: screenData[index].keys.toList()[0],
|
2022-11-04 17:04:25 +01:00
|
|
|
trailing: Text(
|
|
|
|
screenData[index].values.toList()[0].toString(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant
|
|
|
|
),
|
|
|
|
),
|
2022-10-25 19:41:49 +02:00
|
|
|
subtitleWidget: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
if (name != null) ...[
|
|
|
|
Text(
|
|
|
|
name,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14,
|
2022-11-04 17:04:25 +01:00
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
2022-10-25 19:41:49 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
],
|
2022-10-25 20:02:48 +02:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: 50,
|
|
|
|
child: Text(
|
|
|
|
"${doubleFormat((screenData[index].values.toList()[0]/total*100), Platform.localeName)}%",
|
|
|
|
style: TextStyle(
|
2022-11-05 02:35:35 +01:00
|
|
|
color: Theme.of(context).listTileTheme.textColor
|
2022-10-25 20:02:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
Flexible(
|
|
|
|
child: LinearPercentIndicator(
|
|
|
|
animation: true,
|
|
|
|
lineHeight: 4,
|
|
|
|
animationDuration: 500,
|
|
|
|
curve: Curves.easeOut,
|
|
|
|
percent: screenData[index].values.toList()[0]/total,
|
|
|
|
barRadius: const Radius.circular(5),
|
|
|
|
progressColor: Theme.of(context).primaryColor,
|
2022-11-04 17:04:25 +01:00
|
|
|
backgroundColor: Theme.of(context).primaryColor.withOpacity(0.15),
|
2022-10-25 20:02:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
],
|
2022-10-22 03:16:08 +02:00
|
|
|
),
|
2022-10-25 19:41:49 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
: Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.noItemsSearch,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Colors.grey
|
2022-10-22 03:16:08 +02:00
|
|
|
),
|
2022-10-25 19:41:49 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-09-28 13:59:09 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|