2023-03-16 23:29:18 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-10-15 20:52:31 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-07-09 22:16:25 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-10-15 20:52:31 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2023-07-09 22:16:25 +02:00
|
|
|
import 'package:adguard_home_manager/screens/settings/dns_rewrites/server_version_needed.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/providers/status_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/functions/compare_versions.dart';
|
2022-10-15 20:52:31 +02:00
|
|
|
import 'package:adguard_home_manager/models/rewrite_rules.dart';
|
|
|
|
|
2023-07-09 22:16:25 +02:00
|
|
|
class DnsRewriteModal extends StatefulWidget {
|
|
|
|
final void Function(RewriteRules newRule, RewriteRules? previousRule) onConfirm;
|
2023-05-01 04:23:55 +02:00
|
|
|
final bool dialog;
|
2023-07-09 22:16:25 +02:00
|
|
|
final RewriteRules? rule;
|
|
|
|
final void Function(RewriteRules) onDelete;
|
2022-10-15 20:52:31 +02:00
|
|
|
|
2023-07-09 22:16:25 +02:00
|
|
|
const DnsRewriteModal({
|
2022-10-15 20:52:31 +02:00
|
|
|
Key? key,
|
2023-05-01 04:23:55 +02:00
|
|
|
required this.onConfirm,
|
2023-07-09 22:16:25 +02:00
|
|
|
required this.dialog,
|
|
|
|
this.rule,
|
|
|
|
required this.onDelete
|
2022-10-15 20:52:31 +02:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
2023-07-09 22:16:25 +02:00
|
|
|
State<DnsRewriteModal> createState() => _AddDnsRewriteModalState();
|
2022-10-15 20:52:31 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:16:25 +02:00
|
|
|
class _AddDnsRewriteModalState extends State<DnsRewriteModal> {
|
2022-10-15 20:52:31 +02:00
|
|
|
final TextEditingController domainController = TextEditingController();
|
|
|
|
String? domainError;
|
|
|
|
final TextEditingController answerController = TextEditingController();
|
|
|
|
|
|
|
|
bool validData = false;
|
|
|
|
|
|
|
|
void validateDomain(String value) {
|
|
|
|
final domainRegex = RegExp(r'^([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+$');
|
|
|
|
if (domainRegex.hasMatch(value)) {
|
|
|
|
setState(() => domainError = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => domainError = AppLocalizations.of(context)!.domainNotValid);
|
|
|
|
}
|
|
|
|
checkValidValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkValidValues() {
|
|
|
|
if (
|
|
|
|
domainController.text != '' &&
|
|
|
|
domainError == null &&
|
|
|
|
answerController.text != ''
|
|
|
|
) {
|
|
|
|
setState(() => validData = true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => validData = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:16:25 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (widget.rule != null) {
|
|
|
|
domainController.text = widget.rule!.domain;
|
|
|
|
answerController.text = widget.rule!.answer;
|
|
|
|
validData = true;
|
|
|
|
}
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-10-15 20:52:31 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-07-09 22:16:25 +02:00
|
|
|
final statusProvider = Provider.of<StatusProvider>(context);
|
|
|
|
|
2023-05-01 04:23:55 +02:00
|
|
|
Widget content() {
|
|
|
|
return Column(
|
2023-05-01 04:49:40 +02:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
2023-05-01 04:23:55 +02:00
|
|
|
children: [
|
2023-05-01 04:49:40 +02:00
|
|
|
Flexible(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Wrap(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 24),
|
|
|
|
child: Icon(
|
2023-07-09 22:16:25 +02:00
|
|
|
widget.rule != null
|
|
|
|
? Icons.edit
|
|
|
|
: Icons.add,
|
2023-05-01 04:49:40 +02:00
|
|
|
size: 24,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor
|
|
|
|
),
|
2023-05-01 04:23:55 +02:00
|
|
|
),
|
2023-05-01 04:49:40 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
Text(
|
2023-07-09 22:16:25 +02:00
|
|
|
widget.rule != null
|
|
|
|
? AppLocalizations.of(context)!.editRewriteRule
|
|
|
|
: AppLocalizations.of(context)!.addDnsRewrite,
|
2023-05-01 04:49:40 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
|
|
|
),
|
2023-05-01 04:23:55 +02:00
|
|
|
),
|
2023-05-01 04:49:40 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
2023-05-01 04:49:40 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 24, right: 24, bottom: 12
|
|
|
|
),
|
|
|
|
child: TextFormField(
|
|
|
|
controller: domainController,
|
|
|
|
onChanged: validateDomain,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.link_rounded),
|
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
errorText: domainError,
|
|
|
|
labelText: AppLocalizations.of(context)!.domain,
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
|
|
|
),
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2023-05-01 04:49:40 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 24, right: 24, top: 12
|
|
|
|
),
|
|
|
|
child: TextFormField(
|
|
|
|
controller: answerController,
|
|
|
|
onChanged: (_) => checkValidValues(),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.system_update_alt_rounded),
|
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
labelText: AppLocalizations.of(context)!.answer,
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-05-01 04:49:40 +02:00
|
|
|
],
|
|
|
|
),
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2023-05-01 04:23:55 +02:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(24),
|
|
|
|
child: Row(
|
2023-07-09 22:16:25 +02:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
2023-05-01 04:23:55 +02:00
|
|
|
children: [
|
2023-07-09 22:16:25 +02:00
|
|
|
if (widget.rule != null) TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
widget.onDelete(
|
|
|
|
RewriteRules(
|
|
|
|
domain: domainController.text,
|
|
|
|
answer: answerController.text
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: Text(AppLocalizations.of(context)!.delete),
|
2023-05-01 04:23:55 +02:00
|
|
|
),
|
2023-07-09 22:16:25 +02:00
|
|
|
if (widget.rule == null) const SizedBox(),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2023-07-09 22:16:25 +02:00
|
|
|
const SizedBox(width: 20),
|
|
|
|
TextButton(
|
|
|
|
onPressed: validData == true
|
|
|
|
? () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
widget.onConfirm(
|
|
|
|
RewriteRules(
|
|
|
|
domain: domainController.text,
|
|
|
|
answer: answerController.text
|
|
|
|
),
|
|
|
|
widget.rule
|
|
|
|
);
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.confirm,
|
|
|
|
style: TextStyle(
|
|
|
|
color: validData == true
|
|
|
|
? Theme.of(context).colorScheme.primary
|
|
|
|
: Theme.of(context).colorScheme.onSurface.withOpacity(0.38)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2023-05-01 04:23:55 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (Platform.isIOS) const SizedBox(height: 16)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.dialog == true) {
|
|
|
|
return Dialog(
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(
|
|
|
|
maxWidth: 400
|
|
|
|
),
|
|
|
|
child: content()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Padding(
|
|
|
|
padding: MediaQuery.of(context).viewInsets,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: const BorderRadius.only(
|
|
|
|
topLeft: Radius.circular(28),
|
|
|
|
topRight: Radius.circular(28)
|
2023-03-16 23:29:18 +01:00
|
|
|
),
|
2023-05-01 04:23:55 +02:00
|
|
|
color: Theme.of(context).dialogBackgroundColor,
|
|
|
|
),
|
|
|
|
child: content()
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2023-05-01 04:23:55 +02:00
|
|
|
);
|
|
|
|
}
|
2022-10-15 20:52:31 +02:00
|
|
|
}
|
|
|
|
}
|