Android Kaki

Build beautiful, usable products using required Components for Android.

Google Account Switcher with Jetpack Compose


information class Account(@DrawableRes val picture: Int)
non-public val accounts = listOf(
Account(picture = R.drawable.goat),
Account(picture = R.drawable.horse),
Account(picture = R.drawable.monkey)
)

@Composable
enjoyable AccountSwitcher(
accounts: Record,
currentAccount: Account,
onAccountChanged: (Account) -> Unit,
modifier: Modifier = Modifier
) {
...
}
val imageSize = 36.dp
val imageSizePx = with(LocalDensity.present) { imageSize.toPx() }
val currentAccountIndex = accounts.indexOf(currentAccount)
var nextAccountIndex by bear in mind { mutableStateOf(null) }
var delta by bear in mind(currentAccountIndex) { mutableStateOf(0f) }
val draggableState = rememberDraggableState(onDelta = { delta = it })
val targetAnimation = bear in mind { Animatable(0f) }

LaunchedEffect(key1 = currentAccountIndex) {
snapshotFlow { delta }
.filter { nextAccountIndex == null }
.filter { it.absoluteValue > 1f }
.throttleFirst(300)
.map { delta ->
if (delta < 0) { // Scroll down (Backside -> Prime)
if (currentAccountIndex < accounts.dimension - 1) 1 else 0
} else { // Scroll up (Prime -> Backside)
if (currentAccountIndex > 0) -1 else 0
}
}
.filter { it != 0 }
.acquire { change ->
nextAccountIndex = currentAccountIndex + change
targetAnimation.animateTo(
change.toFloat(),
animationSpec = tween(easing = LinearEasing, durationMillis = 200)
)


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

enjoyable  Circulation.throttleFirst(periodMillis: Lengthy): Circulation {
require(periodMillis > 0) { "interval ought to be optimistic" }
return circulate {
var lastTime = 0L
acquire { worth ->
val currentTime = System.currentTimeMillis()
if (currentTime - lastTime >= periodMillis) {
lastTime = currentTime
emit(worth)
}
}
}
}
Field(modifier = Modifier.dimension(imageSize)) {
nextAccountIndex?.let { index ->
Picture(
painter = painterResource(id = accounts[index].picture),
contentScale = ContentScale.Crop,
contentDescription = "Account picture",
modifier = Modifier
.graphicsLayer {
scaleX = abs(targetAnimation.worth)
scaleY = abs(targetAnimation.worth)
}
.clip(CircleShape)
)
}
Picture(
painter = painterResource(id = accounts[currentAccountIndex].picture),
contentScale = ContentScale.Crop,
contentDescription = "Account picture",
modifier = Modifier
.draggable(
state = draggableState,
orientation = Orientation.Vertical,
)
.graphicsLayer {
this.translationY = targetAnimation.worth * imageSizePx * -1.5f
}
.clip(CircleShape)
)
}

var selectedAccount by bear in mind { mutableStateOf(accounts[0]) }
AccountSwitcher(
accounts = accounts,
currentAccount = selectedAccount,
onAccountChanged = { selectedAccount = it }
)

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie
Updated: April 14, 2023 — 6:29 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

androidkaki.com © 2023 Android kaki