marco.pms.mobileapp/lib/helpers/services/app_initializer.dart
2025-12-12 15:24:37 +05:30

125 lines
4.4 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/services.dart';
import 'package:url_strategy/url_strategy.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:on_field_work/helpers/services/storage/local_storage.dart';
import 'package:on_field_work/helpers/services/app_logger.dart';
import 'package:on_field_work/helpers/services/auth_service.dart';
import 'package:on_field_work/helpers/services/firebase/firebase_messaging_service.dart';
import 'package:on_field_work/helpers/services/device_info_service.dart';
import 'package:on_field_work/helpers/theme/theme_customizer.dart';
import 'package:on_field_work/helpers/theme/app_theme.dart';
import 'package:on_field_work/helpers/services/gemini_service.dart';
Future<void> initializeApp() async {
try {
logSafe("💡 Starting app initialization...");
await Future.wait([
_setupUI(),
_setupFirebase(),
_setupLocalStorage(),
]);
await _setupDeviceInfo();
await _handleAuthTokens();
await _setupTheme();
await _setupFirebaseMessaging();
_finalizeAppStyle();
logSafe("✅ App initialization completed successfully.");
} catch (e, stacktrace) {
logSafe(
"⛔ Error during app initialization",
level: LogLevel.error,
error: e,
stackTrace: stacktrace,
);
rethrow;
}
}
/// ---------------------------------------------------------------------------
/// 🔹 AUTH TOKEN HANDLER
/// ---------------------------------------------------------------------------
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. User must login again.");
}
} else {
logSafe("❌ No refresh token found. Skipping refresh.");
}
}
/// ---------------------------------------------------------------------------
/// 🔹 UI SETUP
/// ---------------------------------------------------------------------------
Future<void> _setupUI() async {
setPathUrlStrategy();
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
logSafe("💡 UI setup completed.");
}
/// ---------------------------------------------------------------------------
/// 🔹 FIREBASE + GEMINI SETUP
/// ---------------------------------------------------------------------------
Future<void> _setupFirebase() async {
// Firebase Core
await Firebase.initializeApp();
logSafe("🔥 Firebase initialized.");
await GeminiService.initialize();
logSafe("✨ Gemini service initialized (Gemini 2.5 Flash).");
}
/// ---------------------------------------------------------------------------
/// 🔹 LOCAL STORAGE SETUP
/// ---------------------------------------------------------------------------
Future<void> _setupLocalStorage() async {
if (!LocalStorage.isInitialized) {
await LocalStorage.init();
logSafe("💾 Local storage initialized.");
} else {
logSafe(" Local storage already initialized. Skipping.");
}
}
/// ---------------------------------------------------------------------------
/// 🔹 DEVICE INFO
/// ---------------------------------------------------------------------------
Future<void> _setupDeviceInfo() async {
final deviceInfoService = DeviceInfoService();
await deviceInfoService.init();
logSafe("📱 Device Info Loaded: ${deviceInfoService.deviceData}");
}
/// ---------------------------------------------------------------------------
/// 🔹 THEME SETUP
/// ---------------------------------------------------------------------------
Future<void> _setupTheme() async {
await ThemeCustomizer.init();
logSafe("🎨 Theme customizer initialized.");
}
/// ---------------------------------------------------------------------------
/// 🔹 FIREBASE CLOUD MESSAGING (PUSH)
/// ---------------------------------------------------------------------------
Future<void> _setupFirebaseMessaging() async {
await FirebaseNotificationService().initialize();
logSafe("📨 Firebase Messaging initialized.");
}
/// ---------------------------------------------------------------------------
/// 🔹 FINAL APP STYLE
/// ---------------------------------------------------------------------------
void _finalizeAppStyle() {
AppStyle.init();
logSafe("🎯 AppStyle initialized.");
}