Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out job cards into Composables for new data entry points #2892

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class SurveyRunnerTest : AutomatorRunner {
private fun zoomIntoLocation() {
clickLocationLock()
allowPermissions()
val loiCardSelector = byClass(CardView::class).hasDescendant(byText(R.string.collect_data))
val loiCardSelector = byClass(CardView::class).hasDescendant(byText(R.string.add_data))
if (device.wait(Until.hasObject(loiCardSelector), LONG_TIMEOUT) == null) {
captureScreenshot()
fail("Failed to zoom in to location.")
Expand All @@ -130,8 +130,8 @@ class SurveyRunnerTest : AutomatorRunner {
val cards = device.findObjects(loiCardSelector)
cards.forEach { it.swipe(Direction.LEFT, 1F) }
val loiCollectDataButtonSelector =
byText(R.string.collect_data)
.hasAncestor(loiCardSelector.hasDescendant(byText(R.string.new_site)))
byText(R.string.add_data)
.hasAncestor(loiCardSelector.hasDescendant(byText(R.string.add_site)))
if (!waitClickGone(loiCollectDataButtonSelector)) {
captureScreenshot()
fail("Failed to start ad-hoc loi data collection.")
Expand All @@ -145,7 +145,7 @@ class SurveyRunnerTest : AutomatorRunner {
fail("Failed to find predefined loi card")
}
// Assume that the first card is the predefined LOI.
val loiCollectDataButtonSelector = byText(R.string.collect_data)
val loiCollectDataButtonSelector = byText(R.string.add_data)
if (!waitClickGone(loiCollectDataButtonSelector)) {
captureScreenshot()
fail("Failed to start predefined loi data collection.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SnapHelper
import com.google.android.ground.R
import com.google.android.ground.coroutines.ApplicationScope
import com.google.android.ground.coroutines.IoDispatcher
import com.google.android.ground.coroutines.MainDispatcher
import com.google.android.ground.databinding.BasemapLayoutBinding
import com.google.android.ground.databinding.LoiCardsRecyclerViewBinding
import com.google.android.ground.databinding.MenuButtonBinding
import com.google.android.ground.model.locationofinterest.LOI_NAME_PROPERTY
import com.google.android.ground.model.locationofinterest.LocationOfInterest
import com.google.android.ground.proto.Survey.DataSharingTerms
import com.google.android.ground.repository.SubmissionRepository
import com.google.android.ground.repository.UserRepository
Expand All @@ -47,8 +40,8 @@
import com.google.android.ground.ui.home.DataSharingTermsDialog
import com.google.android.ground.ui.home.HomeScreenFragmentDirections
import com.google.android.ground.ui.home.HomeScreenViewModel
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.home.mapcontainer.jobs.JobMapAdapter
import com.google.android.ground.ui.home.mapcontainer.jobs.MapUiData
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -59,7 +52,7 @@
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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 @@ -76,54 +69,55 @@
private lateinit var mapContainerViewModel: HomeScreenMapContainerViewModel
private lateinit var homeScreenViewModel: HomeScreenViewModel
private lateinit var binding: BasemapLayoutBinding
private lateinit var adapter: MapCardAdapter
private lateinit var adapter: JobMapAdapter
private lateinit var infoPopup: EphemeralPopups.InfoPopup

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mapContainerViewModel = getViewModel(HomeScreenMapContainerViewModel::class.java)
homeScreenViewModel = getViewModel(HomeScreenViewModel::class.java)
adapter = MapCardAdapter { loi, view -> updateSubmissionCount(loi, view) }
adapter = JobMapAdapter { loi -> submissionRepository.getTotalSubmissionCount(loi) }

launchWhenStarted {
Timber.v("GOT HERE: launchWhenStarted")
sufyanAbbasi marked this conversation as resolved.
Show resolved Hide resolved
val canUserSubmitData = userRepository.canUserSubmitData()

// Handle collect button clicks
adapter.setCollectDataListener { mapCardUiData ->
adapter.setCollectDataListener { mapUiData ->
Timber.v("GOT HERE: setCollectDataListener")

Check warning on line 87 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt#L87

Added line #L87 was not covered by tests
sufyanAbbasi marked this conversation as resolved.
Show resolved Hide resolved
val job =
lifecycleScope.launch {
mapContainerViewModel.activeSurveyDataSharingTermsFlow.cancellable().collectLatest {
hasDataSharingTerms ->
onCollectData(
canUserSubmitData,
hasValidTasks(mapCardUiData),
hasValidTasks(mapUiData),

Check warning on line 94 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt#L94

Added line #L94 was not covered by tests
hasDataSharingTerms,
mapCardUiData,
mapUiData,

Check warning on line 96 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerFragment.kt#L96

Added line #L96 was not covered by tests
)
}
}
job.cancel()
}

// Bind data for cards
mapContainerViewModel.getMapCardUiData().launchWhenStartedAndCollect { (mapCards, loiCount) ->
adapter.updateData(canUserSubmitData, mapCards, loiCount - 1)
mapContainerViewModel.getMapCardUiData().launchWhenStartedAndCollect { (loiCard, jobCards) ->
sufyanAbbasi marked this conversation as resolved.
Show resolved Hide resolved
runBlocking { adapter.updateData(canUserSubmitData, loiCard, jobCards) }
}
}

map.featureClicks.launchWhenStartedAndCollect { mapContainerViewModel.onFeatureClicked(it) }
}

private fun hasValidTasks(cardUiData: MapCardUiData) =
private fun hasValidTasks(cardUiData: MapUiData) =
when (cardUiData) {
// LOI tasks are filtered out of the tasks list for pre-defined tasks.
is MapCardUiData.LoiCardUiData ->
cardUiData.loi.job.tasks.values.count { !it.isAddLoiTask } > 0
is MapCardUiData.AddLoiCardUiData -> cardUiData.job.tasks.values.isNotEmpty()
is MapUiData.LoiUiData -> cardUiData.loi.job.tasks.values.count { !it.isAddLoiTask } > 0
is MapUiData.AddLoiUiData -> cardUiData.job.tasks.values.isNotEmpty()
}

private fun renderDataSharingTermsDialog(
cardUiData: MapCardUiData,
cardUiData: MapUiData,
dataSharingTerms: DataSharingTerms,
) =
ComposeView(requireContext()).apply {
Expand All @@ -149,7 +143,7 @@
canUserSubmitData: Boolean,
hasTasks: Boolean,
hasDataSharingTerms: DataSharingTerms?,
cardUiData: MapCardUiData,
cardUiData: MapUiData,
) {
if (!canUserSubmitData) {
// Skip data collection screen if the user can't submit any data
Expand All @@ -176,17 +170,6 @@
navigateToDataCollectionFragment(cardUiData)
}

/** Updates the given [TextView] with the submission count for the given [LocationOfInterest]. */
private fun updateSubmissionCount(loi: LocationOfInterest, view: TextView) {
externalScope.launch {
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 }
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -202,8 +185,8 @@

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupMenuFab()
setupBottomLoiCards()
val menuBinding = setupMenuFab()
setupBottomLoiCards(menuBinding)
showDataCollectionHint()
}

Expand Down Expand Up @@ -248,54 +231,23 @@
}
}

private fun setupMenuFab() {
private fun setupMenuFab(): MenuButtonBinding {
val mapOverlay = binding.overlay
val menuBinding = MenuButtonBinding.inflate(layoutInflater, mapOverlay, true)
menuBinding.homeScreenViewModel = homeScreenViewModel
menuBinding.lifecycleOwner = this
return menuBinding
}

private fun setupBottomLoiCards() {
val container = binding.bottomContainer
val recyclerViewBinding = LoiCardsRecyclerViewBinding.inflate(layoutInflater, container, true)
val recyclerView = recyclerViewBinding.recyclerView
recyclerView.adapter = adapter
recyclerView.addOnScrollListener(
object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstVisiblePosition = layoutManager.findFirstVisibleItemPosition()
val lastVisiblePosition = layoutManager.findLastVisibleItemPosition()
val firstCompletelyVisiblePosition =
layoutManager.findFirstCompletelyVisibleItemPosition()
var midPosition = (firstVisiblePosition + lastVisiblePosition) / 2

// Focus the last card
if (firstCompletelyVisiblePosition > midPosition) {
midPosition = firstCompletelyVisiblePosition
}

adapter.focusItemAtIndex(midPosition)
}
}
)

val helper: SnapHelper = PagerSnapHelper()
helper.attachToRecyclerView(recyclerView)

mapContainerViewModel.loiClicks.launchWhenStartedAndCollect {
val index = it?.let { adapter.getIndex(it) } ?: -1
if (index != -1) {
recyclerView.scrollToPosition(index)
adapter.focusItemAtIndex(index)
}
}
private fun setupBottomLoiCards(menuBinding: MenuButtonBinding) {
adapter.basemapLayoutBinding = binding
adapter.menuBinding = menuBinding
adapter.render()
}

private fun navigateToDataCollectionFragment(cardUiData: MapCardUiData) {
private fun navigateToDataCollectionFragment(cardUiData: MapUiData) {
when (cardUiData) {
is MapCardUiData.LoiCardUiData ->
is MapUiData.LoiUiData ->
findNavController()
.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
Expand All @@ -307,7 +259,7 @@
"",
)
)
is MapCardUiData.AddLoiCardUiData ->
is MapUiData.AddLoiUiData ->
findNavController()
.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
Expand All @@ -325,13 +277,7 @@
override fun onMapReady(map: MapFragment) {
mapContainerViewModel.mapLoiFeatures.launchWhenStartedAndCollect { map.setFeatures(it) }

adapter.setLoiCardFocusedListener {
when (it) {
is MapCardUiData.LoiCardUiData -> mapContainerViewModel.selectLocationOfInterest(it.loi.id)
is MapCardUiData.AddLoiCardUiData,
null -> mapContainerViewModel.selectLocationOfInterest(null)
}
}
adapter.setSelectedFeature { mapContainerViewModel.selectLocationOfInterest(it) }
}

override fun getMapViewModel(): BaseMapViewModel = mapContainerViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.google.android.ground.system.SettingsManager
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.common.SharedViewModel
import com.google.android.ground.ui.home.mapcontainer.cards.MapCardUiData
import com.google.android.ground.ui.home.mapcontainer.jobs.MapUiData
import com.google.android.ground.ui.map.Feature
import com.google.android.ground.ui.map.FeatureType
import com.google.android.ground.ui.map.isLocationOfInterest
Expand Down Expand Up @@ -110,8 +110,8 @@
*/
private val loisInViewport: StateFlow<List<LocationOfInterest>>

/** [LocationOfInterest] clicked by the user. */
val loiClicks: MutableStateFlow<LocationOfInterest?> = MutableStateFlow(null)
/** [Feature] clicked by the user. */
val featureClicked: MutableStateFlow<Feature?> = MutableStateFlow(null)

/**
* List of [Job]s which allow LOIs to be added during field collection, populated only when zoomed
Expand Down Expand Up @@ -181,15 +181,22 @@
}

/**
* Returns a flow of [MapCardUiData] associated with the active survey's LOIs and adhoc jobs for
* Returns a flow of [MapUiData] associated with the active survey's LOIs and adhoc jobs for
* displaying the cards.
*/
fun getMapCardUiData(): Flow<Pair<List<MapCardUiData>, Int>> =
loisInViewport.combine(adHocLoiJobs) { lois, jobs ->
val loiCards = lois.map { MapCardUiData.LoiCardUiData(it) }
val jobCards = jobs.map { MapCardUiData.AddLoiCardUiData(it) }

Pair(loiCards + jobCards, lois.size)
fun getMapCardUiData(): Flow<Pair<MapUiData.LoiUiData?, List<MapUiData.AddLoiUiData>>> =
combine(loisInViewport, featureClicked, adHocLoiJobs) { loisInView, feature, jobs ->
val loiCard =
loisInView

Check warning on line 190 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L189-L190

Added lines #L189 - L190 were not covered by tests
.filter { it.geometry == feature?.geometry }
.firstOrNull()

Check warning on line 192 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L192

Added line #L192 was not covered by tests
?.let { MapUiData.LoiUiData(it) }
if (loiCard == null && feature != null) {
// The feature is not in view anymore.
featureClicked.value = null

Check warning on line 196 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L196

Added line #L196 was not covered by tests
}
val jobCard = jobs.map { MapUiData.AddLoiUiData(it) }
Pair(loiCard, jobCard)

Check warning on line 199 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L198-L199

Added lines #L198 - L199 were not covered by tests
}

private fun updatedLoiSelectedStates(
Expand All @@ -207,12 +214,7 @@
* list of provided features is empty.
*/
fun onFeatureClicked(features: Set<Feature>) {
val geometry = features.map { it.geometry }.minByOrNull { it.area } ?: return
for (loi in loisInViewport.value) {
if (loi.geometry == geometry) {
loiClicks.value = loi
}
}
featureClicked.value = features.minByOrNull { it.geometry.area }

Check warning on line 217 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L217

Added line #L217 was not covered by tests
}

suspend fun updateDataSharingConsent(dataSharingTerms: Boolean) {
Expand Down Expand Up @@ -245,5 +247,8 @@

fun selectLocationOfInterest(id: String?) {
selectedLoiIdFlow.value = id
if (id == null) {
featureClicked.value = null

Check warning on line 251 in ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L251

Added line #L251 was not covered by tests
}
}
}
Loading
Loading