126 lines
3.7 KiB
Dart
126 lines
3.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:url_strategy/url_strategy.dart';
|
|
import 'package:firebase_core/firebase_core.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/services/app_logger.dart';
|
|
import 'package:marco/helpers/services/auth_service.dart';
|
|
import 'package:marco/helpers/services/firebase/firebase_messaging_service.dart';
|
|
import 'package:marco/helpers/services/device_info_service.dart';
|
|
import 'package:marco/helpers/theme/theme_customizer.dart';
|
|
import 'package:marco/helpers/theme/app_theme.dart';
|
|
|
|
Future<void> initializeApp() async {
|
|
try {
|
|
logSafe("💡 Starting app initialization...");
|
|
|
|
await Future.wait([
|
|
_setupUI(),
|
|
_setupFirebase(),
|
|
_setupLocalStorage(),
|
|
]);
|
|
|
|
await _setupDeviceInfo();
|
|
await _handleAuthTokens();
|
|
await _setupTheme();
|
|
await _setupControllers();
|
|
await _setupFirebaseMessaging();
|
|
|
|
_finalizeAppStyle();
|
|
|
|
logSafe("✅ App initialization completed successfully.");
|
|
} catch (e, stacktrace) {
|
|
logSafe(
|
|
"⛔ Error during app initialization",
|
|
level: LogLevel.error,
|
|
error: e,
|
|
stackTrace: stacktrace,
|
|
);
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> _setupUI() async {
|
|
setPathUrlStrategy();
|
|
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
|
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
|
|
statusBarColor: Colors.transparent,
|
|
systemNavigationBarColor: Colors.transparent,
|
|
statusBarIconBrightness: Brightness.light,
|
|
systemNavigationBarIconBrightness: Brightness.dark,
|
|
));
|
|
logSafe("💡 UI setup completed.");
|
|
}
|
|
|
|
Future<void> _setupFirebase() async {
|
|
await Firebase.initializeApp();
|
|
logSafe("💡 Firebase initialized.");
|
|
}
|
|
|
|
Future<void> _setupLocalStorage() async {
|
|
await LocalStorage.init();
|
|
logSafe("💡 Local storage initialized.");
|
|
}
|
|
|
|
Future<void> _setupDeviceInfo() async {
|
|
final deviceInfoService = DeviceInfoService();
|
|
await deviceInfoService.init();
|
|
logSafe("📱 Device Info: ${deviceInfoService.deviceData}");
|
|
}
|
|
|
|
Future<void> _handleAuthTokens() async {
|
|
final refreshToken = await LocalStorage.getRefreshToken();
|
|
if (refreshToken?.isNotEmpty ?? false) {
|
|
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.");
|
|
}
|
|
}
|
|
|
|
Future<void> _setupTheme() async {
|
|
await ThemeCustomizer.init();
|
|
logSafe("💡 Theme customizer initialized.");
|
|
}
|
|
|
|
Future<void> _setupControllers() async {
|
|
final token = LocalStorage.getString('jwt_token');
|
|
if (token?.isEmpty ?? true) {
|
|
logSafe("⚠️ No valid JWT token found. Skipping controller initialization.");
|
|
return;
|
|
}
|
|
|
|
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 Future.wait([
|
|
Get.find<PermissionController>().loadData(token!),
|
|
Get.find<ProjectController>().fetchProjects(),
|
|
]);
|
|
}
|
|
|
|
Future<void> _setupFirebaseMessaging() async {
|
|
await FirebaseNotificationService().initialize();
|
|
logSafe("💡 Firebase Messaging initialized.");
|
|
}
|
|
|
|
void _finalizeAppStyle() {
|
|
AppStyle.init();
|
|
logSafe("💡 AppStyle initialized.");
|
|
}
|