Skip to content

Commit

Permalink
Allow zooming in to the current location when toggling the lock 2nd t…
Browse files Browse the repository at this point in the history
…ime (#1957)

* Allow zooming in to the location on 2nd location lock enable

* Reset location updates flow when location lock and disabled and undo the logic for panAndZoom at index 0
  • Loading branch information
shobhitagarwal1612 authored Oct 7, 2023
1 parent 3ea5181 commit c72d4e7
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ import com.google.android.ground.ui.map.gms.GmsExt.toBounds
import com.google.android.ground.ui.map.gms.toCoordinates
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.transform
Expand Down Expand Up @@ -96,7 +100,7 @@ constructor(

val location: StateFlow<Location?> =
locationLock
.combine(locationManager.locationUpdates) { locationLock, latestLocation ->
.combine(getLocationUpdates()) { locationLock, latestLocation ->
if (locationLock.getOrDefault(false)) {
latestLocation
} else {
Expand Down Expand Up @@ -197,16 +201,21 @@ constructor(
/** Emits a stream of current camera position. */
fun getCurrentCameraPosition(): Flow<CameraPosition> = currentCameraPosition.filterNotNull()

fun getLocationUpdates() = locationManager.locationUpdates
fun getLocationUpdates() = locationManager.locationUpdates.distinctUntilChanged()

/**
* Updates map camera when location changes. The first update pans and zooms the camera to the
* appropriate zoom level and subsequent ones only pan the map.
*/
@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun updateCameraPositionOnLocationChange() {
getLocationUpdates()
.map { it.toCoordinates() }
.withIndex()
locationLock
.flatMapLatest { enabled ->
getLocationUpdates()
.map { it.toCoordinates() }
.filter { enabled.getOrDefault(false) }
.withIndex()
}
.collect { (index, coordinates) ->
if (index == 0) {
panAndZoomCamera(coordinates)
Expand Down

0 comments on commit c72d4e7

Please sign in to comment.