Skip to content

Instantly share code, notes, and snippets.

View ahmedrizwan's full-sized avatar
🚀
Remote

Ahmed Rizwan ahmedrizwan

🚀
Remote
View GitHub Profile
@Composable()
fun NumbersPanel(alpha: Int) {
Stack {
Row(modifier = Modifier.fillMaxSize()) {
numberColumns.forEach { numberColumn ->
Column(modifier = Modifier.weight(1f)) {
numberColumn.forEach { text ->
MainContentButton(text)
}
}
@Composable
fun DimOverlay(alpha: Int) {
Box(modifier = Modifier.fillMaxSize(), backgroundColor = Color(50, 50, 50, alpha))
}
@Composable
fun TopView(
boxHeight: Dp,
drag: Drag
) {
val position = drag.position
val flingConfig = drag.flingConfig
val yOffset = with(DensityAmbient.current) { position.value.toDp() }
val scrollerPosition = ScrollerPosition()
// scroll the history list to bottom when dragging the top panel
// Top drag
val topStart = -(constraints.maxHeight.value / 1.4f)
val topMax = 0.dp
val topMin = -(boxHeight / 1.4f)
val (topMinPx, topMaxPx) = with(DensityAmbient.current) {
topMin.toPx().value to topMax.toPx().value
}
val topFlingConfig = AnchorsFlingConfig(listOf(topMinPx, topMaxPx))
val topPosition = animatedFloat(topStart) // for dragging state
topPosition.setBounds(topMinPx, topMaxPx)
@Composable()
fun SideView(
boxHeight: Dp,
drag: Drag
) {
val position = drag.position
val flingConfig = drag.flingConfig
val yOffset = with(DensityAmbient.current) { position.value.toDp() }
val toggleAsset = state { R.drawable.ic_keyboard_arrow_left_24 }
Box(
// Side drag
val sideMin = 90.dp
val sideMax = boxWidth - 30.dp
val (sideMinPx, sideMaxPx) = with(DensityAmbient.current) {
sideMin.toPx().value to sideMax.toPx().value
}
val sideFlingConfig = AnchorsFlingConfig(listOf(sideMinPx, sideMaxPx))
val sidePosition = animatedFloat(sideMaxPx)
sidePosition.setBounds(sideMinPx, sideMaxPx)
class Drag(
val position: AnimatedFloat,
val flingConfig: FlingConfig
)
@ahmedrizwan
ahmedrizwan / MainActivity.kt
Last active May 27, 2020 11:15
Jetpack Compose Calculator
setContent {
MaterialTheme(colors = lightThemeColors) {
WithConstraints { constraints, _ ->
val boxHeight = with(DensityAmbient.current) { constraints.maxHeight.toDp() }
val boxWidth = with(DensityAmbient.current) { constraints.maxWidth.toDp() }
Content(
constraints = constraints,
boxHeight = boxHeight,
boxWidth = boxWidth
)
sealed class UIState<out R> {
object Loading : UIState<Nothing>()
object Retrying : UIState<Nothing>()
object SwipeRefreshing : UIState<Nothing>()
data class Success<T>(val data: T) : UIState<T>()
data class Failure(val exception: Exception) : UIState<Nothing>()
data class SwipeRefreshFailure(val exception: Exception) : UIState<Nothing>()
}
sealed class Action {
object Load : Action()
object SwipeRefresh : Action()
object Retry : Action()
}