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 @override
void initState() { void initState() {
super.initState(); super.initState();
debugPrint('[MainShell] initState called, adding WidgetsBindingObserver');
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
debugPrint('[MainShell] postFrameCallback → 触发首次更新检查');
_checkForUpdateIfNeeded(); _checkForUpdateIfNeeded();
}); });
} }
@override @override
void dispose() { void dispose() {
debugPrint('[MainShell] dispose called, removing WidgetsBindingObserver');
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
super.dispose(); super.dispose();
} }
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
debugPrint('[MainShell] didChangeAppLifecycleState: $state');
if (state == AppLifecycleState.resumed) { if (state == AppLifecycleState.resumed) {
_checkForUpdateIfNeeded(); _checkForUpdateIfNeeded();
} }
@ -45,29 +49,41 @@ class _MainShellState extends State<MainShell> with WidgetsBindingObserver {
final now = DateTime.now(); final now = DateTime.now();
// //
if (_nextCheckAllowedTime == null || now.isAfter(_nextCheckAllowedTime!)) { if (_nextCheckAllowedTime != null && now.isBefore(_nextCheckAllowedTime!)) {
// 90-300 debugPrint('[MainShell] 跳过更新检查,冷却中 (下次: $_nextCheckAllowedTime)');
final randomSeconds = 90 + Random().nextInt(211); return;
_nextCheckAllowedTime = now.add(Duration(seconds: randomSeconds)); }
// 3 // 90-300
await Future.delayed(const Duration(seconds: 3)); 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; if (!mounted) return;
try { if (result.hasUpdate && result.versionInfo != null) {
final result = await UpdateService.instance.checkForUpdate(); debugPrint('[MainShell] 发现新版本: ${result.versionInfo!.versionName},弹出更新对话框');
if (!mounted) return; await SelfHostedUpdater.show(
context,
if (result.hasUpdate && result.versionInfo != null) { versionInfo: result.versionInfo!,
await SelfHostedUpdater.show( isForceUpdate: result.versionInfo!.isForceUpdate,
context, );
versionInfo: result.versionInfo!,
isForceUpdate: result.versionInfo!.isForceUpdate,
);
}
} catch (e) {
debugPrint('[MainShell] 检查更新失败: $e');
} }
} catch (e) {
debugPrint('[MainShell] 检查更新失败: $e');
} }
} }