Orhan Tozan
03/31/2022, 10:20 AMFilip Wiesner
03/31/2022, 10:23 AMrepeat(3) { C() }
D()
repeat(3) { C() }
//or maybe
repeat(7) {
if (it == 3) D() else C()
}
Orhan Tozan
03/31/2022, 10:25 AMFilip Wiesner
03/31/2022, 10:27 AMOrhan Tozan
03/31/2022, 10:29 AMFilip Wiesner
03/31/2022, 10:30 AMOrhan Tozan
03/31/2022, 10:31 AMFilip Wiesner
03/31/2022, 10:32 AMOrhan Tozan
03/31/2022, 10:37 AMbuildList {
repeat(6) { index ->
add(
@Composable {
C(...)
}
)
}
}
@Composable { ... }
is the right way to create a Composable lambda right?Csaba Szugyiczki
03/31/2022, 10:49 AM@Composable
fun CardGroup(count: Int) {
repeat(count) { Card() }
}
and then write
CardGroup(count = 3)
Divider()
CardGroup(count = 3)
Orhan Tozan
03/31/2022, 10:53 AMRow(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier.fillMaxWidth(),
) {
repeat(3) { index ->
Cell(
value = value[index],
onValueChange = { newNumber ->
val newValue = value.toMutableList().apply { set(index, newNumber) }
val newNumberWasEntered = value != newValue && newNumber != null
onValueChange(newValue)
if (newNumberWasEntered) {
focusManager.moveFocus(FocusDirection.Right)
}
},
imeAction = ImeAction.Next,
onKeyboardAction = {
focusManager.moveFocus(FocusDirection.Right)
},
modifier = Modifier
.weight(1f)
.padding(end = 10.dp)
.onFocusChanged { focusState ->
if (focusState.isFocused) {
val newValue = value
.toMutableList()
.apply { set(index, null) }
.toList()
onValueChange(newValue)
}
},
)
}
Divider(
color = MaterialTheme.colors.primary,
thickness = 2.dp,
modifier = Modifier
.width(10.dp)
)
repeat(3) { loopIndex ->
val digitIndex = loopIndex + 3
val isLastCell = digitIndex == value.lastIndex
Cell(
value = value[digitIndex],
onValueChange = { newNumber ->
val newValue = value
.toMutableList()
.apply { set(digitIndex, newNumber) }
.toList()
val newNumberWasEntered = value != newValue && newNumber != null
onValueChange(newValue)
if (newNumberWasEntered) {
focusManager.moveFocus(FocusDirection.Right)
}
},
imeAction =
if (isLastCell) {
ImeAction.Done
} else {
ImeAction.Next
},
onKeyboardAction = {
if (isLastCell) {
focusManager.clearFocus()
} else {
focusManager.moveFocus(FocusDirection.Right)
}
},
modifier = Modifier
.fillMaxWidth()
.padding(start = 10.dp)
.weight(1f)
.onFocusChanged { focusState ->
if (focusState.isFocused) {
val newValue = value
.toMutableList()
.apply { set(digitIndex, null) }
.toList()
onValueChange(newValue)
}
},
)
}
}
Csaba Szugyiczki
03/31/2022, 10:53 AMcontent: @Composable () -> Unit
This concept is all over composeOrhan Tozan
03/31/2022, 10:53 AMCsaba Szugyiczki
03/31/2022, 10:55 AMOrhan Tozan
03/31/2022, 10:57 AMconst items = Array(6) { C() }
items.splice(3, 0, D())
// items is now C C C D C C C
How would you do this in Compose?Csaba Szugyiczki
03/31/2022, 10:58 AMOrhan Tozan
03/31/2022, 10:58 AMCsaba Szugyiczki
03/31/2022, 10:59 AMOrhan Tozan
03/31/2022, 11:01 AMif (showButton) {
Button()
}
instead of
if (showButton) {
Button()
} else {
null
}
but it also has it's flaws, as you now lose the power of the regular programming power of transforming and mapping values. Example is that we can't achieve my React example few messages aboveFilip Wiesner
03/31/2022, 11:08 AMOrhan Tozan
03/31/2022, 11:16 AMAlbert Chang
03/31/2022, 11:33 AMval card = @Composable { Card() }
repeat(3) { card() }
D()
repeat(3) { card() }
will work.Orhan Tozan
03/31/2022, 12:24 PM