import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'update_controller.dart'; import 'update_service.dart'; /// 前后台切换仅低频检查一次,网络失败或无新版本均静默结束。 abstract final class UpdateForegroundService { static DateTime? _lastCheck; static Future? _running; static Future check(BuildContext context, WidgetRef ref) { final now = DateTime.now(); if (_lastCheck != null && now.difference(_lastCheck!) < const Duration(minutes: 30)) { return Future.value(); } return _running ??= _run(context, ref).whenComplete(() => _running = null); } static Future _run(BuildContext context, WidgetRef ref) async { _lastCheck = DateTime.now(); try { final info = await ref .read(updateControllerProvider.notifier) .checkUpgrade(); if (context.mounted) { await UpdateService.showIfNeeded( context, ref, info, source: UpgradeCheckSource.automatic, ); } } catch (_) {} } }