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

[Pager Indicators] Fix: #768 #783

Merged
merged 5 commits into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,7 +49,7 @@ fun Modifier.pagerTabIndicatorOffset(
val targetIndicatorOffset: Dp
val indicatorWidth: Dp

val currentTab = tabPositions[pagerState.currentPage]
val currentTab = tabPositions[minOf(tabPositions.lastIndex, pagerState.currentPage)]
val targetPage = pagerState.targetPage
val targetTab = tabPositions.getOrNull(targetPage)

Expand Down
4 changes: 4 additions & 0 deletions pager/src/main/java/com/google/accompanist/pager/Pager.kt
Expand Up @@ -209,6 +209,10 @@ internal fun Pager(
(flingBehavior as? SnappingFlingBehavior)?.animationTarget
}

LaunchedEffect(count) {
state.currentPage = minOf(count - 1, state.currentPage).coerceAtLeast(0)
}

// Once a fling (scroll) has finished, notify the state
LaunchedEffect(state) {
// When a 'scroll' has finished, notify the state
Expand Down
Expand Up @@ -133,7 +133,7 @@ class PagerState(
@get:IntRange(from = 0)
var currentPage: Int
get() = _currentPage
private set(value) {
internal set(value) {
if (value != _currentPage) {
_currentPage = value
if (DebugLog) {
Expand Down
Expand Up @@ -17,11 +17,15 @@
package com.google.accompanist.pager

import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.junit4.StateRestorationTester
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Ignore
import org.junit.Rule
Expand Down Expand Up @@ -59,4 +63,45 @@ class PagerStateUnitTest {
assertThat(state.currentPage).isEqualTo(4)
assertThat(state.pageCount).isEqualTo(10)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun change_pager_count() {
val pagerState = PagerState()
var count by mutableStateOf(10)

composeTestRule.setContent {
HorizontalPager(count = count, state = pagerState) { page ->
BasicText(text = "Page:$page")
}
}
composeTestRule.waitForIdle()

// Now scroll to page 8
composeTestRule.runOnIdle {
runBlocking {
pagerState.scrollToPage(8)
}
}

// And assert that currentPage and pageCount are correct
assertThat(pagerState.currentPage).isEqualTo(8)
assertThat(pagerState.pageCount).isEqualTo(10)

// Change page count to 4
count = 4
composeTestRule.waitForIdle()

// And assert that currentPage and pageCount are correct
assertThat(pagerState.currentPage).isEqualTo(3)
assertThat(pagerState.pageCount).isEqualTo(4)

// Change page count to 0
count = 0
composeTestRule.waitForIdle()

// And assert that currentPage and pageCount are correct
assertThat(pagerState.currentPage).isEqualTo(0)
assertThat(pagerState.pageCount).isEqualTo(0)
}
}