Add auto ip check option

This commit is contained in:
problematicconsumer
2024-02-17 13:11:50 +03:30
parent 71fb84bea1
commit 611dff8a5b
5 changed files with 38 additions and 2 deletions

View File

@@ -187,7 +187,8 @@
"ignoreBatteryOptimizations": "Disable Battery Optimization", "ignoreBatteryOptimizations": "Disable Battery Optimization",
"ignoreBatteryOptimizationsMsg": "Remove restrictions for optimal VPN performance", "ignoreBatteryOptimizationsMsg": "Remove restrictions for optimal VPN performance",
"dynamicNotification": "Display speed in notification", "dynamicNotification": "Display speed in notification",
"hapticFeedback": "Haptic Feedback" "hapticFeedback": "Haptic Feedback",
"autoIpCheck": "Automatically check connection IP"
}, },
"advanced": { "advanced": {
"sectionTitle": "Advanced", "sectionTitle": "Advanced",

View File

@@ -186,3 +186,20 @@ class DynamicNotification extends _$DynamicNotification {
return _pref.update(value); 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<void> update(bool value) {
state = value;
return _pref.update(value);
}
}

View File

@@ -1,5 +1,6 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:hiddify/core/haptic/haptic_service.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/core/utils/throttler.dart';
import 'package:hiddify/features/connection/notifier/connection_notifier.dart'; import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
import 'package:hiddify/features/proxy/data/proxy_data_providers.dart'; import 'package:hiddify/features/proxy/data/proxy_data_providers.dart';
@@ -23,11 +24,15 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
cancelToken.cancel(); cancelToken.cancel();
}); });
final autoCheck = ref.watch(autoCheckIpProvider);
final serviceRunning = await ref.watch(serviceRunningProvider.future); final serviceRunning = await ref.watch(serviceRunningProvider.future);
if (!serviceRunning) { if (!_userRequestedFetch && !serviceRunning) {
throw const ServiceNotRunning(); throw const ServiceNotRunning();
} else if (!_userRequestedFetch && serviceRunning && !autoCheck) {
throw const UnknownIp();
} }
_userRequestedFetch = false;
return ref return ref
.watch(proxyRepositoryProvider) .watch(proxyRepositoryProvider)
.getCurrentIpInfo(cancelToken) .getCurrentIpInfo(cancelToken)
@@ -39,10 +44,13 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
).run(); ).run();
} }
bool _userRequestedFetch = false;
Future<void> refresh() async { Future<void> refresh() async {
if (state is AsyncLoading) return; if (state is AsyncLoading) return;
loggy.debug("refreshing"); loggy.debug("refreshing");
await ref.read(hapticServiceProvider.notifier).lightImpact(); await ref.read(hapticServiceProvider.notifier).lightImpact();
_userRequestedFetch = true;
ref.invalidateSelf(); ref.invalidateSelf();
} }
} }

View File

@@ -17,6 +17,9 @@ sealed class ProxyFailure with _$ProxyFailure, Failure {
@With<ExpectedFailure>() @With<ExpectedFailure>()
const factory ProxyFailure.serviceNotRunning() = ServiceNotRunning; const factory ProxyFailure.serviceNotRunning() = ServiceNotRunning;
@With<ExpectedFailure>()
const factory ProxyFailure.unknownIp() = UnknownIp;
@override @override
({String type, String? message}) present(TranslationsEn t) { ({String type, String? message}) present(TranslationsEn t) {
return switch (this) { return switch (this) {
@@ -28,6 +31,7 @@ sealed class ProxyFailure with _$ProxyFailure, Failure {
type: t.failure.singbox.serviceNotRunning, type: t.failure.singbox.serviceNotRunning,
message: null, message: null,
), ),
UnknownIp() => (type: t.general.unknown, message: null),
}; };
} }
} }

View File

@@ -55,6 +55,12 @@ class GeneralSettingTiles extends HookConsumerWidget {
}, },
), ),
const EnableAnalyticsPrefTile(), 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) ...[ if (Platform.isAndroid) ...[
SwitchListTile( SwitchListTile(
title: Text(t.settings.general.dynamicNotification), title: Text(t.settings.general.dynamicNotification),