2022-10-19 22:47:34 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2022-10-19 19:29:05 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-19 22:47:34 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-10-19 19:29:05 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/widgets/custom_switch_list_tile.dart';
|
2023-01-15 21:34:19 +01:00
|
|
|
import 'package:adguard_home_manager/screens/settings/dns/clear_dns_cache_dialog.dart';
|
2022-10-19 19:29:05 +02:00
|
|
|
|
2022-10-19 20:26:40 +02:00
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
|
2022-10-19 22:47:34 +02:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
2023-01-15 21:34:19 +01:00
|
|
|
import 'package:adguard_home_manager/functions/clear_dns_cache.dart';
|
2022-10-19 22:47:34 +02:00
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
|
|
|
import 'package:adguard_home_manager/models/dns_info.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
|
|
|
|
2022-10-19 19:29:05 +02:00
|
|
|
class CacheConfigDnsScreen extends StatefulWidget {
|
2022-10-19 20:26:40 +02:00
|
|
|
final ServersProvider serversProvider;
|
|
|
|
|
|
|
|
const CacheConfigDnsScreen({
|
|
|
|
Key? key,
|
|
|
|
required this.serversProvider
|
|
|
|
}) : super(key: key);
|
2022-10-19 19:29:05 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<CacheConfigDnsScreen> createState() => _CacheConfigDnsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CacheConfigDnsScreenState extends State<CacheConfigDnsScreen> {
|
|
|
|
final TextEditingController cacheSizeController = TextEditingController();
|
|
|
|
String? cacheSizeError;
|
|
|
|
|
|
|
|
final TextEditingController overrideMinTtlController = TextEditingController();
|
|
|
|
String? overrideMinTtlError;
|
|
|
|
|
|
|
|
final TextEditingController overrideMaxTtlController = TextEditingController();
|
|
|
|
String? overrideMaxTtlError;
|
|
|
|
|
|
|
|
bool optimisticCache = false;
|
|
|
|
|
2022-10-19 21:57:48 +02:00
|
|
|
bool validData = false;
|
|
|
|
|
|
|
|
void checkValidData() {
|
|
|
|
if (
|
|
|
|
cacheSizeController.text != '' &&
|
|
|
|
cacheSizeError == null &&
|
|
|
|
overrideMinTtlController.text != '' &&
|
|
|
|
overrideMinTtlError == null &&
|
|
|
|
overrideMaxTtlController.text != '' &&
|
|
|
|
overrideMaxTtlError == null
|
|
|
|
) {
|
|
|
|
setState(() => validData = true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => validData = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:26:40 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
cacheSizeController.text = widget.serversProvider.dnsInfo.data!.cacheSize.toString();
|
|
|
|
overrideMinTtlController.text = widget.serversProvider.dnsInfo.data!.cacheTtlMin.toString();
|
|
|
|
overrideMaxTtlController.text = widget.serversProvider.dnsInfo.data!.cacheTtlMax.toString();
|
|
|
|
optimisticCache = widget.serversProvider.dnsInfo.data!.cacheOptimistic;
|
2022-10-19 21:57:48 +02:00
|
|
|
validData = true;
|
2022-10-19 20:26:40 +02:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:29:05 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-19 22:47:34 +02:00
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
|
|
|
void saveData() async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.savingConfig);
|
|
|
|
|
|
|
|
final result = await setDnsConfig(server: serversProvider.selectedServer!, data: {
|
|
|
|
"cache_size": int.parse(cacheSizeController.text),
|
|
|
|
"cache_ttl_min": int.parse(overrideMinTtlController.text),
|
|
|
|
"cache_ttl_max": int.parse(overrideMaxTtlController.text),
|
|
|
|
"cache_optimistic": optimisticCache
|
|
|
|
});
|
|
|
|
|
|
|
|
processModal.close();
|
2022-10-19 19:29:05 +02:00
|
|
|
|
2022-10-19 22:47:34 +02:00
|
|
|
if (result['result'] == 'success') {
|
|
|
|
DnsInfoData data = serversProvider.dnsInfo.data!;
|
|
|
|
data.cacheSize = int.parse(cacheSizeController.text);
|
|
|
|
data.cacheTtlMin = int.parse(overrideMinTtlController.text);
|
|
|
|
data.cacheTtlMax = int.parse(overrideMaxTtlController.text);
|
|
|
|
data.cacheOptimistic = optimisticCache;
|
|
|
|
serversProvider.setDnsInfoData(data);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsConfigSaved,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (result['log'] != null && result['log'].statusCode == '400') {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.someValueNotValid,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsConfigNotSaved,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 19:29:05 +02:00
|
|
|
Widget numericField({
|
|
|
|
required TextEditingController controller,
|
|
|
|
required String label,
|
|
|
|
String? helper,
|
|
|
|
String? error,
|
|
|
|
required void Function(String) onChanged,
|
|
|
|
}) {
|
|
|
|
return Padding(
|
2022-11-05 03:56:04 +01:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
2022-10-19 19:29:05 +02:00
|
|
|
child: TextFormField(
|
2022-10-19 20:26:40 +02:00
|
|
|
controller: controller,
|
2022-10-19 19:29:05 +02:00
|
|
|
onChanged: onChanged,
|
|
|
|
decoration: InputDecoration(
|
2022-10-20 01:12:59 +02:00
|
|
|
prefixIcon: const Icon(Icons.storage_rounded),
|
2022-10-19 19:29:05 +02:00
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
errorText: error,
|
|
|
|
helperText: helper,
|
|
|
|
helperMaxLines: 10,
|
|
|
|
labelText: label,
|
|
|
|
),
|
|
|
|
keyboardType: TextInputType.number,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:34:19 +01:00
|
|
|
void clearCache() async {
|
|
|
|
final result = await clearDnsCache(context, serversProvider.selectedServer!);
|
|
|
|
if (result == true) {
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsCacheCleared,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showSnacbkar(
|
|
|
|
context: context,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsCacheNotCleared,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 19:29:05 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.dnsCacheConfig),
|
2022-10-19 21:57:48 +02:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: validData == true
|
2022-10-19 22:47:34 +02:00
|
|
|
? () => saveData()
|
2022-10-19 21:57:48 +02:00
|
|
|
: null,
|
|
|
|
icon: const Icon(Icons.save_rounded),
|
|
|
|
tooltip: AppLocalizations.of(context)!.save,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10)
|
|
|
|
],
|
2022-10-19 19:29:05 +02:00
|
|
|
),
|
|
|
|
body: ListView(
|
|
|
|
padding: const EdgeInsets.only(top: 10),
|
|
|
|
children: [
|
|
|
|
numericField(
|
|
|
|
controller: cacheSizeController,
|
|
|
|
label: AppLocalizations.of(context)!.cacheSize,
|
|
|
|
helper: AppLocalizations.of(context)!.inBytes,
|
|
|
|
error: cacheSizeError,
|
|
|
|
onChanged: (value) {
|
|
|
|
if (int.tryParse(value) != null) {
|
|
|
|
setState(() => cacheSizeError = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => cacheSizeError = AppLocalizations.of(context)!.valueNotNumber);
|
|
|
|
}
|
2022-10-19 21:57:48 +02:00
|
|
|
checkValidData();
|
2022-10-19 19:29:05 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
numericField(
|
|
|
|
controller: overrideMinTtlController,
|
|
|
|
label: AppLocalizations.of(context)!.overrideMinimumTtl,
|
|
|
|
helper: AppLocalizations.of(context)!.overrideMinimumTtlDescription,
|
|
|
|
error: overrideMinTtlError,
|
|
|
|
onChanged: (value) {
|
|
|
|
if (int.tryParse(value) != null) {
|
|
|
|
setState(() => overrideMinTtlError = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => overrideMinTtlError = AppLocalizations.of(context)!.valueNotNumber);
|
|
|
|
}
|
2022-10-19 21:57:48 +02:00
|
|
|
checkValidData();
|
2022-10-19 19:29:05 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
numericField(
|
|
|
|
controller: overrideMaxTtlController,
|
|
|
|
label: AppLocalizations.of(context)!.overrideMaximumTtl,
|
|
|
|
helper: AppLocalizations.of(context)!.overrideMaximumTtlDescription,
|
|
|
|
error: overrideMaxTtlError,
|
|
|
|
onChanged: (value) {
|
|
|
|
if (int.tryParse(value) != null) {
|
|
|
|
setState(() => overrideMaxTtlError = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => overrideMaxTtlError = AppLocalizations.of(context)!.valueNotNumber);
|
|
|
|
}
|
2022-10-19 21:57:48 +02:00
|
|
|
checkValidData();
|
2022-10-19 19:29:05 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
CustomSwitchListTile(
|
|
|
|
value: optimisticCache,
|
|
|
|
onChanged: (value) => setState(() => optimisticCache = value),
|
|
|
|
title: AppLocalizations.of(context)!.optimisticCaching,
|
|
|
|
subtitle: AppLocalizations.of(context)!.optimisticCachingDescription,
|
2023-01-15 21:34:19 +01:00
|
|
|
),
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
|
|
|
onPressed: () => showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ClearDnsCacheDialog(
|
|
|
|
onConfirm: clearCache
|
|
|
|
)
|
|
|
|
),
|
|
|
|
icon: const Icon(Icons.delete_rounded),
|
|
|
|
label: Text(AppLocalizations.of(context)!.clearDnsCache),
|
|
|
|
),
|
|
|
|
],
|
2022-10-19 19:29:05 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|