diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/AbstractTaskFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/AbstractTaskFragment.kt index 171cfee1db..3cde535964 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/AbstractTaskFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/AbstractTaskFragment.kt @@ -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 @@ -91,6 +91,13 @@ abstract class AbstractTaskFragment : 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 @@ -100,6 +107,9 @@ abstract class AbstractTaskFragment : 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() diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragment.kt index 5b649309ed..c7c48ec257 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragment.kt @@ -96,7 +96,9 @@ class DrawAreaTaskFragment : AbstractTaskFragment() { viewLifecycleOwner.lifecycleScope.launch { viewModel.draftArea.collectLatest { onFeatureUpdated(it) } } + } + override fun onTaskVisibleToUser() { if (!viewModel.instructionsDialogShown) { showInstructionsDialog() } diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragmentTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragmentTest.kt index 00e35211ea..1c666f088c 100644 --- a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragmentTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/polygon/DrawAreaTaskFragmentTest.kt @@ -110,6 +110,7 @@ class DrawAreaTaskFragmentTest : @Test fun testDrawArea_incompleteWhenTaskIsOptional() = runWithTestDispatcher { setupTaskFragment(job, task.copy(isRequired = false)) + fragment.onTaskVisibleToUser() // Dismiss the instructions dialog ShadowDialog.getLatestDialog().dismiss() @@ -142,6 +143,7 @@ class DrawAreaTaskFragmentTest : @Test fun testDrawArea() = runWithTestDispatcher { setupTaskFragment(job, task.copy(isRequired = false)) + fragment.onTaskVisibleToUser() // Dismiss the instructions dialog ShadowDialog.getLatestDialog().dismiss() @@ -178,6 +180,7 @@ class DrawAreaTaskFragmentTest : @Test fun `Instructions dialog is shown`() = runWithTestDispatcher { setupTaskFragment(job, task) + fragment.onTaskVisibleToUser() assertThat(ShadowDialog.getLatestDialog()).isNotNull() }