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:
hailin 2026-01-30 12:20:56 -08:00
parent 53a2e64cad
commit bde7f0c53b
1 changed files with 35 additions and 19 deletions

View File

@ -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');
}
}