93 lines
3.2 KiB
Dart
93 lines
3.2 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:marco/controller/permission_controller.dart';
|
|
import 'package:marco/controller/project_controller.dart';
|
|
import 'package:marco/helpers/services/storage/local_storage.dart';
|
|
import 'package:marco/helpers/theme/theme_customizer.dart';
|
|
import 'package:marco/helpers/theme/app_theme.dart';
|
|
import 'package:url_strategy/url_strategy.dart';
|
|
import 'package:marco/helpers/services/app_logger.dart';
|
|
import 'package:marco/helpers/services/auth_service.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:marco/helpers/services/firebase_messaging_services.dart';
|
|
|
|
Future<void> initializeApp() async {
|
|
try {
|
|
logSafe("💡 Starting app initialization...");
|
|
|
|
// 1. Set Flutter web path URL strategy (optional but early)
|
|
setPathUrlStrategy();
|
|
logSafe("💡 URL strategy set.");
|
|
|
|
// 2. Set system UI overlays
|
|
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
|
|
statusBarColor: Color.fromARGB(255, 255, 0, 0),
|
|
statusBarIconBrightness: Brightness.light,
|
|
));
|
|
logSafe("💡 System UI overlay style set.");
|
|
|
|
// 3. Initialize Firebase (should be before any Firebase service usage)
|
|
await Firebase.initializeApp();
|
|
logSafe("💡 Firebase initialized.");
|
|
|
|
// 4. Local storage
|
|
await LocalStorage.init();
|
|
logSafe("💡 Local storage initialized.");
|
|
|
|
// 5. Try to refresh JWT token if available
|
|
final refreshToken = await LocalStorage.getRefreshToken();
|
|
if (refreshToken != null && refreshToken.isNotEmpty) {
|
|
logSafe("🔁 Refresh token found. Attempting to refresh JWT...");
|
|
final success = await AuthService.refreshToken();
|
|
|
|
if (!success) {
|
|
logSafe("⚠️ Refresh token invalid or expired. Skipping controller injection.");
|
|
}
|
|
} else {
|
|
logSafe("❌ No refresh token found. Skipping refresh.");
|
|
}
|
|
|
|
// 6. Initialize theme customization
|
|
await ThemeCustomizer.init();
|
|
logSafe("💡 Theme customizer initialized.");
|
|
|
|
// 7. Inject controllers if token is valid
|
|
final token = LocalStorage.getString('jwt_token');
|
|
if (token != null && token.isNotEmpty) {
|
|
if (!Get.isRegistered<PermissionController>()) {
|
|
Get.put(PermissionController());
|
|
logSafe("💡 PermissionController injected.");
|
|
}
|
|
|
|
if (!Get.isRegistered<ProjectController>()) {
|
|
Get.put(ProjectController(), permanent: true);
|
|
logSafe("💡 ProjectController injected as permanent.");
|
|
}
|
|
|
|
await Get.find<PermissionController>().loadData(token);
|
|
await Get.find<ProjectController>().fetchProjects();
|
|
} else {
|
|
logSafe("⚠️ No valid JWT token found. Skipping controller initialization.");
|
|
}
|
|
|
|
// 8. Firebase Messaging setup
|
|
final notificationService = FirebaseNotificationService();
|
|
await notificationService.initialize();
|
|
logSafe("💡 Firebase Messaging initialized.");
|
|
|
|
// 9. App style setup
|
|
AppStyle.init();
|
|
logSafe("💡 AppStyle initialized.");
|
|
|
|
logSafe("✅ App initialization completed successfully.");
|
|
} catch (e, stacktrace) {
|
|
logSafe(
|
|
"⛔ Error during app initialization",
|
|
level: LogLevel.error,
|
|
error: e,
|
|
stackTrace: stacktrace,
|
|
);
|
|
rethrow;
|
|
}
|
|
}
|