2022-10-01 03:13:50 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2022-09-30 23:33:57 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-01 03:13:50 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-10-09 17:45:18 +02:00
|
|
|
import 'package:bottom_sheet/bottom_sheet.dart';
|
2022-09-30 23:33:57 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2022-10-01 02:00:51 +02:00
|
|
|
import 'package:adguard_home_manager/screens/logs/log_details_modal.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-10-01 03:13:50 +02:00
|
|
|
import 'package:adguard_home_manager/models/filtering_status.dart';
|
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.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;
|
|
|
|
|
|
|
|
const LogTile({
|
|
|
|
Key? key,
|
|
|
|
required this.log,
|
|
|
|
required this.length,
|
|
|
|
required this.index
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-01 03:13:50 +02:00
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
2022-10-03 22:41:19 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
2022-10-01 03:13:50 +02:00
|
|
|
|
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
|
|
|
|
}) {
|
2022-10-21 12:33:22 +02:00
|
|
|
return Flexible(
|
|
|
|
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() {
|
2022-10-21 12:33:22 +02:00
|
|
|
final filter = getFilteredStatus(context, 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
|
|
|
}
|
2022-10-01 03:13:50 +02:00
|
|
|
|
|
|
|
void blockUnblock(Log log, String newStatus) async {
|
2022-10-03 17:56:01 +02:00
|
|
|
final ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.savingUserFilters);
|
2022-10-01 03:13:50 +02:00
|
|
|
|
2022-10-03 17:56:01 +02:00
|
|
|
final rules = await getFilteringRules(server: serversProvider.selectedServer!);
|
|
|
|
|
|
|
|
if (rules['result'] == 'success') {
|
|
|
|
FilteringStatus oldStatus = serversProvider.filteringStatus!;
|
|
|
|
|
|
|
|
List<String> newRules = rules['data'].userRules.where((domain) => !domain.contains(log.question.name)).toList();
|
|
|
|
if (newStatus == 'block') {
|
|
|
|
newRules.add("||${log.question.name}^");
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
2022-10-03 17:56:01 +02:00
|
|
|
else if (newStatus == 'unblock') {
|
|
|
|
newRules.add("@@||${log.question.name}^");
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
2022-10-03 17:56:01 +02:00
|
|
|
FilteringStatus newObj = serversProvider.filteringStatus!;
|
|
|
|
newObj.userRules = newRules;
|
|
|
|
serversProvider.setFilteringStatus(newObj);
|
2022-10-01 03:13:50 +02:00
|
|
|
|
2022-10-03 17:56:01 +02:00
|
|
|
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 {
|
2022-10-03 22:41:19 +02:00
|
|
|
appConfigProvider.addLog(result['log']);
|
2022-10-03 17:56:01 +02:00
|
|
|
serversProvider.setFilteringStatus(oldStatus);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-10-03 22:41:19 +02:00
|
|
|
appConfigProvider.addLog(rules['log']);
|
2022-10-01 03:13:50 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 23:33:57 +02:00
|
|
|
|
2022-10-01 02:00:51 +02:00
|
|
|
void openLogDetailsModal() {
|
2022-10-20 01:08:39 +02:00
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
2022-10-09 17:45:18 +02:00
|
|
|
showFlexibleBottomSheet(
|
|
|
|
minHeight: 0.6,
|
|
|
|
initHeight: 0.6,
|
|
|
|
maxHeight: 0.95,
|
|
|
|
isCollapsible: true,
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
anchors: [0.95],
|
2022-10-01 02:00:51 +02:00
|
|
|
context: context,
|
2022-10-09 17:45:18 +02:00
|
|
|
builder: (ctx, controller, offset) => LogDetailsModal(
|
|
|
|
scrollController: controller,
|
2022-10-01 03:13:50 +02:00
|
|
|
log: log,
|
|
|
|
blockUnblock: blockUnblock,
|
2022-10-01 02:00:51 +02:00
|
|
|
),
|
2022-10-09 17:45:18 +02:00
|
|
|
bottomSheetColor: Colors.transparent,
|
2022-10-01 02:00:51 +02:00
|
|
|
);
|
|
|
|
}
|
2022-09-30 23:33:57 +02:00
|
|
|
|
|
|
|
return Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: InkWell(
|
2022-10-01 02:00:51 +02:00
|
|
|
onTap: openLogDetailsModal,
|
2022-09-30 23:33:57 +02:00
|
|
|
child: Container(
|
|
|
|
width: double.maxFinite,
|
2022-10-21 12:33:22 +02:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
|
2022-09-30 23:33:57 +02:00
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: index < length
|
|
|
|
? Border(
|
|
|
|
bottom: BorderSide(
|
|
|
|
color: Theme.of(context).dividerColor
|
|
|
|
)
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2022-10-21 12:33:22 +02:00
|
|
|
SizedBox(
|
|
|
|
width: width-130,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
2022-09-30 23:33:57 +02:00
|
|
|
log.question.name,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: const TextStyle(
|
2022-10-21 12:33:22 +02:00
|
|
|
fontSize: 18
|
2022-09-30 23:33:57 +02:00
|
|
|
),
|
|
|
|
),
|
2022-10-21 12:33:22 +02:00
|
|
|
const SizedBox(height: 10),
|
|
|
|
if (log.client.length <= 15) Row(
|
|
|
|
children: [
|
|
|
|
...[
|
|
|
|
Icon(
|
|
|
|
Icons.smartphone_rounded,
|
|
|
|
size: 16,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 5),
|
|
|
|
SizedBox(
|
|
|
|
child: Text(
|
|
|
|
log.client,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
fontSize: 13
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
const SizedBox(width: 15),
|
|
|
|
...[
|
|
|
|
Icon(
|
|
|
|
Icons.schedule_rounded,
|
|
|
|
size: 16,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 5),
|
|
|
|
SizedBox(
|
|
|
|
child: Text(
|
|
|
|
formatTimestampUTCFromAPI(log.time, 'HH:mm:ss'),
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
fontSize: 13
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
],
|
2022-09-30 23:33:57 +02:00
|
|
|
),
|
2022-10-21 12:33:22 +02:00
|
|
|
if (log.client.length > 15) Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.smartphone_rounded,
|
|
|
|
size: 16,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 15),
|
|
|
|
SizedBox(
|
|
|
|
child: Text(
|
|
|
|
log.client,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
fontSize: 13
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.schedule_rounded,
|
|
|
|
size: 16,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 15),
|
|
|
|
SizedBox(
|
|
|
|
child: Text(
|
|
|
|
formatTimestampUTCFromAPI(log.time, 'HH:mm:ss'),
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
|
|
|
fontSize: 13
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-09-30 23:33:57 +02:00
|
|
|
),
|
2022-10-21 12:33:22 +02:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
generateLogStatus()
|
2022-09-30 23:33:57 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|