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 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1663746
Add new LOI flow (unstyled)
sufyanAbbasi Nov 15, 2024
f8654dc
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Dec 2, 2024
f5fec71
Refactor out cards
sufyanAbbasi Dec 7, 2024
5858af2
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Dec 7, 2024
5aa9496
Refactor cards into Composables.
sufyanAbbasi Dec 19, 2024
4d0d5e6
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Dec 19, 2024
34dc21e
Formatting.
sufyanAbbasi Dec 19, 2024
5c420e8
Address `checkCode` issues.
sufyanAbbasi Dec 19, 2024
55393d8
Merge branch 'master' into sufy/2446/new-loi-flow
gino-m Dec 19, 2024
7e66573
Remove debug logs.
sufyanAbbasi Dec 19, 2024
2581739
Merge branch 'sufy/2446/new-loi-flow' of https://github.com/google/gr…
sufyanAbbasi Dec 19, 2024
6630320
Rename MapUIData and fields.
sufyanAbbasi Dec 19, 2024
f91494f
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Dec 19, 2024
ac03e37
Update strings.xml
jo-spek Dec 20, 2024
a6a5838
Update strings.xml
jo-spek Dec 20, 2024
b0eb253
Update strings.xml
jo-spek Dec 20, 2024
cce58a5
Update strings.xml
jo-spek Dec 20, 2024
da4a801
Update strings.xml
jo-spek Dec 20, 2024
d28e429
Update strings.xml
jo-spek Dec 20, 2024
0e30070
Update strings.xml
jo-spek Dec 20, 2024
fddb470
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Jan 16, 2025
6a64f25
Add missing import
sufyanAbbasi Jan 24, 2025
4466139
Merge branch 'sufy/2446/new-loi-flow' of https://github.com/google/gr…
sufyanAbbasi Jan 24, 2025
2705df0
Formatting
sufyanAbbasi Jan 24, 2025
818e354
Merge branch 'master' into sufy/2446/new-loi-flow
sufyanAbbasi Jan 24, 2025
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 @@ -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.DataCollectionEntryPointData
import com.google.android.ground.ui.home.mapcontainer.jobs.JobMapAdapter
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 {
val canUserSubmitData = userRepository.canUserSubmitData()

// Handle collect button clicks
adapter.setCollectDataListener { mapCardUiData ->
adapter.setCollectDataListener { mapUiData ->
val job =
lifecycleScope.launch {
mapContainerViewModel.activeSurveyDataSharingTermsFlow.cancellable().collectLatest {
hasDataSharingTerms ->
onCollectData(
canUserSubmitData,
hasValidTasks(mapCardUiData),
hasValidTasks(mapUiData),

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L94 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) ->
runBlocking { adapter.updateData(canUserSubmitData, loiCard, jobCards) }
}
}

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L115 was not covered by tests
cardUiData.job.tasks.values.isNotEmpty()
}

private fun renderDataSharingTermsDialog(
cardUiData: MapCardUiData,
cardUiData: DataCollectionEntryPointData,
dataSharingTerms: DataSharingTerms,
) =
ComposeView(requireContext()).apply {
Expand All @@ -149,7 +143,7 @@
canUserSubmitData: Boolean,
hasTasks: Boolean,
hasDataSharingTerms: DataSharingTerms?,
cardUiData: MapCardUiData,
cardUiData: DataCollectionEntryPointData,
) {
if (!canUserSubmitData) {
// Skip data collection screen if the user can't submit any data
Expand Down Expand Up @@ -177,17 +171,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 @@ -203,8 +186,8 @@

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

Expand Down Expand Up @@ -249,54 +232,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: DataCollectionEntryPointData) {
when (cardUiData) {
is MapCardUiData.LoiCardUiData ->
is DataCollectionEntryPointData.SelectedLoiSheetData ->
findNavController()
.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
Expand All @@ -308,7 +260,7 @@
"",
)
)
is MapCardUiData.AddLoiCardUiData ->
is DataCollectionEntryPointData.AdHocDataCollectionButtonData ->
findNavController()
.navigate(
HomeScreenFragmentDirections.actionHomeScreenFragmentToDataCollectionFragment(
Expand All @@ -326,13 +278,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.DataCollectionEntryPointData
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 @@ -178,15 +178,28 @@
}

/**
* Returns a flow of [MapCardUiData] associated with the active survey's LOIs and adhoc jobs for
* displaying the cards.
* Returns a flow of [DataCollectionEntryPointData] 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<
DataCollectionEntryPointData.SelectedLoiSheetData?,
List<DataCollectionEntryPointData.AdHocDataCollectionButtonData>,
>
> =
combine(loisInViewport, featureClicked, adHocLoiJobs) { loisInView, feature, jobs ->
val loiCard =
loisInView

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L199 was not covered by tests
}
val jobCard = jobs.map { DataCollectionEntryPointData.AdHocDataCollectionButtonData(it) }
Pair(loiCard, jobCard)

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

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt#L201-L202

Added lines #L201 - L202 were not covered by tests
}

private fun updatedLoiSelectedStates(
Expand All @@ -204,12 +217,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 220 in app/src/main/java/com/google/android/ground/ui/home/mapcontainer/HomeScreenMapContainerViewModel.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L220 was not covered by tests
}

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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