Skip to content

Commit

Permalink
Endless PagedIsland
Browse files Browse the repository at this point in the history
  • Loading branch information
sadellie committed Feb 14, 2024
1 parent 4be1756 commit 8b21721
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import kotlinx.coroutines.launch
* [HorizontalPager] with a background and a page indicator.
*
* @param modifier [Modifier] that will be applied to the surround [Column].
* @param pagerState [PagerState] that will passed [HorizontalPager].
* @param pageCount Page count for [PagerState] that will passed [HorizontalPager].
* @param backgroundColor [Color] for background.
* @param pageIndicatorAlignment [Alignment.Horizontal] for page indicator.
* @param onClick Called on all clicks, even if the page didn't change.
Expand All @@ -58,7 +58,7 @@ import kotlinx.coroutines.launch
@Composable
fun PagedIsland(
modifier: Modifier = Modifier,
pagerState: PagerState,
pageCount: Int,
backgroundColor: Color = MaterialTheme.colorScheme.secondaryContainer,
pageIndicatorAlignment: Alignment.Horizontal = Alignment.End,
onClick: () -> Unit = {},
Expand All @@ -67,6 +67,8 @@ fun PagedIsland(
val contentColor = MaterialTheme.colorScheme.contentColorFor(backgroundColor)
val disabledContentColor = contentColor.copy(alpha = 0.5f)
val corScope = rememberCoroutineScope()
// https://stackoverflow.com/a/75469260
val pagerState = rememberPagerState { pageCount * 1_000 }

ProvideColor(color = contentColor) {
Column(
Expand All @@ -92,8 +94,8 @@ fun PagedIsland(
.padding(vertical = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp, pageIndicatorAlignment),
) {
repeat(pagerState.pageCount) {
PageDot(if (it == pagerState.currentPage) contentColor else disabledContentColor)
repeat(pageCount) {
PageDot(if (it == pagerState.currentPage % pageCount) contentColor else disabledContentColor)
}
}

Expand All @@ -102,10 +104,9 @@ fun PagedIsland(
.animateContentSize()
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
state = pagerState
) { page ->
pageContent(page)
}
state = pagerState,
pageContent = { page -> pageContent(page % pageCount) }
)
}
}
}
Expand All @@ -124,7 +125,7 @@ private fun PageDot(
private fun PreviewPagedIsland() {
PagedIsland(
modifier = Modifier.size(400.dp, 250.dp),
pagerState = rememberPagerState { 5 }
pageCount = 5,
) { currentPage ->
Column {
Text("Current page: $currentPage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package com.sadellie.unitto.feature.datecalculator.components
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
Expand All @@ -45,7 +44,7 @@ internal fun DateTimeResultBlock(

PagedIsland(
modifier = modifier,
pagerState = rememberPagerState { 6 },
pageCount = 6,
onClick = { focusManager.clearFocus() },
backgroundColor = MaterialTheme.colorScheme.tertiaryContainer
) { currentPage ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Architecture
Expand Down Expand Up @@ -128,7 +127,7 @@ fun FormattingScreen(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
pagerState = rememberPagerState { 2 },
pageCount = 2,
) { currentPage ->
val preview = when (currentPage) {
0 -> "123456.${"789123456".repeat(ceil(uiState.precision.toDouble() / 9.0).toInt())}"
Expand Down

0 comments on commit 8b21721

Please sign in to comment.