From bdb69a603a0d42091c5756b1b125c43666a6af74 Mon Sep 17 00:00:00 2001 From: hiddify-com <114227601+hiddify-com@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:51:54 +0200 Subject: [PATCH] reset direct dns when region change --- lib/core/utils/preferences_utils.dart | 48 +++++++++---------- lib/features/common/general_pref_tiles.dart | 3 ++ .../data/config_option_repository.dart | 1 + .../overview/config_options_page.dart | 1 + .../config_option/widget/preference_tile.dart | 7 ++- lib/features/intro/widget/intro_page.dart | 1 + 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/core/utils/preferences_utils.dart b/lib/core/utils/preferences_utils.dart index 2cbd32dd..be995b24 100644 --- a/lib/core/utils/preferences_utils.dart +++ b/lib/core/utils/preferences_utils.dart @@ -106,12 +106,36 @@ class PreferencesNotifier extends StateNotifier { static StateNotifierProvider, T> create( String key, T defaultValue, { + T Function(Ref ref)? defaultValueFunction, T Function(P value)? mapFrom, P Function(T value)? mapTo, bool Function(T value)? validator, T? overrideValue, }) => StateNotifierProvider( + (ref) => PreferencesNotifier._( + ref: ref, + entry: PreferencesEntry( + preferences: ref.read(sharedPreferencesProvider).requireValue, + key: key, + defaultValue: defaultValueFunction?.call(ref) ?? defaultValue, + mapFrom: mapFrom, + mapTo: mapTo, + validator: validator, + ), + overrideValue: overrideValue, + ), + ); + + static AutoDisposeStateNotifierProvider, T> createAutoDispose( + String key, + T defaultValue, { + T Function(P value)? mapFrom, + P Function(T value)? mapTo, + bool Function(T value)? validator, + T? overrideValue, + }) => + StateNotifierProvider.autoDispose( (ref) => PreferencesNotifier._( ref: ref, entry: PreferencesEntry( @@ -126,30 +150,6 @@ class PreferencesNotifier extends StateNotifier { ), ); - static AutoDisposeStateNotifierProvider, T> - createAutoDispose( - String key, - T defaultValue, { - T Function(P value)? mapFrom, - P Function(T value)? mapTo, - bool Function(T value)? validator, - T? overrideValue, - }) => - StateNotifierProvider.autoDispose( - (ref) => PreferencesNotifier._( - ref: ref, - entry: PreferencesEntry( - preferences: ref.read(sharedPreferencesProvider).requireValue, - key: key, - defaultValue: defaultValue, - mapFrom: mapFrom, - mapTo: mapTo, - validator: validator, - ), - overrideValue: overrideValue, - ), - ); - P raw() { final value = overrideValue ?? state; if (entry.mapTo != null) return entry.mapTo!(value); diff --git a/lib/features/common/general_pref_tiles.dart b/lib/features/common/general_pref_tiles.dart index 5073f3b9..aad736f8 100644 --- a/lib/features/common/general_pref_tiles.dart +++ b/lib/features/common/general_pref_tiles.dart @@ -84,8 +84,11 @@ class RegionPrefTile extends HookConsumerWidget { ); if (selectedRegion != null) { // await ref.read(Preferences.region.notifier).update(selectedRegion); + await ref.watch(ConfigOptions.region.notifier).update(selectedRegion); + await ref.watch(ConfigOptions.directDnsAddress.notifier).reset(); + // await ref.read(configOptionNotifierProvider.notifier).build(); // await ref.watch(ConfigOptions.resolveDestination.notifier).update(!ref.watch(ConfigOptions.resolveDestination.notifier).raw()); //for reload config diff --git a/lib/features/config_option/data/config_option_repository.dart b/lib/features/config_option/data/config_option_repository.dart index db28f988..a96b557b 100644 --- a/lib/features/config_option/data/config_option_repository.dart +++ b/lib/features/config_option/data/config_option_repository.dart @@ -71,6 +71,7 @@ abstract class ConfigOptions { static final directDnsAddress = PreferencesNotifier.create( "direct-dns-address", "1.1.1.1", + defaultValueFunction: (ref) => ref.read(region) == Region.cn ? "223.5.5.5" : "1.1.1.1", validator: (value) => value.isNotBlank, ); diff --git a/lib/features/config_option/overview/config_options_page.dart b/lib/features/config_option/overview/config_options_page.dart index b8af676b..221aac53 100644 --- a/lib/features/config_option/overview/config_options_page.dart +++ b/lib/features/config_option/overview/config_options_page.dart @@ -146,6 +146,7 @@ class ConfigOptionsPage extends HookConsumerWidget { choices: Region.values, title: t.settings.general.region, presentChoice: (value) => value.present(t), + onChanged: (val) => ref.watch(ConfigOptions.directDnsAddress.notifier).reset(), ), SwitchListTile( title: Text(experimental(t.config.blockAds)), diff --git a/lib/features/config_option/widget/preference_tile.dart b/lib/features/config_option/widget/preference_tile.dart index ddcf072e..868f8db2 100644 --- a/lib/features/config_option/widget/preference_tile.dart +++ b/lib/features/config_option/widget/preference_tile.dart @@ -61,6 +61,7 @@ class ChoicePreferenceWidget extends StatelessWidget { required this.title, required this.presentChoice, this.validateInput, + this.onChanged, }); final T selected; @@ -70,7 +71,7 @@ class ChoicePreferenceWidget extends StatelessWidget { final String title; final String Function(T value) presentChoice; final bool Function(String value)? validateInput; - + final ValueChanged? onChanged; @override Widget build(BuildContext context) { return ListTile( @@ -88,7 +89,9 @@ class ChoicePreferenceWidget extends StatelessWidget { if (selection == null) { return; } - await preferences.update(selection); + final out = await preferences.update(selection); + onChanged?.call(selection); + return out; }, ); } diff --git a/lib/features/intro/widget/intro_page.dart b/lib/features/intro/widget/intro_page.dart index 5f222e0f..7940109b 100644 --- a/lib/features/intro/widget/intro_page.dart +++ b/lib/features/intro/widget/intro_page.dart @@ -124,6 +124,7 @@ class IntroPage extends HookConsumerWidget with PresLogger { 'Timezone Region: ${regionLocale.region} Locale: ${regionLocale.locale}', ); await ref.read(ConfigOptions.region.notifier).update(regionLocale.region); + await ref.watch(ConfigOptions.directDnsAddress.notifier).reset(); await ref.read(localePreferencesProvider.notifier).changeLocale(regionLocale.locale); return; } catch (e) {