vfs

mail@pastecode.io avatar
unknown
dart
2 years ago
6.4 kB
1
Indexable
Never
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.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/Models/Main/Banks.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/CanceledOrderReasons.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/CarClasses.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/Cars.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/PaymentMethods.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/Regions.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/TimeEstimatesDeliveryDriver.dart';
import 'package:iketfaa_delivery/App/Common/Services/AuthenticationService/Core/manager.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Read.dart';
import 'package:iketfaa_delivery/App/Delivery/Backend/Update.dart';
import 'package:iketfaa_delivery/App/Delivery/Models/Main/Cart.dart';

class DashboardController extends GetxController {
  final GetStorage globalStorage = GetStorage();
  final ReadDatabase readDatabase = new ReadDatabase();
  final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
  StreamSubscription<Position>? _positionStreamSubscription;
  AuthenticationManager authManager = Get.find();
  RxInt currentTab = 1.obs;
  RxInt currentPos = 0.obs;
  final UpdateDatabase updateDatabase = UpdateDatabase();
  List<String> testImgData = [
    'assets/img/corr1.png',
    'assets/img/corr2.png',
  ];
  BitmapDescriptor? userLocationMarker,
      driverLocationMarker,
      placeLocationMarker;
  Rx<Cars> cars = Cars().obs;
  Rx<Banks> banks = Banks().obs;
  Rx<CarClasses> carClasses = CarClasses().obs;
  Rx<Regions> regions = Regions().obs;
  Rx<PaymentMethods> paymentMethods = PaymentMethods().obs;
  Rx<TimeEstimatesDeliveryDriver> timeEstimatesDeliveryDriver =
      TimeEstimatesDeliveryDriver().obs;
  Rx<Cart> deliveryCart = Cart().obs;
  Rx<OrderCanceledReasons> orderCanceledReasons = OrderCanceledReasons().obs;

  @override
  Future<void> onInit() async {
    super.onInit();
  }

  @override
  Future<void> onReady() async {
    super.onReady();

    await getAllImportantData();
    if (Get.arguments == null || Get.arguments[0] == 'login') {
      authManager.commonTools.showLoading();

      if (authManager.isLogged.value) {
        bool result = await readDatabase.getDeliveryCurrentUserDataByID();
        if (result) {
          Get.back();
        } else {
          Get.back();
          Get.back();
        }
      } else {
        Get.back();
      }
    }

    _toggleListening();
  }

  Future<void> getAllImportantData() async {
    driverLocationMarker = await authManager.deliveryTools
        .getBitmapDescriptorFromAssetBytes(
            "assets/img/sports_motorsports_black_24dp.png", 100);

    userLocationMarker = await authManager.deliveryTools
        .getBitmapDescriptorFromAssetBytes(
            "assets/img/emoji_people_black_24dp.png", 100);

    placeLocationMarker = await authManager.deliveryTools
        .getBitmapDescriptorFromAssetBytes("assets/img/Group 973.png", 100);
    String jsonString = await rootBundle.loadString('assets/json/cars.json');
    Map<String, dynamic> jsonMap = json.decode(jsonString);
    cars.value = Cars.fromJson(jsonMap);

    jsonString = await rootBundle.loadString('assets/json/car_classes.json');
    jsonMap = json.decode(jsonString);
    carClasses.value = CarClasses.fromJson(jsonMap);

    jsonString = await rootBundle.loadString('assets/json/regions.json');
    jsonMap = json.decode(jsonString);
    regions.value = Regions.fromJson(jsonMap);

    jsonString = await rootBundle.loadString('assets/json/banks.json');
    jsonMap = json.decode(jsonString);
    banks.value = Banks.fromJson(jsonMap);

    jsonString =
        await rootBundle.loadString('assets/json/payment_methods.json');
    jsonMap = json.decode(jsonString);
    paymentMethods.value = PaymentMethods.fromJson(jsonMap);

    jsonString = await rootBundle
        .loadString('assets/json/time_estimate_delivery_driver.json');
    jsonMap = json.decode(jsonString);
    timeEstimatesDeliveryDriver.value =
        TimeEstimatesDeliveryDriver.fromJson(jsonMap);

    jsonString =
        await rootBundle.loadString('assets/json/canceled_orders_reasons.json');
    jsonMap = json.decode(jsonString);
    orderCanceledReasons.value = OrderCanceledReasons.fromJson(jsonMap);

    var deliveryCartFromLocal = globalStorage.read('deliveryCartFromLocal');

    if (deliveryCartFromLocal != null) {
      deliveryCart.value =
          Cart.fromJson(json.decode(json.encode(deliveryCartFromLocal)));
    }
  }

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

      _positionStreamSubscription?.pause();
    }

    if (_positionStreamSubscription == null) {
      return;
    }

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

    print(statusDisplayValue);
  }

  @override
  Future<void> onClose() async {
    if (_positionStreamSubscription != null) {
      _positionStreamSubscription!.cancel();
      _positionStreamSubscription = null;
    }
    super.onClose();
  }
}