import 'package:get/get.dart'; import '../../core/utils/logger.dart'; import '../../routes/app_routes.dart'; import '../../modules/ota_upgrade/controllers/ota_core_contropller.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; /// OTA检查服务 /// 负责管理全局OTA检查状态,确保在应用生命周期内只检查一次 class OtaCheckService extends GetxService { static const String _tag = 'OtaCheckService'; // 全局状态管理 static bool _hasCheckedInCurrentSession = false; static bool _isCheckingInProgress = false; final _otaCoreService = OtaCoreService(); /// 获取服务实例 static OtaCheckService get to => Get.find(); @override void onInit() { super.onInit(); Logger.d(_tag, 'OTA检查服务已初始化'); } /// 重置检查状态(仅在应用重启时调用) static void resetCheckStatus() { _hasCheckedInCurrentSession = false; _isCheckingInProgress = false; Logger.d(_tag, 'OTA检查状态已重置'); } /// 检查是否应该进行OTA检查 bool shouldCheckOta() { return !_hasCheckedInCurrentSession && !_isCheckingInProgress; } /// 标记为已检查 void _markAsChecked() { _hasCheckedInCurrentSession = true; _isCheckingInProgress = false; Logger.d(_tag, 'OTA检查状态已标记为完成'); } /// 检查当前是否在Home页面 bool _isCurrentlyOnHomePage() { final currentRoute = Get.currentRoute; final isOnHome = currentRoute == Routes.home || currentRoute == Routes.initial; Logger.d(_tag, '当前路由: $currentRoute, 是否在Home页面: $isOnHome'); return isOnHome; } /// 执行OTA升级检查 /// [isFullyConnected] 设备是否完全连接 /// 返回是否成功执行了检查 Future performOtaCheck(bool isFullyConnected) async { bool isForceUpdate = false; try { // 检查是否应该进行OTA检查 if (!shouldCheckOta()) { Logger.d(_tag, '跳过OTA检查 - 已检查过或正在检查中'); return false; } // 检查设备连接状态 if (!isFullyConnected) { Logger.d(_tag, '设备未完全连接,跳过OTA检查'); return false; } // 检查当前页面 if (!_isCurrentlyOnHomePage()) { Logger.d(_tag, '当前不在Home页面,跳过OTA检查'); return false; } // 标记为检查中 _isCheckingInProgress = true; Logger.d(_tag, '开始执行OTA固件升级检查...'); // 获取当前版本信息 final currentInfo = await _otaCoreService.getCurrentVersionInfo(); final currentVersion = currentInfo['currentVersion'] as String; // 获取最新版本信息 final latestInfo = await _otaCoreService.getLatestVersionInfo(); final latestVersion = latestInfo['latestVersion'] as String; Logger.d(_tag, '当前版本: $currentVersion, 最新版本: $latestVersion'); // 检查是否有更新 final hasUpdate = _otaCoreService.isNewVersionAvailable(currentVersion, latestVersion); if (hasUpdate) { isForceUpdate = latestInfo['isForceUpdate'] as bool? ?? false; if (isForceUpdate) { Logger.d(_tag, '发现强制更新版本,准备显示升级提醒'); } else { Logger.d(_tag, '发现新版本,准备显示升级提醒'); } // 再次确认当前在Home页面(防止异步执行期间页面切换) if (_isCurrentlyOnHomePage()) { _showOtaUpgradeDialog( latestVersion, latestInfo['updateDescription'] as String?); } else { Logger.d(_tag, '检查完成时已不在Home页面,取消显示弹窗'); } } else { Logger.d(_tag, '当前已是最新版本'); } // 标记为已检查(无论是否有更新都标记为已检查) if (!isForceUpdate) { _markAsChecked(); } _isCheckingInProgress = false; return true; } catch (e) { Logger.e(_tag, '检查OTA升级失败: $e'); // 即使检查失败,也标记为已检查,避免重复尝试 if (!isForceUpdate) { _markAsChecked(); } _isCheckingInProgress = false; return false; } } /// 显示OTA升级提醒对话框 /// [latestVersion] 最新版本号 /// [updateDescription] 更新描述 void _showOtaUpgradeDialog(String latestVersion, String? updateDescription) { // 最后一次确认当前页面状态 if (!_isCurrentlyOnHomePage()) { Logger.d(_tag, '显示弹窗时发现不在Home页面,取消显示'); return; } final isDarkMode = Get.isDarkMode; Logger.d(_tag, '显示OTA升级提醒对话框'); Get.dialog( AlertDialog( title: Row( children: [ Icon( Icons.headset, color: isDarkMode ? const Color(0xFF0066CC) : Colors.blue[600], size: 24.sp, ), SizedBox(width: 8.w), // 使用Flexible确保标题文本能够自适应不同语言长度,避免溢出 Flexible( child: Text( 'ota_newVersionFound'.tr, // 发现新版本 style: TextStyle( color: isDarkMode ? Colors.white : Colors.blue[700], fontWeight: FontWeight.w600, fontSize: 20.sp, // 设置合适的字体大小 ), softWrap: true, // 启用软换行 overflow: TextOverflow.visible, // 确保文本完全显示 maxLines: 2, // 最多显示两行 ), ), ], ), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '${'latestFirmwareVersion'.tr}: $latestVersion', style: TextStyle( color: isDarkMode ? Colors.grey[400] : Colors.grey[600], fontWeight: FontWeight.w500, fontSize: 16.sp, ), ), ], ), actions: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ // 稍后再说 (靠左) TextButton( onPressed: () { Logger.d(_tag, '用户取消OTA升级'); Get.back(); }, child: Text( 'talkLater'.tr, style: TextStyle( color: isDarkMode ? Colors.grey[400] : Colors.grey[600], ), ), ), // 立即升级 (靠右) ElevatedButton( onPressed: () { Logger.d(_tag, '用户确认OTA升级,跳转到升级页面'); Get.back(); Get.toNamed(Routes.OTA_UPGRADE); }, style: ElevatedButton.styleFrom( backgroundColor: isDarkMode ? const Color(0xFF0066CC) : Colors.blue[600], foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.sp), ), side: isDarkMode ? BorderSide(color: Colors.grey[800]!, width: 1) : BorderSide.none, ), child: Text( 'ota_updateNow'.tr, style: TextStyle(fontSize: 14.sp), ), ), ], ), ], shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.sp), ), backgroundColor: isDarkMode ? const Color(0xFF1A1A1A) : Colors.white, ), barrierDismissible: true, // 允许点击外部关闭 ); } /// 获取当前检查状态(用于调试) Map getCheckStatus() { return { 'hasCheckedInCurrentSession': _hasCheckedInCurrentSession, 'isCheckingInProgress': _isCheckingInProgress, 'currentRoute': Get.currentRoute, 'isOnHomePage': _isCurrentlyOnHomePage(), }; } }