2022-12-31 17:11:57 +01:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2023-05-01 14:38:46 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-09-27 15:07:22 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-22 03:16:08 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-09-27 15:07:22 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2023-05-04 22:38:37 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/domain_options.dart';
|
|
|
|
import 'package:adguard_home_manager/screens/top_items/top_items_modal.dart';
|
2022-09-28 13:59:09 +02:00
|
|
|
import 'package:adguard_home_manager/screens/top_items/top_items.dart';
|
|
|
|
|
2022-12-31 17:33:46 +01:00
|
|
|
import 'package:adguard_home_manager/models/applied_filters.dart';
|
2023-05-24 13:51:22 +02:00
|
|
|
import 'package:adguard_home_manager/providers/status_provider.dart';
|
2022-12-31 17:33:46 +01:00
|
|
|
import 'package:adguard_home_manager/providers/logs_provider.dart';
|
2022-12-31 17:11:57 +01:00
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
2023-06-03 04:04:30 +02:00
|
|
|
|
2022-09-27 15:07:22 +02:00
|
|
|
class TopItems extends StatelessWidget {
|
2022-09-28 13:59:09 +02:00
|
|
|
final String type;
|
2022-09-27 15:07:22 +02:00
|
|
|
final String label;
|
|
|
|
final List<Map<String, dynamic>> data;
|
2022-10-22 03:16:08 +02:00
|
|
|
final bool? clients;
|
2022-09-27 15:07:22 +02:00
|
|
|
|
|
|
|
const TopItems({
|
|
|
|
Key? key,
|
2022-09-28 13:59:09 +02:00
|
|
|
required this.type,
|
2022-09-27 15:07:22 +02:00
|
|
|
required this.label,
|
|
|
|
required this.data,
|
2022-10-22 03:16:08 +02:00
|
|
|
this.clients
|
2022-09-27 15:07:22 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-24 13:51:22 +02:00
|
|
|
final statusProvider = Provider.of<StatusProvider>(context);
|
2022-12-31 17:11:57 +01:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
2022-12-31 17:33:46 +01:00
|
|
|
final logsProvider = Provider.of<LogsProvider>(context);
|
2022-12-31 17:11:57 +01:00
|
|
|
|
2023-05-01 14:38:46 +02:00
|
|
|
final width = MediaQuery.of(context).size.width;
|
|
|
|
|
2022-09-27 22:49:58 +02:00
|
|
|
Widget rowItem(Map<String, dynamic> item) {
|
2022-10-22 03:16:08 +02:00
|
|
|
String? name;
|
|
|
|
if (clients != null && clients == true) {
|
|
|
|
try {
|
2023-05-24 13:51:22 +02:00
|
|
|
name = statusProvider.serverStatus!.clients.firstWhere((c) => c.ids.contains(item.keys.toList()[0])).name;
|
2022-10-22 03:16:08 +02:00
|
|
|
} catch (e) {
|
|
|
|
// ---- //
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 17:11:57 +01:00
|
|
|
return Material(
|
|
|
|
color: Colors.transparent,
|
2023-05-04 22:38:37 +02:00
|
|
|
child: DomainOptions(
|
|
|
|
item: item.keys.toList()[0],
|
|
|
|
isClient: type == 'topClients',
|
|
|
|
isBlocked: type == 'topBlockedDomains',
|
2023-02-04 20:47:08 +01:00
|
|
|
onTap: () {
|
|
|
|
if (type == 'topQueriedDomains' || type == 'topBlockedDomains') {
|
2023-02-18 14:52:10 +01:00
|
|
|
logsProvider.setSearchText(item.keys.toList()[0]);
|
2023-02-04 20:47:08 +01:00
|
|
|
logsProvider.setSelectedClients(null);
|
|
|
|
logsProvider.setAppliedFilters(
|
|
|
|
AppliedFiters(
|
|
|
|
selectedResultStatus: 'all',
|
2023-02-18 14:52:10 +01:00
|
|
|
searchText: item.keys.toList()[0],
|
2023-02-04 20:47:08 +01:00
|
|
|
clients: null
|
|
|
|
)
|
|
|
|
);
|
|
|
|
appConfigProvider.setSelectedScreen(2);
|
|
|
|
}
|
|
|
|
else if (type == 'topClients') {
|
2023-02-18 14:52:10 +01:00
|
|
|
logsProvider.setSearchText(null);
|
2023-02-04 20:47:08 +01:00
|
|
|
logsProvider.setSelectedClients([item.keys.toList()[0]]);
|
|
|
|
logsProvider.setAppliedFilters(
|
|
|
|
AppliedFiters(
|
|
|
|
selectedResultStatus: 'all',
|
2023-02-18 14:52:10 +01:00
|
|
|
searchText: null,
|
2023-02-04 20:47:08 +01:00
|
|
|
clients: [item.keys.toList()[0]]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2022-12-31 17:11:57 +01:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 20,
|
|
|
|
vertical: 8
|
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
item.keys.toList()[0],
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
|
|
|
),
|
2022-10-22 03:16:08 +02:00
|
|
|
),
|
2022-12-31 17:11:57 +01:00
|
|
|
if (name != null) ...[
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
Text(
|
|
|
|
name,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
item.values.toList()[0].toString(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2022-09-27 22:49:58 +02:00
|
|
|
),
|
2022-12-31 17:11:57 +01:00
|
|
|
),
|
2022-09-27 22:49:58 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-25 19:41:49 +02:00
|
|
|
List<Map<String, dynamic>> generateData() {
|
|
|
|
switch (type) {
|
|
|
|
case 'topQueriedDomains':
|
2023-05-24 13:51:22 +02:00
|
|
|
return statusProvider.serverStatus!.stats.topQueriedDomains;
|
2022-10-25 19:41:49 +02:00
|
|
|
|
|
|
|
case 'topBlockedDomains':
|
2023-05-24 13:51:22 +02:00
|
|
|
return statusProvider.serverStatus!.stats.topBlockedDomains;
|
2022-10-25 19:41:49 +02:00
|
|
|
|
|
|
|
case 'topClients':
|
2023-05-24 13:51:22 +02:00
|
|
|
return statusProvider.serverStatus!.stats.topClients;
|
2022-10-25 19:41:49 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 15:07:22 +02:00
|
|
|
return SizedBox(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
label,
|
2022-11-04 22:56:00 +01:00
|
|
|
style: TextStyle(
|
2022-09-27 15:07:22 +02:00
|
|
|
fontSize: 18,
|
2022-11-04 22:56:00 +01:00
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
2022-09-27 15:07:22 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
2022-10-17 01:08:17 +02:00
|
|
|
if (data.isEmpty) Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
bottom: 20,
|
|
|
|
top: 10
|
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.noItems,
|
2023-01-29 21:52:37 +01:00
|
|
|
style: TextStyle(
|
2022-10-17 01:08:17 +02:00
|
|
|
fontSize: 16,
|
2023-01-29 21:52:37 +01:00
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
2022-10-17 01:08:17 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-09-27 15:07:22 +02:00
|
|
|
if (data.isNotEmpty) rowItem(data[0]),
|
|
|
|
if (data.length >= 2) rowItem(data[1]),
|
|
|
|
if (data.length >= 3) rowItem(data[2]),
|
|
|
|
if (data.length >= 4) rowItem(data[3]),
|
|
|
|
if (data.length >= 5) rowItem(data[4]),
|
|
|
|
if (data.length > 5) ...[
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 20),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
2023-05-01 14:38:46 +02:00
|
|
|
onPressed: () => {
|
|
|
|
if (width > 700 || !(Platform.isAndroid || Platform.isIOS)) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (context) => TopItemsModal(
|
|
|
|
type: type,
|
|
|
|
title: label,
|
|
|
|
isClient: clients,
|
|
|
|
data: generateData(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => TopItemsScreen(
|
|
|
|
type: type,
|
|
|
|
title: label,
|
|
|
|
isClient: clients,
|
|
|
|
data: generateData(),
|
|
|
|
)
|
|
|
|
)
|
2022-09-28 13:59:09 +02:00
|
|
|
)
|
2023-05-01 14:38:46 +02:00
|
|
|
}
|
|
|
|
},
|
2022-09-27 15:07:22 +02:00
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Text(AppLocalizations.of(context)!.viewMore),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
const Icon(
|
|
|
|
Icons.arrow_forward,
|
|
|
|
size: 20,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-09-27 17:54:00 +02:00
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
2022-09-27 15:07:22 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|