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

478 lines
20 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
2023-05-01 15:58:06 +02:00
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
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/classes/process_modal.dart';
import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/functions/copy_clipboard.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);
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
);
}
}
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),
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'
)
),
MenuOption(
title: AppLocalizations.of(context)!.copyClipboard,
icon: Icons.copy_rounded,
action: (v) => copyToClipboard(value: v, 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'
)
),
2023-11-27 01:04:23 +01:00
if (log.question.name != null) MenuOption(
title: AppLocalizations.of(context)!.copyClipboard,
icon: Icons.copy_rounded,
2023-11-27 01:04:23 +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),
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
}
}