import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:driver/constant/collection_name.dart'; import 'package:driver/constant/constant.dart'; import 'package:driver/constant/show_toast_dialog.dart'; import 'package:driver/models/order_model.dart'; import 'package:driver/models/user_model.dart'; import 'package:driver/utils/fire_store_utils.dart'; import 'package:driver/utils/preferences.dart'; import 'package:get/get.dart'; import 'package:location/location.dart'; import '../themes/theme_controller.dart'; class DashBoardController extends GetxController { RxInt drawerIndex = 0.obs; @override void onInit() { // TODO: implement onInit getUser(); updateDriverOrder(); getTheme(); super.onInit(); } Rx userModel = UserModel().obs; DateTime? currentBackPressTime; RxBool canPopNow = false.obs; Future getUser() async { await updateCurrentLocation(); FireStoreUtils.fireStore.collection(CollectionName.users).doc(FireStoreUtils.getCurrentUid()).snapshots().listen( (event) { if (event.exists) { userModel.value = UserModel.fromJson(event.data()!); Constant.userModel = UserModel.fromJson(event.data()!); } }, ); } RxString isDarkMode = "Light".obs; RxBool isDarkModeSwitch = false.obs; void getTheme() { bool isDark = Preferences.getBoolean(Preferences.themKey); isDarkMode.value = isDark ? "Dark" : "Light"; isDarkModeSwitch.value = isDark; } void toggleDarkMode(bool value) { isDarkModeSwitch.value = value; isDarkMode.value = value ? "Dark" : "Light"; Preferences.setBoolean(Preferences.themKey, value); // Update ThemeController for instant app theme change if (Get.isRegistered()) { final themeController = Get.find(); themeController.isDark.value = value; } } Future updateDriverOrder() async { Timestamp startTimestamp = Timestamp.now(); DateTime currentDate = startTimestamp.toDate(); currentDate = currentDate.subtract(const Duration(hours: 3)); startTimestamp = Timestamp.fromDate(currentDate); List orders = []; await FirebaseFirestore.instance .collection(CollectionName.vendorOrders) .where('status', whereIn: [Constant.orderAccepted, Constant.orderRejected]) .where('createdAt', isGreaterThan: startTimestamp) .get() .then((value) async { await Future.forEach(value.docs, (QueryDocumentSnapshot> element) { try { orders.add(OrderModel.fromJson(element.data())); } catch (e, s) { print('watchOrdersStatus parse error ${element.id}$e $s'); } }); }); orders.forEach((element) async { OrderModel orderModel = element; orderModel.triggerDelivery = Timestamp.now(); await FireStoreUtils.setOrder(orderModel); }); } Location location = Location(); Future updateCurrentLocation() async { try { PermissionStatus permissionStatus = await location.hasPermission(); if (permissionStatus == PermissionStatus.granted) { location.enableBackgroundMode(enable: true); location.changeSettings(accuracy: LocationAccuracy.high, distanceFilter: double.parse(Constant.driverLocationUpdate)); location.onLocationChanged.listen((locationData) async { Constant.locationDataFinal = locationData; await FireStoreUtils.getUserProfile(FireStoreUtils.getCurrentUid()).then((value) async { if (value != null) { userModel.value = value; if (userModel.value.isActive == true) { userModel.value.location = UserLocation(latitude: locationData.latitude ?? 0.0, longitude: locationData.longitude ?? 0.0); userModel.value.rotation = locationData.heading; await FireStoreUtils.updateUser(userModel.value); } } }); }); } else { location.requestPermission().then((permissionStatus) { if (permissionStatus == PermissionStatus.granted) { location.enableBackgroundMode(enable: true); location.changeSettings(accuracy: LocationAccuracy.high, distanceFilter: double.parse(Constant.driverLocationUpdate)); location.onLocationChanged.listen((locationData) async { Constant.locationDataFinal = locationData; await FireStoreUtils.getUserProfile(FireStoreUtils.getCurrentUid()).then((value) async { if (value != null) { userModel.value = value; if (userModel.value.isActive == true) { userModel.value.location = UserLocation(latitude: locationData.latitude ?? 0.0, longitude: locationData.longitude ?? 0.0); userModel.value.rotation = locationData.heading; await FireStoreUtils.updateUser(userModel.value); } ShowToastDialog.closeLoader(); } }); }); } else { ShowToastDialog.closeLoader(); } }); } } catch (e) { print(e); } } }