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/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}); List predictions = []; PlacePrediction? selectedPrediction; bool isPredictionLoading = false; GeocodeResponse? geocodeResponse; PlaceDetails? placeDetails; Location? mapCapturedLocation; 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); } }