Change ip refresh

This commit is contained in:
problematicconsumer
2024-02-17 14:49:44 +03:30
parent 611dff8a5b
commit 7f187b24f1
5 changed files with 132 additions and 55 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:hiddify/core/haptic/haptic_service.dart';
import 'package:hiddify/core/preferences/general_preferences.dart';
@@ -19,21 +21,32 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
Future<IpInfo> build() async {
ref.disposeDelay(const Duration(seconds: 20));
final cancelToken = CancelToken();
Timer? timer;
ref.onDispose(() {
loggy.debug("disposing");
cancelToken.cancel();
timer?.cancel();
});
ref.listen(
serviceRunningProvider,
(_, next) => _idle = false,
);
final autoCheck = ref.watch(autoCheckIpProvider);
final serviceRunning = await ref.watch(serviceRunningProvider.future);
if (!_userRequestedFetch && !serviceRunning) {
// loggy.debug(
// "idle? [$_idle], forced? [$_forceCheck], connected? [$serviceRunning]",
// );
if (!_forceCheck && !serviceRunning) {
throw const ServiceNotRunning();
} else if (!_userRequestedFetch && serviceRunning && !autoCheck) {
} else if ((_idle && !_forceCheck) ||
(!_forceCheck && serviceRunning && !autoCheck)) {
throw const UnknownIp();
}
_userRequestedFetch = false;
return ref
_forceCheck = false;
final info = await ref
.watch(proxyRepositoryProvider)
.getCurrentIpInfo(cancelToken)
.getOrElse(
@@ -42,15 +55,28 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
throw err;
},
).run();
timer = Timer(
const Duration(seconds: 10),
() {
loggy.debug("entering idle mode");
_idle = true;
ref.invalidateSelf();
},
);
return info;
}
bool _userRequestedFetch = false;
bool _idle = false;
bool _forceCheck = false;
Future<void> refresh() async {
if (state is AsyncLoading) return;
if (state.isLoading) return;
loggy.debug("refreshing");
state = const AsyncLoading();
await ref.read(hapticServiceProvider.notifier).lightImpact();
_userRequestedFetch = true;
_forceCheck = true;
ref.invalidateSelf();
}
}