Skip to content

Commit

Permalink
Add submission count to card data (#3037)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored Feb 7, 2025
1 parent 8d453f3 commit 648c8bc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import timber.log.Timber

/** Main app view, displaying the map and related controls (center cross-hairs, add button, etc). */
Expand All @@ -77,9 +76,8 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
super.onCreate(savedInstanceState)
mapContainerViewModel = getViewModel(HomeScreenMapContainerViewModel::class.java)
homeScreenViewModel = getViewModel(HomeScreenViewModel::class.java)
jobMapComposables = JobMapComposables { loi ->
submissionRepository.getTotalSubmissionCount(loi)
}
jobMapComposables = JobMapComposables()
jobMapComposables.setSelectedFeature { mapContainerViewModel.selectLocationOfInterest(it) }

launchWhenStarted {
val canUserSubmitData = userRepository.canUserSubmitData()
Expand All @@ -104,7 +102,7 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
// Bind data for cards
mapContainerViewModel.processDataCollectionEntryPoints().launchWhenStartedAndCollect {
(loiCard, jobCards) ->
runBlocking { jobMapComposables.updateData(canUserSubmitData, loiCard, jobCards) }
jobMapComposables.updateData(canUserSubmitData, loiCard, jobCards)
}
}

Expand Down Expand Up @@ -276,8 +274,6 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {

override fun onMapReady(map: MapFragment) {
mapContainerViewModel.mapLoiFeatures.launchWhenStartedAndCollect { map.setFeatures(it) }

jobMapComposables.setSelectedFeature { mapContainerViewModel.selectLocationOfInterest(it) }
}

override fun getMapViewModel(): BaseMapViewModel = mapContainerViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,13 @@ internal constructor(
combine(loisInViewport, featureClicked, adHocLoiJobs) { loisInView, feature, jobs ->
val loiCard =
loisInView
.filter { it.geometry == feature?.geometry }
.firstOrNull()
?.let { SelectedLoiSheetData(it) }
.firstOrNull { it.geometry == feature?.geometry }
?.let {
SelectedLoiSheetData(
loi = it,
submissionCount = submissionRepository.getTotalSubmissionCount(it),
)
}
if (loiCard == null && feature != null) {
// The feature is not in view anymore.
featureClicked.value = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.android.ground.model.locationofinterest.LocationOfInterest
/** Data classes used to populate the data collection entry UI, like the LOI bottom sheet. */
sealed interface DataCollectionEntryPointData

data class SelectedLoiSheetData(val loi: LocationOfInterest) : DataCollectionEntryPointData
data class SelectedLoiSheetData(val loi: LocationOfInterest, val submissionCount: Int) :
DataCollectionEntryPointData

data class AdHocDataCollectionButtonData(val job: Job) : DataCollectionEntryPointData
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import com.google.android.ground.ui.common.LocationOfInterestHelper
import kotlinx.coroutines.launch

/** Manages a set of [Composable] components that renders [LocationOfInterest] cards and dialogs. */
class JobMapComposables(private val getSubmissionCount: suspend (loi: LocationOfInterest) -> Int) {
class JobMapComposables {
private var collectDataListener: MutableState<(DataCollectionEntryPointData) -> Unit> =
mutableStateOf({})
private var canUserSubmitData = mutableStateOf(false)
Expand All @@ -98,7 +98,7 @@ class JobMapComposables(private val getSubmissionCount: suspend (loi: LocationOf
}

/** Overwrites existing cards. */
suspend fun updateData(
fun updateData(
canUserSubmitData: Boolean,
selectedLoi: SelectedLoiSheetData?,
addLoiJobs: List<AdHocDataCollectionButtonData>,
Expand All @@ -108,7 +108,7 @@ class JobMapComposables(private val getSubmissionCount: suspend (loi: LocationOf
newLoiJobs.clear()
newLoiJobs.addAll(addLoiJobs)
if (selectedLoi != null) {
submissionCount.intValue = getSubmissionCount(selectedLoi.loi)
submissionCount.intValue = selectedLoi.submissionCount
jobCardOpened.value = true
selectedFeatureListener(selectedLoi.loi.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class HomeScreenMapContainerViewModelTest : BaseHiltTest() {
fun `renders the job card when zoomed into LOI and clicked on`() = runWithTestDispatcher {
viewModel.onFeatureClicked(features = setOf(LOCATION_OF_INTEREST_FEATURE))
val pair = viewModel.processDataCollectionEntryPoints().first()
assertThat(pair.first).isEqualTo(SelectedLoiSheetData(LOCATION_OF_INTEREST))
assertThat(pair.first).isEqualTo(SelectedLoiSheetData(LOCATION_OF_INTEREST, 0))
assertThat(pair.second).isEqualTo(listOf(AdHocDataCollectionButtonData(ADHOC_JOB)))
}

Expand Down

0 comments on commit 648c8bc

Please sign in to comment.