adguard-home-manager/lib/screens/logs/log_tile.dart

642 lines
26 KiB
Dart
Raw Normal View History

2022-09-30 23:33:57 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-09-30 23:33:57 +02:00
2024-03-09 13:44:07 +01:00
import 'package:adguard_home_manager/screens/clients/client/client_screen_functions.dart';
import 'package:adguard_home_manager/screens/clients/client/client_screen.dart';
import 'package:adguard_home_manager/widgets/options_menu.dart';
2022-10-01 02:00:51 +02:00
import 'package:adguard_home_manager/providers/status_provider.dart';
import 'package:adguard_home_manager/providers/filtering_provider.dart';
import 'package:adguard_home_manager/classes/process_modal.dart';
import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/functions/copy_clipboard.dart';
2024-03-09 13:44:07 +01:00
import 'package:adguard_home_manager/models/clients.dart';
import 'package:adguard_home_manager/providers/clients_provider.dart';
import 'package:adguard_home_manager/models/menu_option.dart';
2022-10-03 22:41:19 +02:00
import 'package:adguard_home_manager/providers/app_config_provider.dart';
2022-10-02 04:25:11 +02:00
import 'package:adguard_home_manager/functions/get_filtered_status.dart';
2022-09-30 23:33:57 +02:00
import 'package:adguard_home_manager/models/logs.dart';
import 'package:adguard_home_manager/functions/format_time.dart';
class LogTile extends StatelessWidget {
final Log log;
final int length;
final int index;
2023-05-01 15:58:06 +02:00
final bool? isLogSelected;
final void Function(Log) onLogTap;
2023-05-02 14:01:49 +02:00
final bool? useAlwaysNormalTile;
2023-10-29 02:19:00 +01:00
final bool twoColumns;
2022-09-30 23:33:57 +02:00
const LogTile({
super.key,
2022-09-30 23:33:57 +02:00
required this.log,
required this.length,
2023-05-01 15:58:06 +02:00
required this.index,
this.isLogSelected,
2023-05-02 14:01:49 +02:00
required this.onLogTap,
2023-10-29 02:19:00 +01:00
this.useAlwaysNormalTile,
required this.twoColumns,
});
2022-09-30 23:33:57 +02:00
@override
Widget build(BuildContext context) {
2022-10-03 22:41:19 +02:00
final appConfigProvider = Provider.of<AppConfigProvider>(context);
final statusProvider = Provider.of<StatusProvider>(context);
2024-03-09 13:44:07 +01:00
final clientsProvider = Provider.of<ClientsProvider>(context);
final filteringProvider = Provider.of<FilteringProvider>(context);
2022-09-30 23:33:57 +02:00
Widget logStatusWidget({
required IconData icon,
required Color color,
required String text
}) {
2023-01-25 20:51:23 +01:00
return SizedBox(
2023-05-01 21:34:00 +02:00
width: 80,
2022-10-21 12:33:22 +02:00
child: Column(
children: [
Icon(
icon,
2022-09-30 23:33:57 +02:00
color: color,
2022-10-21 12:33:22 +02:00
size: 14,
),
const SizedBox(height: 5),
Text(
text,
textAlign: TextAlign.center,
style: TextStyle(
color: color,
fontSize: 12
),
)
]
),
2022-09-30 23:33:57 +02:00
);
}
Widget generateLogStatus() {
final filter = getFilteredStatus(context, appConfigProvider, log.reason, false);
2022-10-02 04:25:11 +02:00
return logStatusWidget(
icon: filter['icon'],
color: filter['color'],
text: filter['label'],
);
2022-09-30 23:33:57 +02:00
}
2023-05-20 21:12:52 +02:00
String logClient() {
if (appConfigProvider.showIpLogs == true) {
return log.client;
}
else if (log.clientInfo != null && log.clientInfo!.name != "") {
return log.clientInfo!.name;
}
else {
return log.client;
}
}
void blockUnblock({required String domain, required String newStatus}) async {
final ProcessModal processModal = ProcessModal();
processModal.open(AppLocalizations.of(context)!.savingUserFilters);
final rules = await statusProvider.blockUnblockDomain(
domain: domain,
newStatus: newStatus
);
processModal.close();
if (!context.mounted) return;
if (rules == true) {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.userFilteringRulesUpdated,
color: Colors.green
);
}
else {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.userFilteringRulesNotUpdated,
color: Colors.red
);
}
}
2024-03-09 13:44:07 +01:00
void confirmAddClient(Client client) async {
ProcessModal processModal = ProcessModal();
processModal.open(AppLocalizations.of(context)!.addingClient);
final result = await clientsProvider.addClient(client);
processModal.close();
2024-04-24 17:52:20 +02:00
if (!context.mounted) return;
2024-03-09 13:44:07 +01:00
if (result == true) {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAddedSuccessfully,
color: Colors.green
);
}
else {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientNotAdded,
color: Colors.red
);
}
}
void blockUnblockRuleClient() async {
ProcessModal processModal = ProcessModal();
processModal.open(AppLocalizations.of(context)!.addingRule);
final rule = isDomainBlocked(log.reason) == true
? "@@||${log.question.name}^\$client='${log.client}'"
: "||${log.question.name}^\$client='${log.client}'";
final result = await filteringProvider.addCustomRule(rule);
processModal.close();
if (!context.mounted) return;
if (result == true) {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: isDomainBlocked(log.reason) == true
? AppLocalizations.of(context)!.domainUnblockedThisClient(log.question.name!)
: AppLocalizations.of(context)!.domainBlockedThisClient(log.question.name!),
color: Colors.green
);
}
else {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.ruleNotAdded,
color: Colors.red
);
}
}
void allowDisallowClient() async {
ProcessModal processModal = ProcessModal();
processModal.open(
log.clientInfo!.disallowed == true
? AppLocalizations.of(context)!.allowingClient
: AppLocalizations.of(context)!.disallowingClient
);
final result = await clientsProvider.addClientList(
log.client,
log.clientInfo!.disallowed == true
? AccessSettingsList.allowed
: AccessSettingsList.disallowed
);
processModal.close();
if (!context.mounted) return;
if (result.successful == true) {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAddedSuccessfully,
color: Colors.green
);
}
else if (result.successful == false && result.content == 'client_another_list') {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.clientAnotherList,
color: Colors.red
);
}
else {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.changesNotSaved,
color: Colors.red
);
}
}
2024-03-09 13:44:07 +01:00
void openAddClient() {
Future.delayed(
const Duration(milliseconds: 0),
2024-04-24 17:52:20 +02:00
() {
if (!context.mounted) return;
openClientFormModal(
context: context,
width: MediaQuery.of(context).size.width,
onConfirm: confirmAddClient,
initialData: ClientInitialData(name: "Client ${log.client}", ip: log.client)
);
}
2024-03-09 13:44:07 +01:00
);
}
final domainBlocked = isDomainBlocked(log.reason);
2023-10-29 02:19:00 +01:00
if (twoColumns && !(useAlwaysNormalTile == true)) {
2023-05-01 15:58:06 +02:00
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
2023-05-04 22:38:37 +02:00
child: InkWell(
2023-05-01 15:58:06 +02:00
borderRadius: BorderRadius.circular(28),
child: OptionsMenu(
onTap: (_) => onLogTap(log),
2023-05-04 22:38:37 +02:00
borderRadius: BorderRadius.circular(28),
2023-12-20 15:45:42 +01:00
options: (_) => [
if (log.question.name != null) MenuOption(
title: domainBlocked == true
? AppLocalizations.of(context)!.unblockDomain
: AppLocalizations.of(context)!.blockDomain,
icon: domainBlocked == true
? Icons.check_rounded
: Icons.block_rounded,
action: () => blockUnblock(
domain: log.question.name!,
newStatus: domainBlocked == true ? 'unblock' : 'block'
)
),
2024-03-10 20:30:25 +01:00
if (filteringProvider.filtering != null) MenuOption(
title: domainBlocked == true
? AppLocalizations.of(context)!.unblockThisClientOnly
: AppLocalizations.of(context)!.blockThisClientOnly,
icon: domainBlocked == true
? Icons.check_rounded
: Icons.block_rounded,
action: blockUnblockRuleClient
),
2024-03-09 13:44:07 +01:00
if (log.clientInfo?.name == "") MenuOption(
title: AppLocalizations.of(context)!.addPersistentClient,
icon: Icons.add_rounded,
action: openAddClient
),
MenuOption(
title: log.clientInfo!.disallowed == true
? AppLocalizations.of(context)!.allowThisClient
: AppLocalizations.of(context)!.disallowThisClient,
icon: log.clientInfo!.disallowed == true
? Icons.check_rounded
: Icons.block_rounded,
action: allowDisallowClient
),
2023-12-20 15:45:42 +01:00
if (log.question.name != null) MenuOption(
title: AppLocalizations.of(context)!.copyClipboard,
icon: Icons.copy_rounded,
2023-12-20 15:45:42 +01:00
action: () => copyToClipboard(value: log.question.name!, successMessage: AppLocalizations.of(context)!.copiedClipboard)
)
],
2023-05-01 15:58:06 +02:00
child: Container(
width: double.maxFinite,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(28),
color: isLogSelected == true
? Theme.of(context).colorScheme.primaryContainer
: null
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Row(
mainAxisSize: MainAxisSize.min,
2022-10-21 12:33:22 +02:00
children: [
2023-05-01 15:58:06 +02:00
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
2023-05-20 20:31:48 +02:00
log.question.name ?? "N/A",
2023-05-01 15:58:06 +02:00
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
color: Theme.of(context).colorScheme.onSurface,
),
2022-10-21 12:33:22 +02:00
),
2023-05-01 15:58:06 +02:00
const SizedBox(height: 5),
2023-05-20 21:12:52 +02:00
if (log.client.length <= 15 && appConfigProvider.showTimeLogs == false) Row(
2023-05-01 15:58:06 +02:00
children: [
...[
Icon(
Icons.smartphone_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
Flexible(
child: Text(
2023-05-20 21:12:52 +02:00
logClient(),
2023-05-01 15:58:06 +02:00
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 14,
height: 1.4,
fontWeight: FontWeight.w400,
),
),
)
],
2023-05-20 21:12:52 +02:00
const SizedBox(width: 16),
2023-05-01 15:58:06 +02:00
...[
Icon(
Icons.schedule_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
Flexible(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
),
],
],
2022-10-21 12:33:22 +02:00
),
2023-05-20 21:12:52 +02:00
if (log.client.length > 15 || appConfigProvider.showTimeLogs == true) Column(
2023-05-01 15:58:06 +02:00
children: [
Row(
children: [
Icon(
Icons.smartphone_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
Flexible(
child: Text(
2023-05-20 21:12:52 +02:00
logClient(),
2023-05-01 15:58:06 +02:00
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.schedule_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
SizedBox(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
2023-05-20 21:12:52 +02:00
if (appConfigProvider.showTimeLogs == true && log.elapsedMs != '') ...[
2023-05-01 15:58:06 +02:00
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.timer,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
SizedBox(
child: Text(
"${double.parse(log.elapsedMs).toStringAsFixed(2)} ms",
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
],
],
),
],
),
2023-05-01 15:58:06 +02:00
)
2022-10-21 12:33:22 +02:00
],
2022-09-30 23:33:57 +02:00
),
2023-05-01 15:58:06 +02:00
),
generateLogStatus()
],
)
),
),
),
);
}
else {
return Material(
color: Colors.transparent,
child: OptionsMenu(
onTap: (_) => onLogTap(log),
options: (_) => [
if (log.question.name != null) MenuOption(
title: domainBlocked == true
? AppLocalizations.of(context)!.unblockDomain
: AppLocalizations.of(context)!.blockDomain,
icon: domainBlocked == true
? Icons.check_rounded
: Icons.block_rounded,
action: () => blockUnblock(
domain: log.question.name!,
newStatus: domainBlocked == true ? 'unblock' : 'block'
)
),
2024-03-10 20:30:25 +01:00
if (filteringProvider.filtering != null) MenuOption(
title: domainBlocked == true
? AppLocalizations.of(context)!.unblockThisClientOnly
: AppLocalizations.of(context)!.blockThisClientOnly,
icon: domainBlocked == true
? Icons.check_rounded
: Icons.block_rounded,
action: blockUnblockRuleClient
),
2024-03-09 13:44:07 +01:00
if (log.clientInfo?.name == "") MenuOption(
title: AppLocalizations.of(context)!.addPersistentClient,
icon: Icons.add_rounded,
action: openAddClient
),
MenuOption(
title: log.clientInfo!.disallowed == true
? AppLocalizations.of(context)!.allowThisClient
: AppLocalizations.of(context)!.disallowThisClient,
icon: log.clientInfo!.disallowed == true
? Icons.check_rounded
: Icons.block_rounded,
action: allowDisallowClient
),
2023-11-27 01:04:23 +01:00
if (log.question.name != null) MenuOption(
title: AppLocalizations.of(context)!.copyClipboard,
icon: Icons.copy_rounded,
action: () => copyToClipboard(
2023-11-27 01:04:23 +01:00
value: log.question.name!,
successMessage: AppLocalizations.of(context)!.copiedClipboard
)
)
],
2023-05-01 15:58:06 +02:00
child: Container(
width: double.maxFinite,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
2023-05-20 20:31:48 +02:00
log.question.name ?? "N/A",
2023-05-01 15:58:06 +02:00
style: TextStyle(
fontSize: 16,
height: 1.5,
fontWeight: FontWeight.w400,
color: Theme.of(context).colorScheme.onSurface
),
),
const SizedBox(height: 5),
2023-05-20 21:12:52 +02:00
if (log.client.length <= 15 && appConfigProvider.showTimeLogs == false) Row(
2023-05-01 15:58:06 +02:00
children: [
...[
2022-10-21 12:33:22 +02:00
Icon(
Icons.smartphone_rounded,
size: 16,
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
2022-10-21 12:33:22 +02:00
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2022-12-17 22:18:39 +01:00
Flexible(
2022-10-21 12:33:22 +02:00
child: Text(
2023-05-20 21:12:52 +02:00
logClient(),
2022-10-21 12:33:22 +02:00
overflow: TextOverflow.ellipsis,
style: TextStyle(
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
2023-05-01 15:58:06 +02:00
fontSize: 14,
height: 1.4,
fontWeight: FontWeight.w400,
2022-10-21 12:33:22 +02:00
),
),
)
],
2023-05-20 21:12:52 +02:00
const SizedBox(width: 16),
2023-05-01 15:58:06 +02:00
...[
Icon(
Icons.schedule_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2023-05-01 15:58:06 +02:00
Flexible(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
),
],
],
),
2023-05-20 21:12:52 +02:00
if (log.client.length > 15 || appConfigProvider.showTimeLogs == true) Column(
2023-05-01 15:58:06 +02:00
children: [
Row(
children: [
Icon(
2023-05-01 15:58:06 +02:00
Icons.smartphone_rounded,
size: 16,
2023-05-01 15:58:06 +02:00
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
2022-12-17 22:18:39 +01:00
Flexible(
child: Text(
2023-05-20 21:12:52 +02:00
logClient(),
overflow: TextOverflow.ellipsis,
style: TextStyle(
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
const SizedBox(height: 10),
Row(
children: [
2023-05-20 21:12:52 +02:00
Row(
children: [
Icon(
Icons.schedule_rounded,
size: 16,
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
SizedBox(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
if (appConfigProvider.showTimeLogs == true && log.elapsedMs != '') ...[
const SizedBox(width: 16),
Row(
children: [
Icon(
Icons.timer,
size: 16,
2023-05-01 15:58:06 +02:00
color: Theme.of(context).listTileTheme.textColor,
),
2023-05-20 21:12:52 +02:00
const SizedBox(width: 8),
SizedBox(
child: Text(
"${double.parse(log.elapsedMs).toStringAsFixed(2)} ms",
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
2023-05-01 15:58:06 +02:00
],
2023-05-20 21:12:52 +02:00
],
),
],
2023-05-01 15:58:06 +02:00
),
],
),
2022-10-21 12:33:22 +02:00
),
2023-05-01 15:58:06 +02:00
const SizedBox(width: 10),
generateLogStatus()
],
),
2022-09-30 23:33:57 +02:00
),
),
2023-05-01 15:58:06 +02:00
);
}
2022-09-30 23:33:57 +02:00
}
}