Skip to content

Commit

Permalink
Make CaptureLocationTaskData not a GeometryTaskData since we are stor…
Browse files Browse the repository at this point in the history
…ing the coordinates and not the geometry
  • Loading branch information
shobhitagarwal1612 committed Dec 22, 2024
1 parent 5fbfd4d commit 64f1042
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,15 @@
*/
package com.google.android.ground.model.submission

import android.location.Location
import com.google.android.ground.model.geometry.Coordinates
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.proto.Task

/** User-provided response to a "capture location" data collection [Task]. */
data class CaptureLocationTaskData(
val location: Point,
val altitude: Double?, // in metres
val accuracy: Double?, // in metres
) : GeometryTaskData(location) {
) : TaskData {

override fun isEmpty(): Boolean = false

companion object {
fun Location.toCaptureLocationResult(): CaptureLocationTaskData {
val altitude = if (hasAltitude()) altitude else null
val accuracy = if (hasAccuracy()) accuracy else null
return CaptureLocationTaskData(
Point(Coordinates(latitude, longitude)),
altitude,
accuracy?.toDouble(),
)
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@
package com.google.android.ground.model.submission

import com.google.android.ground.model.geometry.Geometry
import com.google.android.ground.model.geometry.LineString
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.geometry.Polygon
import com.google.android.ground.model.task.Task

/** A user-provided response to a geometry-based task ("drop a pin" or "draw an area"). */
abstract class GeometryTaskData(val geometry: Geometry) : TaskData {
sealed class GeometryTaskData(val geometry: Geometry) : TaskData

/** User-provided response to a "drop a pin" data collection [Task]. */
data class DropPinTaskData(val location: Point) : GeometryTaskData(location) {
override fun isEmpty(): Boolean = false
}

/** User-provided response to a "draw an area" data collection [Task]. */
data class DrawAreaTaskData(val area: Polygon) : GeometryTaskData(area) {
override fun isEmpty(): Boolean = area.isEmpty()
}

/** User-provided "ongoing" response to a "draw an area" data collection [Task]. */
data class DrawAreaTaskIncompleteData(val lineString: LineString) : GeometryTaskData(lineString) {
override fun isEmpty(): Boolean = lineString.isEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object CaptureLocationResultConverter {
JSONObject().apply {
put(ACCURACY_KEY, accuracy)
put(ALTITUDE_KEY, altitude)
put(GEOMETRY_KEY, GeometryWrapperTypeConverter.toString(geometry))
put(GEOMETRY_KEY, GeometryWrapperTypeConverter.toString(location))
}

fun JSONObject.toCaptureLocationTaskData(): CaptureLocationTaskData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.ground.R
import com.google.android.ground.databinding.MapTaskFragBinding
import com.google.android.ground.model.submission.CaptureLocationTaskData.Companion.toCaptureLocationResult
import com.google.android.ground.ui.datacollection.DataCollectionViewModel
import com.google.android.ground.ui.map.CameraPosition
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.map.gms.toAccuracy
import com.google.android.ground.ui.map.gms.toCoordinates
import com.google.android.ground.util.toDmsFormat
import java.math.RoundingMode
import java.text.DecimalFormat
Expand Down Expand Up @@ -66,12 +67,11 @@ abstract class AbstractMapFragmentWithControls : AbstractMapContainerFragment()
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
getMapViewModel().location.collect {
val taskData = it?.toCaptureLocationResult()
val locationText = taskData?.location?.coordinates?.toDmsFormat()
val locationText = it?.toCoordinates()?.toDmsFormat()

val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.DOWN
val accuracyText = taskData?.accuracy?.let { value -> df.format(value) + "m" } ?: "?"
val accuracyText = it?.toAccuracy()?.let { value -> df.format(value) + "m" } ?: "?"

updateLocationInfoCard(R.string.current_location, locationText, accuracyText)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package com.google.android.ground.ui.datacollection.tasks.location

import android.location.Location
import androidx.lifecycle.viewModelScope
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.submission.CaptureLocationTaskData
import com.google.android.ground.model.submission.CaptureLocationTaskData.Companion.toCaptureLocationResult
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskViewModel
import com.google.android.ground.ui.map.gms.toAccuracy
import com.google.android.ground.ui.map.gms.toAltitude
import com.google.android.ground.ui.map.gms.toCoordinates
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -43,20 +46,27 @@ enum class LocationLockEnabledState {

class CaptureLocationTaskViewModel @Inject constructor() : AbstractTaskViewModel() {

private val lastLocation = MutableStateFlow<CaptureLocationTaskData?>(null)
private val lastLocation = MutableStateFlow<Location?>(null)
/** Allows control for triggering the location lock programmatically. */
private val _enableLocationLockFlow = MutableStateFlow(LocationLockEnabledState.UNKNOWN)
val enableLocationLockFlow = _enableLocationLockFlow.asStateFlow()

suspend fun updateLocation(location: Location) {
lastLocation.emit(location.toCaptureLocationResult())
lastLocation.emit(location)
}

fun updateResponse() {
if (lastLocation.value == null) {
val location = lastLocation.value
if (location == null) {
viewModelScope.launch { _enableLocationLockFlow.emit(LocationLockEnabledState.ENABLE) }
} else {
setValue(lastLocation.value)
setValue(
CaptureLocationTaskData(
Point(location.toCoordinates()),
location.toAltitude(),
location.toAccuracy(),
)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ fun Coordinates.toLatLng(): LatLng = LatLng(lat, lng)
fun List<Coordinates>.toLatLngList(): List<LatLng> = map { it.toLatLng() }

fun Location.toCoordinates(): Coordinates = Coordinates(latitude, longitude)

fun Location.toAltitude(): Double? = if (hasAltitude()) altitude else null

fun Location.toAccuracy(): Double? = if (hasAccuracy()) accuracy.toDouble() else null

0 comments on commit 64f1042

Please sign in to comment.