2026-02-20-overlay
This commit is contained in:
@@ -3,7 +3,6 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:geolocator/geolocator.dart' as geo;
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
@@ -13,6 +12,7 @@ import 'package:permission_handler/permission_handler.dart' as ph;
|
||||
import 'package:sefer_driver/views/home/Captin/orderCaptin/order_request_page.dart';
|
||||
import 'package:socket_io_client/socket_io_client.dart' as IO;
|
||||
import 'package:sefer_driver/constant/table_names.dart';
|
||||
import 'package:trip_overlay_plugin/trip_overlay_plugin.dart';
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
@@ -282,14 +282,23 @@ class LocationController extends GetxController with WidgetsBindingObserver {
|
||||
Map<String, dynamic> rideData, String source) async {
|
||||
print("📦 Socket Order Received from ($source)");
|
||||
|
||||
// 🔴 1. التحقق من حالة التطبيق قبل أي شيء 🔴
|
||||
bool isAppInForeground = box.read(BoxName.isAppInForeground) ?? false;
|
||||
|
||||
if (!isAppInForeground) {
|
||||
print(
|
||||
"🛑 التطبيق في الخلفية. السوكيت سيتجاهل التوجيه ويترك المهمة للـ Overlay.");
|
||||
return; // 👈 هذا السطر يمنع السوكيت من إكمال العمل وفتح الصفحة
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. التحقق من صحة البيانات
|
||||
// 2. التحقق من صحة البيانات
|
||||
if (rideData.isEmpty || !rideData.containsKey('16')) {
|
||||
print("❌ Socket Error: Invalid Ride Data.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. تجهيز البيانات (DriverList)
|
||||
// 3. تجهيز البيانات (DriverList)
|
||||
List<dynamic> driverList = [];
|
||||
if (rideData.length > 0) {
|
||||
var sortedKeys = rideData.keys.map((e) => int.tryParse(e) ?? 0).toList()
|
||||
@@ -298,22 +307,19 @@ class LocationController extends GetxController with WidgetsBindingObserver {
|
||||
driverList.add(rideData[key.toString()]);
|
||||
}
|
||||
}
|
||||
/////////
|
||||
// 3. التنقل (باستخدام الاسم لضمان عمل الشرط)
|
||||
|
||||
// 4. إغلاق النافذة (إن وجدت بالخطأ) والتنقل
|
||||
try {
|
||||
if (await FlutterOverlayWindow.isActive()) {
|
||||
if (await TripOverlayPlugin.isOverlayActive()) {
|
||||
print("📲 Closing Overlay because App took control via Socket");
|
||||
await FlutterOverlayWindow.closeOverlay();
|
||||
await TripOverlayPlugin.hideOverlay();
|
||||
}
|
||||
} catch (e) {
|
||||
print("Overlay check error: $e");
|
||||
}
|
||||
// ✅ هذا الشرط سيعمل الآن بدقة لأننا سنستخدم toNamed
|
||||
|
||||
if (Get.currentRoute != '/OrderRequestPage') {
|
||||
print("🚀 Socket: Navigating to OrderRequestPage...");
|
||||
|
||||
// 🔥 التعديل هنا: استخدمنا Get.toNamed بدلاً من Get.to
|
||||
// هذا يضمن تطابق الاسم مع ما هو موجود في main.dart
|
||||
Get.toNamed('/OrderRequestPage', arguments: {
|
||||
'myListString': jsonEncode(driverList),
|
||||
'DriverList': driverList,
|
||||
|
||||
568
lib/main.dart
568
lib/main.dart
@@ -1,31 +1,32 @@
|
||||
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/constant/box_name.dart';
|
||||
import 'package:trip_overlay_plugin/trip_overlay_plugin.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_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:permission_handler/permission_handler.dart'; // ✅ جديد
|
||||
import 'package:device_info_plus/device_info_plus.dart'; // ✅ جديد
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
|
||||
import 'constant/api_key.dart';
|
||||
import 'constant/info.dart';
|
||||
import 'constant/notification.dart';
|
||||
import 'constant/links.dart';
|
||||
import 'controller/firebase/firbase_messge.dart';
|
||||
import 'controller/firebase/local_notification.dart';
|
||||
import 'controller/functions/background_service.dart';
|
||||
import 'controller/functions/crud.dart';
|
||||
import 'controller/home/captin/home_captain_controller.dart';
|
||||
import 'controller/local/local_controller.dart';
|
||||
import 'controller/local/translations.dart';
|
||||
import 'firebase_options.dart';
|
||||
@@ -34,7 +35,6 @@ 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';
|
||||
|
||||
final box = GetStorage();
|
||||
const storage = FlutterSecureStorage();
|
||||
@@ -42,12 +42,10 @@ DbSql sql = DbSql.instance;
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
const platform = MethodChannel('com.intaleq_driver/app_control');
|
||||
|
||||
// ✅ قنوات الإشعارات المطلوبة
|
||||
const String backgroundServiceChannelId = 'driver_service_channel';
|
||||
const String locationServiceChannelId = 'location_service_channel';
|
||||
const String geolocatorChannelId = 'geolocator_channel';
|
||||
|
||||
/// تهيئة Firebase بوعي لمنع تهيئة مكرّرة على أندرويد (isolates متعددة)
|
||||
Future<void> initFirebaseIfNeeded() async {
|
||||
if (Firebase.apps.isEmpty) {
|
||||
await Firebase.initializeApp(
|
||||
@@ -57,28 +55,19 @@ Future<void> initFirebaseIfNeeded() async {
|
||||
}
|
||||
}
|
||||
|
||||
/// ✅ طلب إذن الإشعارات على Android 13+
|
||||
Future<bool> requestNotificationPermission() async {
|
||||
if (Platform.isAndroid) {
|
||||
try {
|
||||
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
||||
|
||||
if (androidInfo.version.sdkInt >= 33) {
|
||||
final status = await Permission.notification.request();
|
||||
|
||||
if (status.isGranted) {
|
||||
print('✅ Notification permission granted');
|
||||
return true;
|
||||
} else if (status.isDenied) {
|
||||
print('⚠️ Notification permission denied');
|
||||
return false;
|
||||
} else if (status.isPermanentlyDenied) {
|
||||
print('⚠️ Notification permission permanently denied');
|
||||
if (status.isGranted) return true;
|
||||
if (status.isDenied) return false;
|
||||
if (status.isPermanentlyDenied) {
|
||||
await openAppSettings();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
print('✅ Android < 13, no runtime notification permission needed');
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -86,261 +75,200 @@ Future<bool> requestNotificationPermission() async {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true; // iOS
|
||||
return true;
|
||||
}
|
||||
|
||||
/// ✅ إنشاء جميع قنوات الإشعارات المطلوبة
|
||||
Future<void> createAllNotificationChannels() async {
|
||||
if (!Platform.isAndroid) return;
|
||||
|
||||
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
|
||||
// قناة Background Service
|
||||
const AndroidNotificationChannel backgroundChannel =
|
||||
AndroidNotificationChannel(
|
||||
backgroundServiceChannelId,
|
||||
'خدمة السائق',
|
||||
description: 'استقبال الطلبات في الخلفية',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false,
|
||||
);
|
||||
|
||||
// قناة Location Service
|
||||
AndroidNotificationChannel(backgroundServiceChannelId, 'خدمة السائق',
|
||||
description: 'استقبال الطلبات في الخلفية',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false);
|
||||
const AndroidNotificationChannel locationChannel = AndroidNotificationChannel(
|
||||
locationServiceChannelId,
|
||||
'خدمة الموقع',
|
||||
description: 'تتبع موقع السائق',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false,
|
||||
);
|
||||
|
||||
// قناة Geolocator
|
||||
locationServiceChannelId, 'خدمة الموقع',
|
||||
description: 'تتبع موقع السائق',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false);
|
||||
const AndroidNotificationChannel geolocatorChannel =
|
||||
AndroidNotificationChannel(
|
||||
geolocatorChannelId,
|
||||
'تحديد الموقع',
|
||||
description: 'خدمة تحديد الموقع الجغرافي',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false,
|
||||
);
|
||||
AndroidNotificationChannel(geolocatorChannelId, 'تحديد الموقع',
|
||||
description: 'خدمة تحديد الموقع الجغرافي',
|
||||
importance: Importance.low,
|
||||
playSound: false,
|
||||
enableVibration: false,
|
||||
showBadge: false);
|
||||
|
||||
try {
|
||||
// إنشاء جميع القنوات
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(backgroundChannel);
|
||||
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(locationChannel);
|
||||
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(geolocatorChannel);
|
||||
|
||||
print('✅ All notification channels created successfully');
|
||||
} catch (e) {
|
||||
print('❌ Error creating notification channels: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// ============ Handlers: Background ============
|
||||
// في ملف main.dart (خارج كلاس MyApp)
|
||||
// ==============================================================================
|
||||
// 🔴 دوال مساعدة 🔴
|
||||
// ==============================================================================
|
||||
String _getVal(List<dynamic> data, int index) {
|
||||
if (data.length > index && data[index] != null) {
|
||||
return data[index].toString();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
// 1. تهيئة بيئة فلاتر في الخلفية
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Future<void> _processRejectOrderBackground(List<dynamic> data) async {
|
||||
try {
|
||||
final box = GetStorage();
|
||||
final driverId = box.read(BoxName.driverID);
|
||||
String orderId = _getVal(data, 16);
|
||||
|
||||
// 2. تهيئة الكونترولر (لأنه isolate منفصل)
|
||||
// ملاحظة: تأكد أنك لا تعتمد على Context هنا
|
||||
final NotificationController notificationController =
|
||||
NotificationController();
|
||||
|
||||
// مهم جداً: إعادة تهيئة الإشعارات داخل هذه العملية المنفصلة
|
||||
await notificationController.initNotifications();
|
||||
|
||||
print("🟢 Background Message Received: ${message.data}");
|
||||
|
||||
// 3. استخراج البيانات (الآن العنوان والنص داخل data وليس notification)
|
||||
String? title = message.data['title'];
|
||||
String? body = message.data['body'];
|
||||
String? tone = message.data['tone'] ?? 'order';
|
||||
String? myListString = message.data['DriverList'];
|
||||
|
||||
// 4. شرط الأمان: التأكد من وجود البيانات المطلوبة
|
||||
if (title != null && body != null && myListString != null) {
|
||||
// 5. عرض الإشعار المحلي
|
||||
notificationController.showOrderNotification(
|
||||
title,
|
||||
body,
|
||||
'ding.wav',
|
||||
myListString,
|
||||
);
|
||||
} else {
|
||||
print("⚠️ Received empty data message or missing fields.");
|
||||
if (driverId != null && orderId.isNotEmpty) {
|
||||
print("📤 Rejecting Order: $orderId");
|
||||
await CRUD().post(link: AppLink.addDriverOrder, payload: {
|
||||
'driver_id': driverId,
|
||||
'order_id': orderId,
|
||||
'status': 'Refused'
|
||||
});
|
||||
print("✅ Order Rejected Silently!");
|
||||
}
|
||||
} catch (e) {
|
||||
print("❌ Error rejecting order: $e");
|
||||
}
|
||||
}
|
||||
// @pragma('vm:entry-point')
|
||||
// Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// await initFirebaseIfNeeded();
|
||||
// await GetStorage.init();
|
||||
|
||||
// if (!Get.isRegistered<NotificationController>()) {
|
||||
// Get.put(NotificationController());
|
||||
// }
|
||||
// if (!Get.isRegistered<FirebaseMessagesController>()) {
|
||||
// Get.put(FirebaseMessagesController());
|
||||
// }
|
||||
|
||||
// if (!await FlutterOverlayWindow.isPermissionGranted()) {
|
||||
// Log.print("Overlay permission not granted; showing only notification.");
|
||||
// }
|
||||
|
||||
// if (Platform.isAndroid) {
|
||||
// String category = message.data['category'] ?? '';
|
||||
// Log.print('category: ${category}');
|
||||
// // if (message.notification != null) {
|
||||
// // Log.print('message.notification!.title: ${message.notification!.title}');
|
||||
|
||||
// if (category == 'Order' || category == 'OrderSpeed') {
|
||||
// final myListString = message.data['DriverList'] ?? '[]';
|
||||
// Log.print('myListString: $myListString');
|
||||
|
||||
// List<dynamic> myList;
|
||||
// try {
|
||||
// myList = jsonDecode(myListString) as List<dynamic>;
|
||||
// } catch (e) {
|
||||
// Log.print('Error decoding JSON: $e');
|
||||
// myList = [];
|
||||
// }
|
||||
|
||||
// // final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
// // if (isOverlayActive) {
|
||||
// // await FlutterOverlayWindow.shareData(myList);
|
||||
// // } else {
|
||||
// // await FlutterOverlayWindow.showOverlay(
|
||||
// // enableDrag: true,
|
||||
// // flag: OverlayFlag.focusPointer,
|
||||
// // positionGravity: PositionGravity.auto,
|
||||
// // height: WindowSize.matchParent,
|
||||
// // width: WindowSize.matchParent,
|
||||
// // startPosition: const OverlayPosition(0, -30),
|
||||
// // );
|
||||
// // await FlutterOverlayWindow.shareData(myList);
|
||||
// // }
|
||||
// NotificationController().showOrderNotification(
|
||||
// message.notification?.title ?? "طلب جديد", // العنوان
|
||||
// message.notification?.body ?? "لديك طلب توصيل جديد", // النص الأساسي
|
||||
// 'ding', // اسم نغمة الإشعار (تأكد أنها موجودة في raw)
|
||||
// myListString, // البيانات القادمة من السيرفر (JSON String List)
|
||||
// );
|
||||
// } else {
|
||||
// FirebaseMessagesController().fireBaseTitles(message);
|
||||
// }
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
|
||||
// ==============================================================================
|
||||
// 🔴 التعامل مع الإشعارات في الخلفية 🔴
|
||||
// ==============================================================================
|
||||
@pragma('vm:entry-point')
|
||||
void notificationTapBackground(NotificationResponse notificationResponse) {
|
||||
Log.print('Notification tapped in background!');
|
||||
NotificationController().handleNotificationResponse(notificationResponse);
|
||||
}
|
||||
|
||||
/// ============ Entrypoint: Overlay ============
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void overlayMain() async {
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await GetStorage.init();
|
||||
|
||||
if (!Get.isRegistered<NotificationController>()) {
|
||||
Get.put(NotificationController());
|
||||
}
|
||||
await TripOverlayPlugin.initialize();
|
||||
|
||||
runApp(const MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: OrderOverlay(),
|
||||
));
|
||||
}
|
||||
// 🔴 الاستماع لزر الرفض في الخلفية باستخدام storage 🔴
|
||||
TripOverlayPlugin.onTripRejected.listen((tripId) async {
|
||||
print("❌ [Background] Overlay Reject Clicked!");
|
||||
String? savedTrip = await storage.read(key: 'pending_driver_list');
|
||||
if (savedTrip != null) {
|
||||
await storage.delete(key: 'pending_driver_list');
|
||||
List<dynamic> driverList = jsonDecode(savedTrip);
|
||||
await _processRejectOrderBackground(driverList);
|
||||
}
|
||||
});
|
||||
|
||||
/// إغلاق الـ Overlay عند الحاجة
|
||||
Future<void> closeOverLay() async {
|
||||
final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.closeOverlay();
|
||||
final NotificationController notificationController =
|
||||
NotificationController();
|
||||
await notificationController.initNotifications();
|
||||
|
||||
String? title = message.data['title'] ?? message.notification?.title;
|
||||
String? body = message.data['body'] ?? message.notification?.body;
|
||||
String? myListString = message.data['DriverList'];
|
||||
String? category = message.data['category'] ?? message.data['type'];
|
||||
|
||||
if (title != null && body != null && myListString != null) {
|
||||
notificationController.showOrderNotification(
|
||||
title, body, 'ding.wav', myListString);
|
||||
|
||||
if (category == 'Order' || category == 'OrderSpeed' || category == null) {
|
||||
List<dynamic> myList = [];
|
||||
try {
|
||||
myList = jsonDecode(myListString);
|
||||
} catch (e) {}
|
||||
|
||||
String orderId = message.data['order_id']?.toString() ?? '';
|
||||
if (orderId.isEmpty && myList.isNotEmpty) orderId = myList[0].toString();
|
||||
|
||||
double fare = 0.0;
|
||||
double distance = 0.0;
|
||||
String passengerName = title;
|
||||
String pickup = 'موقع الانطلاق';
|
||||
String dropoff = 'موقع الوصول';
|
||||
|
||||
if (myList.isNotEmpty && myList.length > 29) {
|
||||
fare = double.tryParse(myList[26].toString()) ?? 0.0;
|
||||
distance = double.tryParse(myList[11].toString()) ?? 0.0;
|
||||
passengerName = myList[8].toString();
|
||||
pickup = myList[29].toString();
|
||||
dropoff = myList[30].toString();
|
||||
}
|
||||
|
||||
final tripData = TripData(
|
||||
tripId: orderId,
|
||||
passengerName: passengerName,
|
||||
pickupAddress: pickup,
|
||||
dropoffAddress: dropoff,
|
||||
distanceKm: distance,
|
||||
estimatedFare: fare,
|
||||
estimatedMinutes: 0,
|
||||
pickupLat: 0.0,
|
||||
pickupLng: 0.0,
|
||||
);
|
||||
|
||||
bool isAppInForeground = box.read(BoxName.isAppInForeground) ?? false;
|
||||
|
||||
if (!isAppInForeground) {
|
||||
await TripOverlayPlugin.showOverlay(tripData, autoCloseSeconds: 15);
|
||||
}
|
||||
|
||||
// 🔴 الكتابة على القرص مباشرة بدلاً من الرام لضمان انتقالها للتطبيق 🔴
|
||||
await storage.write(key: 'pending_driver_list', value: myListString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ============ Entrypoint: App ============
|
||||
@pragma('vm:entry-point')
|
||||
void notificationTapBackground(NotificationResponse notificationResponse) {
|
||||
NotificationController().handleNotificationResponse(notificationResponse);
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
await SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
|
||||
// ✅ الترتيب الصحيح: الإذونات → القنوات → الخدمات
|
||||
|
||||
// 1. طلب إذن الإشعارات أولاً (Android 13+)
|
||||
bool notificationPermissionGranted = await requestNotificationPermission();
|
||||
if (!notificationPermissionGranted) {
|
||||
print('⚠️ تحذير: لم يتم منح إذن الإشعارات - قد لا تعمل بعض الميزات');
|
||||
}
|
||||
|
||||
// 2. إنشاء جميع قنوات الإشعارات
|
||||
await requestNotificationPermission();
|
||||
await TripOverlayPlugin.initialize();
|
||||
await createAllNotificationChannels();
|
||||
|
||||
// 3. تهيئة الخدمة (بدون تشغيلها)
|
||||
await BackgroundServiceHelper.initialize();
|
||||
|
||||
// 4. سجل الهاندلر تبع رسائل الخلفية
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
|
||||
runApp(const MyApp());
|
||||
}, (error, stack) {
|
||||
final errorString = error.toString();
|
||||
|
||||
print("Caught Dart error: $error");
|
||||
print(stack);
|
||||
|
||||
final isIgnoredError = errorString.contains('PERMISSION_DENIED') ||
|
||||
errorString.contains('FormatException') ||
|
||||
errorString.contains('Null check operator used on a null value');
|
||||
|
||||
if (!isIgnoredError) {
|
||||
if (!errorString.contains('PERMISSION_DENIED') &&
|
||||
!errorString.contains('FormatException') &&
|
||||
!errorString.contains('Null check operator')) {
|
||||
CRUD.addError(error.toString(), stack.toString(), 'main');
|
||||
} else {
|
||||
print("Ignoring error and not sending to server: $errorString");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_MyAppState createState() => _MyAppState();
|
||||
}
|
||||
@@ -351,6 +279,26 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_initApp();
|
||||
// 🔥 التقاط رسالة القبول من الأندرويد بشكل مباشر 100% 🔥
|
||||
const platformAppControl = MethodChannel('com.intaleq_driver/app_control');
|
||||
platformAppControl.setMethodCallHandler((call) async {
|
||||
if (call.method == 'onOverlayTripAccepted') {
|
||||
print("✅ [Native Intent] تم التقاط زر القبول بنجاح من الأندرويد!");
|
||||
|
||||
// 🔴 القراءة من القرص مباشرة (يعمل بنجاح بين الـ Isolates) 🔴
|
||||
String? savedTrip = await storage.read(key: 'pending_driver_list');
|
||||
|
||||
if (savedTrip != null && savedTrip.isNotEmpty) {
|
||||
await storage.delete(key: 'pending_driver_list');
|
||||
List<dynamic> driverList = jsonDecode(savedTrip);
|
||||
|
||||
// تنفيذ دالة القبول فوراً
|
||||
await _processAcceptOrder(driverList);
|
||||
} else {
|
||||
print("⚠️ خطأ: لم يتم العثور على بيانات الطلب في الذاكرة!");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -361,34 +309,199 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
|
||||
Future<void> _initApp() async {
|
||||
try {
|
||||
if (!Get.isRegistered<NotificationController>()) {
|
||||
if (!Get.isRegistered<NotificationController>())
|
||||
Get.put(NotificationController());
|
||||
}
|
||||
|
||||
if (!Get.isRegistered<FirebaseMessagesController>()) {
|
||||
if (!Get.isRegistered<FirebaseMessagesController>())
|
||||
Get.put(FirebaseMessagesController()).getToken();
|
||||
}
|
||||
|
||||
await FirebaseMessaging.instance.requestPermission();
|
||||
|
||||
await NotificationController().initNotifications();
|
||||
|
||||
// Generate a random index to pick a message
|
||||
final random = Random();
|
||||
final randomMessage =
|
||||
syrianDriverMessages[random.nextInt(syrianDriverMessages.length)];
|
||||
await TripOverlayPlugin.initialize();
|
||||
bool isOverlayGranted = await TripOverlayPlugin.isPermissionGranted();
|
||||
if (!isOverlayGranted) {
|
||||
await TripOverlayPlugin.requestPermission();
|
||||
}
|
||||
|
||||
// Schedule the notification with the random message
|
||||
NotificationController().scheduleNotificationsForSevenDays(
|
||||
randomMessage.split(':')[0],
|
||||
randomMessage.split(':')[1],
|
||||
"tone1",
|
||||
);
|
||||
// 🔴 أعدنا الاستماع لأزرار النافذة إلى هنا (المكان الصحيح الذي لا يموت) 🔴
|
||||
_listenToOverlayEvents();
|
||||
} catch (e) {
|
||||
Log.print("Error during _initApp: $e");
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================================================================
|
||||
// 🔴 الاستماع الحي لأزرار النافذة 🔴
|
||||
// ==============================================================================
|
||||
void _listenToOverlayEvents() {
|
||||
TripOverlayPlugin.onTripAccepted.listen((result) async {
|
||||
Log.print('✅ [Main Isolate] Trip ACCEPTED via Overlay: ${result.tripId}');
|
||||
final savedTrip = box.read('pending_overlay_trip');
|
||||
|
||||
if (savedTrip != null) {
|
||||
List<dynamic> driverList = jsonDecode(savedTrip['driverList']);
|
||||
box.remove('pending_overlay_trip'); // مسح حتى لا يتكرر
|
||||
|
||||
// تنفيذ قبول الطلب فوراً!
|
||||
await _processAcceptOrder(driverList);
|
||||
}
|
||||
});
|
||||
|
||||
TripOverlayPlugin.onTripRejected.listen((tripId) async {
|
||||
Log.print('❌ [Main Isolate] Trip REJECTED via Overlay: $tripId');
|
||||
final savedTrip = box.read('pending_overlay_trip');
|
||||
|
||||
if (savedTrip != null) {
|
||||
List<dynamic> driverList = jsonDecode(savedTrip['driverList']);
|
||||
box.remove('pending_overlay_trip');
|
||||
|
||||
// تنفيذ الرفض الصامت
|
||||
await _processRejectOrderBackground(driverList);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==============================================================================
|
||||
// 🔴 التحكم بتوجيه التطبيق عند فتحه 🔴
|
||||
// ==============================================================================
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
box.write(BoxName.isAppInForeground, true);
|
||||
// تأخير بسيط جداً، لكي نعطي فرصة لـ _listenToOverlayEvents لتعمل أولاً إذا كان الفتح بسبب زر "قبول"
|
||||
Future.delayed(const Duration(milliseconds: 500), () {
|
||||
_checkPendingTrip();
|
||||
});
|
||||
} else if (state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.inactive) {
|
||||
box.write(BoxName.isAppInForeground, false);
|
||||
}
|
||||
}
|
||||
|
||||
void _checkPendingTrip() async {
|
||||
// 🔴 القراءة من القرص مباشرة 🔴
|
||||
String? savedTrip = await storage.read(key: 'pending_driver_list');
|
||||
|
||||
if (savedTrip != null && savedTrip.isNotEmpty) {
|
||||
await storage.delete(key: 'pending_driver_list');
|
||||
List<dynamic> driverList = jsonDecode(savedTrip);
|
||||
|
||||
print('👀 التطبيق فتح من الإشعار أو يدوياً، توجيه لصفحة الطلب...');
|
||||
Get.toNamed('/OrderRequestPage', arguments: {
|
||||
'myListString': savedTrip,
|
||||
'DriverList': driverList,
|
||||
'body': 'طلب جديد'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================================================================
|
||||
// 🟢 دوال معالجة قبول الطلب وتجهيز المتغيرات 🟢
|
||||
// ==============================================================================
|
||||
Future<void> _processAcceptOrder(List<dynamic> data) async {
|
||||
Get.dialog(
|
||||
WillPopScope(
|
||||
onWillPop: () async => false,
|
||||
child:
|
||||
const Center(child: CircularProgressIndicator(color: Colors.white)),
|
||||
),
|
||||
barrierDismissible: false,
|
||||
);
|
||||
|
||||
try {
|
||||
final driverId = box.read(BoxName.driverID);
|
||||
String orderId = _getVal(data, 16);
|
||||
String passengerToken = _getVal(data, 9);
|
||||
|
||||
print("🚀 Sending Accept Request for Order: $orderId");
|
||||
|
||||
var res = await CRUD().post(
|
||||
link: "${AppLink.ride}/rides/acceptRide.php",
|
||||
payload: {
|
||||
'id': orderId,
|
||||
'rideTimeStart': DateTime.now().toString(),
|
||||
'status': 'Apply',
|
||||
'passengerToken': passengerToken,
|
||||
'driver_id': driverId,
|
||||
},
|
||||
);
|
||||
|
||||
print("📥 Server Response: $res");
|
||||
|
||||
if (Get.isDialogOpen == true) Get.back();
|
||||
|
||||
bool isFailure = false;
|
||||
if (res is Map && res['status'] == 'failure')
|
||||
isFailure = true;
|
||||
else if (res == 'failure') isFailure = true;
|
||||
|
||||
if (isFailure) {
|
||||
Get.defaultDialog(
|
||||
title: "تنبيه",
|
||||
middleText: "عذراً، الطلب أخذه سائق آخر.",
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () => Get.back(),
|
||||
textConfirm: "حسناً",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Get.isRegistered<HomeCaptainController>()) {
|
||||
Get.put(HomeCaptainController());
|
||||
} else {
|
||||
Get.find<HomeCaptainController>().changeRideId();
|
||||
}
|
||||
|
||||
box.write(BoxName.statusDriverLocation, 'on');
|
||||
box.write(BoxName.rideStatus, 'Apply');
|
||||
|
||||
var rideArgs = _buildRideArgs(data);
|
||||
box.write(BoxName.rideArguments, rideArgs);
|
||||
|
||||
Get.offAll(() => PassengerLocationMapPage(), arguments: rideArgs);
|
||||
} catch (e) {
|
||||
if (Get.isDialogOpen == true) Get.back();
|
||||
print("❌ Error in accept process: $e");
|
||||
Get.snackbar("خطأ", "حدث خطأ غير متوقع");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _buildRideArgs(List<dynamic> data) {
|
||||
return {
|
||||
'passengerLocation': '${_getVal(data, 0)},${_getVal(data, 1)}',
|
||||
'passengerDestination': '${_getVal(data, 3)},${_getVal(data, 4)}',
|
||||
'Duration': _getVal(data, 4),
|
||||
'totalCost': _getVal(data, 26),
|
||||
'Distance': _getVal(data, 5),
|
||||
'name': _getVal(data, 8),
|
||||
'phone': _getVal(data, 10),
|
||||
'email': _getVal(data, 28),
|
||||
'WalletChecked': _getVal(data, 13),
|
||||
'tokenPassenger': _getVal(data, 9),
|
||||
'direction':
|
||||
'https://www.google.com/maps/dir/${_getVal(data, 0)}/${_getVal(data, 1)}/',
|
||||
'DurationToPassenger': _getVal(data, 15),
|
||||
'rideId': _getVal(data, 16),
|
||||
'passengerId': _getVal(data, 7),
|
||||
'driverId': _getVal(data, 18),
|
||||
'durationOfRideValue': _getVal(data, 19),
|
||||
'paymentAmount': _getVal(data, 2),
|
||||
'paymentMethod': _getVal(data, 13) == 'true' ? 'visa' : 'cash',
|
||||
'isHaveSteps': _getVal(data, 20),
|
||||
'step0': _getVal(data, 21),
|
||||
'step1': _getVal(data, 22),
|
||||
'step2': _getVal(data, 23),
|
||||
'step3': _getVal(data, 24),
|
||||
'step4': _getVal(data, 25),
|
||||
'passengerWalletBurc': _getVal(data, 26),
|
||||
'timeOfOrder': DateTime.now().toString(),
|
||||
'totalPassenger': _getVal(data, 2),
|
||||
'carType': _getVal(data, 31),
|
||||
'kazan': _getVal(data, 32),
|
||||
'startNameLocation': _getVal(data, 29),
|
||||
'endNameLocation': _getVal(data, 30),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final LocaleController localController = Get.put(LocaleController());
|
||||
@@ -404,9 +517,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
GetPage(name: '/', page: () => SplashScreen()),
|
||||
GetPage(name: '/OrderRequestPage', page: () => OrderRequestPage()),
|
||||
GetPage(
|
||||
name: '/passenger-location-map',
|
||||
page: () => PassengerLocationMapPage(),
|
||||
),
|
||||
name: '/passenger-location-map',
|
||||
page: () => PassengerLocationMapPage()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user