adguard-home-manager/lib/screens/settings/dns/cache_config.dart

167 lines
5.4 KiB
Dart
Raw Normal View History

2022-10-19 19:29:05 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/widgets/custom_switch_list_tile.dart';
2022-10-19 20:26:40 +02:00
import 'package:adguard_home_manager/providers/servers_provider.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) {
Widget numericField({
required TextEditingController controller,
required String label,
String? helper,
String? error,
required void Function(String) onChanged,
}) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: TextFormField(
2022-10-19 20:26:40 +02:00
controller: controller,
2022-10-19 19:29:05 +02:00
onChanged: onChanged,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.timer_rounded),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10)
)
),
errorText: error,
helperText: helper,
helperMaxLines: 10,
labelText: label,
),
keyboardType: TextInputType.number,
),
);
}
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.dnsCacheConfig),
2022-10-19 21:57:48 +02:00
actions: [
IconButton(
onPressed: validData == true
? () => {}
: 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,
)
],
),
);
}
}