Skip to content

Commit

Permalink
Add a small delay after the page scroll is idle to calculate the prog…
Browse files Browse the repository at this point in the history
…ress bar position.
  • Loading branch information
sufyanAbbasi committed Mar 1, 2024
1 parent 3dcff36 commit 69a44c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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<View>(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<View>(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<Task>) {
val currentAdapter = viewPager.adapter as? DataCollectionViewPagerAdapter
if (currentAdapter == null || currentAdapter.tasks != tasks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 69a44c8

Please sign in to comment.