Skip to content

Commit

Permalink
Reuse the logic for displaying the label for an LOI (#2296)
Browse files Browse the repository at this point in the history
* Cleanup LoiCardUtil

* Remove getLabel from LOIHelper which was used only in tests

* Remove getCreatedBy from LOIHelper which was used only in tests

* Cleanup getSubtitle

* Merge LoiCardUtil into LoiHelper

* Reuse getDisplayLoiName in data collection and sync status screens

* Update unit test

* Declare submission count text to strings.xml

* Fix detekt code smell

* Replace if-elseif-else with when
  • Loading branch information
shobhitagarwal1612 authored Mar 4, 2024
1 parent 77a1e91 commit 26f2447
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.google.android.ground.model.locationofinterest

import com.google.android.ground.model.AuditInfo
import com.google.android.ground.model.geometry.*
import com.google.android.ground.model.geometry.Geometry
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.mutation.LocationOfInterestMutation
import com.google.android.ground.model.mutation.Mutation
Expand Down Expand Up @@ -75,6 +75,8 @@ data class LocationOfInterest(
submissionCount = submissionCount,
ownerEmail = ownerEmail,
isOpportunistic = isOpportunistic,
properties = properties
properties = properties,
)

fun getProperty(key: String): String = properties[key]?.toString() ?: ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,48 @@ package com.google.android.ground.ui.common

import android.content.res.Resources
import com.google.android.ground.R
import com.google.android.ground.model.AuditInfo
import com.google.android.ground.model.User
import com.google.android.ground.model.geometry.LineString
import com.google.android.ground.model.geometry.LinearRing
import com.google.android.ground.model.geometry.Geometry
import com.google.android.ground.model.geometry.MultiPolygon
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.geometry.Polygon
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import java.util.Optional
import javax.inject.Inject

/** Common logic for formatting attributes of [LocationOfInterest] for display to the user. */
/** Helper class for creating user-visible text. */
class LocationOfInterestHelper @Inject internal constructor(private val resources: Resources) {
fun getCreatedBy(locationOfInterest: Optional<LocationOfInterest>): String =
getUserName(locationOfInterest).map { resources.getString(R.string.added_by, it) }.orElse("")

// TODO(#793): Allow user-defined LOI names for other LOI types.
fun getLabel(locationOfInterest: Optional<LocationOfInterest>): String =
locationOfInterest.map(::getLabel).orElse("")
fun getDisplayLoiName(loi: LocationOfInterest): String {
val loiId = loi.customId.ifEmpty { loi.getProperty("id") }
val loiName = loi.getProperty("name")

fun getLabel(loi: LocationOfInterest): String {
// TODO(#2046): Reuse logic from card util to display LOI label
val caption = loi.customId?.trim { it <= ' ' } ?: ""
return caption.ifEmpty { getLocationOfInterestType(loi) }
return when {
loiName.isNotEmpty() && loiId.isNotEmpty() -> "$loiName ($loiId)"
loiName.isNotEmpty() -> loiName
loiId.isNotEmpty() -> "${loi.geometry.toType()} ($loiId)"
else -> loi.geometry.toDefaultName()
}
}

private fun getLocationOfInterestType(locationOfInterest: LocationOfInterest): String =
when (locationOfInterest.geometry) {
is Polygon -> "Polygon"
is Point -> "Point"
is LineString -> "LineString"
is LinearRing -> "LinearRing"
is MultiPolygon -> "MultiPolygon"
fun getJobName(loi: LocationOfInterest): String? = loi.job.name

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

fun getSubtitle(locationOfInterest: Optional<LocationOfInterest>): String =
locationOfInterest
.map { resources.getString(R.string.layer_label_format, it.job.name) }
.orElse("")
/** Returns a default user-visible name for the geometry. */
private fun Geometry.toDefaultName(): String =
when (this) {
is Point -> resources.getString(R.string.unnamed_point)
is Polygon,
is MultiPolygon -> resources.getString(R.string.unnamed_area)
else -> throw IllegalArgumentException("Unsupported geometry type $this")
}

private fun getUserName(locationOfInterest: Optional<LocationOfInterest>): Optional<String> =
locationOfInterest.map(LocationOfInterest::created).map(AuditInfo::user).map(User::displayName)
fun getSubtitle(loi: LocationOfInterest): String =
resources.getString(R.string.layer_label_format, loi.job.name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal constructor(
else
flow {
val loi = locationOfInterestRepository.getOfflineLoi(surveyId, loiId)
val label = locationOfInterestHelper.getLabel(loi)
val label = locationOfInterestHelper.getDisplayLoiName(loi)
emit(label)
})
.stateIn(viewModelScope, SharingStarted.Lazily, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import com.google.android.ground.ui.common.EphemeralPopups
import com.google.android.ground.ui.home.HomeScreenFragmentDirections
import com.google.android.ground.ui.home.HomeScreenViewModel
import com.google.android.ground.ui.home.mapcontainer.HomeScreenMapContainerViewModel.SurveyProperties
import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil
import com.google.android.ground.ui.home.mapcontainer.cards.MapCardAdapter
import com.google.android.ground.ui.home.mapcontainer.cards.MapCardUiData
import com.google.android.ground.ui.map.MapFragment
Expand Down Expand Up @@ -108,8 +107,10 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
/** Updates the given [TextView] with the submission count for the given [LocationOfInterest]. */
private fun updateSubmissionCount(loi: LocationOfInterest, view: TextView) {
externalScope.launch {
val submissionCount = submissionRepository.getTotalSubmissionCount(loi)
val submissionText = LoiCardUtil.getSubmissionsText(submissionCount)
val count = submissionRepository.getTotalSubmissionCount(loi)
val submissionText =
if (count == 0) resources.getString(R.string.no_submissions)
else resources.getQuantityString(R.plurals.submission_count, count, count)
withContext(mainDispatcher) { view.text = submissionText }
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.google.android.ground.databinding.AddLoiCardItemBinding
import com.google.android.ground.databinding.LoiCardItemBinding
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.ui.common.LocationOfInterestHelper

/**
* An implementation of [RecyclerView.Adapter] that associates [LocationOfInterest] data with the
Expand Down Expand Up @@ -138,10 +139,12 @@ class MapCardAdapter(
private val canUserSubmitData: Boolean,
private val updateSubmissionCount: (loi: LocationOfInterest, view: TextView) -> Unit,
) : CardViewHolder(binding.root) {
private val loiHelper = LocationOfInterestHelper(itemView.resources)

fun bind(loi: LocationOfInterest) {
with(binding) {
loiName.text = LoiCardUtil.getDisplayLoiName(binding.wrapperView.context, loi)
jobName.text = LoiCardUtil.getJobName(loi)
loiName.text = loiHelper.getDisplayLoiName(loi)
jobName.text = loiHelper.getJobName(loi)
collectData.visibility =
if (canUserSubmitData && loi.job.hasTasks()) View.VISIBLE else View.GONE
updateSubmissionCount(loi, submissions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.google.android.ground.repository.SurveyRepository
import com.google.android.ground.repository.UserRepository
import com.google.android.ground.ui.common.AbstractViewModel
import com.google.android.ground.ui.common.LocationOfInterestHelper
import java.util.Optional
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -71,8 +70,8 @@ internal constructor(
return MutationDetail(
user = user.displayName,
mutation = mutation,
loiLabel = locationOfInterestHelper.getLabel(loi),
loiSubtitle = locationOfInterestHelper.getSubtitle(Optional.of(loi)),
loiLabel = locationOfInterestHelper.getDisplayLoiName(loi),
loiSubtitle = locationOfInterestHelper.getSubtitle(loi),
)
}
}
1 change: 0 additions & 1 deletion ground/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<string name="offline_area_viewer_title">Área del mapa base sin conexión</string>
<string name="offline_area_viewer_remove_button">Eliminar</string>
<string name="unnamed_area">Área sin nombre</string>
<string name="added_by">Añadido por %s</string>
<!-- Label of button used enter the offline area selector. -->
<string name="offline_area_selector_select">Seleccionar área</string>
<string name="no_basemaps_downloaded">No se descargaron mapas base.</string>
Expand Down
1 change: 0 additions & 1 deletion ground/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<string name="offline_area_viewer_title">Visualizador offline de Títulos de Base de Mapas</string>
<string name="offline_area_viewer_remove_button">Remover</string>
<string name="unnamed_area">Area sem nome</string>
<string name="added_by">Adicionado por %s</string>
<!-- Label of button used enter the offline area selector. -->
<string name="offline_area_selector_select">Selecionar área</string>
<string name="no_basemaps_downloaded">Nenhum mapa carregado.</string>
Expand Down
6 changes: 5 additions & 1 deletion ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<string name="settings">Settings</string>
<string name="offline_area_viewer_title">Downloaded area</string>
<string name="offline_area_viewer_remove_button">Remove from device</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 area</string>
<string name="no_basemaps_downloaded">No imagery downloaded for offline use. Tap “Select and download” to get started.</string>
Expand Down Expand Up @@ -139,4 +138,9 @@
<string name="suggest_data_collection_hint">Zoom in to start collecting data</string>
<string name="read_only_data_collection_hint">Survey is read-only</string>
<string name="predefined_data_collection_hint">Zoom in to a data collection site to collect data</string>
<string name="no_submissions">No submissions</string>
<plurals name="submission_count">
<item quantity="one">%d submission</item>
<item quantity="other">%d submissions</item>
</plurals>
</resources>
Loading

0 comments on commit 26f2447

Please sign in to comment.