Skip to content

Commit

Permalink
Display the task only when the task fragment is actually visible to t…
Browse files Browse the repository at this point in the history
…he user (#2262)

Co-authored-by: Gino Miceli <228050+gino-m@users.noreply.github.com>
  • Loading branch information
shobhitagarwal1612 and gino-m authored Feb 28, 2024
1 parent ed2af89 commit 5184ee4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.google.android.ground.ui.datacollection.components.ButtonAction
import com.google.android.ground.ui.datacollection.components.TaskButton
import com.google.android.ground.ui.datacollection.components.TaskButtonFactory
import com.google.android.ground.ui.datacollection.components.TaskView
import java.util.*
import java.util.EnumMap
import kotlin.properties.Delegates
import kotlinx.coroutines.launch
import org.jetbrains.annotations.TestOnly
Expand Down Expand Up @@ -91,6 +91,13 @@ abstract class AbstractTaskFragment<T : AbstractTaskViewModel> : AbstractFragmen
}
}

override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible)
if (menuVisible) {
onTaskVisibleToUser()
}
}

/** Creates the view for common task template with/without header. */
abstract fun onCreateTaskView(inflater: LayoutInflater): TaskView

Expand All @@ -100,6 +107,9 @@ abstract class AbstractTaskFragment<T : AbstractTaskViewModel> : AbstractFragmen
/** Invoked after the task view gets attached to the fragment. */
open fun onTaskViewAttached() {}

/** Invoked when the task fragment is visible to the user. */
open fun onTaskVisibleToUser() {}

/** Invoked when the fragment is ready to add buttons to the current [TaskView]. */
open fun onCreateActionButtons() {
addSkipButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ class DrawAreaTaskFragment : AbstractTaskFragment<DrawAreaTaskViewModel>() {
viewLifecycleOwner.lifecycleScope.launch {
viewModel.draftArea.collectLatest { onFeatureUpdated(it) }
}
}

override fun onTaskVisibleToUser() {
if (!viewModel.instructionsDialogShown) {
showInstructionsDialog()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class DrawAreaTaskFragmentTest :
@Test
fun testDrawArea_incompleteWhenTaskIsOptional() = runWithTestDispatcher {
setupTaskFragment<DrawAreaTaskFragment>(job, task.copy(isRequired = false))
fragment.onTaskVisibleToUser()

// Dismiss the instructions dialog
ShadowDialog.getLatestDialog().dismiss()
Expand Down Expand Up @@ -142,6 +143,7 @@ class DrawAreaTaskFragmentTest :
@Test
fun testDrawArea() = runWithTestDispatcher {
setupTaskFragment<DrawAreaTaskFragment>(job, task.copy(isRequired = false))
fragment.onTaskVisibleToUser()

// Dismiss the instructions dialog
ShadowDialog.getLatestDialog().dismiss()
Expand Down Expand Up @@ -178,6 +180,7 @@ class DrawAreaTaskFragmentTest :
@Test
fun `Instructions dialog is shown`() = runWithTestDispatcher {
setupTaskFragment<DrawAreaTaskFragment>(job, task)
fragment.onTaskVisibleToUser()

assertThat(ShadowDialog.getLatestDialog()).isNotNull()
}
Expand Down

0 comments on commit 5184ee4

Please sign in to comment.