Skip to content

Commit

Permalink
Use correct color for drop a pin flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m committed Oct 2, 2023
1 parent 2244e93 commit 8b44e2f
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ground/src/main/java/com/google/android/ground/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Config {

// Local db settings.
// TODO(#128): Reset version to 1 before releasing.
const val DB_VERSION = 106
const val DB_VERSION = 107
const val DB_NAME = "ground.db"

// Firebase Cloud Firestore settings.
Expand Down
15 changes: 13 additions & 2 deletions ground/src/main/java/com/google/android/ground/model/job/Job.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
package com.google.android.ground.model.job

import android.graphics.Color
import com.google.android.ground.model.task.Task
import java.lang.IllegalArgumentException
import timber.log.Timber

/**
* @param suggestLoiTaskType the type of task used to suggest the LOI for this Job. Null if the job
* is already associated with an LOI.
* is already associated with an LOI.
*/
data class Job(
val id: String,
val style: Style,
val style: Style?,
val name: String? = null,
val tasks: Map<String, Task> = mapOf(),
val suggestLoiTaskType: Task.Type? = null,
Expand All @@ -35,3 +38,11 @@ data class Job(

fun hasData(): Boolean = tasks.isNotEmpty()
}

fun Job.getDefaultColor(): Int =
try {
Color.parseColor(style?.color ?: "")
} catch (e: IllegalArgumentException) {
Timber.w("Invalid or missing color ${style?.color} in job $id")
0
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ fun Job.toLocalDataStoreObject(surveyId: String): JobEntity =
surveyId = surveyId,
name = name,
suggestLoiTaskType = suggestLoiTaskType?.toString(),
style = style.toLocalDataStoreObject()
style = style?.toLocalDataStoreObject()
)

fun JobEntityAndRelations.toModelObject(): Job {
val taskMap = taskEntityAndRelations.map { it.toModelObject() }.associateBy { it.id }
return Job(
jobEntity.id,
jobEntity.style.toModelObject(),
jobEntity.style?.toModelObject(),
jobEntity.name,
taskMap.toPersistentMap(),
jobEntity.suggestLoiTaskType?.let { Task.Type.valueOf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ data class JobEntity(
@ColumnInfo(name = "name") val name: String?,
@ColumnInfo(name = "survey_id") val surveyId: String?,
@ColumnInfo(name = "suggest_loi_task_type") val suggestLoiTaskType: String?,
@Embedded(prefix = "style_") val style: StyleEntity
@Embedded(prefix = "style_") val style: StyleEntity?
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal object JobConverter {
}
return Job(
id,
obj.defaultStyle.toStyle(),
obj.defaultStyle?.toStyle(),
obj.name,
taskMap.toPersistentMap(),
TaskConverter.toSuggestLoiTaskType(obj.suggestLoiTaskType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.google.firebase.firestore.IgnoreExtraProperties
/** Firestore representation of map layers. */
@IgnoreExtraProperties
data class JobNestedObject(
val defaultStyle: StyleNestedObject = StyleNestedObject(),
val defaultStyle: StyleNestedObject? = null,
val name: String? = null,
val tasks: Map<String, TaskNestedObject>? = null,
val suggestLoiTaskType: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ import com.google.android.ground.model.job.Style
import com.google.firebase.firestore.IgnoreExtraProperties

/** Firestore representation of map layers. */
@IgnoreExtraProperties data class StyleNestedObject(val color: String = "#ff9131")
@IgnoreExtraProperties data class StyleNestedObject(val color: String = "")

fun StyleNestedObject.toStyle(): Style = Style(color)
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ constructor(
fun findLocationsOfInterestFeatures(survey: Survey) =
findLocationsOfInterest(survey).map { toLocationOfInterestFeatures(it) }

// TODO: Refactor: `Feature`s are speciifc to the map UI and don't belong here.
private suspend fun toLocationOfInterestFeatures(
locationsOfInterest: Set<LocationOfInterest>
): Set<Feature> = // TODO: Add support for polylines similar to mapPins.
Expand All @@ -155,14 +156,13 @@ constructor(
type = FeatureType.LOCATION_OF_INTEREST.ordinal,
flag = submissionCount > 0,
geometry = it.geometry,
style = toFeatureStyle(it.job.style),
// TODO: Reuse Job.getDefaultColor(), remove duplicate toFeatureStyle.
style = it.job.style!!.toFeatureStyle(),
clusterable = true
)
}
.toPersistentSet()

private fun toFeatureStyle(style: Style) = Feature.Style(Color.parseColor(style.color))

/** Returns a list of geometries associated with the given [Survey]. */
suspend fun getAllGeometries(survey: Survey): List<Geometry> =
getLocationsOfInterestOnceAndStream(survey).awaitFirst().map { it.geometry }
Expand All @@ -176,3 +176,5 @@ constructor(
.map { lois -> lois.filter { bounds.contains(it.geometry) } }
.distinctUntilChanged()
}

private fun Style.toFeatureStyle() = Feature.Style(Color.parseColor(color))
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ internal constructor(
}
val viewModel = viewModelFactory.create(getViewModelClass(task.type))
// TODO(#1146): Pass in the existing taskData if there is one
viewModel.initialize(task, null)
viewModel.initialize(job, task, null)
addTaskViewModel(viewModel)
return viewModel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.toLiveData
import androidx.lifecycle.viewModelScope
import com.google.android.ground.R
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.submission.TaskData
import com.google.android.ground.model.submission.isNullOrEmpty
import com.google.android.ground.model.task.Task
Expand Down Expand Up @@ -65,7 +66,7 @@ open class AbstractTaskViewModel internal constructor(private val resources: Res
}

// TODO: Add a reference of Task in TaskData for simplification.
fun initialize(task: Task, taskData: TaskData?) {
open fun initialize(job: Job, task: Task, taskData: TaskData?) {
this.task = task
setResponse(taskData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package com.google.android.ground.ui.datacollection.tasks.point

import android.content.res.Resources
import android.graphics.Color
import androidx.lifecycle.MutableLiveData
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.job.getDefaultColor
import com.google.android.ground.model.submission.GeometryData
import com.google.android.ground.model.submission.TaskData
import com.google.android.ground.model.task.Task
import com.google.android.ground.persistence.uuid.OfflineUuidGenerator
import com.google.android.ground.rx.annotations.Hot
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskViewModel
Expand All @@ -33,9 +36,15 @@ class DropAPinTaskViewModel
constructor(resources: Resources, private val uuidGenerator: OfflineUuidGenerator) :
AbstractTaskViewModel(resources) {

var lastCameraPosition: CameraPosition? = null
private var pinColor: Int = 0
private var lastCameraPosition: CameraPosition? = null
val features: @Hot MutableLiveData<Set<Feature>> = MutableLiveData()

override fun initialize(job: Job, task: Task, taskData: TaskData?) {
super.initialize(job, task, taskData)
pinColor = job.getDefaultColor()
}

fun updateCameraPosition(position: CameraPosition) {
lastCameraPosition = position
}
Expand All @@ -57,7 +66,7 @@ constructor(resources: Resources, private val uuidGenerator: OfflineUuidGenerato
type = FeatureType.USER_POINT.ordinal,
geometry = point,
// TODO: Set correct pin color.
style = Feature.Style(Color.CYAN),
style = Feature.Style(pinColor),
clusterable = false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import com.google.android.ground.model.geometry.LineString
import com.google.android.ground.model.geometry.LinearRing
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.geometry.Polygon
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.job.getDefaultColor
import com.google.android.ground.model.submission.GeometryData
import com.google.android.ground.model.submission.TaskData
import com.google.android.ground.model.task.Task
import com.google.android.ground.persistence.uuid.OfflineUuidGenerator
import com.google.android.ground.ui.common.SharedViewModel
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskViewModel
Expand Down Expand Up @@ -62,6 +66,13 @@ internal constructor(private val uuidGenerator: OfflineUuidGenerator, resources:
/** Represents whether the user has completed drawing the polygon or not. */
private var isMarkedComplete: Boolean = false

private var strokeColor: Int = 0

override fun initialize(job: Job, task: Task, taskData: TaskData?) {
super.initialize(job, task, taskData)
strokeColor = job.getDefaultColor()
}

fun isMarkedComplete(): Boolean = isMarkedComplete

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import app.cash.turbine.test
import com.google.android.ground.BaseHiltTest
import com.google.android.ground.R
import com.google.android.ground.launchFragmentWithNavController
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.submission.TaskData
import com.google.android.ground.model.task.Task
import com.google.android.ground.ui.common.ViewModelFactory
Expand Down Expand Up @@ -103,9 +104,9 @@ abstract class BaseTaskFragmentTest<F : AbstractTaskFragment<VM>, VM : AbstractT
onView(withText(buttonText)).check(matches(isDisplayed())).check(matches(not(isEnabled())))
}

protected inline fun <reified T : Fragment> setupTaskFragment(task: Task) {
protected inline fun <reified T : Fragment> setupTaskFragment(job: Job, task: Task) {
viewModel = viewModelFactory.create(DataCollectionViewModel.getViewModelClass(task.type)) as VM
viewModel.initialize(task, null)
viewModel.initialize(job, task, null)
whenever(dataCollectionViewModel.getTaskViewModel(task.index)).thenReturn(viewModel)

launchFragmentWithNavController<T>(
Expand Down

0 comments on commit 8b44e2f

Please sign in to comment.