157 lines
5.9 KiB
Dart
157 lines
5.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:siro_rider/constant/links.dart';
|
|
import 'package:siro_rider/app_bindings.dart';
|
|
import 'package:siro_rider/controller/functions/crud.dart';
|
|
import 'package:siro_rider/views/home/HomePage/contact_us.dart';
|
|
import 'package:siro_rider/views/home/HomePage/share_app_page.dart';
|
|
import 'package:siro_rider/views/home/my_wallet/passenger_wallet.dart';
|
|
import 'package:siro_rider/views/home/profile/passenger_profile_page.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.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/info.dart';
|
|
import 'constant/box_name.dart';
|
|
import 'controller/home/ios_live_activity_service.dart';
|
|
import 'controller/local/local_controller.dart';
|
|
import 'controller/local/translations.dart';
|
|
import 'controller/themes/themes.dart';
|
|
import 'firebase_options.dart';
|
|
import 'models/db_sql.dart';
|
|
import 'print.dart';
|
|
import 'splash_screen_page.dart';
|
|
import 'services/geofencing_service.dart';
|
|
|
|
// -- Global instances for easy access --
|
|
final box = GetStorage();
|
|
final storage = FlutterSecureStorage();
|
|
DbSql sql = DbSql.instance;
|
|
|
|
// Firebase background message handler must be a top-level function.
|
|
@pragma('vm:entry-point')
|
|
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
|
Log.print("Handling a background message: ${message.messageId}");
|
|
|
|
if (message.data.isNotEmpty) {
|
|
try {
|
|
await GetStorage.init();
|
|
final storage = GetStorage();
|
|
final passengerId = storage.read('passenger_id') ?? '';
|
|
if (passengerId.toString().isEmpty) return;
|
|
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled) return;
|
|
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) return;
|
|
|
|
final position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
|
|
|
|
final url = Uri.parse('${AppLink.server}/api/location/sync_location.php');
|
|
await http.post(url, body: {
|
|
'passenger_id': passengerId.toString(),
|
|
'lat': position.latitude.toString(),
|
|
'lng': position.longitude.toString(),
|
|
'source': 'silent_push',
|
|
});
|
|
|
|
// Update geofences on device silently
|
|
await SiroGeofencingService.syncZonesWithServer(position.latitude, position.longitude);
|
|
} catch (e) {
|
|
Log.print("Silent push location update failed: $e");
|
|
}
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
// 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();
|
|
await GetStorage.init();
|
|
// Purge any previously stored CVV (PCI-DSS compliance)
|
|
await storage.delete(key: BoxName.cvvCode);
|
|
WakelockPlus.enable();
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
await Firebase.initializeApp(
|
|
options: DefaultFirebaseOptions.currentPlatform);
|
|
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
|
}
|
|
// ✅ التعديل هنا: تهيئة خدمة الـ Live Activity للآيفون
|
|
IosLiveActivityService.init();
|
|
|
|
// Initialize Geofencing Service
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
await SiroGeofencingService.initAndStart();
|
|
}
|
|
|
|
// 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());
|
|
}, (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_zone_guard');
|
|
}
|
|
});
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Get.find() is used here because LocaleController is already put in AppBindings.
|
|
final LocaleController localController = Get.find<LocaleController>();
|
|
|
|
return GetMaterialApp(
|
|
title: AppInformation.appName,
|
|
translations: MyTranslation(),
|
|
debugShowCheckedModeBanner: false,
|
|
locale: localController.language,
|
|
theme: localController.language?.languageCode.startsWith('ar') == true
|
|
? lightThemeArabic
|
|
: lightThemeEnglish,
|
|
darkTheme: localController.language?.languageCode.startsWith('ar') == true
|
|
? darkThemeArabic
|
|
: darkThemeEnglish,
|
|
themeMode: localController.themeMode,
|
|
|
|
// --- [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: () => const SplashScreen()),
|
|
// These routes are used by QuickActions and other navigation events.
|
|
GetPage(name: '/shareApp', page: () => ShareAppPage()),
|
|
GetPage(name: '/wallet', page: () => const PassengerWallet()),
|
|
GetPage(name: '/profile', page: () => PassengerProfilePage()),
|
|
GetPage(name: '/contactSupport', page: () => ContactUsPage()),
|
|
],
|
|
);
|
|
}
|
|
}
|