import 'dart:async'; import 'package:flutter/foundation.dart' show ChangeNotifier; import 'package:flutter/material.dart'; import 'package:google_maps_flutter_platform_interface/src/types/camera.dart'; import 'package:hmg_patient_app_new/core/app_state.dart'; import 'package:hmg_patient_app_new/core/dependencies.dart'; import 'package:hmg_patient_app_new/features/location/GeocodeResponse.dart'; import 'package:hmg_patient_app_new/features/location/PlaceDetails.dart'; import 'package:hmg_patient_app_new/features/location/location_repo.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; import 'package:huawei_map/huawei_map.dart' as HMSCameraServices; import 'package:google_maps_flutter/google_maps_flutter.dart' as GMSMapServices; import 'PlacePrediction.dart'; class LocationViewModel extends ChangeNotifier { final LocationRepo locationRepo; final ErrorHandlerService errorHandlerService; LocationViewModel({required this.locationRepo, required this.errorHandlerService}){ placeValueInController(); } List predictions = []; PlacePrediction? selectedPrediction; bool isPredictionLoading = false; GeocodeResponse? geocodeResponse; PlaceDetails? placeDetails; Location? mapCapturedLocation; Completer? gmsController; Completer? hmsController; HMSCameraServices.CameraPosition getHMSLocation() { return HMSCameraServices.CameraPosition(target: HMSCameraServices.LatLng(getIt().userLat, getIt().userLong), zoom: 18); } GMSMapServices.CameraPosition getGMSLocation() { return GMSMapServices.CameraPosition(target: GMSMapServices.LatLng(getIt().userLat, getIt().userLong), zoom: 18); } void placeValueInController() async{ if (await getIt().isGMSAvailable) { gmsController = Completer(); } else { hmsController = Completer(); } } FutureOr getPlacesPrediction(String input) async { predictions = []; isPredictionLoading= true; final result = await locationRepo.getPlacePredictionsAsInput(input); result.fold( (failure) { errorHandlerService.handleError(failure: failure); }, (apiModel) { predictions = apiModel.data??[]; }, ); isPredictionLoading = false; notifyListeners(); } FutureOr getPlaceEncodedData(double? lat, double? lng) async { geocodeResponse = null; final result = await locationRepo.getGeoCodeFromLatLng(lat!, lng!); result.fold( (failure) { errorHandlerService.handleError(failure: failure); }, (apiModel) { print("Geocode Response: ${apiModel.data}"); geocodeResponse = apiModel.data; }, ); notifyListeners(); } FutureOr getPlaceDetails(String placeID) async { placeDetails = null; final result = await locationRepo.getPlaceDetailsOfSelectedPrediction(placeID); result.fold( (failure) { errorHandlerService.handleError(failure: failure); }, (apiModel) { placeDetails = apiModel.data; }, ); notifyListeners(); } handleGMSMapCameraMoved(GMSMapServices.CameraPosition value) { mapCapturedLocation = Location(lat: value.target.latitude, lng: value.target.longitude); } handleHMSMapCameraMoved(HMSCameraServices.CameraPosition value) { mapCapturedLocation = Location(lat: value.target.lat, lng: value.target.lng); } handleOnCameraIdle(){ if(mapCapturedLocation != null) { getPlaceEncodedData(mapCapturedLocation!.lat, mapCapturedLocation!.lng); } } void updateSearchQuery(String? value) { if(value == null || value.isEmpty){ predictions = []; return; } getPlacesPrediction(value); } void flushSearchPredictions() { predictions = []; mapCapturedLocation= null; placeDetails= null; geocodeResponse= null; selectedPrediction= null; notifyListeners(); } FutureOr selectPlacePrediction(PlacePrediction placePrediction) async{ selectedPrediction= placePrediction; await getPlaceDetails(placePrediction.placeID); } void moveToCurrentLocation() { moveController(Location(lat: getIt().userLat, lng: getIt().userLong)); } void moveController(Location location) { print("moving to location"); print("gmsController is null or not $gmsController"); if (getIt().isGMSAvailable) { gmsController?.future.then((controller) { controller.animateCamera( GMSMapServices.CameraUpdate.newCameraPosition( GMSMapServices.CameraPosition( target: GMSMapServices.LatLng(location.lat, location.lng), zoom: 18, ), ), ); }); } else { print("hmsController is null or not $hmsController"); hmsController?.future.then((controller) { controller.animateCamera( HMSCameraServices.CameraUpdate.newCameraPosition( HMSCameraServices.CameraPosition( target: HMSCameraServices.LatLng(location.lat, location.lng), zoom: 18, ), ), ); }); } } }