From 611dff8a5b89d7930869bb539ea0c4771faed46c Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 17 Feb 2024 13:11:50 +0330 Subject: [PATCH] Add auto ip check option --- assets/translations/strings_en.i18n.json | 3 ++- lib/core/preferences/general_preferences.dart | 17 +++++++++++++++++ .../proxy/active/active_proxy_notifier.dart | 10 +++++++++- lib/features/proxy/model/proxy_failure.dart | 4 ++++ .../settings/widgets/general_setting_tiles.dart | 6 ++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/assets/translations/strings_en.i18n.json b/assets/translations/strings_en.i18n.json index 4934c35b..77a5876a 100644 --- a/assets/translations/strings_en.i18n.json +++ b/assets/translations/strings_en.i18n.json @@ -187,7 +187,8 @@ "ignoreBatteryOptimizations": "Disable Battery Optimization", "ignoreBatteryOptimizationsMsg": "Remove restrictions for optimal VPN performance", "dynamicNotification": "Display speed in notification", - "hapticFeedback": "Haptic Feedback" + "hapticFeedback": "Haptic Feedback", + "autoIpCheck": "Automatically check connection IP" }, "advanced": { "sectionTitle": "Advanced", diff --git a/lib/core/preferences/general_preferences.dart b/lib/core/preferences/general_preferences.dart index e6912f83..b76b5df1 100644 --- a/lib/core/preferences/general_preferences.dart +++ b/lib/core/preferences/general_preferences.dart @@ -186,3 +186,20 @@ class DynamicNotification extends _$DynamicNotification { return _pref.update(value); } } + +@riverpod +class AutoCheckIp extends _$AutoCheckIp { + late final _pref = Pref( + ref.watch(sharedPreferencesProvider).requireValue, + "auto_check_ip", + true, + ); + + @override + bool build() => _pref.getValue(); + + Future update(bool value) { + state = value; + return _pref.update(value); + } +} diff --git a/lib/features/proxy/active/active_proxy_notifier.dart b/lib/features/proxy/active/active_proxy_notifier.dart index f7509983..1b60bc31 100644 --- a/lib/features/proxy/active/active_proxy_notifier.dart +++ b/lib/features/proxy/active/active_proxy_notifier.dart @@ -1,5 +1,6 @@ import 'package:dio/dio.dart'; import 'package:hiddify/core/haptic/haptic_service.dart'; +import 'package:hiddify/core/preferences/general_preferences.dart'; import 'package:hiddify/core/utils/throttler.dart'; import 'package:hiddify/features/connection/notifier/connection_notifier.dart'; import 'package:hiddify/features/proxy/data/proxy_data_providers.dart'; @@ -23,11 +24,15 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger { cancelToken.cancel(); }); + final autoCheck = ref.watch(autoCheckIpProvider); final serviceRunning = await ref.watch(serviceRunningProvider.future); - if (!serviceRunning) { + if (!_userRequestedFetch && !serviceRunning) { throw const ServiceNotRunning(); + } else if (!_userRequestedFetch && serviceRunning && !autoCheck) { + throw const UnknownIp(); } + _userRequestedFetch = false; return ref .watch(proxyRepositoryProvider) .getCurrentIpInfo(cancelToken) @@ -39,10 +44,13 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger { ).run(); } + bool _userRequestedFetch = false; + Future refresh() async { if (state is AsyncLoading) return; loggy.debug("refreshing"); await ref.read(hapticServiceProvider.notifier).lightImpact(); + _userRequestedFetch = true; ref.invalidateSelf(); } } diff --git a/lib/features/proxy/model/proxy_failure.dart b/lib/features/proxy/model/proxy_failure.dart index 8fcf1b35..7645bff6 100644 --- a/lib/features/proxy/model/proxy_failure.dart +++ b/lib/features/proxy/model/proxy_failure.dart @@ -17,6 +17,9 @@ sealed class ProxyFailure with _$ProxyFailure, Failure { @With() const factory ProxyFailure.serviceNotRunning() = ServiceNotRunning; + @With() + const factory ProxyFailure.unknownIp() = UnknownIp; + @override ({String type, String? message}) present(TranslationsEn t) { return switch (this) { @@ -28,6 +31,7 @@ sealed class ProxyFailure with _$ProxyFailure, Failure { type: t.failure.singbox.serviceNotRunning, message: null, ), + UnknownIp() => (type: t.general.unknown, message: null), }; } } diff --git a/lib/features/settings/widgets/general_setting_tiles.dart b/lib/features/settings/widgets/general_setting_tiles.dart index fe446d50..bdac67bb 100644 --- a/lib/features/settings/widgets/general_setting_tiles.dart +++ b/lib/features/settings/widgets/general_setting_tiles.dart @@ -55,6 +55,12 @@ class GeneralSettingTiles extends HookConsumerWidget { }, ), const EnableAnalyticsPrefTile(), + SwitchListTile( + title: Text(t.settings.general.autoIpCheck), + secondary: const Icon(FluentIcons.globe_search_24_regular), + value: ref.watch(autoCheckIpProvider), + onChanged: ref.read(autoCheckIpProvider.notifier).update, + ), if (Platform.isAndroid) ...[ SwitchListTile( title: Text(t.settings.general.dynamicNotification),