25-10-5/1
This commit is contained in:
288
lib/main.dart
288
lib/main.dart
@@ -1,9 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:Intaleq/app_bindings.dart';
|
||||
import 'package:Intaleq/controller/functions/crud.dart';
|
||||
import 'package:Intaleq/controller/payment/paymob/paymob_response.dart';
|
||||
import 'package:Intaleq/views/home/HomePage/contact_us.dart';
|
||||
import 'package:Intaleq/views/home/HomePage/share_app_page.dart';
|
||||
import 'package:Intaleq/views/home/my_wallet/passenger_wallet.dart';
|
||||
@@ -16,287 +14,73 @@ import 'package:flutter_stripe/flutter_stripe.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:quick_actions/quick_actions.dart';
|
||||
import 'package:wakelock_plus/wakelock_plus.dart';
|
||||
import 'constant/api_key.dart';
|
||||
import 'constant/info.dart';
|
||||
import 'constant/notification.dart';
|
||||
import 'controller/firebase/firbase_messge.dart';
|
||||
import 'controller/firebase/local_notification.dart';
|
||||
import 'controller/functions/encrypt_decrypt.dart';
|
||||
import 'controller/functions/secure_storage.dart';
|
||||
import 'controller/home/deep_link_controller.dart';
|
||||
import 'controller/local/local_controller.dart';
|
||||
import 'controller/local/translations.dart';
|
||||
import 'controller/payment/paymob/paymob_wallet.dart';
|
||||
import 'firebase_options.dart';
|
||||
import 'models/db_sql.dart';
|
||||
import 'splash_screen_page.dart';
|
||||
|
||||
// -- Global instances for easy access --
|
||||
final box = GetStorage();
|
||||
final storage = FlutterSecureStorage();
|
||||
// final PaymobPayment paymobPayment = PaymobPayment();
|
||||
// final PaymobPayment paymobPayment = PaymobPayment();
|
||||
// final PaymobPaymentWallet paymobPaymentWallet = PaymobPaymentWallet();
|
||||
|
||||
DbSql sql = DbSql.instance;
|
||||
// EncryptionHelper encryptionHelper = EncryptionHelper.instance;
|
||||
|
||||
// Firebase background message handler must be a top-level function.
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
await Firebase.initializeApp();
|
||||
|
||||
FirebaseMessagesController().fireBaseTitles(message);
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
print("Handling a background message: ${message.messageId}");
|
||||
}
|
||||
|
||||
void main() {
|
||||
// ⚠️ لا تستدعِ ensureInitialized هنا خارج الزون
|
||||
|
||||
runZonedGuarded(() async {
|
||||
// 1) أنشئ الـ Binding في نفس الـ Zone
|
||||
// Use runZonedGuarded to catch all unhandled exceptions in the app.
|
||||
runZonedGuarded<Future<void>>(() async {
|
||||
// --- Step 1: Critical initializations before runApp() ---
|
||||
// These must complete before the UI can be built.
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 2) تهيئات خفيفة فقط قبل runApp
|
||||
await GetStorage.init();
|
||||
WakelockPlus.enable();
|
||||
if (Platform.isAndroid || Platform.isIOS) {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform);
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
await SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
}
|
||||
|
||||
// Stripe key initialization is very fast.
|
||||
Stripe.publishableKey = AK.publishableKey;
|
||||
Get.put(DeepLinkController(), permanent: true);
|
||||
|
||||
// 3) شغّل التطبيق فوراً
|
||||
// Lock screen orientation.
|
||||
await SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
Get.put(LocaleController());
|
||||
// --- Step 2: Run the app immediately ---
|
||||
// All heavy initializations are deferred to the SplashScreen.
|
||||
runApp(const MyApp());
|
||||
|
||||
// 4) بعد أول إطار: التهيئات الثقيلة
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
// لا Wakelock عالمي — فعّله فقط أثناء الرحلة
|
||||
await WakelockPlus.enable();
|
||||
final AppInitializer initializer = AppInitializer();
|
||||
|
||||
await initializer.initializeApp();
|
||||
await EncryptionHelper.initialize();
|
||||
|
||||
final notificationController = Get.put(NotificationController());
|
||||
await notificationController.initNotifications();
|
||||
|
||||
// أجّل طلب صلاحيات/توكن FCM
|
||||
final fcm = FirebaseMessagesController();
|
||||
await fcm.requestFirebaseMessagingPermission();
|
||||
await fcm.getNotificationSettings();
|
||||
await fcm.getToken();
|
||||
|
||||
// Generate a random index to pick a message
|
||||
// Generate and schedule passenger daily notifications safely
|
||||
try {
|
||||
// 1) تأكد أن القائمة موجودة وغير فارغة
|
||||
final List<String> msgs = passengerMessages ?? const [];
|
||||
if (msgs.isEmpty) {
|
||||
// fallback افتراضي إذا ما في رسائل
|
||||
const fallbackTitle = 'Intaleq';
|
||||
const fallbackBody = 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.';
|
||||
notificationController.scheduleNotificationsForSevenDays(
|
||||
fallbackTitle,
|
||||
fallbackBody,
|
||||
"tone1",
|
||||
);
|
||||
} else {
|
||||
// 2) اختَر عنصرًا عشوائيًا بأمان
|
||||
final rnd = Random();
|
||||
final idx = rnd.nextInt(msgs.length); // msgs.length > 0 أكيد
|
||||
final raw = msgs[idx];
|
||||
|
||||
// 3) افصل العنوان/النص بأمان حتى لو ما في ':'
|
||||
final parts = raw.split(':');
|
||||
final title = parts.isNotEmpty ? parts.first.trim() : 'Intaleq';
|
||||
final body = parts.length > 1
|
||||
? parts.sublist(1).join(':').trim() // يحافظ على أي ':' إضافية
|
||||
: 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.';
|
||||
|
||||
// 4) جدولة الإشعارات
|
||||
notificationController.scheduleNotificationsForSevenDays(
|
||||
title.isEmpty ? 'Intaleq' : title,
|
||||
body.isEmpty ? 'مرحباً بك! تابع رحلاتك بأمان مع انطلق.' : body,
|
||||
"tone1",
|
||||
);
|
||||
}
|
||||
} catch (e, st) {
|
||||
// لا تعطل التشغيل بسبب إشعار اختياري
|
||||
CRUD.addError('notif_init: $e', st.toString(), 'main');
|
||||
}
|
||||
|
||||
final QuickActions quickActions = QuickActions();
|
||||
|
||||
quickActions.initialize((String shortcutType) {
|
||||
// print('Activated shortcut: $shortcutType');
|
||||
if (shortcutType == 'share_app') {
|
||||
Get.toNamed('/shareApp');
|
||||
} else if (shortcutType == 'wallet') {
|
||||
Get.toNamed('/wallet');
|
||||
} else if (shortcutType == 'profile') {
|
||||
Get.toNamed('/profile');
|
||||
} else if (shortcutType == 'contact_support') {
|
||||
Get.toNamed('/contactSupport');
|
||||
}
|
||||
});
|
||||
quickActions.setShortcutItems(<ShortcutItem>[
|
||||
ShortcutItem(
|
||||
type: 'share_app',
|
||||
localizedTitle: 'Share App'.tr,
|
||||
icon: 'icon_share',
|
||||
),
|
||||
ShortcutItem(
|
||||
type: 'wallet',
|
||||
localizedTitle: 'Wallet'.tr,
|
||||
icon: 'icon_wallet',
|
||||
),
|
||||
ShortcutItem(
|
||||
type: 'profile',
|
||||
localizedTitle: 'Profile'.tr,
|
||||
icon: 'icon_user',
|
||||
),
|
||||
ShortcutItem(
|
||||
type: 'contact_support',
|
||||
localizedTitle: 'Contact Support'.tr,
|
||||
icon: 'icon_support',
|
||||
),
|
||||
]);
|
||||
});
|
||||
}, (error, stack) {
|
||||
// بديل آمن للتجميع
|
||||
// Global error handler.
|
||||
final s = error.toString();
|
||||
final ignored = s.contains('PERMISSION_DENIED') ||
|
||||
s.contains('FormatException') ||
|
||||
s.contains('Null check operator used on a null value');
|
||||
if (!ignored) CRUD.addError(s, stack.toString(), 'main');
|
||||
|
||||
if (!ignored) {
|
||||
CRUD.addError(s, stack.toString(), 'main_zone_guard');
|
||||
}
|
||||
});
|
||||
} // void main() async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// WakelockPlus.enable();
|
||||
|
||||
// await GetStorage.init();
|
||||
// // --- إضافة جديدة: تهيئة وحدة التحكم في الروابط العميقة ---
|
||||
// Get.put(DeepLinkController(), permanent: true);
|
||||
// // ----------------------------------------------------
|
||||
// final AppInitializer initializer = AppInitializer();
|
||||
|
||||
// await initializer.initializeApp();
|
||||
// await EncryptionHelper.initialize();
|
||||
|
||||
// NotificationController notificationController =
|
||||
// Get.put(NotificationController());
|
||||
|
||||
// // Stripe.publishableKey = AK.publishableKey;
|
||||
// // if (box.read(BoxName.driverID) != null) {}
|
||||
|
||||
// if (Platform.isAndroid || Platform.isIOS) {
|
||||
// await Firebase.initializeApp(
|
||||
// options: DefaultFirebaseOptions.currentPlatform,
|
||||
// );
|
||||
// await FirebaseMessagesController().requestFirebaseMessagingPermission();
|
||||
|
||||
// FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
|
||||
// List<Future> initializationTasks = [
|
||||
// FirebaseMessagesController().getNotificationSettings(),
|
||||
// FirebaseMessagesController().getToken(),
|
||||
// ];
|
||||
// // cameras = await availableCameras();
|
||||
// await Future.wait(initializationTasks);
|
||||
// SystemChrome.setPreferredOrientations([
|
||||
// DeviceOrientation.portraitUp,
|
||||
// DeviceOrientation.portraitDown,
|
||||
// ]);
|
||||
// }
|
||||
|
||||
// await notificationController.initNotifications();
|
||||
|
||||
// // Generate a random index to pick a message
|
||||
// final random = Random();
|
||||
// final randomMessage = messages[random.nextInt(messages.length)];
|
||||
|
||||
// // Schedule the notification with the random message
|
||||
// notificationController.scheduleNotificationsForSevenDays(
|
||||
// randomMessage.split(':')[0],
|
||||
// randomMessage.split(':')[1],
|
||||
// "tone1",
|
||||
// );
|
||||
|
||||
// final QuickActions quickActions = QuickActions();
|
||||
|
||||
// quickActions.initialize((String shortcutType) {
|
||||
// // print('Activated shortcut: $shortcutType');
|
||||
// if (shortcutType == 'share_app') {
|
||||
// Get.toNamed('/shareApp');
|
||||
// } else if (shortcutType == 'wallet') {
|
||||
// Get.toNamed('/wallet');
|
||||
// } else if (shortcutType == 'profile') {
|
||||
// Get.toNamed('/profile');
|
||||
// } else if (shortcutType == 'contact_support') {
|
||||
// Get.toNamed('/contactSupport');
|
||||
// }
|
||||
// });
|
||||
// quickActions.setShortcutItems(<ShortcutItem>[
|
||||
// ShortcutItem(
|
||||
// type: 'share_app',
|
||||
// localizedTitle: 'Share App'.tr,
|
||||
// icon: 'icon_share',
|
||||
// ),
|
||||
// ShortcutItem(
|
||||
// type: 'wallet',
|
||||
// localizedTitle: 'Wallet'.tr,
|
||||
// icon: 'icon_wallet',
|
||||
// ),
|
||||
// ShortcutItem(
|
||||
// type: 'profile',
|
||||
// localizedTitle: 'Profile'.tr,
|
||||
// icon: 'icon_user',
|
||||
// ),
|
||||
// ShortcutItem(
|
||||
// type: 'contact_support',
|
||||
// localizedTitle: 'Contact Support'.tr,
|
||||
// icon: 'icon_support',
|
||||
// ),
|
||||
// ]);
|
||||
|
||||
// runZonedGuarded<Future<void>>(() async {
|
||||
// runApp(const MyApp());
|
||||
// }, (error, stack) {
|
||||
// // ==== START: ERROR FILTER ====
|
||||
// String errorString = error.toString();
|
||||
|
||||
// // Print all errors to the local debug console for development
|
||||
// print("Caught Dart error: $error");
|
||||
// print(stack);
|
||||
|
||||
// // We will check if the error contains keywords for errors we want to ignore.
|
||||
// // If it's one of them, we will NOT send it to the server.
|
||||
// bool isIgnoredError = errorString.contains('PERMISSION_DENIED') ||
|
||||
// errorString.contains('FormatException') ||
|
||||
// errorString.contains('Null check operator used on a null value');
|
||||
|
||||
// if (!isIgnoredError) {
|
||||
// // Only send the error to the server if it's not in our ignore list.
|
||||
// CRUD.addError(error.toString(), stack.toString(), 'main');
|
||||
// } else {
|
||||
// print("Ignoring error and not sending to server: $errorString");
|
||||
// }
|
||||
// // ==== END: ERROR FILTER ====
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
// This widget is the root of your application.
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
LocaleController localController = Get.put(LocaleController());
|
||||
// Get.find() is used here because LocaleController is already put in AppBindings.
|
||||
final LocaleController localController = Get.find<LocaleController>();
|
||||
|
||||
return GetMaterialApp(
|
||||
title: AppInformation.appName,
|
||||
@@ -304,19 +88,21 @@ class MyApp extends StatelessWidget {
|
||||
debugShowCheckedModeBanner: false,
|
||||
locale: localController.language,
|
||||
theme: localController.appTheme,
|
||||
key: UniqueKey(),
|
||||
|
||||
// routes: {'/':const HomePage()},
|
||||
// home: LoginCaptin());
|
||||
// --- [CRITICAL] ---
|
||||
// initialBinding tells GetX to run AppBindings once at the start.
|
||||
// This sets up all our controllers (put, lazyPut) for the entire app lifecycle.
|
||||
initialBinding: AppBindings(),
|
||||
|
||||
initialRoute: '/',
|
||||
getPages: [
|
||||
GetPage(name: '/', page: () => SplashScreen()),
|
||||
GetPage(name: '/', page: () => const SplashScreen()),
|
||||
// These routes are used by QuickActions and other navigation events.
|
||||
GetPage(name: '/shareApp', page: () => ShareAppPage()),
|
||||
GetPage(name: '/wallet', page: () => PassengerWallet()),
|
||||
GetPage(name: '/wallet', page: () => const PassengerWallet()),
|
||||
GetPage(name: '/profile', page: () => PassengerProfilePage()),
|
||||
GetPage(name: '/contactSupport', page: () => ContactUsPage()),
|
||||
],
|
||||
// home: SplashScreen()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user