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';
|
2022-10-22 02:17:30 +02:00
|
|
|
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/models/dns_info.dart';
|
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
2022-10-19 20:26:40 +02:00
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
|
2022-10-19 18:34:11 +02:00
|
|
|
class UpstreamDnsScreen extends StatefulWidget {
|
2022-10-19 20:26:40 +02:00
|
|
|
final ServersProvider serversProvider;
|
|
|
|
|
|
|
|
const UpstreamDnsScreen({
|
|
|
|
Key? key,
|
|
|
|
required this.serversProvider,
|
|
|
|
}) : super(key: key);
|
2022-10-19 18:34:11 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<UpstreamDnsScreen> createState() => _UpstreamDnsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UpstreamDnsScreenState extends State<UpstreamDnsScreen> {
|
2022-10-22 02:17:30 +02:00
|
|
|
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 (
|
2022-10-22 02:17:30 +02:00
|
|
|
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() {
|
|
|
|
for (var item in widget.serversProvider.dnsInfo.data!.upstreamDns) {
|
2023-05-12 23:11:38 +02:00
|
|
|
if (item == '#') {
|
2022-10-22 02:17:30 +02:00
|
|
|
dnsServers.add({
|
|
|
|
'comment': item
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
final controller = TextEditingController();
|
|
|
|
controller.text = item;
|
|
|
|
dnsServers.add({
|
|
|
|
'controller': controller
|
|
|
|
});
|
|
|
|
}
|
2022-10-19 20:26:40 +02:00
|
|
|
}
|
|
|
|
upstreamMode = widget.serversProvider.dnsInfo.data!.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) {
|
2022-10-19 22:47:34 +02:00
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
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
|
|
|
|
2022-10-22 02:17:30 +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,
|
|
|
|
builder: (context) => CommentModal(
|
|
|
|
onConfirm: (value) {
|
|
|
|
setState(() {
|
|
|
|
dnsServers.add({
|
|
|
|
'comment': value
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
dialog: false,
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true,
|
|
|
|
isDismissible: true
|
|
|
|
);
|
|
|
|
}
|
2022-10-22 02:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
builder: (context) => CommentModal(
|
|
|
|
comment: item['comment'],
|
|
|
|
onConfirm: (value) {
|
|
|
|
setState(() => dnsServers[position] = { 'comment': value });
|
|
|
|
},
|
|
|
|
dialog: false,
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true,
|
|
|
|
isDismissible: true
|
|
|
|
);
|
|
|
|
}
|
2022-10-22 02:17:30 +02:00
|
|
|
}
|
|
|
|
|
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 setDnsConfig(server: serversProvider.selectedServer!, data: {
|
2022-10-22 02:17:30 +02:00
|
|
|
"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['result'] == 'success') {
|
|
|
|
DnsInfoData data = serversProvider.dnsInfo.data!;
|
2022-10-22 02:17:30 +02:00
|
|
|
data.upstreamDns = List<String>.from(dnsServers.map((e) => e['controller'] != null ? e['controller'].text : e['comment']));
|
2022-10-19 22:47:34 +02:00
|
|
|
data.upstreamMode = upstreamMode;
|
|
|
|
serversProvider.setDnsInfoData(data);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsConfigSaved,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (result['log'] != null && result['log'].statusCode == '400') {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.someValueNotValid,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
|
|
|
|
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),
|
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: [
|
2022-10-22 02:17:30 +02:00
|
|
|
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),
|
|
|
|
],
|
|
|
|
),
|
2022-10-22 02:17:30 +02:00
|
|
|
...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(
|
2022-10-22 02:17:30 +02:00
|
|
|
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),
|
2022-10-22 02:17:30 +02:00
|
|
|
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: () {
|
2022-10-22 02:17:30 +02:00
|
|
|
setState(() => dnsServers = dnsServers.where((i) => i != item).toList());
|
2022-10-19 21:57:48 +02:00
|
|
|
checkValidValues();
|
|
|
|
},
|
2022-10-22 02:17:30 +02:00
|
|
|
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(
|
2022-10-22 02:17:30 +02:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
2022-10-19 18:34:11 +02:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2022-10-22 02:17:30 +02:00
|
|
|
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: () {
|
2022-10-22 02:17:30 +02:00
|
|
|
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),
|
2022-10-22 02:17:30 +02:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|