diff --git a/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.dart b/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.dart index a43da172..13c31aba 100644 --- a/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.dart +++ b/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.dart @@ -1,6 +1,6 @@ import 'package:data/api/auth/auth_models.dart'; import 'package:data/log/logger.dart'; -import 'package:data/service/location_manager.dart'; +import 'package:data/service/location_service.dart'; import 'package:data/service/permission_service.dart'; import 'package:data/service/place_service.dart'; import 'package:data/service/space_service.dart'; @@ -17,42 +17,52 @@ final locateOnMapViewStateProvider = StateNotifierProvider.autoDispose( (ref) { return LocateOnMapVieNotifier( - ref.read(locationManagerProvider), ref.read(permissionServiceProvider), ref.read(currentUserPod), ref.read(placeServiceProvider), ref.read(spaceServiceProvider), + ref.read(locationServiceProvider), ); }); class LocateOnMapVieNotifier extends StateNotifier { - final LocationManager locationManager; final PermissionService permissionService; final ApiUser? _currentUser; final PlaceService placesService; final SpaceService spaceService; + final LocationService locationService; LocateOnMapVieNotifier( - this.locationManager, this.permissionService, this._currentUser, this.placesService, this.spaceService, + this.locationService, ) : super(const LocateOnMapState()) { getCurrentUserLocation(); } void getCurrentUserLocation() async { - final isEnabled = await permissionService.isLocationPermissionGranted(); - if (isEnabled) { - state = state.copyWith(loading: true); - final position = await locationManager.getLastLocation(); - final latLng = LatLng(position!.latitude, position.longitude); - state = state.copyWith( - currentLatLng: latLng, - centerPosition: CameraPosition(target: latLng, zoom: defaultCameraZoom), - loading: false, - ); + try { + final isEnabled = await permissionService.isLocationPermissionGranted(); + if (isEnabled && _currentUser != null) { + state = state.copyWith(loading: true); + final location = + await locationService.getCurrentLocation(_currentUser.id); + if (location != null) { + final latLng = + LatLng(location.latitude, location.longitude); + state = state.copyWith( + currentLatLng: latLng, + centerPosition: + CameraPosition(target: latLng, zoom: defaultCameraZoom), + loading: false, + ); + } + } + } catch (error, stack) { + logger.e('LocateONMapViewNotifier: Error while fetch user last location', + error: error, stackTrace: stack); } } @@ -70,18 +80,18 @@ class LocateOnMapVieNotifier extends StateNotifier { void onTapAddPlaceBtn(String spaceId, String placeName) async { try { + if (_currentUser != null && state.cameraLatLng != null) { state = state.copyWith(addingPlace: true); final members = await spaceService.getMemberBySpaceId(spaceId); final memberIds = members.map((member) => member.user_id).toList(); - - await placesService.addPlace( - spaceId, - placeName, - state.cameraLatLng!.latitude, - state.cameraLatLng!.longitude, - _currentUser!.id, - memberIds - ); + await placesService.addPlace( + spaceId, + placeName, + state.cameraLatLng!.latitude, + state.cameraLatLng!.longitude, + _currentUser.id, + memberIds); + } state = state.copyWith(popToPlaceList: DateTime.now(), addingPlace: false); } catch (error, stack) { diff --git a/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.freezed.dart b/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.freezed.dart index 3c7d853d..31e2d4fd 100644 --- a/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.freezed.dart +++ b/app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.freezed.dart @@ -232,13 +232,13 @@ class _$LocateOnMapStateImpl implements _LocateOnMapState { .equals(other.addingPlace, addingPlace) && const DeepCollectionEquality() .equals(other.gettingAddress, gettingAddress) && - const DeepCollectionEquality() - .equals(other.currentLatLng, currentLatLng) && - const DeepCollectionEquality() - .equals(other.cameraLatLng, cameraLatLng) && + (identical(other.currentLatLng, currentLatLng) || + other.currentLatLng == currentLatLng) && + (identical(other.cameraLatLng, cameraLatLng) || + other.cameraLatLng == cameraLatLng) && (identical(other.address, address) || other.address == address) && - const DeepCollectionEquality() - .equals(other.centerPosition, centerPosition) && + (identical(other.centerPosition, centerPosition) || + other.centerPosition == centerPosition) && (identical(other.popToPlaceList, popToPlaceList) || other.popToPlaceList == popToPlaceList) && const DeepCollectionEquality().equals(other.error, error)); @@ -250,10 +250,10 @@ class _$LocateOnMapStateImpl implements _LocateOnMapState { const DeepCollectionEquality().hash(loading), const DeepCollectionEquality().hash(addingPlace), const DeepCollectionEquality().hash(gettingAddress), - const DeepCollectionEquality().hash(currentLatLng), - const DeepCollectionEquality().hash(cameraLatLng), + currentLatLng, + cameraLatLng, address, - const DeepCollectionEquality().hash(centerPosition), + centerPosition, popToPlaceList, const DeepCollectionEquality().hash(error)); diff --git a/app/lib/ui/flow/home/map/map_view_model.freezed.dart b/app/lib/ui/flow/home/map/map_view_model.freezed.dart index 40b3ea63..7bb795eb 100644 --- a/app/lib/ui/flow/home/map/map_view_model.freezed.dart +++ b/app/lib/ui/flow/home/map/map_view_model.freezed.dart @@ -398,10 +398,10 @@ class _$MapViewStateImpl implements _MapViewState { .equals(other._userInfos, _userInfos) && (identical(other.selectedUser, selectedUser) || other.selectedUser == selectedUser) && - const DeepCollectionEquality() - .equals(other.currentUserLocation, currentUserLocation) && - const DeepCollectionEquality() - .equals(other.defaultPosition, defaultPosition) && + (identical(other.currentUserLocation, currentUserLocation) || + other.currentUserLocation == currentUserLocation) && + (identical(other.defaultPosition, defaultPosition) || + other.defaultPosition == defaultPosition) && (identical(other.spaceInvitationCode, spaceInvitationCode) || other.spaceInvitationCode == spaceInvitationCode) && (identical(other.mapType, mapType) || other.mapType == mapType) && @@ -424,8 +424,8 @@ class _$MapViewStateImpl implements _MapViewState { const DeepCollectionEquality().hash(_places), const DeepCollectionEquality().hash(_userInfos), selectedUser, - const DeepCollectionEquality().hash(currentUserLocation), - const DeepCollectionEquality().hash(defaultPosition), + currentUserLocation, + defaultPosition, spaceInvitationCode, mapType, const DeepCollectionEquality().hash(error), diff --git a/data/lib/service/location_service.dart b/data/lib/service/location_service.dart index 80cfa393..35037432 100644 --- a/data/lib/service/location_service.dart +++ b/data/lib/service/location_service.dart @@ -42,6 +42,19 @@ class LocationService { }); } + Future getCurrentLocation(String userId) async { + var snapshot = await _locationRef(userId) + .where("user_id", isEqualTo: userId) + .orderBy('created_at', descending: true) + .limit(1) + .get(); + + if (snapshot.docs.isNotEmpty) { + return snapshot.docs.map((doc) => doc.data() as ApiLocation).first; + } + return null; + } + Future saveCurrentLocation( String userId, LocationData locationData,