25-6-23/1
This commit is contained in:
241
lib/main.dart
241
lib/main.dart
@@ -1,9 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:sefer_driver/constant/box_name.dart';
|
||||
import 'package:sefer_driver/controller/payment/paymob/paymob_response.dart';
|
||||
import 'package:sefer_driver/views/home/Captin/orderCaptin/order_request_page.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
@@ -15,103 +12,102 @@ 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:permission_handler/permission_handler.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/location_controller.dart';
|
||||
import 'controller/functions/secure_storage.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 'print.dart';
|
||||
import 'splash_screen_page.dart';
|
||||
import 'views/home/Captin/driver_map_page.dart';
|
||||
import 'views/home/Captin/orderCaptin/order_over_lay.dart';
|
||||
|
||||
//--- Global Variables ---
|
||||
final box = GetStorage();
|
||||
const storage = FlutterSecureStorage();
|
||||
final PaymobPayment paymobPayment = PaymobPayment();
|
||||
final PaymobPaymentWallet paymobPaymentWallet = PaymobPaymentWallet();
|
||||
DbSql sql = DbSql.instance;
|
||||
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
const platform = MethodChannel('com.sefer_driver/app_control');
|
||||
|
||||
//--- Entry Points for Background/Terminated States ---
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp();
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
await GetStorage.init();
|
||||
|
||||
if (!await FlutterOverlayWindow.isPermissionGranted()) {
|
||||
// بإمكانك تجاهل الطلب في الخلفية والاكتفاء بالتنبيه
|
||||
Log.print("Overlay permission not granted; showing only notification.");
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
if (message.notification != null && message.notification!.title != null) {
|
||||
Log.print('message.notification!.title: ${message.notification!.title}');
|
||||
|
||||
if (message.notification?.title == 'طلب' ||
|
||||
if (message.notification?.title == 'Order' ||
|
||||
message.notification?.title == 'OrderSpeed') {
|
||||
var myListString = message.data['DriverList'] ?? '[]';
|
||||
Log.print('myListString: $myListString');
|
||||
|
||||
// Decode the JSON string to a list
|
||||
List<dynamic> myList;
|
||||
try {
|
||||
myList = jsonDecode(myListString) as List<dynamic>;
|
||||
Log.print('myList: ${myList}');
|
||||
} catch (e) {
|
||||
Log.print('Error decoding JSON: $e');
|
||||
myList = [];
|
||||
}
|
||||
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
|
||||
bool isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.closeOverlay();
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
} else {
|
||||
await FlutterOverlayWindow.showOverlay(
|
||||
enableDrag: true,
|
||||
flag: OverlayFlag.focusPointer,
|
||||
positionGravity: PositionGravity.auto,
|
||||
height: 1400,
|
||||
width: WindowSize.matchParent,
|
||||
startPosition: const OverlayPosition(0, -30),
|
||||
);
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
}
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
await FlutterOverlayWindow.showOverlay(
|
||||
enableDrag: true,
|
||||
flag: OverlayFlag.focusPointer,
|
||||
// visibility: NotificationVisibility.visibilityPublic,
|
||||
positionGravity: PositionGravity.auto,
|
||||
height: 1300,
|
||||
width: WindowSize.matchParent,
|
||||
startPosition: const OverlayPosition(0, -40),
|
||||
);
|
||||
|
||||
// It's better to manage notifications in one place if possible
|
||||
// but this is fine if it works for you.
|
||||
NotificationController().showNotification(
|
||||
message.notification!.title.toString(),
|
||||
message.notification!.body.toString(),
|
||||
'order',
|
||||
myListString,
|
||||
);
|
||||
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
} else {
|
||||
// Handle other types of notifications
|
||||
FirebaseMessagesController().fireBaseTitles(message);
|
||||
}
|
||||
} else {
|
||||
FirebaseMessagesController().fireBaseTitles(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void notificationTapBackground(NotificationResponse notificationResponse) {
|
||||
// handle background notification taps here
|
||||
print('Notification tapped in background!');
|
||||
Log.print('Notification tapped in background!');
|
||||
NotificationController().handleNotificationResponse(notificationResponse);
|
||||
// You can add your logic here to handle the notification tap
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void overlayMain() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await GetStorage.init();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
runApp(const MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: OrderOverlay(),
|
||||
@@ -119,12 +115,12 @@ void overlayMain() async {
|
||||
}
|
||||
|
||||
Future<void> closeOverLay() async {
|
||||
// FlutterOverlayWindow.closeOverlay();
|
||||
bool isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.closeOverlay();
|
||||
}
|
||||
}
|
||||
//--- Main Application ---
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -136,21 +132,29 @@ void main() async {
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
|
||||
runApp(MyApp());
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_MyAppState createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_initApp();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _initApp() async {
|
||||
try {
|
||||
await Firebase.initializeApp(
|
||||
@@ -159,62 +163,23 @@ class _MyAppState extends State<MyApp> {
|
||||
await initializer.initializeApp();
|
||||
await EncryptionHelper.initialize();
|
||||
|
||||
if (Platform.isAndroid || Platform.isIOS) {
|
||||
Get.put(
|
||||
NotificationController()); // Initialize NotificationController here
|
||||
await FirebaseMessaging.instance.requestPermission();
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
await NotificationController().initNotifications();
|
||||
Get.put(NotificationController());
|
||||
Get.put(FirebaseMessagesController());
|
||||
|
||||
// Generate a random index to pick a message
|
||||
final random = Random();
|
||||
final randomMessage =
|
||||
driverMessages[random.nextInt(driverMessages.length)];
|
||||
// Schedule the notification with the random message
|
||||
NotificationController().scheduleNotificationsForSevenDays(
|
||||
randomMessage.split(':')[0],
|
||||
randomMessage.split(':')[1],
|
||||
"ding",
|
||||
);
|
||||
await FirebaseMessaging.instance.requestPermission();
|
||||
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
await NotificationController().initNotifications();
|
||||
|
||||
await Future.wait([
|
||||
FirebaseMessagesController().getNotificationSettings(),
|
||||
FirebaseMessagesController().getToken(),
|
||||
]);
|
||||
// You can add your other initializations here
|
||||
// For example:
|
||||
|
||||
// PermissionStatus status1 = await Permission.location.status;
|
||||
// if (status1.isGranted) {
|
||||
// await LocationController().startLocationUpdates();
|
||||
// }
|
||||
}
|
||||
|
||||
String? key = (await storage.read(key: BoxName.payMobApikey));
|
||||
if (key != null) {
|
||||
String? apiKey = (key);
|
||||
if (apiKey != null) {
|
||||
PaymobPayment.instance.initialize(
|
||||
// Use .instance
|
||||
apiKey: apiKey,
|
||||
integrationID: int.parse(AK.integrationIdPayMob),
|
||||
userTokenExpiration: 200,
|
||||
iFrameID: 837992,
|
||||
);
|
||||
|
||||
PaymobPaymentWallet.instance.initialize(
|
||||
// Use .instance
|
||||
apiKey: apiKey,
|
||||
integrationID: int.parse(AK.integrationIdPayMobWallet),
|
||||
userTokenExpiration: 200,
|
||||
iFrameID: 837992,
|
||||
);
|
||||
}
|
||||
}
|
||||
// Your other startup logic...
|
||||
} catch (e) {
|
||||
print("Error initializing app: $e");
|
||||
// Handle initialization errors appropriately. Maybe show an error message to the user.
|
||||
Log.print("Error during _initApp: $e");
|
||||
}
|
||||
}
|
||||
|
||||
//--- Build Method ---
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
LocaleController localController = Get.put(LocaleController());
|
||||
@@ -231,105 +196,11 @@ class _MyAppState extends State<MyApp> {
|
||||
GetPage(
|
||||
name: '/order-page',
|
||||
page: () => OrderRequestPage(),
|
||||
arguments: box.read(BoxName.rideArguments),
|
||||
),
|
||||
GetPage(
|
||||
name: '/passenger-location-map',
|
||||
page: () => PassengerLocationMapPage()),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// void main() async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// await WakelockPlus.enable();
|
||||
//
|
||||
// await GetStorage.init();
|
||||
//
|
||||
// final AppInitializer initializer = AppInitializer();
|
||||
//
|
||||
// await initializer.initializeApp();
|
||||
// await Future.delayed(Duration.zero);
|
||||
// await EncryptionHelper.initialize();
|
||||
//
|
||||
// Stripe.publishableKey = AK.publishableKeyStripe;
|
||||
//
|
||||
// PermissionStatus status1 = await Permission.location.status;
|
||||
// // if (status1.isGranted) {
|
||||
// await LocationController().startLocationUpdates();
|
||||
// // }
|
||||
//
|
||||
// if (Platform.isAndroid || Platform.isIOS) {
|
||||
// NotificationController notificationController =
|
||||
// Get.put(NotificationController());
|
||||
// await Firebase.initializeApp(
|
||||
// options: DefaultFirebaseOptions.currentPlatform,
|
||||
// );
|
||||
// await FirebaseMessagesController().requestFirebaseMessagingPermission();
|
||||
//
|
||||
// FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
||||
//
|
||||
// await notificationController.initNotifications();
|
||||
//
|
||||
// // Generate a random index to pick a message
|
||||
// final random = Random();
|
||||
// final randomMessage = driverMessages[random.nextInt(driverMessages.length)];
|
||||
//
|
||||
// // Schedule the notification with the random message
|
||||
// notificationController.scheduleNotificationsForSevenDays(
|
||||
// randomMessage.split(':')[0],
|
||||
// randomMessage.split(':')[1],
|
||||
// "ding",
|
||||
// );
|
||||
//
|
||||
// await Future.wait([
|
||||
// FirebaseMessagesController().getNotificationSettings(),
|
||||
// FirebaseMessagesController().getToken(),
|
||||
// ]);
|
||||
// // await FacebookAuth.instance.init();
|
||||
// SystemChrome.setPreferredOrientations([
|
||||
// DeviceOrientation.portraitUp,
|
||||
// DeviceOrientation.portraitDown,
|
||||
// ]);
|
||||
// }
|
||||
//
|
||||
// String? key = (await storage.read(key: BoxName.payMobApikey));
|
||||
//
|
||||
// String? apiKey = EncryptionHelper.instance.decryptData(key!);
|
||||
// PaymobPayment.instance.initialize(
|
||||
// apiKey: apiKey,
|
||||
// integrationID: int.parse(AK.integrationIdPayMob),
|
||||
// userTokenExpiration: 200,
|
||||
// iFrameID: 837992,
|
||||
// );
|
||||
//
|
||||
// PaymobPaymentWallet.instance.initialize(
|
||||
// apiKey: apiKey,
|
||||
// integrationID: int.parse(AK.integrationIdPayMobWallet),
|
||||
// userTokenExpiration: 200,
|
||||
// iFrameID: 837992,
|
||||
// );
|
||||
// runApp(MyApp());
|
||||
// }
|
||||
//
|
||||
// class MyApp extends StatelessWidget {
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// LocaleController localController = Get.put(LocaleController());
|
||||
// return GetMaterialApp(
|
||||
// navigatorKey: navigatorKey,
|
||||
// title: AppInformation.appName,
|
||||
// translations: MyTranslation(),
|
||||
// debugShowCheckedModeBanner: false,
|
||||
// locale: localController.language,
|
||||
// theme: localController.appTheme,
|
||||
// initialRoute: '/',
|
||||
// getPages: [
|
||||
// GetPage(name: '/', page: () => SplashScreen()),
|
||||
// GetPage(
|
||||
// name: '/order-page',
|
||||
// page: () => OrderRequestPage(),
|
||||
// arguments: box.read(BoxName.rideArguments),
|
||||
// ),
|
||||
// ],
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user