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

Hide "Collect data" button when the job has no more tasks to complete #2598

Merged
merged 16 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -54,6 +54,9 @@ data class Job(

/** Returns true if the job has one or more tasks. */
fun hasTasks() = tasks.values.isNotEmpty()

/** Returns whether the job has non-LOI tasks. */
fun hasNonLoiTasks() = tasks.values.count { !it.isAddLoiTask } > 0
}

fun Job.getDefaultColor(): Int =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,18 @@ internal constructor(
if (tasks.isEmpty()) {
error("Can't generate sequence for empty task list")
}

val task = tasks.filter { it.id == (startId ?: tasks[0].id) }

// TODO(#2539): Cleanup once https://github.com/google/ground-android/issues/2539 is resolved.
if (task.isEmpty()) {
error(
"Unable to find a task with id startId=$startId, firstTaskId=${tasks[0].id}, allTasks=${tasks.map { it.id }}"
)
}

val startIndex = tasks.indexOf(task.first())
val startIndex =
tasks
.indexOfFirst { it.id == (startId ?: tasks[0].id) }
sufyanAbbasi marked this conversation as resolved.
Show resolved Hide resolved
.let {
// NOTE(#2539) Fallback to the first task if startId is not found.
if (it < 0) {
Timber.w("startId, $startId, was not found. Defaulting to 0")
0
} else {
it
}
}
return if (reversed) {
tasks.subList(0, startIndex + 1).reversed()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
val canUserSubmitData = userRepository.canUserSubmitData()

// Handle collect button clicks
adapter.setCollectDataListener { onCollectData(canUserSubmitData, it) }
adapter.setCollectDataListener { onCollectData(canUserSubmitData, hasValidTasks(it), it) }

// Bind data for cards
mapContainerViewModel.getMapCardUiData().launchWhenStartedAndCollect { (mapCards, loiCount) ->
Expand All @@ -90,15 +90,32 @@ class HomeScreenMapContainerFragment : AbstractMapContainerFragment() {
map.featureClicks.launchWhenStartedAndCollect { mapContainerViewModel.onFeatureClicked(it) }
}

private fun hasValidTasks(cardUiData: MapCardUiData) =
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()
}

/** Invoked when user clicks on the map cards to collect data. */
private fun onCollectData(canUserSubmitData: Boolean, cardUiData: MapCardUiData) {
if (canUserSubmitData) {
navigateToDataCollectionFragment(cardUiData)
} else {
private fun onCollectData(
canUserSubmitData: Boolean,
hasTasks: Boolean,
cardUiData: MapCardUiData,
) {
if (!canUserSubmitData) {
// Skip data collection screen if the user can't submit any data
// TODO(#1667): Revisit UX for displaying view only mode
ephemeralPopups.ErrorPopup().show(getString(R.string.collect_data_viewer_error))
return
}
if (!hasTasks) {
// NOTE(#2539): The DataCollectionFragment will crash if there are no tasks.
ephemeralPopups.ErrorPopup().show(getString(R.string.no_tasks_error))
return
}
navigateToDataCollectionFragment(cardUiData)
}

/** Updates the given [TextView] with the submission count for the given [LocationOfInterest]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ class MapCardAdapter(
with(binding) {
loiName.text = loiHelper.getDisplayLoiName(loi)
jobName.text = loiHelper.getJobName(loi)
// NOTE(#2539): The DataCollectionFragment will crash if there are no non-LOI tasks.
collectData.visibility =
if (canUserSubmitData && loi.job.hasTasks()) View.VISIBLE else View.GONE
if (canUserSubmitData && loi.job.hasNonLoiTasks()) View.VISIBLE else View.GONE
updateSubmissionCount(loi, submissions)
}
}
Expand Down
1 change: 1 addition & 0 deletions ground/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<string name="drop_a_pin_tooltip_text">Drag your map until the center pin is on the desired location</string>
<string name="current_location">Current location:</string>
<string name="loading">Loading…</string>
<string name="no_tasks_error">This job has no more tasks to complete</string>
<string name="collect_data_viewer_error">Can’t collect data as user is a VIEWER</string>
<string name="offline_map_imagery">Offline map imagery</string>
<string name="offline_map_imagery_pref_description">Hide or show downloaded imagery</string>
Expand Down