Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
7.9 kB
1
Indexable
Never
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/AppUser.dart';
import 'package:iketfaa_delivery/App/Common/Modules/Dashboard/controller/dashboard_controller.dart';
import 'package:iketfaa_delivery/App/Common/Services/AuthenticationService/Core/manager.dart';
import 'package:iketfaa_delivery/App/Common/Utilities/Constants/Strings/Backend.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Add.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Delete.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Read.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Storage.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Update.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/ActiveOrder.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/AmoutCalculatorFactor.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/Cart.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/OrderAddress.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/PersonalAddress.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryCustomerCurrentOrder/view/delivery_customer_current_order_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryCustomerMyOrders/view/delivery_customer_completed_orders_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryDriverActiveOrder/controller/delivery_driver_active_order_controller.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryDriverActiveOrder/view/delivery_driver_active_order_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryDriverCompletedOrders/view/delivery_driver_completed_orders_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryHome/view/delivery_home_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryHub/view/delivery_required_login_page..dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryHub/view/dummy_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryUser/DeliveryCustomer/Profile/view/user_profile_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DeliveryUser/DeliveryDriver/DeliveryDriverProfile/view/driver_profile_view.dart';
import 'package:iketfaa_delivery/App/Delivery/Modules/DriverNewOrders/view/delivery_driver_new_order.dart';

class DeliveryHubController extends GetxController {
  final AuthenticationManager authManager = Get.find();
  final RxList<Widget> customerPages = <Widget>[].obs;
  final RxList<Widget> driverPages = <Widget>[].obs;
  final RxList<Widget> guestPages = <Widget>[].obs;
  final DashboardController dashboardController = Get.find();
  final AddToDatabase addToDatabase = new AddToDatabase();
  final ReadDatabase readDatabase = new ReadDatabase();
  final DeleteDatabase deleteDatabase = new DeleteDatabase();
  final StorageFiles storageFiles = new StorageFiles();
  final UpdateDatabase updateDatabase = new UpdateDatabase();
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
  var data = Get.arguments;
  final GetStorage globalStorage = GetStorage();
  RxInt currentPos = 0.obs;
  StreamSubscription<Position>? _positionStreamSubscription;
  final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
  ActiveOrder activeOrder = ActiveOrder();
  Rx<PersonalAddress> currentPersonalAddress = PersonalAddress().obs;
  Rx<Cart> cart = Cart().obs;
  AmonutCalculatorFactor amoutCalculatorFactor = AmonutCalculatorFactor();
  RxInt cartLength = 0.obs;
  int mainOrderIndex = 0;
  RxInt currentPage = 0.obs;
  RxBool isChecked = false.obs;
  LatLng? pos;

  void clearCart() {
    cart.value.orders!.clear();
    cartLength.value = 0;
    cart.refresh();
  }

  void removeItem(int index) {
    cart.value.orders!.removeAt(index);
    cartLength.value -= 1;
    cart.refresh();
  }

  @override
  Future<void> onInit() async {
    cart.value.orders = [];
    if (authManager.isLogged.value) {
      customerPages.value = [
        DeliveryHomeView(),
        DeliveryCustomerCurrentOrderView(),
        DummyView(),
        DeliveryCustomerCompletedOrdersView(),
        UserProfileView()
      ];

      driverPages.value = [
        DeliveryDriverActiveOrderView(),
        DeliveryDrierNewOrderView(),
        DummyView(),
        DeliveryDriverCompletedOrdersView(),
        DriverProfileView()
      ];
    } else
      guestPages.value = [
        DeliveryHomeView(),
        DeliveryRequiredLoginPage(),
        DeliveryRequiredLoginPage(),
        DeliveryRequiredLoginPage(),
        DeliveryRequiredLoginPage()
      ];

    super.onInit();
  }

  @override
  Future<void> onReady() async {
    if (dashboardController.deliveryCart.value.orderID != null) {
      cart.value = dashboardController.deliveryCart.value;
    }
    _toggleListening();

    super.onReady();
  }

  void openDrawer() {
    scaffoldKey.currentState?.openDrawer();
  }

  void closeDrawer() {
    scaffoldKey.currentState?.openEndDrawer();
  }

  void _toggleListening() {
    if (_positionStreamSubscription == null) {
      final positionStream = _geolocatorPlatform.getPositionStream(
          locationSettings: LocationSettings(
              timeLimit: Duration(seconds: 5),
              accuracy: LocationAccuracy.bestForNavigation));
      _positionStreamSubscription = positionStream.handleError((error) {
        _positionStreamSubscription?.cancel();
        _positionStreamSubscription = null;
      }).listen((position) {
        if (authManager.appUser.value.locationData == null) {
          final DeliveryDriverActiveOrderController
              deliveryDriverActiveOrderController =
              Get.put(DeliveryDriverActiveOrderController());

          if (deliveryDriverActiveOrderController.driverActiveOrder.orderID !=
              null) {
            OrderAddress address = OrderAddress(
              geoPoint: GeoPoint(position.latitude, position.longitude),
            );

            Map<String, dynamic> values = {
              ADDRESS_FIELD: address.toMap(),
            };
            updateDatabase.updateActiveOrderOfferLocation(
                deliveryDriverActiveOrderController.driverActiveOrder.offerID!,
                values);
          }
        } else if (authManager
                .appUser.value.locationData!.localPosition!.latitude !=
            position.latitude) {
          final DeliveryDriverActiveOrderController
              deliveryDriverActiveOrderController =
              Get.put(DeliveryDriverActiveOrderController());

          if (deliveryDriverActiveOrderController.driverActiveOrder.orderID !=
              null) {
            OrderAddress address = OrderAddress(
              geoPoint: GeoPoint(position.latitude, position.longitude),
            );

            Map<String, dynamic> values = {
              ADDRESS_FIELD: address.toMap(),
            };
            updateDatabase.updateActiveOrderOfferLocation(
                deliveryDriverActiveOrderController.driverActiveOrder.offerID!,
                values);
          }
        } else {}
      });

      _positionStreamSubscription?.pause();
    }

    if (_positionStreamSubscription == null) {
      return;
    }

    String statusDisplayValue;
    if (_positionStreamSubscription!.isPaused) {
      _positionStreamSubscription!.resume();
      statusDisplayValue = 'resumed';
    } else {
      _positionStreamSubscription!.pause();
      statusDisplayValue = 'paused';
    }

    print(statusDisplayValue);
  }
}