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

461 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/screens/home/top_items_options_modal.dart';
2022-10-01 02:00:51 +02:00
import 'package:adguard_home_manager/functions/copy_clipboard.dart';
2023-01-04 14:28:00 +01:00
import 'package:adguard_home_manager/functions/block_unblock_domain.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';
2023-01-04 14:28:00 +01:00
import 'package:adguard_home_manager/functions/snackbar.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;
2022-09-30 23:33:57 +02:00
const LogTile({
Key? key,
required this.log,
required this.length,
2023-05-01 15:58:06 +02:00
required this.index,
this.isLogSelected,
required this.onLogTap
2022-09-30 23:33:57 +02:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-10-03 22:41:19 +02:00
final appConfigProvider = Provider.of<AppConfigProvider>(context);
2022-09-30 23:33:57 +02:00
final width = MediaQuery.of(context).size.width;
Widget logStatusWidget({
required IconData icon,
required Color color,
required String text
}) {
2023-01-25 20:51:23 +01:00
return SizedBox(
width: 70,
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-01-04 14:28:00 +01:00
void changeBlockStatus(String status) async {
final result = await blockUnblock(context, log.question.name, status);
showSnacbkar(
context: context,
appConfigProvider: appConfigProvider,
label: result['message'],
color: result['success'] == true ? Colors.green : Colors.red
);
}
void openOptionsModal(Log log) {
showDialog(
context: context,
builder: (context) => TopItemsOptionsModal(
isBlocked: getFilteredStatus(context, appConfigProvider, log.reason, false)['color'] == Colors.red
? true : false,
2023-01-04 14:28:00 +01:00
changeStatus: changeBlockStatus,
copyToClipboard: () => copyToClipboard(
context: context,
value: log.question.name,
successMessage: AppLocalizations.of(context)!.domainCopiedClipboard
2023-02-04 20:47:08 +01:00
),
type: 'topQueriedDomains', // topQueriedDomains can also be used here. It's the same
)
);
}
2023-05-01 15:58:06 +02:00
if (width > 1100) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(28),
child: InkWell(
borderRadius: BorderRadius.circular(28),
onTap: () => onLogTap(log),
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(
log.question.name,
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),
if (log.client.length <= 15 && appConfigProvider.showNameTimeLogs == false) Row(
children: [
...[
Icon(
Icons.smartphone_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 5),
Flexible(
child: Text(
log.client,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 14,
height: 1.4,
fontWeight: FontWeight.w400,
),
),
)
],
const SizedBox(width: 15),
...[
Icon(
Icons.schedule_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 5),
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-01 15:58:06 +02:00
if (log.client.length > 15 || appConfigProvider.showNameTimeLogs == true) Column(
children: [
Row(
children: [
Icon(
Icons.smartphone_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 15),
Flexible(
child: Text(
log.client,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
if (appConfigProvider.showNameTimeLogs == true && log.clientInfo!.name != '') ...[
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.badge_rounded,
size: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 15),
Flexible(
child: Text(
log.clientInfo!.name,
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,
),
const SizedBox(width: 15),
SizedBox(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
if (appConfigProvider.showNameTimeLogs == true && log.elapsedMs != '') ...[
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.timer,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 15),
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: InkWell(
onTap: () => onLogTap(log),
onLongPress: () => openOptionsModal(log),
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(
log.question.name,
style: TextStyle(
fontSize: 16,
height: 1.5,
fontWeight: FontWeight.w400,
color: Theme.of(context).colorScheme.onSurface
),
),
const SizedBox(height: 5),
if (log.client.length <= 15 && appConfigProvider.showNameTimeLogs == false) Row(
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-01 15:58:06 +02:00
const SizedBox(width: 5),
2022-12-17 22:18:39 +01:00
Flexible(
2022-10-21 12:33:22 +02:00
child: Text(
log.client,
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-01 15:58:06 +02:00
const SizedBox(width: 15),
...[
Icon(
Icons.schedule_rounded,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 5),
Flexible(
child: Text(
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
),
],
],
),
if (log.client.length > 15 || appConfigProvider.showNameTimeLogs == true) Column(
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,
),
const SizedBox(width: 15),
2022-12-17 22:18:39 +01:00
Flexible(
child: Text(
2023-05-01 15:58:06 +02:00
log.client,
overflow: TextOverflow.ellipsis,
style: TextStyle(
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
2023-05-01 15:58:06 +02:00
if (appConfigProvider.showNameTimeLogs == true && log.clientInfo!.name != '') ...[
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.badge_rounded,
size: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
2022-10-21 12:33:22 +02:00
),
2023-05-01 15:58:06 +02:00
const SizedBox(width: 15),
Flexible(
child: Text(
log.clientInfo!.name,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
2022-10-21 12:33:22 +02:00
],
const SizedBox(height: 10),
Row(
children: [
Icon(
2023-05-01 15:58:06 +02:00
Icons.schedule_rounded,
size: 16,
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 15),
SizedBox(
child: Text(
2023-05-01 15:58:06 +02:00
convertTimestampLocalTimezone(log.time, 'HH:mm:ss'),
overflow: TextOverflow.ellipsis,
style: TextStyle(
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.textColor,
fontSize: 13
),
),
)
],
),
2023-05-01 15:58:06 +02:00
if (appConfigProvider.showNameTimeLogs == true && log.elapsedMs != '') ...[
const SizedBox(height: 10),
Row(
children: [
Icon(
Icons.timer,
size: 16,
color: Theme.of(context).listTileTheme.textColor,
),
const SizedBox(width: 15),
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
),
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
}
}