refactor: 重构 main 函数,将初始化逻辑提取到 AppInitializer
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'config/app_config.dart';
|
||||
import 'utils/app_theme.dart';
|
||||
import 'utils/screen_adapter.dart';
|
||||
import 'utils/app_initializer.dart';
|
||||
import 'utils/force_guide_checker.dart';
|
||||
import 'routes/app_routes.dart';
|
||||
import 'constants/app_constants.dart';
|
||||
@@ -11,73 +9,16 @@ import 'controllers/sqlite_storage_controller.dart';
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 初始化SQLite存储
|
||||
await SQLiteStorageController.init();
|
||||
|
||||
// 初始化SharedPreferences
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final result = await AppInitializer.initialize();
|
||||
|
||||
debugPrint('========================================');
|
||||
debugPrint('======== 应用启动 - 强制引导页 ========');
|
||||
debugPrint('========================================');
|
||||
|
||||
// 1. 先获取保存的版本号
|
||||
final savedVersion = prefs.getInt(AppConfig.keyAppVersion);
|
||||
debugPrint('保存的版本号: $savedVersion');
|
||||
|
||||
// 2. 如果是首次安装(没有版本号)或者版本不匹配
|
||||
bool isFirstInstall =
|
||||
savedVersion == null || savedVersion != AppConfig.appVersionCode;
|
||||
debugPrint('是否首次安装/升级: $isFirstInstall');
|
||||
|
||||
if (isFirstInstall) {
|
||||
debugPrint('--- 首次安装/升级,清空所有配置 ---');
|
||||
await prefs.clear();
|
||||
debugPrint('配置已清空');
|
||||
|
||||
// 3. 强制设置引导页相关的标志
|
||||
await prefs.setBool(AppConfig.keyFirstLaunch, true);
|
||||
await prefs.setBool(AppConfig.keyAgreementAccepted, false);
|
||||
await prefs.setBool(AppConfig.keyShowGuideOnStartup, false);
|
||||
await prefs.setInt(AppConfig.keyAppVersion, AppConfig.appVersionCode);
|
||||
debugPrint('已设置 firstLaunch=true');
|
||||
debugPrint('已设置 agreementAccepted=false');
|
||||
debugPrint('已设置 showGuideOnStartup=false');
|
||||
debugPrint('已设置 appVersion=${AppConfig.appVersionCode}');
|
||||
}
|
||||
|
||||
// 4. 验证设置是否正确
|
||||
final bool firstLaunch = prefs.getBool(AppConfig.keyFirstLaunch) ?? true;
|
||||
final bool agreementAccepted =
|
||||
prefs.getBool(AppConfig.keyAgreementAccepted) ?? false;
|
||||
final int? appVersion = prefs.getInt(AppConfig.keyAppVersion);
|
||||
final allKeys = prefs.getKeys();
|
||||
|
||||
debugPrint('--- 验证配置 ---');
|
||||
debugPrint('firstLaunch: $firstLaunch');
|
||||
debugPrint('agreementAccepted: $agreementAccepted');
|
||||
debugPrint('appVersion: $appVersion');
|
||||
debugPrint('所有配置项: $allKeys');
|
||||
|
||||
// 5. 强制检查引导页状态
|
||||
GuideCheckResult? guideCheckResult;
|
||||
try {
|
||||
guideCheckResult = await ForceGuideChecker.checkAndValidate(prefs);
|
||||
debugPrint('引导页检查结果: ${guideCheckResult.needGuide ? '需要引导页' : '不需要引导页'}');
|
||||
debugPrint('原因: ${guideCheckResult.reason}');
|
||||
} catch (e) {
|
||||
debugPrint('引导页检查异常: $e');
|
||||
}
|
||||
|
||||
// 6. 设置屏幕适配
|
||||
ScreenAdapter.setPreferredOrientations();
|
||||
ScreenAdapter.enableSystemUIOverlay();
|
||||
ScreenAdapter.setSystemUIOverlayStyle(statusBarColor: Colors.transparent);
|
||||
|
||||
// 7. 获取初始路由
|
||||
String initialRoute = await AppRoutes.getInitialRoute(prefs);
|
||||
|
||||
runApp(MyApp(initialRoute: initialRoute, guideCheckResult: guideCheckResult));
|
||||
runApp(
|
||||
MyApp(
|
||||
initialRoute: result.initialRoute,
|
||||
guideCheckResult: result.guideCheckResult,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
|
||||
112
lib/utils/app_initializer.dart
Normal file
112
lib/utils/app_initializer.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
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,
|
||||
});
|
||||
}
|
||||
@@ -102,9 +102,7 @@ class _ProfilePageState extends State<ProfilePage>
|
||||
setState(() {
|
||||
_poetryHistory = history;
|
||||
});
|
||||
} catch (e) {
|
||||
print('加载历史记录失败: $e');
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
Future<void> _savePoetryToHistory(Map<String, dynamic> poetryData) async {
|
||||
@@ -117,7 +115,6 @@ class _ProfilePageState extends State<ProfilePage>
|
||||
_showSnackBar('该诗词已在历史记录中');
|
||||
}
|
||||
} catch (e) {
|
||||
print('保存历史记录失败: $e');
|
||||
_showSnackBar('保存失败');
|
||||
}
|
||||
}
|
||||
@@ -135,7 +132,6 @@ class _ProfilePageState extends State<ProfilePage>
|
||||
_showSnackBar('清空失败');
|
||||
}
|
||||
} catch (e) {
|
||||
print('清空历史记录失败: $e');
|
||||
_showSnackBar('清空失败');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user