Skip to content

Commit

Permalink
Fix fetch current location to locate on map for adding place (#184)
Browse files Browse the repository at this point in the history
* fix fetch current location

* add null check operator condition

* code refactoring
  • Loading branch information
cp-ishita-g authored Dec 27, 2024
1 parent 0be178b commit 8a36a27
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
56 changes: 33 additions & 23 deletions app/lib/ui/flow/geofence/add/locate/locate_on_map_view_model.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -17,42 +17,52 @@ final locateOnMapViewStateProvider =
StateNotifierProvider.autoDispose<LocateOnMapVieNotifier, LocateOnMapState>(
(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<LocateOnMapState> {
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);
}
}

Expand All @@ -70,18 +80,18 @@ class LocateOnMapVieNotifier extends StateNotifier<LocateOnMapState> {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));

Expand Down
12 changes: 6 additions & 6 deletions app/lib/ui/flow/home/map/map_view_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand All @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions data/lib/service/location_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ class LocationService {
});
}

Future<ApiLocation?> 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<void> saveCurrentLocation(
String userId,
LocationData locationData,
Expand Down

0 comments on commit 8a36a27

Please sign in to comment.