diff --git a/lib/app/model/business/kr_outbound_item.dart b/lib/app/model/business/kr_outbound_item.dart index fd6b4ff..30db256 100755 --- a/lib/app/model/business/kr_outbound_item.dart +++ b/lib/app/model/business/kr_outbound_item.dart @@ -27,7 +27,10 @@ class KROutboundItem { /// URL String url = ""; - + @override + String toString() { + return 'KROutboundItem(tag: $tag, country: $country, delay: ${urlTestDelay.value}ms)'; + } /// 服务器类型 /// 构造函数,接受 KrNodeListItem 对象并初始化 KROutboundItem diff --git a/lib/app/modules/hi_node_list/controllers/hi_node_list_controller.dart b/lib/app/modules/hi_node_list/controllers/hi_node_list_controller.dart index d242a18..5158143 100644 --- a/lib/app/modules/hi_node_list/controllers/hi_node_list_controller.dart +++ b/lib/app/modules/hi_node_list/controllers/hi_node_list_controller.dart @@ -38,59 +38,6 @@ class HINodeListController extends GetxController { } } - /// 根据国家选择最快的节点 - void kr_selectFastestNodeByCountry(String country) { - KRLogUtil.kr_i('开始为国家 $country 选择最快节点', tag: 'HINodeListController'); - - // 通知kr_home当前选择的国家 - homeController.setCurrentSelectedCountry(country); - - // 查找国家分组 - final countryGroup = kr_subscribeService.countryOutboundList - .firstWhereOrNull((group) => group.country == country); - - if (countryGroup == null) { - KRLogUtil.kr_w('未找到国家分组: $country', tag: 'HINodeListController'); - return; - } - - if (countryGroup.outboundList.isEmpty) { - KRLogUtil.kr_w('国家 $country 的节点列表为空', tag: 'HINodeListController'); - return; - } - - // 查找延迟最小的有效节点 - String? fastestNodeTag; - int minDelay = 65535; - int validNodeCount = 0; - - for (var node in countryGroup.outboundList) { - final delay = node.urlTestDelay.value; - - // 统计有效节点数量 - if (delay < 65535 && delay > 0) { - validNodeCount++; - - // 找到延迟更小的节点 - if (delay < minDelay) { - minDelay = delay; - fastestNodeTag = node.tag; - } - } - } - - KRLogUtil.kr_i('国家 $country 内有 $validNodeCount 个有效节点可供切换', tag: 'HINodeListController'); - - // 选择最快的节点,如果没有有效延迟则选择第一个 - final selectedTag = fastestNodeTag ?? countryGroup.outboundList.first.tag; - - KRLogUtil.kr_i('选择节点: $selectedTag (延迟: ${minDelay == 65535 ? "未知" : "${minDelay}ms"})', - tag: 'HINodeListController'); - - // 调用homeController的选择节点方法 - homeController.kr_selectNode(selectedTag); - } - /// 获取当前国家内的有效节点标签列表 List getValidNodesInCurrentCountry() { final country = homeController.currentSelectedCountry.value; diff --git a/lib/app/modules/hi_node_list/views/hi_node_list_view.dart b/lib/app/modules/hi_node_list/views/hi_node_list_view.dart index b571ed7..c9e8daa 100755 --- a/lib/app/modules/hi_node_list/views/hi_node_list_view.dart +++ b/lib/app/modules/hi_node_list/views/hi_node_list_view.dart @@ -39,20 +39,66 @@ class HINodeListView extends GetView { return _fakeDelays[item.tag] ?? 0; } + /// 获取分组内最快节点的延迟值(单位:ms) + /// 如果列表为空,返回 0 + int getFastestNodeDelay( + HINodeListController controller, List outboundList) { + if (outboundList.isEmpty) return 0; + + int fastestDelay = _getDisplayDelay(controller, outboundList.first); + + for (final item in outboundList.skip(1)) { + final delay = _getDisplayDelay(controller, item); + if (delay < fastestDelay) { + fastestDelay = delay; + } + } + + return fastestDelay; + } + + /// 找出延迟最低(最快)的节点对象 + KROutboundItem findFastestNode(List outboundList) { + if (outboundList.isEmpty) { + throw ArgumentError('outboundList 不能为空'); + } + + KROutboundItem fastest = outboundList.first; + int fastestDelay = fastest.urlTestDelay.value; + + for (final item in outboundList.skip(1)) { + final delay = item.urlTestDelay.value; + print('国家分组下的节点$delay'); + if (delay < fastestDelay) { + fastest = item; + fastestDelay = delay; + } + } + + return fastest; + } + @override Widget build(BuildContext context) { // 1. 使用 Material 作为根组件,确保 InkWell 的水波纹效果正常 // 并设置透明背景,让父组件的背景可以透出来 return Material( color: Colors.transparent, - // child: _buildSubscribeList(context) - child: _kr_buildRegionList(context) + child: _buildSubscribeList(context) + // child: _kr_buildRegionList(context) ); } /// 构建国家/地区分组列表 Widget _kr_buildRegionList(BuildContext context) { return Obx(() { + // 自动触发延迟测试(仅在未连接状态下) + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!controller.homeController.kr_isConnected.value && !controller.homeController.kr_isLatency.value) { + KRLogUtil.kr_i('🔄 节点列表显示 - 自动触发延迟测试', tag: 'HINodeListView'); + controller.homeController.kr_urlTest(); + } + }); return _kr_buildListContainer( context, child: ListView( @@ -142,10 +188,15 @@ class HINodeListView extends GetView { ...controller.kr_subscribeService.countryOutboundList.map((country) { return InkWell( onTap: () { - // 选择国家 - controller.homeController.kr_coutryText.value = country.country; // 自动选择这个国家下的节点延迟中最快的 - controller.kr_selectFastestNodeByCountry(country.country); + final item = findFastestNode(country.outboundList); + KRLogUtil.kr_i(item.tag); + print('item${item.toString()}'); + controller.homeController.kr_selectNode(item.tag); + controller.homeController.kr_currentListStatus.value = + KRHomeViewsListStatus.kr_none; + controller.homeController.kr_coutryText.value = country.country; + }, child: _kr_buildCountryListItem(context, country: country), ); @@ -349,12 +400,6 @@ class HINodeListView extends GetView { Obx(() { // 2. 获取用于显示的延迟值 final int delay = _getDisplayDelay(controller, item); - // 3. 根据延迟值显示不同内容 - // if (delay <= 0) { - // // 如果延迟为0或负数(初始状态或测试失败),则不显示 - // return const SizedBox.shrink(); - // } - // 4. 显示延迟值,并添加 "ms" 单位 return Text( '${delay}ms', style: KrAppTextStyle( @@ -391,6 +436,18 @@ class HINodeListView extends GetView { /// 构建国家列表项的UI Widget _kr_buildCountryListItem(BuildContext context, {required country}) { + // 获取延迟颜色 + Color getLatencyColor(int delay) { + if (delay == 0) { + return Colors.transparent; + } else if (delay < 500) { + return krModernGreen; + } else if (delay < 3000) { + return Color(0xFFFFB700); // 使用更容易看清的黄色 + } else { + return Colors.red; + } + } return Container( key: ValueKey(country), decoration: BoxDecoration( @@ -413,6 +470,22 @@ class HINodeListView extends GetView { style: KrAppTextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.white), ), ), + Obx(() { + final int delay = getFastestNodeDelay(controller, country.outboundList); + return Text( + delay == 0 + ? '' + : delay >= 3000 + ? AppTranslations.kr_home.timeout + : '${delay}ms', + style: KrAppTextStyle( + fontSize: 10, + color: getLatencyColor(delay), + fontWeight: FontWeight.w500, + ), + ); + }), + SizedBox(width: 12.w), Obx(() => controller.homeController.kr_coutryText.value == country.country ? KrLocalImage( imageName: 'radio-active-icon', diff --git a/lib/app/modules/kr_home/controllers/kr_home_controller.dart b/lib/app/modules/kr_home/controllers/kr_home_controller.dart index fb06779..dd042a3 100755 --- a/lib/app/modules/kr_home/controllers/kr_home_controller.dart +++ b/lib/app/modules/kr_home/controllers/kr_home_controller.dart @@ -203,7 +203,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { KRLogUtil.kr_i('开始执行闪连自动连接', tag: 'QuickConnect'); // 防止重复操作 - if (KRSingBoxImp.instance.kr_status == SingboxStarted) { + if (KRSingBoxImp.instance.kr_status.value == SingboxStarted()) { KRLogUtil.kr_w('连接操作正在进行中,跳过自动连接', tag: 'QuickConnect'); return; } @@ -439,9 +439,6 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { kr_cutTag.value = 'auto'; kr_cutSeletedTag.value = 'auto'; kr_currentNodeName.value = "auto"; - // 当订阅服务初始化成功后,检查闪连自动连接 - KRLogUtil.kr_i( - '订阅服务初始化完成,检查闪连自动连接', tag: 'QuickConnect'); if (kr_currentListStatus.value != KRHomeViewsListStatus.kr_none) { kr_currentListStatus.value = KRHomeViewsListStatus.kr_none; } else { @@ -793,7 +790,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { _kr_updateNodeLatency(element); kr_currentNodeName.value = kr_truncateText(element.selected, maxLength: 25); - kr_moveToSelectedNode(); + // kr_moveToSelectedNode(); } } catch (e) { KRLogUtil.kr_e('处理手动模式出错: $e', tag: 'HomeController'); @@ -858,7 +855,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { kr_currentNodeName.value = kr_truncateText("${item.selected}(auto)", maxLength: 25); - kr_moveToSelectedNode(); + // kr_moveToSelectedNode(); kr_updateConnectionInfo(); break; } else { @@ -889,7 +886,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { kr_cutSeletedTag.value = bestNode; kr_currentNodeName.value = kr_truncateText("${bestNode}(auto)", maxLength: 25); - kr_moveToSelectedNode(); + // kr_moveToSelectedNode(); kr_updateConnectionInfo(); } } @@ -1149,7 +1146,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver { } // 移动到选中的节点 - kr_moveToSelectedNode(); + // kr_moveToSelectedNode(); } catch (e) { KRLogUtil.kr_e('选择节点失败: $e', tag: 'HomeController'); // 🔧 修复:选择节点失败时,根据连接状态设置合适的延迟值 diff --git a/lib/app/modules/kr_home/views/hi_animated_connect_button.dart b/lib/app/modules/kr_home/views/hi_animated_connect_button.dart index 06caea8..bc37677 100644 --- a/lib/app/modules/kr_home/views/hi_animated_connect_button.dart +++ b/lib/app/modules/kr_home/views/hi_animated_connect_button.dart @@ -22,7 +22,8 @@ class HIAnimatedConnectButton extends GetView { return Obx(() { final isConnected = controller.kr_isConnected.value; final delay = controller.kr_currentNodeLatency.value; - final isShow = delay == -1 || isConnected; + print('当前连接情况$delay----$isConnected'); + final isShow = isConnected; // delay == -1 || isConnected; final Color buttonColor = Theme.of(context).primaryColor; final double screenWidth = Get.width;