25-10-2/1
This commit is contained in:
111
lib/main.dart
111
lib/main.dart
@@ -1,21 +1,25 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
import 'package:sefer_driver/views/home/Captin/orderCaptin/order_request_page.dart';
|
||||
import 'package:wakelock_plus/wakelock_plus.dart';
|
||||
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
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: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/add_error.dart';
|
||||
@@ -29,6 +33,7 @@ import 'firebase_options.dart';
|
||||
import 'models/db_sql.dart';
|
||||
import 'print.dart';
|
||||
import 'splash_screen_page.dart';
|
||||
import 'views/home/Captin/orderCaptin/order_request_page.dart';
|
||||
import 'views/home/Captin/driver_map_page.dart';
|
||||
import 'views/home/Captin/orderCaptin/order_over_lay.dart';
|
||||
|
||||
@@ -38,10 +43,22 @@ DbSql sql = DbSql.instance;
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
const platform = MethodChannel('com.example.intaleq_driver/app_control');
|
||||
|
||||
/// تهيئة Firebase بوعي لمنع تهيئة مكرّرة على أندرويد (isolates متعددة)
|
||||
Future<void> initFirebaseIfNeeded() async {
|
||||
if (Firebase.apps.isEmpty) {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform);
|
||||
} else {
|
||||
Firebase.app();
|
||||
}
|
||||
}
|
||||
|
||||
/// ============ Handlers: Background ============
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
await initFirebaseIfNeeded();
|
||||
await GetStorage.init();
|
||||
|
||||
if (!Get.isRegistered<NotificationController>()) {
|
||||
@@ -61,7 +78,7 @@ Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
|
||||
if (message.notification?.title == 'Order' ||
|
||||
message.notification?.title == 'OrderSpeed') {
|
||||
var myListString = message.data['DriverList'] ?? '[]';
|
||||
final myListString = message.data['DriverList'] ?? '[]';
|
||||
Log.print('myListString: $myListString');
|
||||
|
||||
List<dynamic> myList;
|
||||
@@ -72,7 +89,7 @@ Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
myList = [];
|
||||
}
|
||||
|
||||
bool isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
} else {
|
||||
@@ -106,13 +123,13 @@ void notificationTapBackground(NotificationResponse notificationResponse) {
|
||||
NotificationController().handleNotificationResponse(notificationResponse);
|
||||
}
|
||||
|
||||
/// ============ Entrypoint: Overlay ============
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void overlayMain() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await GetStorage.init();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
await initFirebaseIfNeeded();
|
||||
|
||||
if (!Get.isRegistered<NotificationController>()) {
|
||||
Get.put(NotificationController());
|
||||
@@ -124,43 +141,52 @@ void overlayMain() async {
|
||||
));
|
||||
}
|
||||
|
||||
/// إغلاق الـ Overlay عند الحاجة
|
||||
Future<void> closeOverLay() async {
|
||||
bool isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.closeOverlay();
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
/// ============ Entrypoint: App ============
|
||||
|
||||
void main() {
|
||||
runZonedGuarded(() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await initFirebaseIfNeeded();
|
||||
await WakelockPlus.enable();
|
||||
await GetStorage.init();
|
||||
await initializeDateFormatting();
|
||||
|
||||
Stripe.publishableKey = AK.publishableKeyStripe;
|
||||
|
||||
await SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
|
||||
// سجل الهاندلر تبع رسائل الخلفية (لازم يكون Top-Level ومع @pragma)
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
|
||||
await WakelockPlus.enable();
|
||||
await GetStorage.init();
|
||||
await initializeDateFormatting();
|
||||
Stripe.publishableKey = AK.publishableKeyStripe;
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
runZonedGuarded<Future<void>>(() async {
|
||||
runApp(const MyApp());
|
||||
}, (error, stack) {
|
||||
// ==== START: ERROR FILTER ====
|
||||
String errorString = error.toString();
|
||||
final errorString = error.toString();
|
||||
|
||||
// Print all errors to the local debug console for development
|
||||
// اطبع كل شيء محلياً
|
||||
// (يمكنك استبدال print بـ Log.print إن رغبت)
|
||||
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') ||
|
||||
// تجاهُل بعض الأخطاء المعروفة
|
||||
final 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");
|
||||
@@ -186,6 +212,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -204,9 +231,20 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
await FirebaseMessaging.instance.requestPermission();
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
await FirebaseMessagesController().getToken();
|
||||
|
||||
// يمكن أيضاً تفعيل foreground presentation options هنا لو احتجت
|
||||
await NotificationController().initNotifications();
|
||||
// Generate a random index to pick a message
|
||||
final random = Random();
|
||||
final randomMessage =
|
||||
syrianDriverMessages[random.nextInt(syrianDriverMessages.length)];
|
||||
|
||||
// Schedule the notification with the random message
|
||||
NotificationController().scheduleNotificationsForSevenDays(
|
||||
randomMessage.split(':')[0],
|
||||
randomMessage.split(':')[1],
|
||||
"tone1",
|
||||
);
|
||||
} catch (e) {
|
||||
Log.print("Error during _initApp: $e");
|
||||
}
|
||||
@@ -214,7 +252,7 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
LocaleController localController = Get.put(LocaleController());
|
||||
final LocaleController localController = Get.put(LocaleController());
|
||||
return GetMaterialApp(
|
||||
navigatorKey: navigatorKey,
|
||||
title: AppInformation.appName,
|
||||
@@ -227,8 +265,9 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
GetPage(name: '/', page: () => SplashScreen()),
|
||||
GetPage(name: '/order-page', page: () => OrderRequestPage()),
|
||||
GetPage(
|
||||
name: '/passenger-location-map',
|
||||
page: () => PassengerLocationMapPage()),
|
||||
name: '/passenger-location-map',
|
||||
page: () => PassengerLocationMapPage(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user