Refactor prev and next buttons

This commit is contained in:
andrew 2023-07-03 17:41:30 +09:30
parent db4ff94084
commit 1d29b5465f

View File

@ -337,8 +337,10 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() {
Column(verticalArrangement = Arrangement.spacedBy(16.dp)) { Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
Row { Row {
if (imageAttachments.size >= 2) PrevButton(pagerState, modifier = Modifier.align(Alignment.CenterVertically)) CarouselButtonOrSpace(
else Spacer(modifier = Modifier.width(32.dp)) Direction.PREVIOUS,
pagerState,
modifier = Modifier.align(Alignment.CenterVertically))
Box(modifier = Modifier.weight(1f)) { Box(modifier = Modifier.weight(1f)) {
CellNoMargin { CellNoMargin {
HorizontalPager(state = pagerState) { i -> HorizontalPager(state = pagerState) { i ->
@ -360,24 +362,39 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() {
) )
} }
} }
if (imageAttachments.size >= 2) NextButton(pagerState, modifier = Modifier.align(Alignment.CenterVertically)) CarouselButtonOrSpace(
else Spacer(modifier = Modifier.width(32.dp)) Direction.NEXT,
pagerState,
modifier = Modifier.align(Alignment.CenterVertically)
)
} }
FileDetails(attachments, pagerState) FileDetails(attachments, pagerState)
} }
} }
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable enum class Direction constructor(
fun PrevButton(pagerState: PagerState, modifier: Modifier = Modifier) { val enabled: (PagerState) -> Boolean,
CarouselButton(pagerState, modifier = modifier, enabled = pagerState.canScrollBackward, id = R.drawable.ic_prev, delta = -1) @DrawableRes val id: Int,
val delta: Int
) {
PREVIOUS(
PagerState::canScrollBackward,
R.drawable.ic_prev,
-1
),
NEXT(
PagerState::canScrollForward,
R.drawable.ic_next,
1
)
} }
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun NextButton(pagerState: PagerState, modifier: Modifier = Modifier) { fun CarouselButtonOrSpace(direction: Direction, pagerState: PagerState, modifier: Modifier = Modifier) {
CarouselButton(pagerState, modifier = modifier, enabled = pagerState.canScrollForward, id = R.drawable.ic_next, delta = 1) if (pagerState.pageCount >= 2) CarouselButton(pagerState, modifier = modifier, direction = direction)
else Spacer(modifier = Modifier.width(32.dp))
} }
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@ -385,9 +402,7 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() {
fun CarouselButton( fun CarouselButton(
pagerState: PagerState, pagerState: PagerState,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
enabled: Boolean, direction: Direction
@DrawableRes id: Int,
delta: Int
) { ) {
val animationScope = rememberCoroutineScope() val animationScope = rememberCoroutineScope()
pagerState.apply { pagerState.apply {
@ -395,10 +410,10 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() {
modifier = Modifier modifier = Modifier
.width(40.dp) .width(40.dp)
.then(modifier), .then(modifier),
enabled = enabled, enabled = direction.enabled(pagerState),
onClick = { animationScope.launch { animateScrollToPage(currentPage + delta) } }) { onClick = { animationScope.launch { animateScrollToPage(currentPage + direction.delta) } }) {
Icon( Icon(
painter = painterResource(id = id), painter = painterResource(id = direction.id),
contentDescription = "", contentDescription = "",
) )
} }