From 068fe14d89557edb47cd3b84e25dada6619bafd9 Mon Sep 17 00:00:00 2001 From: chen08209 Date: Thu, 6 Jun 2024 17:13:32 +0800 Subject: [PATCH] Optimize delay test2 --- lib/common/request.dart | 3 +- .../dashboard/network_detection.dart | 44 ++++++++++++------- lib/fragments/dashboard/start_button.dart | 1 - lib/l10n/arb/intl_en.arb | 3 +- lib/l10n/arb/intl_zh_CN.arb | 3 +- lib/l10n/intl/messages_en.dart | 2 + lib/l10n/intl/messages_zh_CN.dart | 1 + lib/l10n/l10n.dart | 10 +++++ lib/models/selector.dart | 4 -- 9 files changed, 47 insertions(+), 24 deletions(-) diff --git a/lib/common/request.dart b/lib/common/request.dart index 80b7ece..552166f 100644 --- a/lib/common/request.dart +++ b/lib/common/request.dart @@ -86,12 +86,11 @@ class Request { "https://ipinfo.io/json/": IpInfo.fromIpInfoIoJson, }; - Future checkIp(CancelToken cancelToken) async { + Future checkIp() async { for (final source in _ipInfoSources.entries) { try { final response = await _dio.get>( source.key, - cancelToken: cancelToken, ); if (response.statusCode == 200 && response.data != null) { return source.value(response.data!); diff --git a/lib/fragments/dashboard/network_detection.dart b/lib/fragments/dashboard/network_detection.dart index 5455c9d..76a5268 100644 --- a/lib/fragments/dashboard/network_detection.dart +++ b/lib/fragments/dashboard/network_detection.dart @@ -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 { final ipInfoNotifier = ValueNotifier(null); - CancelToken? cancelToken; + final timeoutNotifier = ValueNotifier(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 { 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 { ), ], ) - : 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(), + ); + }, ), ), ) diff --git a/lib/fragments/dashboard/start_button.dart b/lib/fragments/dashboard/start_button.dart index 2fc9e3c..1abd5ef 100644 --- a/lib/fragments/dashboard/start_button.dart +++ b/lib/fragments/dashboard/start_button.dart @@ -53,7 +53,6 @@ class _StartButtonState extends State final appController = globalState.appController; await appController.updateSystemProxy(isStart); }); - ; } @override diff --git a/lib/l10n/arb/intl_en.arb b/lib/l10n/arb/intl_en.arb index 762ef13..51ed2b5 100644 --- a/lib/l10n/arb/intl_en.arb +++ b/lib/l10n/arb/intl_en.arb @@ -158,5 +158,6 @@ "geoData": "GeoData", "externalResources": "External resources", "checking": "Checking...", - "country": "Country" + "country": "Country", + "ipCheckError": "Ip check timeout" } \ No newline at end of file diff --git a/lib/l10n/arb/intl_zh_CN.arb b/lib/l10n/arb/intl_zh_CN.arb index db4aec1..73fb0a5 100644 --- a/lib/l10n/arb/intl_zh_CN.arb +++ b/lib/l10n/arb/intl_zh_CN.arb @@ -158,5 +158,6 @@ "geoData": "地理数据", "externalResources": "外部资源", "checking": "检测中...", - "country": "区域" + "country": "区域", + "ipCheckError": "Ip检测超时" } \ No newline at end of file diff --git a/lib/l10n/intl/messages_en.dart b/lib/l10n/intl/messages_en.dart index 95d0d53..2a8922d 100644 --- a/lib/l10n/intl/messages_en.dart +++ b/lib/l10n/intl/messages_en.dart @@ -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"), diff --git a/lib/l10n/intl/messages_zh_CN.dart b/lib/l10n/intl/messages_zh_CN.dart index dbac9d4..6591e51 100644 --- a/lib/l10n/intl/messages_zh_CN.dart +++ b/lib/l10n/intl/messages_zh_CN.dart @@ -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("浅色"), diff --git a/lib/l10n/l10n.dart b/lib/l10n/l10n.dart index 401821e..8ea02d5 100644 --- a/lib/l10n/l10n.dart +++ b/lib/l10n/l10n.dart @@ -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 { diff --git a/lib/models/selector.dart b/lib/models/selector.dart index 989e547..280ae3c 100644 --- a/lib/models/selector.dart +++ b/lib/models/selector.dart @@ -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';