Skip to content

Commit

Permalink
Move converters for CaptureLocationTaskData (#2712)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Sep 5, 2024
1 parent 446cef4 commit e4c8399
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import com.google.android.ground.model.submission.TextTaskData
import com.google.android.ground.model.submission.TimeTaskData
import com.google.android.ground.model.task.Task
import com.google.android.ground.persistence.remote.DataStoreException
import com.google.android.ground.persistence.remote.firebase.schema.CaptureLocationResultConverter.ACCURACY_KEY
import com.google.android.ground.persistence.remote.firebase.schema.CaptureLocationResultConverter.ALTITUDE_KEY
import com.google.android.ground.persistence.remote.firebase.schema.CaptureLocationResultConverter.GEOMETRY_KEY
import com.google.android.ground.persistence.remote.firebase.schema.CaptureLocationResultConverter.toCaptureLocationTaskData
import com.google.android.ground.persistence.remote.firebase.schema.CaptureLocationResultConverter.toJSONObject
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
Expand All @@ -58,12 +57,7 @@ internal object ValueJsonConverter {
is PhotoTaskData -> taskData.remoteFilename
is DrawAreaTaskData -> GeometryWrapperTypeConverter.toString(taskData.geometry)
is DropPinTaskData -> GeometryWrapperTypeConverter.toString(taskData.geometry)
is CaptureLocationTaskData ->
JSONObject().apply {
put("accuracy", taskData.accuracy)
put("altitude", taskData.altitude)
put("geometry", GeometryWrapperTypeConverter.toString(taskData.geometry))
}
is CaptureLocationTaskData -> taskData.toJSONObject()
else -> throw UnsupportedOperationException("Unimplemented value class ${taskData.javaClass}")
}
}
Expand Down Expand Up @@ -127,25 +121,14 @@ internal object ValueJsonConverter {
}
Task.Type.CAPTURE_LOCATION -> {
DataStoreException.checkType(JSONObject::class.java, obj)
captureLocationResultFromJsonObject(obj as JSONObject).getOrThrow()
(obj as JSONObject).toCaptureLocationTaskData()
}
Task.Type.UNKNOWN -> {
throw DataStoreException("Unknown type in task: " + obj.javaClass.name)
}
}
}

private fun captureLocationResultFromJsonObject(
data: JSONObject
): Result<CaptureLocationTaskData> =
Result.runCatching {
val accuracy = data.getDouble(ACCURACY_KEY)
val altitude = data.getDouble(ALTITUDE_KEY)
val geometry =
GeometryWrapperTypeConverter.fromString(data.getString(GEOMETRY_KEY))?.getGeometry()
CaptureLocationTaskData(geometry as Point, accuracy, altitude)
}

private fun toList(jsonArray: JSONArray): List<String> {
val list: MutableList<String> = ArrayList(jsonArray.length())
for (i in 0 until jsonArray.length()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,28 @@
*/
package com.google.android.ground.persistence.remote.firebase.schema

import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.submission.CaptureLocationTaskData
import com.google.android.ground.persistence.local.room.converter.GeometryWrapperTypeConverter
import org.json.JSONObject

/** Converts between [CaptureLocationTaskData] and its equivalent remote representation. */
/** Converts between [CaptureLocationTaskData] and its equivalent [JSONObject] representation. */
object CaptureLocationResultConverter {
const val ACCURACY_KEY = "accuracy"
const val ALTITUDE_KEY = "altitude"
const val GEOMETRY_KEY = "geometry"
private const val ACCURACY_KEY = "accuracy"
private const val ALTITUDE_KEY = "altitude"
private const val GEOMETRY_KEY = "geometry"

fun CaptureLocationTaskData.toJSONObject(): JSONObject =
JSONObject().apply {
put(ACCURACY_KEY, accuracy)
put(ALTITUDE_KEY, altitude)
put(GEOMETRY_KEY, GeometryWrapperTypeConverter.toString(geometry))
}

fun JSONObject.toCaptureLocationTaskData(): CaptureLocationTaskData {
val accuracy = getDouble(ACCURACY_KEY)
val altitude = getDouble(ALTITUDE_KEY)
val geometry = GeometryWrapperTypeConverter.fromString(getString(GEOMETRY_KEY))?.getGeometry()
return CaptureLocationTaskData(geometry as Point, accuracy, altitude)
}
}

0 comments on commit e4c8399

Please sign in to comment.