From 69a44c8277802891097802a8499c113fa4bbc890 Mon Sep 17 00:00:00 2001 From: Sufyan Abbasi Date: Thu, 29 Feb 2024 19:04:52 -0800 Subject: [PATCH] Add a small delay after the page scroll is idle to calculate the progress bar position. --- .../datacollection/DataCollectionFragment.kt | 38 ++++++++++++++----- .../ground/ui/home/HomeScreenFragmentTest.kt | 11 +----- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt index 18c3e1b1c2..e2064725a0 100644 --- a/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt +++ b/ground/src/main/java/com/google/android/ground/ui/datacollection/DataCollectionFragment.kt @@ -17,11 +17,14 @@ package com.google.android.ground.ui.datacollection import android.animation.ValueAnimator import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ProgressBar import androidx.constraintlayout.widget.Guideline +import androidx.core.view.WindowInsetsCompat import androidx.hilt.navigation.fragment.hiltNavGraphViewModels import androidx.interpolator.view.animation.FastOutSlowInInterpolator import androidx.lifecycle.lifecycleScope @@ -33,13 +36,14 @@ import com.google.android.ground.ui.common.AbstractFragment import com.google.android.ground.ui.common.BackPressListener import com.google.android.ground.ui.common.Navigator import dagger.hilt.android.AndroidEntryPoint -import javax.inject.Inject import kotlinx.coroutines.launch +import javax.inject.Inject /** Fragment allowing the user to collect data to complete a task. */ @AndroidEntryPoint class DataCollectionFragment : AbstractFragment(), BackPressListener { @Inject lateinit var navigator: Navigator + @Inject lateinit var viewPagerAdapterFactory: DataCollectionViewPagerAdapterFactory private val viewModel: DataCollectionViewModel by hiltNavGraphViewModels(R.id.data_collection) @@ -76,20 +80,34 @@ class DataCollectionFragment : AbstractFragment(), BackPressListener { viewPager.registerOnPageChangeCallback( object : ViewPager2.OnPageChangeCallback() { - override fun onPageSelected(position: Int) { - super.onPageSelected(position) - - val buttonContainer = view.findViewById(R.id.action_buttons) ?: return - val anchorLocation = IntArray(2) - buttonContainer.getLocationInWindow(anchorLocation) - val guidelineTop = - anchorLocation[1] - buttonContainer.rootWindowInsets.systemWindowInsetTop - guideline.setGuidelineBegin(guidelineTop) + override fun onPageScrollStateChanged(state: Int) { + super.onPageScrollStateChanged(state) + if (state == ViewPager2.SCROLL_STATE_IDLE) { + Handler(Looper.getMainLooper()) + .postDelayed( + { + // Reset the progress bar position after a delay to wait for the keyboard to + // close. + setProgressBarPosition(view) + }, + 100 + ) + } } } ) } + private fun setProgressBarPosition(view: View) { + val buttonContainer = view.findViewById(R.id.action_buttons) ?: return + val anchorLocation = IntArray(2) + buttonContainer.getLocationInWindow(anchorLocation) + val windowInsets = WindowInsetsCompat.toWindowInsetsCompat(buttonContainer.rootWindowInsets) + val guidelineTop = + anchorLocation[1] - windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()).top + guideline.setGuidelineBegin(guidelineTop) + } + private fun loadTasks(tasks: List) { val currentAdapter = viewPager.adapter as? DataCollectionViewPagerAdapter if (currentAdapter == null || currentAdapter.tasks != tasks) { diff --git a/ground/src/test/java/com/google/android/ground/ui/home/HomeScreenFragmentTest.kt b/ground/src/test/java/com/google/android/ground/ui/home/HomeScreenFragmentTest.kt index 59cf986b8c..0cc155138d 100644 --- a/ground/src/test/java/com/google/android/ground/ui/home/HomeScreenFragmentTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/home/HomeScreenFragmentTest.kt @@ -39,7 +39,6 @@ import com.google.android.ground.ui.common.Navigator import com.sharedtest.FakeData import com.squareup.picasso.Picasso import dagger.hilt.android.testing.HiltAndroidTest -import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.advanceUntilIdle import org.hamcrest.CoreMatchers.not @@ -48,6 +47,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.ParameterizedRobolectricTestRunner import org.robolectric.RobolectricTestRunner +import javax.inject.Inject abstract class AbstractHomeScreenFragmentTest : BaseHiltTest() { @@ -131,15 +131,6 @@ class HomeScreenFragmentTest : AbstractHomeScreenFragmentTest() { mapOf(Pair(FakeData.USER.email, "data-collector")) ) - private val surveyWithTileSources: Survey = - surveyWithoutBasemap.copy( - tileSources = - listOf( - TileSource("http://google.com", TileSource.Type.MOG_COLLECTION), - ), - id = "SURVEY_WITH_TILE_SOURCES" - ) - @Test fun offlineMapImageryMenuIsDisabledWhenActiveSurveyHasNoBasemap() = runWithTestDispatcher { surveyRepository.selectedSurveyId = surveyWithoutBasemap.id