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

318 lines
10 KiB
Dart
Raw Normal View History

2022-10-19 22:47:34 +02:00
// ignore_for_file: use_build_context_synchronously
2023-05-01 03:48:23 +02:00
import 'dart:io';
2022-10-19 18:34:11 +02:00
import 'package:flutter/material.dart';
2022-10-19 22:47:34 +02:00
import 'package:provider/provider.dart';
2022-10-19 18:34:11 +02:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-10-25 21:12:00 +02:00
import 'package:adguard_home_manager/widgets/section_label.dart';
import 'package:adguard_home_manager/screens/settings/dns/comment_modal.dart';
2022-10-19 18:34:11 +02:00
import 'package:adguard_home_manager/widgets/custom_radio_list_tile.dart';
2022-10-19 22:47:34 +02:00
import 'package:adguard_home_manager/classes/process_modal.dart';
2023-10-29 02:47:14 +01:00
import 'package:adguard_home_manager/functions/desktop_mode.dart';
2023-05-24 20:40:45 +02:00
import 'package:adguard_home_manager/providers/dns_provider.dart';
2022-10-19 22:47:34 +02:00
import 'package:adguard_home_manager/functions/snackbar.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
2022-10-19 20:26:40 +02:00
2022-10-19 18:34:11 +02:00
class UpstreamDnsScreen extends StatefulWidget {
2023-05-24 20:40:45 +02:00
const UpstreamDnsScreen({Key? key}) : super(key: key);
2022-10-19 18:34:11 +02:00
@override
State<UpstreamDnsScreen> createState() => _UpstreamDnsScreenState();
}
class _UpstreamDnsScreenState extends State<UpstreamDnsScreen> {
List<Map<String, dynamic>> dnsServers = [];
2022-10-19 18:34:11 +02:00
2022-10-19 21:57:48 +02:00
String upstreamMode = "";
bool validValues = false;
checkValidValues() {
if (
dnsServers.isNotEmpty &&
dnsServers.every((element) => element['controller'] != null ? element['controller.text'] != '' : true)
2022-10-19 21:57:48 +02:00
) {
setState(() => validValues = true);
}
else {
setState(() => validValues = false);
}
}
2022-10-19 18:34:11 +02:00
2022-10-19 20:26:40 +02:00
@override
void initState() {
2023-05-24 20:40:45 +02:00
final dnsProvider = Provider.of<DnsProvider>(context, listen: false);
for (var item in dnsProvider.dnsInfo!.upstreamDns) {
2023-05-12 23:11:38 +02:00
if (item == '#') {
dnsServers.add({
'comment': item
});
}
else {
final controller = TextEditingController();
controller.text = item;
dnsServers.add({
'controller': controller
});
}
2022-10-19 20:26:40 +02:00
}
2023-06-16 14:38:19 +02:00
upstreamMode = dnsProvider.dnsInfo!.upstreamMode ?? "";
2022-10-19 21:57:48 +02:00
validValues = true;
2022-10-19 20:26:40 +02:00
super.initState();
}
2022-10-19 18:34:11 +02:00
@override
Widget build(BuildContext context) {
2023-05-24 20:40:45 +02:00
final dnsProvider = Provider.of<DnsProvider>(context);
2022-10-19 22:47:34 +02:00
final appConfigProvider = Provider.of<AppConfigProvider>(context);
2023-05-01 03:48:23 +02:00
final width = MediaQuery.of(context).size.width;
2022-10-19 22:47:34 +02:00
void openAddCommentModal() {
2023-05-01 03:48:23 +02:00
if (width > 900 || !(Platform.isAndroid || Platform.isIOS)) {
showDialog(
context: context,
builder: (context) => CommentModal(
onConfirm: (value) {
setState(() {
dnsServers.add({
'comment': value
});
});
},
dialog: true,
),
);
}
else {
showModalBottomSheet(
context: context,
2023-10-29 02:47:14 +01:00
useRootNavigator: true,
2023-05-01 03:48:23 +02:00
builder: (context) => CommentModal(
onConfirm: (value) {
setState(() {
dnsServers.add({
'comment': value
});
});
},
dialog: false,
),
backgroundColor: Colors.transparent,
isScrollControlled: true,
isDismissible: true
);
}
}
void openEditCommentModal(Map<String, dynamic> item, int position) {
2023-05-01 03:48:23 +02:00
if (width > 900 || !(Platform.isAndroid || Platform.isIOS)) {
showDialog(
context: context,
builder: (context) => CommentModal(
comment: item['comment'],
onConfirm: (value) {
setState(() => dnsServers[position] = { 'comment': value });
},
dialog: true,
),
);
}
else {
showModalBottomSheet(
context: context,
2023-10-29 02:47:14 +01:00
useRootNavigator: true,
2023-05-01 03:48:23 +02:00
builder: (context) => CommentModal(
comment: item['comment'],
onConfirm: (value) {
setState(() => dnsServers[position] = { 'comment': value });
},
dialog: false,
),
backgroundColor: Colors.transparent,
isScrollControlled: true,
isDismissible: true
);
}
}
2022-10-19 22:47:34 +02:00
void saveData() async {
ProcessModal processModal = ProcessModal(context: context);
processModal.open(AppLocalizations.of(context)!.savingConfig);
final result = await dnsProvider.saveUpstreamDnsConfig({
"upstream_dns": dnsServers.map((e) => e['controller'] != null ? e['controller'].text : e['comment']).toList(),
2022-10-19 22:47:34 +02:00
"upstream_mode": upstreamMode
});
processModal.close();
if (result['success'] == true) {
2022-10-19 22:47:34 +02:00
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.dnsConfigSaved,
color: Colors.green
);
}
else if (result['success'] == false && result['error'] == 400) {
2022-10-19 22:47:34 +02:00
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.someValueNotValid,
color: Colors.red
);
}
else {
showSnacbkar(
appConfigProvider: appConfigProvider,
label: AppLocalizations.of(context)!.dnsConfigNotSaved,
color: Colors.red
);
}
}
2022-10-19 18:34:11 +02:00
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.upstreamDns),
2023-10-29 02:47:14 +01:00
surfaceTintColor: isDesktop(width) ? Colors.transparent : null,
2022-10-19 21:57:48 +02:00
actions: [
IconButton(
onPressed: validValues == 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 18:34:11 +02:00
),
body: ListView(
padding: const EdgeInsets.only(top: 10),
children: [
if (dnsServers.isEmpty) Column(
2022-10-19 18:34:11 +02:00
children: [
Padding(
padding: const EdgeInsets.all(10),
child: Center(
child: Text(
AppLocalizations.of(context)!.noUpstreamDns,
2023-01-29 21:52:37 +01:00
style: TextStyle(
color: Theme.of(context).colorScheme.onSurfaceVariant,
2022-10-19 18:34:11 +02:00
fontSize: 16
),
),
),
),
const SizedBox(height: 20),
],
),
...dnsServers.map((item) => Padding(
2022-10-19 18:34:11 +02:00
padding: const EdgeInsets.only(
2023-05-01 02:50:42 +02:00
left: 16, right: 6, bottom: 24
2022-10-19 18:34:11 +02:00
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2023-05-01 02:50:42 +02:00
if (item['controller'] != null) Expanded(
2022-10-19 18:34:11 +02:00
child: TextFormField(
controller: item['controller'],
2022-10-19 21:57:48 +02:00
onChanged: (_) => checkValidValues(),
2022-10-19 18:34:11 +02:00
decoration: InputDecoration(
prefixIcon: const Icon(Icons.dns_rounded),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10)
)
),
labelText: AppLocalizations.of(context)!.dnsServer,
)
),
),
2023-05-01 02:50:42 +02:00
const SizedBox(width: 8),
if (item['comment'] != null) Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
item['comment'],
style: TextStyle(
fontSize: 16,
color: Theme.of(context).listTileTheme.iconColor
),
),
IconButton(
onPressed: () => openEditCommentModal(item, dnsServers.indexOf(item)),
icon: const Icon(Icons.edit),
tooltip: AppLocalizations.of(context)!.edit,
)
],
),
),
2022-10-19 18:34:11 +02:00
IconButton(
2022-10-19 21:57:48 +02:00
onPressed: () {
setState(() => dnsServers = dnsServers.where((i) => i != item).toList());
2022-10-19 21:57:48 +02:00
checkValidValues();
},
icon: const Icon(Icons.remove_circle_outline),
tooltip: AppLocalizations.of(context)!.remove,
2023-05-01 02:50:42 +02:00
),
const SizedBox(width: 4),
2022-10-19 18:34:11 +02:00
],
),
)).toList(),
2023-05-01 02:50:42 +02:00
const SizedBox(height: 12),
2022-10-19 18:34:11 +02:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
2022-10-19 18:34:11 +02:00
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton.icon(
onPressed: openAddCommentModal,
icon: const Icon(Icons.add),
label: Text(AppLocalizations.of(context)!.comment)
),
2022-10-19 18:34:11 +02:00
ElevatedButton.icon(
2022-10-19 21:57:48 +02:00
onPressed: () {
setState(() => dnsServers.add({
'controller': TextEditingController()
}));
2022-10-19 21:57:48 +02:00
checkValidValues();
},
2022-10-19 18:34:11 +02:00
icon: const Icon(Icons.add),
label: Text(AppLocalizations.of(context)!.address)
2022-10-19 18:34:11 +02:00
),
],
),
2022-11-05 03:56:04 +01:00
const SizedBox(height: 16),
2022-10-19 18:34:11 +02:00
SectionLabel(label: AppLocalizations.of(context)!.dnsMode),
CustomRadioListTile(
groupValue: upstreamMode,
2022-10-19 20:26:40 +02:00
value: "",
2022-10-19 18:34:11 +02:00
radioBackgroundColor: Theme.of(context).dialogBackgroundColor,
title: AppLocalizations.of(context)!.loadBalancing,
subtitle: AppLocalizations.of(context)!.loadBalancingDescription,
onChanged: (value) => setState(() => upstreamMode = value),
),
CustomRadioListTile(
groupValue: upstreamMode,
2022-10-19 20:26:40 +02:00
value: "parallel",
2022-10-19 18:34:11 +02:00
radioBackgroundColor: Theme.of(context).dialogBackgroundColor,
title: AppLocalizations.of(context)!.parallelRequests,
subtitle: AppLocalizations.of(context)!.parallelRequestsDescription,
onChanged: (value) => setState(() => upstreamMode = value),
),
CustomRadioListTile(
groupValue: upstreamMode,
2022-10-19 20:26:40 +02:00
value: "fastest_addr",
2022-10-19 18:34:11 +02:00
radioBackgroundColor: Theme.of(context).dialogBackgroundColor,
title: AppLocalizations.of(context)!.fastestIpAddress,
subtitle: AppLocalizations.of(context)!.fastestIpAddressDescription,
onChanged: (value) => setState(() => upstreamMode = value),
),
],
),
);
}
}