Optimize delay test2

This commit is contained in:
chen08209
2024-06-06 17:13:32 +08:00
parent 43c397007c
commit 068fe14d89
9 changed files with 47 additions and 24 deletions

View File

@@ -86,12 +86,11 @@ class Request {
"https://ipinfo.io/json/": IpInfo.fromIpInfoIoJson,
};
Future<IpInfo?> checkIp(CancelToken cancelToken) async {
Future<IpInfo?> checkIp() async {
for (final source in _ipInfoSources.entries) {
try {
final response = await _dio.get<Map<String, dynamic>>(
source.key,
cancelToken: cancelToken,
);
if (response.statusCode == 200 && response.data != null) {
return source.value(response.data!);

View File

@@ -1,5 +1,4 @@
import 'package:country_flags/country_flags.dart';
import 'package:dio/dio.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/models/models.dart';
import 'package:fl_clash/state.dart';
@@ -16,20 +15,21 @@ class NetworkDetection extends StatefulWidget {
class _NetworkDetectionState extends State<NetworkDetection> {
final ipInfoNotifier = ValueNotifier<IpInfo?>(null);
CancelToken? cancelToken;
final timeoutNotifier = ValueNotifier<bool>(false);
_checkIp(
bool isInit,
) async {
if (!isInit) return;
await Future.delayed(const Duration(milliseconds: 300));
if (cancelToken != null) {
cancelToken!.cancel();
cancelToken = null;
}
ipInfoNotifier.value = null;
cancelToken = CancelToken();
ipInfoNotifier.value = await request.checkIp(cancelToken!);
final ipInfo = await request.checkIp();
if (ipInfo == null) {
timeoutNotifier.value = true;
return;
} else {
timeoutNotifier.value = false;
}
ipInfoNotifier.value = await request.checkIp();
}
_checkIpContainer(Widget child) {
@@ -78,10 +78,10 @@ class _NetworkDetectionState extends State<NetworkDetection> {
child: FadeBox(
child: ipInfo != null
? CountryFlag.fromCountryCode(
ipInfo.countryCode,
width: 24,
height: 24,
)
ipInfo.countryCode,
width: 24,
height: 24,
)
: TooltipText(
text: Text(
appLocalizations.checking,
@@ -123,8 +123,22 @@ class _NetworkDetectionState extends State<NetworkDetection> {
),
],
)
: const SizedBox(
child: CircularProgressIndicator(),
: ValueListenableBuilder(
valueListenable: timeoutNotifier,
builder: (_, timeout, __) {
if(timeout){
return Text(
appLocalizations.ipCheckError,
style: context.textTheme.bodyMedium
?.toSoftBold(),
maxLines: 1,
overflow: TextOverflow.ellipsis,
);
}
return const SizedBox(
child: CircularProgressIndicator(),
);
},
),
),
)

View File

@@ -53,7 +53,6 @@ class _StartButtonState extends State<StartButton>
final appController = globalState.appController;
await appController.updateSystemProxy(isStart);
});
;
}
@override

View File

@@ -158,5 +158,6 @@
"geoData": "GeoData",
"externalResources": "External resources",
"checking": "Checking...",
"country": "Country"
"country": "Country",
"ipCheckError": "Ip check timeout"
}

View File

@@ -158,5 +158,6 @@
"geoData": "地理数据",
"externalResources": "外部资源",
"checking": "检测中...",
"country": "区域"
"country": "区域",
"ipCheckError": "Ip检测超时"
}

View File

@@ -124,6 +124,8 @@ class MessageLookup extends MessageLookupByLibrary {
"hours": MessageLookupByLibrary.simpleMessage("Hours"),
"importFromURL":
MessageLookupByLibrary.simpleMessage("Import from URL"),
"ipCheckError":
MessageLookupByLibrary.simpleMessage("Ip check timeout"),
"just": MessageLookupByLibrary.simpleMessage("Just"),
"language": MessageLookupByLibrary.simpleMessage("Language"),
"light": MessageLookupByLibrary.simpleMessage("Light"),

View File

@@ -101,6 +101,7 @@ class MessageLookup extends MessageLookupByLibrary {
"goDownload": MessageLookupByLibrary.simpleMessage("前往下载"),
"hours": MessageLookupByLibrary.simpleMessage("小时"),
"importFromURL": MessageLookupByLibrary.simpleMessage("从URL导入"),
"ipCheckError": MessageLookupByLibrary.simpleMessage("Ip检测超时"),
"just": MessageLookupByLibrary.simpleMessage("刚刚"),
"language": MessageLookupByLibrary.simpleMessage("语言"),
"light": MessageLookupByLibrary.simpleMessage("浅色"),

View File

@@ -1649,6 +1649,16 @@ class AppLocalizations {
args: [],
);
}
/// `Ip check timeout`
String get ipCheckError {
return Intl.message(
'Ip check timeout',
name: 'ipCheckError',
desc: '',
args: [],
);
}
}
class AppLocalizationDelegate extends LocalizationsDelegate<AppLocalizations> {

View File

@@ -2,10 +2,6 @@ import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart';
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'config.dart';
import 'navigation.dart';
import 'profile.dart';
import 'proxy.dart';
part 'generated/selector.freezed.dart';