Skip to content

Commit

Permalink
Update network detection
Browse files Browse the repository at this point in the history
  • Loading branch information
chen08209 committed Nov 23, 2024
1 parent 96eca03 commit 2d38c27
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/change.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- 'main'
tags:
- 'v*'

jobs:
changelog:
Expand Down
33 changes: 7 additions & 26 deletions lib/fragments/dashboard/network_detection.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:dio/dio.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
Expand All @@ -25,7 +23,6 @@ class _NetworkDetectionState extends State<NetworkDetection> {
);
bool? _preIsStart;
Function? _checkIpDebounce;
Timer? _setTimeoutTimer;
CancelToken? cancelToken;

_checkIp() async {
Expand All @@ -35,7 +32,6 @@ class _NetworkDetectionState extends State<NetworkDetection> {
if (!isInit) return;
final isStart = appFlowingState.isStart;
if (_preIsStart == false && _preIsStart == isStart) return;
_clearSetTimeoutTimer();
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: true,
ipInfo: null,
Expand All @@ -48,34 +44,18 @@ class _NetworkDetectionState extends State<NetworkDetection> {
cancelToken = CancelToken();
try {
final ipInfo = await request.checkIp(cancelToken: cancelToken);
if (ipInfo != null) {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: false,
ipInfo: ipInfo,
);
return;
}
_setTimeoutTimer = Timer(const Duration(milliseconds: 2000), () {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: false,
ipInfo: null,
);
});
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: false,
ipInfo: ipInfo,
);
} catch (_) {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: true,
isTesting: false,
ipInfo: null,
);
}
}

_clearSetTimeoutTimer() {
if (_setTimeoutTimer != null) {
_setTimeoutTimer?.cancel();
_setTimeoutTimer = null;
}
}

_checkIpContainer(Widget child) {
return Selector<AppState, num>(
selector: (_, appState) {
Expand Down Expand Up @@ -156,7 +136,8 @@ class _NetworkDetectionState extends State<NetworkDetection> {
.textTheme
.titleLarge
?.copyWith(
fontFamily: FontFamily.twEmoji.value,
fontFamily:
FontFamily.twEmoji.value,
),
),
)
Expand Down

0 comments on commit 2d38c27

Please sign in to comment.