2023-04-14 01:40:40 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2023-04-15 01:22:44 +02:00
|
|
|
import 'package:adguard_home_manager/widgets/custom_checkbox_list_tile.dart';
|
2023-04-14 01:40:40 +02:00
|
|
|
|
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
|
|
|
import 'package:adguard_home_manager/models/server_status.dart';
|
|
|
|
import 'package:adguard_home_manager/services/http_requests.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/servers_provider.dart';
|
|
|
|
|
|
|
|
class SafeSearchSettingsScreen extends StatelessWidget {
|
|
|
|
const SafeSearchSettingsScreen({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
|
|
|
return SafeSearchSettingsScreenWidget(
|
|
|
|
serversProvider: serversProvider,
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SafeSearchSettingsScreenWidget extends StatefulWidget {
|
|
|
|
final ServersProvider serversProvider;
|
|
|
|
final AppConfigProvider appConfigProvider;
|
|
|
|
|
|
|
|
const SafeSearchSettingsScreenWidget({
|
|
|
|
Key? key,
|
|
|
|
required this.serversProvider,
|
|
|
|
required this.appConfigProvider
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<SafeSearchSettingsScreenWidget> createState() => _SafeSearchSettingsScreenWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SafeSearchSettingsScreenWidgetState extends State<SafeSearchSettingsScreenWidget> {
|
|
|
|
bool generalEnabled = false;
|
|
|
|
bool bingEnabled = false;
|
|
|
|
bool duckduckgoEnabled = false;
|
|
|
|
bool googleEnabled = false;
|
|
|
|
bool pixabayEnabled = false;
|
|
|
|
bool yandexEnabled = false;
|
|
|
|
bool youtubeEnabled = false;
|
|
|
|
|
|
|
|
Future requestSafeSearchSettings() async {
|
|
|
|
if (mounted) {
|
2023-04-30 22:54:39 +02:00
|
|
|
final result = await getServerStatus(widget.serversProvider.selectedServer!);
|
|
|
|
if (mounted) {
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
widget.serversProvider.setServerStatusData(result['data']);
|
|
|
|
widget.serversProvider.setServerStatusLoad(1);
|
|
|
|
setState(() {
|
|
|
|
generalEnabled = result['data'].safeSearchEnabled;
|
|
|
|
bingEnabled = result['data'].safeSeachBing;
|
|
|
|
duckduckgoEnabled = result['data'].safeSearchDuckduckgo;
|
|
|
|
googleEnabled = result['data'].safeSearchGoogle;
|
|
|
|
pixabayEnabled = result['data'].safeSearchPixabay;
|
|
|
|
yandexEnabled = result['data'].safeSearchYandex;
|
|
|
|
youtubeEnabled = result['data'].safeSearchYoutube;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widget.appConfigProvider.addLog(result['log']);
|
|
|
|
widget.serversProvider.setServerStatusLoad(2);
|
|
|
|
}
|
2023-04-14 01:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (widget.serversProvider.serverStatus.loadStatus == 0) {
|
|
|
|
requestSafeSearchSettings();
|
|
|
|
}
|
|
|
|
else if (widget.serversProvider.serverStatus.loadStatus == 1) {
|
|
|
|
generalEnabled = widget.serversProvider.serverStatus.data!.safeSearchEnabled;
|
2023-04-15 02:02:40 +02:00
|
|
|
bingEnabled = widget.serversProvider.serverStatus.data!.safeSeachBing!;
|
|
|
|
duckduckgoEnabled = widget.serversProvider.serverStatus.data!.safeSearchDuckduckgo!;
|
|
|
|
googleEnabled = widget.serversProvider.serverStatus.data!.safeSearchGoogle!;
|
|
|
|
pixabayEnabled = widget.serversProvider.serverStatus.data!.safeSearchPixabay!;
|
|
|
|
yandexEnabled = widget.serversProvider.serverStatus.data!.safeSearchYandex!;
|
|
|
|
youtubeEnabled = widget.serversProvider.serverStatus.data!.safeSearchYoutube!;
|
2023-04-14 01:40:40 +02:00
|
|
|
}
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final serversProvider = Provider.of<ServersProvider>(context);
|
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
|
|
|
void saveConfig() async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.savingSettings);
|
|
|
|
|
|
|
|
final result = await updateSafeSearchSettings(
|
|
|
|
server: serversProvider.selectedServer!,
|
|
|
|
body: {
|
|
|
|
"enabled": generalEnabled,
|
|
|
|
"bing": bingEnabled,
|
|
|
|
"duckduckgo": duckduckgoEnabled,
|
|
|
|
"google": googleEnabled,
|
|
|
|
"pixabay": pixabayEnabled,
|
|
|
|
"yandex": yandexEnabled,
|
|
|
|
"youtube": youtubeEnabled
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
|
|
|
if (result['result'] == 'success') {
|
|
|
|
ServerStatusData data = serversProvider.serverStatus.data!;
|
|
|
|
data.safeSearchEnabled = generalEnabled;
|
|
|
|
data.safeSeachBing = bingEnabled;
|
|
|
|
data.safeSearchDuckduckgo = duckduckgoEnabled;
|
|
|
|
data.safeSearchGoogle = googleEnabled;
|
|
|
|
data.safeSearchPixabay = pixabayEnabled;
|
|
|
|
data.safeSearchYandex = yandexEnabled;
|
|
|
|
data.safeSearchYoutube = youtubeEnabled;
|
|
|
|
|
|
|
|
serversProvider.setServerStatusData(data);
|
|
|
|
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.settingsUpdatedSuccessfully,
|
|
|
|
color: Colors.green,
|
|
|
|
labelColor: Colors.white
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
appConfigProvider.addLog(result['log']);
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.settingsNotSaved,
|
|
|
|
color: Colors.red,
|
|
|
|
labelColor: Colors.white
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget body() {
|
|
|
|
switch (serversProvider.serverStatus.loadStatus) {
|
|
|
|
case 0:
|
|
|
|
return SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.loadingSafeSearchSettings,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: requestSafeSearchSettings,
|
|
|
|
child: ListView(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 16,
|
|
|
|
left: 16,
|
|
|
|
right: 16,
|
|
|
|
bottom: 8
|
|
|
|
),
|
|
|
|
child: Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
borderRadius: BorderRadius.circular(28),
|
|
|
|
child: InkWell(
|
|
|
|
borderRadius: BorderRadius.circular(28),
|
|
|
|
onTap: () => setState(() => generalEnabled = !generalEnabled),
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 24,
|
|
|
|
vertical: 12
|
|
|
|
),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
|
|
|
borderRadius: BorderRadius.circular(28)
|
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.enableSafeSearch,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 18
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Switch(
|
|
|
|
value: generalEnabled,
|
|
|
|
onChanged: (value) => setState(() => generalEnabled = value)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: bingEnabled,
|
|
|
|
onChanged: (value) => setState(() => bingEnabled = value),
|
|
|
|
title: "Bing",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: duckduckgoEnabled,
|
|
|
|
onChanged: (value) => setState(() => duckduckgoEnabled = value),
|
|
|
|
title: "DuckDuckGo",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: googleEnabled,
|
|
|
|
onChanged: (value) => setState(() => googleEnabled = value),
|
|
|
|
title: "Google",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: pixabayEnabled,
|
|
|
|
onChanged: (value) => setState(() => pixabayEnabled = value),
|
|
|
|
title: "Pixabay",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: yandexEnabled,
|
|
|
|
onChanged: (value) => setState(() => yandexEnabled = value),
|
|
|
|
title: "Yandex",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
2023-04-14 22:49:16 +02:00
|
|
|
CustomCheckboxListTile(
|
2023-04-14 01:40:40 +02:00
|
|
|
value: youtubeEnabled,
|
|
|
|
onChanged: (value) => setState(() => youtubeEnabled = value),
|
|
|
|
title: "YouTube",
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 8, left: 40, right: 40, bottom: 8
|
|
|
|
),
|
|
|
|
disabled: !generalEnabled,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return SizedBox(
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Icon(
|
|
|
|
Icons.error,
|
|
|
|
color: Colors.red,
|
|
|
|
size: 50,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 30),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.safeSearchSettingsNotLoaded,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.safeSearchSettings),
|
|
|
|
centerTitle: false,
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: serversProvider.serverStatus.loadStatus == 1
|
|
|
|
? () => saveConfig()
|
|
|
|
: null,
|
|
|
|
icon: const Icon(Icons.save_rounded),
|
|
|
|
tooltip: AppLocalizations.of(context)!.save,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
body: body(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|