|
|
|
@ -9,49 +9,50 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
|
/// 负责管理全局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<OtaCheckService>(); |
|
|
|
|
|
|
|
|
|
|
|
@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; |
|
|
|
final isOnHome = |
|
|
|
currentRoute == Routes.home || currentRoute == Routes.initial; |
|
|
|
Logger.d(_tag, '当前路由: $currentRoute, 是否在Home页面: $isOnHome'); |
|
|
|
return isOnHome; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 执行OTA升级检查 |
|
|
|
/// [isFullyConnected] 设备是否完全连接 |
|
|
|
/// 返回是否成功执行了检查 |
|
|
|
@ -62,53 +63,54 @@ class OtaCheckService extends GetxService { |
|
|
|
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); |
|
|
|
|
|
|
|
final hasUpdate = |
|
|
|
_otaCoreService.isNewVersionAvailable(currentVersion, latestVersion); |
|
|
|
|
|
|
|
if (hasUpdate) { |
|
|
|
Logger.d(_tag, '发现新版本,准备显示升级提醒'); |
|
|
|
|
|
|
|
|
|
|
|
// 再次确认当前在Home页面(防止异步执行期间页面切换) |
|
|
|
if (_isCurrentlyOnHomePage()) { |
|
|
|
_showOtaUpgradeDialog(latestVersion, latestInfo['updateDescription'] as String?); |
|
|
|
_showOtaUpgradeDialog( |
|
|
|
latestVersion, latestInfo['updateDescription'] as String?); |
|
|
|
} else { |
|
|
|
Logger.d(_tag, '检查完成时已不在Home页面,取消显示弹窗'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
Logger.d(_tag, '当前已是最新版本'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 标记为已检查(无论是否有更新都标记为已检查) |
|
|
|
_markAsChecked(); |
|
|
|
return true; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, '检查OTA升级失败: $e'); |
|
|
|
// 即使检查失败,也标记为已检查,避免重复尝试 |
|
|
|
@ -116,7 +118,7 @@ class OtaCheckService extends GetxService { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 显示OTA升级提醒对话框 |
|
|
|
/// [latestVersion] 最新版本号 |
|
|
|
/// [updateDescription] 更新描述 |
|
|
|
@ -126,24 +128,32 @@ class OtaCheckService extends GetxService { |
|
|
|
Logger.d(_tag, '显示弹窗时发现不在Home页面,取消显示'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
final isDarkMode = Get.isDarkMode; |
|
|
|
|
|
|
|
Logger.d(_tag, '显示OTA升级提醒对话框'); |
|
|
|
|
|
|
|
|
|
|
|
Get.dialog( |
|
|
|
AlertDialog( |
|
|
|
title: Row( |
|
|
|
children: [ |
|
|
|
Icon( |
|
|
|
Icons.headset, |
|
|
|
color: Colors.blue[600], |
|
|
|
color: isDarkMode ? const Color(0xFF0066CC) : Colors.blue[600], |
|
|
|
size: 24.sp, |
|
|
|
), |
|
|
|
SizedBox(width: 8.w), |
|
|
|
Text( |
|
|
|
'ota_newVersionFound'.tr, |
|
|
|
style: TextStyle( |
|
|
|
color: Colors.blue[700], |
|
|
|
fontWeight: FontWeight.w600, |
|
|
|
// 使用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, // 最多显示两行 |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
@ -155,6 +165,7 @@ class OtaCheckService extends GetxService { |
|
|
|
Text( |
|
|
|
'${'latestFirmwareVersion'.tr}: $latestVersion', |
|
|
|
style: TextStyle( |
|
|
|
color: isDarkMode ? Colors.grey[400] : Colors.grey[600], |
|
|
|
fontWeight: FontWeight.w500, |
|
|
|
fontSize: 16.sp, |
|
|
|
), |
|
|
|
@ -173,7 +184,9 @@ class OtaCheckService extends GetxService { |
|
|
|
}, |
|
|
|
child: Text( |
|
|
|
'talkLater'.tr, |
|
|
|
style: TextStyle(color: Colors.grey[600]), |
|
|
|
style: TextStyle( |
|
|
|
color: isDarkMode ? Colors.grey[400] : Colors.grey[600], |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
// 立即升级 (靠右) |
|
|
|
@ -184,11 +197,15 @@ class OtaCheckService extends GetxService { |
|
|
|
Get.toNamed(Routes.OTA_UPGRADE); |
|
|
|
}, |
|
|
|
style: ElevatedButton.styleFrom( |
|
|
|
backgroundColor: Colors.blue[600], |
|
|
|
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, |
|
|
|
@ -201,12 +218,12 @@ class OtaCheckService extends GetxService { |
|
|
|
shape: RoundedRectangleBorder( |
|
|
|
borderRadius: BorderRadius.circular(12.sp), |
|
|
|
), |
|
|
|
backgroundColor: Colors.white, |
|
|
|
backgroundColor: isDarkMode ? const Color(0xFF1A1A1A) : Colors.white, |
|
|
|
), |
|
|
|
barrierDismissible: true, // 允许点击外部关闭 |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 获取当前检查状态(用于调试) |
|
|
|
Map<String, dynamic> getCheckStatus() { |
|
|
|
return { |
|
|
|
|