TheMrCodes
04/20/2021, 12:40 AMdata class RowData(
val data1: Int,
val data2: String,
val data3: Double,
val data4: Int,
val data5: Double,
)
fun main() {
val fetchData = flow<List<RowData>> {
val ret = mutableListOf<RowData>()
for (n in 0 until 20) {
ret.add(RowData(n, "Hi", n.toDouble(), n, n.toDouble()))
emit(ret)
delay(50)
}
}
Window("Test", IntSize(600, 400)) {
MaterialTheme {
val rowList by fetchData.collectAsState(listOf())
println("recomposed")
val state = LazyListState(0, 0)
LazyRow(state = state, modifier = Modifier.fillMaxSize()) {
items(rowList) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillParentMaxWidth(),
) {
Text(it.data1.toString())
Text(it.data2)
Text(it.data3.toString())
Text(it.data4.toString())
}
}
}
}
}
}
plugins {
kotlin("jvm") version "1.4.32"
id("org.jetbrains.compose") version "0.4.0-build180"
}
Zach Klippenstein (he/him) [MOD]
04/20/2021, 2:19 AMTheMrCodes
04/20/2021, 7:56 AMdoesn’t notify changes if the same value is set multiple consecutive times.I thought the hashcode has to be diffrent? I now also tried it out with an Object and in my testings it only worked with 'primitiv' types (tested with List, Array, DataObject, Int and String)
Timo Drick
04/20/2021, 8:15 AMmutableStateListOf<>()
which will keep track of changes.TheMrCodes
04/20/2021, 8:29 AMfun main() {
val itemList = mutableStateListOf<RowData>()
val fetchData = flow<Unit> {
for (n in 0 until 20) {
val toAdd = RowData(n, "Hi", n.toDouble(), n, n.toDouble())
itemList.add(toAdd)
delay(50)
}
}
Window("Test", IntSize(600, 400)) {
MaterialTheme {
println("recomposed")
LaunchedEffect(Unit) {
fetchData.collect()
}
val state = LazyListState(0, 0)
LazyRow(state = state, modifier = Modifier.fillMaxSize()) {
items(itemList) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillParentMaxWidth(),
) {
Text(it.data1.toString())
Text(it.data2)
Text(it.data3.toString())
Text(it.data4.toString())
}
}
}
}
}
}
Doesn't work either 😬
Even if I make it inline wihout using a flow to dispatch the updates
fun main() {
Window("Test", IntSize(600, 400)) {
MaterialTheme {
println("recomposed")
val itemList = mutableStateListOf<RowData>()
LaunchedEffect(Unit) {
for (n in 0 until 20) {
val toAdd = RowData(n, "Hi", n.toDouble(), n, n.toDouble())
itemList.add(toAdd)
delay(50)
}
}
val state = LazyListState(0, 0)
LazyRow(state = state, modifier = Modifier.fillMaxSize()) {
items(itemList) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillParentMaxWidth(),
) {
Text(it.data1.toString())
Text(it.data2)
Text(it.data3.toString())
Text(it.data4.toString())
}
}
}
}
}
}
Timo Drick
04/20/2021, 8:30 AMLaunchedEffect(..) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
....
}
}
TheMrCodes
04/20/2021, 8:32 AMTimo Drick
04/20/2021, 8:33 AMTheMrCodes
04/20/2021, 8:33 AMTimo Drick
04/20/2021, 8:41 AMTheMrCodes
04/20/2021, 8:42 AMModifier.fillParentMaxWidth()
Timo Drick
04/20/2021, 8:44 AMTheMrCodes
04/20/2021, 8:48 AM