Skip to content

Commit

Permalink
[Pager Indicators] Update Indicators to also be usable with Foundatio…
Browse files Browse the repository at this point in the history
…n Pager (#1485)

* Update pager indicators to support foundation PagerState

* Change FoundationTabIndicatorTest name to TabIndicatorWithFoundationPagerTest.

* Clean up formatting

* Update API docs

* Apply code review feedback

* Apply code review feedback - second round
  • Loading branch information
Levi-Moreira authored Jan 19, 2023
1 parent 1a7346f commit c7306a3
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pager-indicators/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.google.accompanist.pager {

public final class PagerIndicatorKt {
method @androidx.compose.runtime.Composable @com.google.accompanist.pager.ExperimentalPagerApi public static void HorizontalPagerIndicator(com.google.accompanist.pager.PagerState pagerState, optional androidx.compose.ui.Modifier modifier, optional int pageCount, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping, optional long activeColor, optional long inactiveColor, optional float indicatorWidth, optional float indicatorHeight, optional float spacing, optional androidx.compose.ui.graphics.Shape indicatorShape);
method @androidx.compose.runtime.Composable @com.google.accompanist.pager.ExperimentalPagerApi public static void HorizontalPagerIndicator(androidx.compose.foundation.pager.PagerState pagerState, int pageCount, optional androidx.compose.ui.Modifier modifier, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping, optional long activeColor, optional long inactiveColor, optional float indicatorWidth, optional float indicatorHeight, optional float spacing, optional androidx.compose.ui.graphics.Shape indicatorShape);
method @androidx.compose.runtime.Composable @com.google.accompanist.pager.ExperimentalPagerApi public static void VerticalPagerIndicator(com.google.accompanist.pager.PagerState pagerState, optional androidx.compose.ui.Modifier modifier, optional int pageCount, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping, optional long activeColor, optional long inactiveColor, optional float indicatorHeight, optional float indicatorWidth, optional float spacing, optional androidx.compose.ui.graphics.Shape indicatorShape);
method @androidx.compose.runtime.Composable @com.google.accompanist.pager.ExperimentalPagerApi public static void VerticalPagerIndicator(androidx.compose.foundation.pager.PagerState pagerState, int pageCount, optional androidx.compose.ui.Modifier modifier, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping, optional long activeColor, optional long inactiveColor, optional float indicatorHeight, optional float indicatorWidth, optional float spacing, optional androidx.compose.ui.graphics.Shape indicatorShape);
}

public final class PagerTabKt {
method @com.google.accompanist.pager.ExperimentalPagerApi public static androidx.compose.ui.Modifier pagerTabIndicatorOffset(androidx.compose.ui.Modifier, com.google.accompanist.pager.PagerState pagerState, java.util.List<androidx.compose.material.TabPosition> tabPositions, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping);
method @com.google.accompanist.pager.ExperimentalPagerApi public static androidx.compose.ui.Modifier pagerTabIndicatorOffset(androidx.compose.ui.Modifier, androidx.compose.foundation.pager.PagerState pagerState, java.util.List<androidx.compose.material.TabPosition> tabPositions, optional kotlin.jvm.functions.Function1<? super java.lang.Integer,java.lang.Integer> pageIndexMapping);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.accompanist.pager

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ScrollableTabRow
import androidx.compose.material.Tab
import androidx.compose.material.TabRowDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.getBoundsInRoot
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.height
import androidx.compose.ui.unit.lerp
import androidx.compose.ui.unit.times
import androidx.compose.ui.unit.width
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import androidx.compose.foundation.pager.HorizontalPager as FoundationHorizontalPager
import androidx.compose.foundation.pager.PagerState as FoundationPagerState
import androidx.compose.foundation.pager.rememberPagerState as rememberFoundationPagerState

@OptIn(ExperimentalFoundationApi::class)
@RunWith(AndroidJUnit4::class)
class TabIndicatorWithFoundationPagerTest {
@get:Rule
val rule = createComposeRule()

private val IndicatorTag = "indicator"
private val TabRowTag = "TabRow"

@Test
fun emptyPager() {
rule.setContent {
val pagerState = rememberFoundationPagerState()
TabRow(pagerState, 0)
}
}

@Test
fun scrollOffsetIsPositive() {
lateinit var pagerState: FoundationPagerState
val pageCount = 4
rule.setContent {
pagerState = rememberFoundationPagerState()
Column {
TabRow(pagerState, pageCount)
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) {
Box(Modifier.fillMaxSize())
}
}
}

rule.runOnIdle {
runBlocking { pagerState.scrollToPage(1, 0.25f) }
}

val tab1Bounds = rule.onNodeWithTag("1").getBoundsInRoot()
val tab2Bounds = rule.onNodeWithTag("2").getBoundsInRoot()
val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot()

with(rule.density) {
assertThat(indicatorBounds.left.roundToPx())
.isEqualTo(lerp(tab1Bounds.left, tab2Bounds.left, 0.25f).roundToPx())
assertThat(indicatorBounds.width.roundToPx())
.isEqualTo(lerp(tab1Bounds.width, tab2Bounds.width, 0.25f).roundToPx())
}
}

@Test
fun scrollOffsetIsNegative() {
lateinit var pagerState: FoundationPagerState
val pageCount = 4
rule.setContent {
pagerState = rememberFoundationPagerState()
Column {
TabRow(pagerState, pageCount)
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) {
Box(Modifier.fillMaxSize())
}
}
}

rule.runOnIdle {
runBlocking { pagerState.scrollToPage(1, -0.25f) }
}

val tab1Bounds = rule.onNodeWithTag("1").getBoundsInRoot()
val tab0Bounds = rule.onNodeWithTag("0").getBoundsInRoot()
val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot()

with(rule.density) {
assertThat(indicatorBounds.left.roundToPx())
.isEqualTo(lerp(tab1Bounds.left, tab0Bounds.left, 0.25f).roundToPx())
assertThat(indicatorBounds.width.roundToPx())
.isEqualTo(lerp(tab1Bounds.width, tab0Bounds.width, 0.25f).roundToPx())
}
}

@Test
fun indicatorIsAtBottom() {
lateinit var pagerState: FoundationPagerState
val pageCount = 4
rule.setContent {
pagerState = rememberFoundationPagerState()
Column {
TabRow(pagerState, pageCount)
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) {
Box(Modifier.fillMaxSize())
}
}
}

rule.runOnIdle {
runBlocking { pagerState.scrollToPage(1, 0.25f) }
}

val tabRowBounds = rule.onNodeWithTag(TabRowTag).getBoundsInRoot()

val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot()

with(rule.density) {
assertThat(indicatorBounds.height.roundToPx()).isEqualTo(2.dp.roundToPx())
assertThat(indicatorBounds.bottom).isEqualTo(tabRowBounds.bottom)
}
}

@OptIn(ExperimentalPagerApi::class)
@Composable
private fun TabRow(pagerState: FoundationPagerState, pageCount: Int) {
ScrollableTabRow(
selectedTabIndex = pagerState.currentPage,
indicator = { tabPositions ->
TabRowDefaults.Indicator(
Modifier
.pagerTabIndicatorOffset(pagerState, tabPositions)
.testTag(IndicatorTag),
height = 2.dp
)
},
modifier = Modifier.testTag(TabRowTag)
) {
// Add tabs for all of our pages
(0 until pageCount).forEach { index ->
Tab(
text = { Text("Tab $index", Modifier.padding(horizontal = index * 5.dp)) },
selected = pagerState.currentPage == index,
modifier = Modifier.testTag("$index"),
onClick = {}
)
}
}
}
}
Loading

0 comments on commit c7306a3

Please sign in to comment.