fix(mining-app): 自动更新检查未触发 — 修复双重节流 + 添加调试日志
问题原因: 1. checkForUpdate() 未传 force:true,被 UpdateService 内部的60分钟 SharedPreferences 间隔二次节流,导致实际请求被跳过 2. 缺少日志输出,无法诊断检查流程是否执行 修复: - checkForUpdate(force: true) 跳过内部60分钟限制, 因为 MainShell 已有自己的90-300秒随机冷却节流 - 在 initState / dispose / didChangeAppLifecycleState / _checkForUpdateIfNeeded 各关键节点添加 debugPrint 日志, 方便 flutter run 控制台追踪完整检查流程 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
53a2e64cad
commit
bde7f0c53b
|
|
@ -22,20 +22,24 @@ class _MainShellState extends State<MainShell> with WidgetsBindingObserver {
|
|||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
debugPrint('[MainShell] initState called, adding WidgetsBindingObserver');
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
debugPrint('[MainShell] postFrameCallback → 触发首次更新检查');
|
||||
_checkForUpdateIfNeeded();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
debugPrint('[MainShell] dispose called, removing WidgetsBindingObserver');
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
debugPrint('[MainShell] didChangeAppLifecycleState: $state');
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
_checkForUpdateIfNeeded();
|
||||
}
|
||||
|
|
@ -45,29 +49,41 @@ class _MainShellState extends State<MainShell> with WidgetsBindingObserver {
|
|||
final now = DateTime.now();
|
||||
|
||||
// 如果从未检查过,或者已过冷却时间,则检查
|
||||
if (_nextCheckAllowedTime == null || now.isAfter(_nextCheckAllowedTime!)) {
|
||||
// 设置下次允许检查时间(90-300秒随机间隔)
|
||||
final randomSeconds = 90 + Random().nextInt(211);
|
||||
_nextCheckAllowedTime = now.add(Duration(seconds: randomSeconds));
|
||||
if (_nextCheckAllowedTime != null && now.isBefore(_nextCheckAllowedTime!)) {
|
||||
debugPrint('[MainShell] 跳过更新检查,冷却中 (下次: $_nextCheckAllowedTime)');
|
||||
return;
|
||||
}
|
||||
|
||||
// 延迟3秒,避免启动时干扰用户
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
// 设置下次允许检查时间(90-300秒随机间隔)
|
||||
final randomSeconds = 90 + Random().nextInt(211);
|
||||
_nextCheckAllowedTime = now.add(Duration(seconds: randomSeconds));
|
||||
debugPrint('[MainShell] 准备检查更新,延迟3秒...');
|
||||
|
||||
// 延迟3秒,避免启动时干扰用户
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
if (!mounted) {
|
||||
debugPrint('[MainShell] widget 已 unmount,取消检查');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
debugPrint('[MainShell] 开始调用 UpdateService.checkForUpdate(force: true)');
|
||||
// force: true 跳过 UpdateService 内部的60分钟间隔限制
|
||||
// 因为我们已有自己的90-300秒冷却节流
|
||||
final result = await UpdateService.instance.checkForUpdate(force: true);
|
||||
debugPrint('[MainShell] 检查结果: hasUpdate=${result.hasUpdate}, error=${result.error}');
|
||||
if (!mounted) return;
|
||||
|
||||
try {
|
||||
final result = await UpdateService.instance.checkForUpdate();
|
||||
if (!mounted) return;
|
||||
|
||||
if (result.hasUpdate && result.versionInfo != null) {
|
||||
await SelfHostedUpdater.show(
|
||||
context,
|
||||
versionInfo: result.versionInfo!,
|
||||
isForceUpdate: result.versionInfo!.isForceUpdate,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[MainShell] 检查更新失败: $e');
|
||||
if (result.hasUpdate && result.versionInfo != null) {
|
||||
debugPrint('[MainShell] 发现新版本: ${result.versionInfo!.versionName},弹出更新对话框');
|
||||
await SelfHostedUpdater.show(
|
||||
context,
|
||||
versionInfo: result.versionInfo!,
|
||||
isForceUpdate: result.versionInfo!.isForceUpdate,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[MainShell] 检查更新失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue