feat(mobile-app): 在主页添加随机3-6秒轮询检查维护状态

- 用户登录后在 HomeShellPage 每 3-6 秒(随机)检查一次维护状态
- 随机间隔可避免所有用户同时请求,减少服务器压力
- 后端发起维护后,用户最多 6 秒内会看到维护弹窗
- App 进入后台时暂停检查,恢复前台时立即检查并重启定时器
- 启动时、从后台恢复时也会立即检查一次

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-28 06:21:01 -08:00
parent 0781c53101
commit 7e8113805d
1 changed files with 45 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import '../../../../core/theme/app_colors.dart';
import '../../../../core/di/injection_container.dart';
import '../../../../core/services/contract_check_service.dart';
import '../../../../core/updater/update_service.dart';
import '../../../../core/providers/maintenance_provider.dart';
import '../../../../routes/route_paths.dart';
import '../../../../routes/app_router.dart';
import '../../../../bootstrap.dart';
@ -33,6 +34,9 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
///
Timer? _contractCheckTimer;
///
Timer? _maintenanceCheckTimer;
///
bool _isShowingDialog = false;
@ -40,12 +44,15 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
//
//
WidgetsBinding.instance.addPostFrameCallback((_) {
_checkMaintenanceStatus();
_checkForUpdateIfNeeded();
_checkContractsAndKyc();
//
_startBackgroundContractCheck();
//
_startMaintenanceCheck();
});
}
@ -53,19 +60,55 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_contractCheckTimer?.cancel();
_maintenanceCheckTimer?.cancel();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
//
//
if (state == AppLifecycleState.resumed) {
_checkMaintenanceStatus();
_checkForUpdateIfNeeded();
//
_scheduleNextContractCheck();
//
_startMaintenanceCheck();
} else if (state == AppLifecycleState.paused) {
//
_contractCheckTimer?.cancel();
_maintenanceCheckTimer?.cancel();
}
}
/// 3-6
void _startMaintenanceCheck() {
_scheduleNextMaintenanceCheck();
}
///
void _scheduleNextMaintenanceCheck() {
_maintenanceCheckTimer?.cancel();
// 3-6
final randomSeconds = 3 + Random().nextInt(4); // 3 + 0~3 = 3~6
_maintenanceCheckTimer = Timer(Duration(seconds: randomSeconds), () async {
await _checkMaintenanceStatus();
//
if (mounted) {
_scheduleNextMaintenanceCheck();
}
});
}
///
Future<void> _checkMaintenanceStatus() async {
if (!mounted) return;
try {
final maintenanceNotifier = ref.read(maintenanceProvider.notifier);
await maintenanceNotifier.showMaintenanceDialogIfNeeded(context);
} catch (e) {
debugPrint('[HomeShellPage] 检查维护状态失败: $e');
}
}