Skip to content

Commit

Permalink
Display customId if available in job card name (#1943)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Oct 1, 2023
1 parent 84fefdb commit 4a7b7b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.google.android.ground.ui.home.mapcontainer.cards

import android.content.Context
import com.google.android.ground.R
import com.google.android.ground.model.geometry.Geometry
import com.google.android.ground.model.geometry.LineString
import com.google.android.ground.model.geometry.LinearRing
import com.google.android.ground.model.geometry.MultiPolygon
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.geometry.Polygon
Expand All @@ -26,7 +26,20 @@ import com.google.android.ground.model.locationofinterest.LocationOfInterest
/** Helper class for creating user-visible text. */
object LoiCardUtil {

fun getDisplayLoiName(loi: LocationOfInterest): String = loi.caption ?: loi.geometry.type()
fun getDisplayLoiName(context: Context, loi: LocationOfInterest): String {
val caption = loi.caption
val customId = loi.customId
val geometry = loi.geometry
return if (caption.isNotNullOrEmpty() && customId.isNotNullOrEmpty()) {
"$caption ($customId)"
} else if (caption.isNotNullOrEmpty()) {
"$caption"
} else if (customId.isNotNullOrEmpty()) {
"${geometry.toType(context)} ($customId)"
} else {
geometry.toDefaultName(context)
}
}

fun getJobName(loi: LocationOfInterest): String? = loi.job.name

Expand All @@ -38,12 +51,22 @@ object LoiCardUtil {
}

/** Returns a user-visible string representing the type of the geometry. */
private fun Geometry.type() =
private fun Geometry.toType(context: Context): String =
when (this) {
is Point -> "Point"
is Polygon -> "Polygon"
is LinearRing -> "LinearRing"
is LineString -> "LineString"
is MultiPolygon -> "MultiPolygon"
is Point -> context.getString(R.string.point)
is Polygon,
is MultiPolygon -> context.getString(R.string.area)
else -> throw IllegalArgumentException("Unsupported geometry type $this")
}

/** Returns a default user-visible name for the geometry. */
private fun Geometry.toDefaultName(context: Context): String =
when (this) {
is Point -> context.getString(R.string.unnamed_point)
is Polygon,
is MultiPolygon -> context.getString(R.string.unnamed_area)
else -> throw IllegalArgumentException("Unsupported geometry type $this")
}

private fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty()
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MapCardAdapter(private val canUserSubmitData: Boolean) :
) : CardViewHolder(binding.root, binding.loiCard) {
fun bind(loi: LocationOfInterest) {
with(binding) {
loiName.text = LoiCardUtil.getDisplayLoiName(loi)
loiName.text = LoiCardUtil.getDisplayLoiName(binding.wrapperView.context, loi)
jobName.text = LoiCardUtil.getJobName(loi)
collectData.visibility = if (canUserSubmitData) View.VISIBLE else View.GONE

Expand Down
5 changes: 4 additions & 1 deletion ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<string name="settings">Settings</string>
<string name="offline_area_viewer_title">Offline area</string>
<string name="offline_area_viewer_remove_button">Remove</string>
<string name="unnamed_area">Unnamed area</string>
<string name="added_by">Added by %s</string>
<!-- Label of button used enter the offline area selector. -->
<string name="offline_area_selector_select">Select and download</string>
Expand Down Expand Up @@ -171,4 +170,8 @@
<string name="surveys">Surveys</string>
<string name="offline_icon_description">Icon shown when the survey is available offline</string>
<string name="remove_offline_access">Remove offline access</string>
<string name="point">Point</string>
<string name="area">Area</string>
<string name="unnamed_point">Unnamed point</string>
<string name="unnamed_area">Unnamed area</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.android.ground.ui.home.mapcontainer

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getDisplayLoiName
import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getJobName
import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getSubmissionsText
Expand All @@ -27,19 +29,22 @@ import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class LoiCardUtilTest {

private val context: Context = ApplicationProvider.getApplicationContext()

@Test
fun testLoiNameWithPoint_whenCaptionIsNull() {
assertThat(getDisplayLoiName(TEST_LOI.copy(caption = null))).isEqualTo("Point")
assertThat(getDisplayLoiName(context, TEST_LOI.copy(caption = null))).isEqualTo("Unnamed point")
}

@Test
fun testLoiNameWithPolygon_whenCaptionIsNull() {
assertThat(getDisplayLoiName(TEST_AREA.copy(caption = null))).isEqualTo("Polygon")
assertThat(getDisplayLoiName(context, TEST_AREA.copy(caption = null))).isEqualTo("Unnamed area")
}

@Test
fun testLoiName_whenCaptionIsAvailable() {
assertThat(getDisplayLoiName(TEST_LOI.copy(caption = "some value"))).isEqualTo("some value")
assertThat(getDisplayLoiName(context, TEST_LOI.copy(caption = "some value")))
.isEqualTo("some value")
}

@Test
Expand Down

0 comments on commit 4a7b7b5

Please sign in to comment.