Blog Infos
Author
Published
Topics
Published

Understanding the component

Code
data class Account(@DrawableRes val image: Int)

private val accounts = listOf(
    Account(image = R.drawable.goat),
    Account(image = R.drawable.horse),
    Account(image = R.drawable.monkey)
)

After that I created the AccountSwitcher component that receives a list of accounts, the current account and a callback that’s called when the account changes.

@Composable
fun AccountSwitcher(
    accounts: List<Account>,
    currentAccount: Account,
    onAccountChanged: (Account) -> Unit,
    modifier: Modifier = Modifier
) {
    ...
}

Inside AccountSwitcher I defined a few variables.

val imageSize = 36.dp
val imageSizePx = with(LocalDensity.current) { imageSize.toPx() }

val currentAccountIndex = accounts.indexOf(currentAccount)
var nextAccountIndex by remember { mutableStateOf<Int?>(null) }
var delta by remember(currentAccountIndex) { mutableStateOf(0f) }
val draggableState = rememberDraggableState(onDelta = { delta = it })
val targetAnimation = remember { Animatable(0f) }
LaunchedEffect(key1 = currentAccountIndex) {
    snapshotFlow { delta }
        .filter { nextAccountIndex == null }
        .filter { it.absoluteValue > 1f }
        .throttleFirst(300)
        .map { delta ->
            if (delta < 0) { // Scroll down (Bottom -> Top)
                if (currentAccountIndex < accounts.size - 1) 1 else 0
            } else { // Scroll up (Top -> Bottom)
                if (currentAccountIndex > 0) -1 else 0
            }
        }
        .filter { it != 0 }
        .collect { change ->
            nextAccountIndex = currentAccountIndex + change
            
            targetAnimation.animateTo(
                change.toFloat(),
                animationSpec = tween(easing = LinearEasing, durationMillis = 200)
            )

            onAccountChanged(accounts[nextAccountIndex!!])
            nextAccountIndex = null
            targetAnimation.snapTo(0f)
        }
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Reimagining Android Dialogs with Jetpack Compose

Traditional Android dialogs are hard to test, easy to leak, and painful to customize — and in a world of Compose-first apps, they’re overdue for an upgrade.
Watch Video

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engineer, Design Systems
Block

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engine ...
Block

Reimagining Android Dialogs with Jetpack Compose

Keith Abdulla
Staff Android Engineer, D ...
Block

Jobs

fun <T> Flow<T>.throttleFirst(periodMillis: Long): Flow<T> {
    require(periodMillis > 0) { "period should be positive" }
    return flow {
        var lastTime = 0L
        collect { value ->
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastTime >= periodMillis) {
                lastTime = currentTime
                emit(value)
            }
        }
    }
}
Box(modifier = Modifier.size(imageSize)) {
        nextAccountIndex?.let { index ->
            Image(
                painter = painterResource(id = accounts[index].image),
                contentScale = ContentScale.Crop,
                contentDescription = "Account image",
                modifier = Modifier
                    .graphicsLayer {
                        scaleX = abs(targetAnimation.value)
                        scaleY = abs(targetAnimation.value)
                    }
                    .clip(CircleShape)
            )
        }
        
        Image(
            painter = painterResource(id = accounts[currentAccountIndex].image),
            contentScale = ContentScale.Crop,
            contentDescription = "Account image",
            modifier = Modifier
                .draggable(
                    state = draggableState,
                    orientation = Orientation.Vertical,
                )
                .graphicsLayer {
                    this.translationY = targetAnimation.value * imageSizePx * -1.5f
                }
                .clip(CircleShape)
        )
    }
var selectedAccount by remember { mutableStateOf(accounts[0]) }

AccountSwitcher(
    accounts = accounts,
    currentAccount = selectedAccount,
    onAccountChanged = { selectedAccount = it }
)

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog
Material3 is the newest iteration of Material Design, with dynamic theming, revised components and…
READ MORE
Menu