Add misc settings ui
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:hiddify/core/core_providers.dart';
|
||||
import 'package:hiddify/features/settings/widgets/miscellaneous_setting_tiles.dart';
|
||||
import 'package:hiddify/features/settings/widgets/widgets.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
@@ -40,6 +41,10 @@ class SettingsPage extends HookConsumerWidget {
|
||||
// await const ClashOverridesRoute().push(context);
|
||||
// },
|
||||
// ),
|
||||
_SettingsSectionHeader(
|
||||
t.settings.miscellaneous.sectionTitle.titleCase,
|
||||
),
|
||||
const MiscellaneousSettingTiles(),
|
||||
const Gap(16),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/core/core_providers.dart';
|
||||
import 'package:hiddify/core/prefs/misc_prefs.dart';
|
||||
import 'package:hiddify/domain/constants.dart';
|
||||
import 'package:hiddify/features/settings/widgets/settings_input_dialog.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
class MiscellaneousSettingTiles extends HookConsumerWidget {
|
||||
const MiscellaneousSettingTiles({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final t = ref.watch(translationsProvider);
|
||||
|
||||
final connectionTestUrl = ref.watch(connectionTestUrlProvider);
|
||||
final concurrentTestCount = ref.watch(concurrentTestCountProvider);
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(t.settings.miscellaneous.connectionTestUrl.titleCase),
|
||||
subtitle: Text(connectionTestUrl),
|
||||
onTap: () async {
|
||||
final url = await SettingsInputDialog<String>(
|
||||
title: t.settings.miscellaneous.connectionTestUrl.titleCase,
|
||||
initialValue: connectionTestUrl,
|
||||
resetValue: Defaults.connectionTestUrl,
|
||||
).show(context);
|
||||
if (url == null || url.isEmpty || !isUrl(url)) return;
|
||||
await ref.read(connectionTestUrlProvider.notifier).update(url);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text(t.settings.miscellaneous.concurrentTestCount.titleCase),
|
||||
subtitle: Text(concurrentTestCount.toString()),
|
||||
onTap: () async {
|
||||
final val = await SettingsInputDialog<int>(
|
||||
title: t.settings.miscellaneous.connectionTestUrl.titleCase,
|
||||
initialValue: concurrentTestCount,
|
||||
resetValue: Defaults.concurrentTestCount,
|
||||
mapTo: (value) => int.tryParse(value),
|
||||
digitsOnly: true,
|
||||
).show(context);
|
||||
if (val == null || val < 1) return;
|
||||
await ref.read(concurrentTestCountProvider.notifier).update(val);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class InputOverrideTile extends HookConsumerWidget {
|
||||
: value.toString(),
|
||||
),
|
||||
onTap: () async {
|
||||
final result = await SettingsInputDialog<int>(
|
||||
final result = await OptionalSettingsInputDialog<int>(
|
||||
title: title,
|
||||
initialValue: value,
|
||||
resetValue: optionOf(resetValue),
|
||||
|
||||
@@ -6,8 +6,9 @@ import 'package:hiddify/core/core_providers.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
class SettingsInputDialog<T> extends HookConsumerWidget with PresLogger {
|
||||
const SettingsInputDialog({
|
||||
class OptionalSettingsInputDialog<T> extends HookConsumerWidget
|
||||
with PresLogger {
|
||||
const OptionalSettingsInputDialog({
|
||||
super.key,
|
||||
required this.title,
|
||||
this.initialValue,
|
||||
@@ -75,3 +76,80 @@ class SettingsInputDialog<T> extends HookConsumerWidget with PresLogger {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsInputDialog<T> extends HookConsumerWidget with PresLogger {
|
||||
const SettingsInputDialog({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.initialValue,
|
||||
this.mapTo,
|
||||
this.resetValue,
|
||||
this.icon,
|
||||
this.digitsOnly = false,
|
||||
});
|
||||
|
||||
final String title;
|
||||
final T initialValue;
|
||||
final T? Function(String value)? mapTo;
|
||||
final T? resetValue;
|
||||
final IconData? icon;
|
||||
final bool digitsOnly;
|
||||
|
||||
Future<T?> show(BuildContext context) async {
|
||||
return showDialog(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
builder: (context) => this,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final t = ref.watch(translationsProvider);
|
||||
final localizations = MaterialLocalizations.of(context);
|
||||
|
||||
final textController = useTextEditingController(
|
||||
text: initialValue?.toString(),
|
||||
);
|
||||
|
||||
return AlertDialog(
|
||||
title: Text(title),
|
||||
icon: icon != null ? Icon(icon) : null,
|
||||
content: TextFormField(
|
||||
controller: textController,
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.singleLineFormatter,
|
||||
if (digitsOnly) FilteringTextInputFormatter.digitsOnly,
|
||||
],
|
||||
autovalidateMode: AutovalidateMode.always,
|
||||
),
|
||||
actions: [
|
||||
if (resetValue != null)
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await Navigator.of(context).maybePop(resetValue);
|
||||
},
|
||||
child: Text(t.general.reset.toUpperCase()),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await Navigator.of(context).maybePop();
|
||||
},
|
||||
child: Text(localizations.cancelButtonLabel.toUpperCase()),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
if (mapTo != null) {
|
||||
await Navigator.of(context)
|
||||
.maybePop(mapTo!.call(textController.value.text));
|
||||
} else {
|
||||
await Navigator.of(context)
|
||||
.maybePop(T == String ? textController.value.text : null);
|
||||
}
|
||||
},
|
||||
child: Text(localizations.okButtonLabel.toUpperCase()),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user