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] Re-write Pager on top of LazyRow|Column #678

Merged
merged 72 commits into from Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
0fd2416
Experimenting with changing Pager to use LazyRow
chrisbanes Aug 19, 2021
bd38e05
Attempt to migrate some state over
chrisbanes Aug 19, 2021
b29d5d0
Add support for center/start/end alignment
chrisbanes Aug 20, 2021
0fea85a
Layout page + offsets calculations are working
chrisbanes Aug 20, 2021
1d0392b
Vaguely working scrolls/flings
chrisbanes Aug 20, 2021
d1e5f99
Try generalising the snapping fling
chrisbanes Aug 20, 2021
ee4e6b6
Allow snapOffset to be configurable
chrisbanes Aug 23, 2021
131f907
Extract snapping fling behavior
chrisbanes Aug 23, 2021
0dc2aa3
Add broken spring animation
chrisbanes Aug 23, 2021
1ba6721
Working snapping fling
chrisbanes Aug 24, 2021
e4c7489
Provide LazyItemScope to Pager content
chrisbanes Aug 24, 2021
bfe7a08
First attempt at getting the tests to pass
chrisbanes Aug 24, 2021
39b39ad
Update state after an animation
chrisbanes Aug 24, 2021
42077ea
Re-enable Robolectric Pager tests
chrisbanes Aug 24, 2021
b2fddcd
Lots of fixes
chrisbanes Aug 24, 2021
cdf4972
Add reverseLayout support
chrisbanes Aug 24, 2021
18514ec
Fixing some more tests
chrisbanes Aug 24, 2021
889f4d1
Remove inifite looping
chrisbanes Aug 24, 2021
245ddce
Use fillParentMax* in samples
chrisbanes Aug 25, 2021
0baf50c
Move away from using contentPadding
chrisbanes Aug 25, 2021
fe77a9f
Tidy up paging offsets
chrisbanes Aug 25, 2021
07b6c16
Finally all tests are passing!
chrisbanes Aug 25, 2021
829181e
Fix scrollToPage
chrisbanes Aug 25, 2021
6da48a4
Add a semi-working animateScrollToPage
chrisbanes Aug 25, 2021
2a2a043
Update API files
chrisbanes Aug 25, 2021
dec9f87
Remove dragEnabled parameter
chrisbanes Aug 25, 2021
758d97f
Fix nested scrolls getting to Pager
chrisbanes Aug 26, 2021
f64263f
Fix device tests
chrisbanes Aug 26, 2021
bd7df36
Add scrollOffset back to scrollToPage
chrisbanes Aug 26, 2021
f8bd852
Expose key parameter
chrisbanes Aug 26, 2021
ee606ab
Expose contentPadding parameter
chrisbanes Aug 26, 2021
2cbe157
Update docs
chrisbanes Aug 26, 2021
01f4892
More tidy-ups
chrisbanes Aug 26, 2021
56a202c
Remove LazyItemScope from PagerScope
chrisbanes Aug 27, 2021
2671e61
Add comment for use of ConsumeFlingNestedScrollConnection
chrisbanes Aug 27, 2021
7ffcf33
Tweak comment
chrisbanes Aug 27, 2021
ee2ec3a
Add comment about usage of BoxWithConstraints
chrisbanes Aug 27, 2021
1d22c48
Remove unnecessary call to onScrollFinished()
chrisbanes Aug 27, 2021
d9ff81e
Move count parameter
chrisbanes Aug 27, 2021
d4ec005
Fix HorizontalPager
chrisbanes Aug 27, 2021
56f2f18
Play around with using contentPadding to provide alignment
chrisbanes Aug 27, 2021
54c2c6d
Fix snapping point
chrisbanes Aug 29, 2021
6a16814
Update horizontal layout assertion
chrisbanes Aug 29, 2021
758ce3c
Update to Compose SNAPSHOT 7655479
chrisbanes Aug 29, 2021
19a97f6
Merge branch 'cb/snapshot-7655479' into cb/pager-lazy-contentpadding
chrisbanes Aug 29, 2021
b6dc4ed
Fix scrollToPage test
chrisbanes Aug 29, 2021
7a79250
Fix snapOffset used in page offset calculation
chrisbanes Aug 29, 2021
9474dcb
Fix calculateHorizontalPadding()
chrisbanes Aug 29, 2021
e10bf32
Fix swipeToEndAndBack test
chrisbanes Aug 29, 2021
0870e3f
Update Pager samples
chrisbanes Aug 29, 2021
bf297b2
Fix derivedStates reading themselves
chrisbanes Aug 29, 2021
cf39c70
Run spotless on tests
chrisbanes Aug 29, 2021
1867739
Some vertical test fixes
chrisbanes Aug 29, 2021
a6a2618
Remove BoxWithConstraints
chrisbanes Aug 29, 2021
da9f75f
Merge branch 'main' into cb/pager-lazy
chrisbanes Sep 8, 2021
f5ee829
Update vertical pager samples
chrisbanes Sep 8, 2021
fc4ad3e
Most tests are now passing!
chrisbanes Sep 9, 2021
9fd2489
Update API files
chrisbanes Sep 9, 2021
2a13c50
Fix compile error
chrisbanes Sep 9, 2021
91872dc
Merge branch 'main' into cb/pager-lazy
chrisbanes Sep 10, 2021
3764dd8
Add some clarifying comments
chrisbanes Sep 10, 2021
62c2574
Update our scrolling tests to use Dp based velocities
chrisbanes Sep 10, 2021
ce8a6c6
Hide SnappingFligBehavior for now
chrisbanes Sep 10, 2021
a2bf597
Add rememberPagerFlingConfig back (deprecated)
chrisbanes Sep 10, 2021
6d8e8ea
Add looping sample
chrisbanes Sep 10, 2021
76dfecc
Update docs and guide
chrisbanes Sep 11, 2021
d5a6e61
Remove unused code
chrisbanes Sep 11, 2021
1e9ddfb
Add some deprecated functions back in
chrisbanes Sep 11, 2021
4e45979
Add pageOffset back to animateScrollToPage
chrisbanes Sep 11, 2021
355d14e
List more doc differences
chrisbanes Sep 11, 2021
290d83a
Update requireCurrentPage
chrisbanes Sep 11, 2021
f7f0052
Merge branch 'main' into cb/pager-lazy
chrisbanes Sep 13, 2021
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
11 changes: 11 additions & 0 deletions sample/src/main/AndroidManifest.xml
Expand Up @@ -126,6 +126,17 @@
</intent-filter>
</activity>

<activity
android:name=".pager.HorizontalPagerLoopingSample"
android:label="@string/horiz_pager_title_looping"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.google.accompanist.sample.SAMPLE_CODE" />
</intent-filter>
</activity>


<activity
android:name=".pager.HorizontalPagerTabsSample"
android:label="@string/horiz_pager_title_tabs"
Expand Down
@@ -0,0 +1,110 @@
/*
* Copyright 2021 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.sample.pager

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.sample.AccompanistSampleTheme
import com.google.accompanist.sample.R

class HorizontalPagerLoopingSample : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
AccompanistSampleTheme {
Surface {
Sample()
}
}
}
}
}

@OptIn(ExperimentalPagerApi::class)
@Composable
private fun Sample() {
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.horiz_pager_title_looping)) },
backgroundColor = MaterialTheme.colors.surface,
)
},
modifier = Modifier.fillMaxSize()
) {
Column(Modifier.fillMaxSize()) {
// Display 10 items
val pageCount = 10

// We start the pager in the middle of the raw number of pages
val startIndex = Int.MAX_VALUE / 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be

val startIndex = (Int.MAX_VALUE / 2).let { it - it.floorMod(pageCount) }

to start from the first index

val pagerState = rememberPagerState(initialPage = startIndex)

HorizontalPager(
// Set the raw page count to a really large number
count = Int.MAX_VALUE,
state = pagerState,
// Add 32.dp horizontal padding to 'center' the pages
contentPadding = PaddingValues(horizontal = 32.dp),
// Add some horizontal spacing between items
itemSpacing = 4.dp,
modifier = Modifier
.weight(1f)
.fillMaxWidth()
) { index ->
// We calculate the page from the given index
val page = (index - startIndex).floorMod(pageCount)
PagerSampleItem(
page = page,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
)
}

ActionsRow(
pagerState = pagerState,
modifier = Modifier.align(Alignment.CenterHorizontally)
)
}
}
}

private fun Int.floorMod(other: Int): Int = when (other) {
0 -> this
else -> this - floorDiv(other) * other
}
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Expand Up @@ -26,6 +26,7 @@
<string name="horiz_pager_title_basics">Horizontal Pager: Basic</string>
<string name="horiz_pager_with_indicator_title">Horizontal Pager: Indicator</string>
<string name="horiz_pager_with_transition_title">Horizontal Pager: Transition</string>
<string name="horiz_pager_title_looping">Horizontal Pager: Looping</string>
<string name="horiz_pager_title_tabs">Horizontal Pager: Tabs</string>
<string name="horiz_pager_title_scroll_content">Horizontal Pager: Scrolling content</string>

Expand Down