From 1d29b5465f2b14c1dfb82e2e865f040aab640f1b Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 3 Jul 2023 17:41:30 +0930 Subject: [PATCH] Refactor prev and next buttons --- .../conversation/v2/MessageDetailActivity.kt | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt index 1c9997854f..9836ef4861 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt @@ -337,8 +337,10 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { Column(verticalArrangement = Arrangement.spacedBy(16.dp)) { Row { - if (imageAttachments.size >= 2) PrevButton(pagerState, modifier = Modifier.align(Alignment.CenterVertically)) - else Spacer(modifier = Modifier.width(32.dp)) + CarouselButtonOrSpace( + Direction.PREVIOUS, + pagerState, + modifier = Modifier.align(Alignment.CenterVertically)) Box(modifier = Modifier.weight(1f)) { CellNoMargin { HorizontalPager(state = pagerState) { i -> @@ -360,24 +362,39 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { ) } } - if (imageAttachments.size >= 2) NextButton(pagerState, modifier = Modifier.align(Alignment.CenterVertically)) - else Spacer(modifier = Modifier.width(32.dp)) + CarouselButtonOrSpace( + Direction.NEXT, + pagerState, + modifier = Modifier.align(Alignment.CenterVertically) + ) } - FileDetails(attachments, pagerState) } } @OptIn(ExperimentalFoundationApi::class) - @Composable - fun PrevButton(pagerState: PagerState, modifier: Modifier = Modifier) { - CarouselButton(pagerState, modifier = modifier, enabled = pagerState.canScrollBackward, id = R.drawable.ic_prev, delta = -1) + enum class Direction constructor( + val enabled: (PagerState) -> Boolean, + @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) @Composable - fun NextButton(pagerState: PagerState, modifier: Modifier = Modifier) { - CarouselButton(pagerState, modifier = modifier, enabled = pagerState.canScrollForward, id = R.drawable.ic_next, delta = 1) + fun CarouselButtonOrSpace(direction: Direction, pagerState: PagerState, modifier: Modifier = Modifier) { + if (pagerState.pageCount >= 2) CarouselButton(pagerState, modifier = modifier, direction = direction) + else Spacer(modifier = Modifier.width(32.dp)) } @OptIn(ExperimentalFoundationApi::class) @@ -385,9 +402,7 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { fun CarouselButton( pagerState: PagerState, modifier: Modifier = Modifier, - enabled: Boolean, - @DrawableRes id: Int, - delta: Int + direction: Direction ) { val animationScope = rememberCoroutineScope() pagerState.apply { @@ -395,10 +410,10 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { modifier = Modifier .width(40.dp) .then(modifier), - enabled = enabled, - onClick = { animationScope.launch { animateScrollToPage(currentPage + delta) } }) { + enabled = direction.enabled(pagerState), + onClick = { animationScope.launch { animateScrollToPage(currentPage + direction.delta) } }) { Icon( - painter = painterResource(id = id), + painter = painterResource(id = direction.id), contentDescription = "", ) }