113 lines
3.4 KiB
Dart
113 lines
3.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../config/app_config.dart';
|
|
import '../routes/app_routes.dart';
|
|
import 'screen_adapter.dart';
|
|
import 'force_guide_checker.dart';
|
|
|
|
class AppInitializer {
|
|
static const bool _isDebugMode =
|
|
bool.fromEnvironment('dart.vm.product') == false;
|
|
|
|
static void _log(String message) {
|
|
if (_isDebugMode) {
|
|
debugPrint(message);
|
|
}
|
|
}
|
|
|
|
static Future<InitializationResult> initialize() async {
|
|
_log('========================================');
|
|
_log('======== 应用启动 - 强制引导页 ========');
|
|
_log('========================================');
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await _handleVersionCheck(prefs);
|
|
_logConfigState(prefs);
|
|
|
|
final guideCheckResult = await _checkGuideStatus(prefs);
|
|
|
|
_setupScreenAdapter();
|
|
|
|
final initialRoute = await _getInitialRoute(prefs);
|
|
|
|
return InitializationResult(
|
|
prefs: prefs,
|
|
guideCheckResult: guideCheckResult,
|
|
initialRoute: initialRoute,
|
|
);
|
|
}
|
|
|
|
static Future<void> _handleVersionCheck(SharedPreferences prefs) async {
|
|
final savedVersion = prefs.getInt(AppConfig.keyAppVersion);
|
|
_log('保存的版本号: $savedVersion');
|
|
|
|
final bool isFirstInstall =
|
|
savedVersion == null || savedVersion != AppConfig.appVersionCode;
|
|
_log('是否首次安装/升级: $isFirstInstall');
|
|
|
|
if (isFirstInstall) {
|
|
_log('--- 首次安装/升级,清空所有配置 ---');
|
|
await prefs.clear();
|
|
|
|
await Future.wait([
|
|
prefs.setBool(AppConfig.keyFirstLaunch, true),
|
|
prefs.setBool(AppConfig.keyAgreementAccepted, false),
|
|
prefs.setBool(AppConfig.keyShowGuideOnStartup, false),
|
|
prefs.setInt(AppConfig.keyAppVersion, AppConfig.appVersionCode),
|
|
]);
|
|
|
|
_log('已设置 firstLaunch=true');
|
|
_log('已设置 agreementAccepted=false');
|
|
_log('已设置 showGuideOnStartup=false');
|
|
_log('已设置 appVersion=${AppConfig.appVersionCode}');
|
|
}
|
|
}
|
|
|
|
static void _logConfigState(SharedPreferences prefs) {
|
|
_log('--- 验证配置 ---');
|
|
_log('firstLaunch: ${prefs.getBool(AppConfig.keyFirstLaunch) ?? true}');
|
|
_log(
|
|
'agreementAccepted: ${prefs.getBool(AppConfig.keyAgreementAccepted) ?? false}',
|
|
);
|
|
_log('appVersion: ${prefs.getInt(AppConfig.keyAppVersion)}');
|
|
_log('所有配置项: ${prefs.getKeys()}');
|
|
}
|
|
|
|
static Future<GuideCheckResult?> _checkGuideStatus(
|
|
SharedPreferences prefs,
|
|
) async {
|
|
try {
|
|
final result = await ForceGuideChecker.checkAndValidate(prefs);
|
|
_log('引导页检查结果: ${result.needGuide ? "需要引导页" : "不需要引导页"}');
|
|
_log('原因: ${result.reason}');
|
|
return result;
|
|
} catch (e) {
|
|
_log('引导页检查异常: $e');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static void _setupScreenAdapter() {
|
|
ScreenAdapter.setPreferredOrientations();
|
|
ScreenAdapter.enableSystemUIOverlay();
|
|
ScreenAdapter.setSystemUIOverlayStyle(statusBarColor: Colors.transparent);
|
|
}
|
|
|
|
static Future<String> _getInitialRoute(SharedPreferences prefs) async {
|
|
return AppRoutes.getInitialRoute(prefs);
|
|
}
|
|
}
|
|
|
|
class InitializationResult {
|
|
final SharedPreferences prefs;
|
|
final GuideCheckResult? guideCheckResult;
|
|
final String initialRoute;
|
|
|
|
const InitializationResult({
|
|
required this.prefs,
|
|
required this.guideCheckResult,
|
|
required this.initialRoute,
|
|
});
|
|
}
|