https://kotlinlang.org logo
#compose
Title
# compose
l

Lilly

06/12/2020, 9:28 PM
Did someone notice that 
Scaffold(bodyContent = { // code here is called twice })
 is called twice? I'm using dev13 but same problem with dev12. I can't test the other versions because my project fails otherwise.
m

matvei

06/13/2020, 10:39 AM
Yeah, this is WAI for now. We need to get sizes for bottom bar and top bar, so we can get you InnerPadding as a paramter in the
bodyContent
slot. This is go away at some point later when we will have lazy subcomposition and we will get sizes synchronously. Can you share the usecase where the number of calls of your composable lambda matters to you?
l

Lilly

06/13/2020, 3:28 PM
Thanks for the reply 🙂
@matvei This was my `bodycontent`:
Copy code
@Composable
fun ScannerScreenContent(
    onItemAction: () -> Unit
): @Composable() (Modifier) -> Unit = { modifier ->
    val viewModel = ScannerViewModelAmbient.current

    val modelList = modelListOf<BluetoothDeviceWrapper>()

    val state by viewModel.device.observeAsState() // prevents preview from working
    state?.let { device ->
        modelList.add(device)
        modelList.forEach {
            Log.d("Items", it.bluetoothDevice.name)
        }
    }
    AdapterModelListComponent(modelList, modifier, onItemAction)
}
But one of the users explained me that I should use
remember
and
launchInComposition
like so:
Copy code
@Composable
fun ScannerScreenContent(
    onItemAction: () -> Unit
): @Composable() (Modifier) -> Unit = { modifier ->
    val viewModel = ScannerViewModelAmbient.current

    val modelList = remember { modelListOf<BluetoothDeviceWrapper>() }

    launchInComposition(viewModel.device) {
        viewModel.device.asFlow().collect { device ->
            modelList.forEach {
                if (it.bluetoothDevice.address == device.bluetoothDevice.address) return@collect
            }
            modelList.add(device)
            Log.d("Found", "size: ${modelList.size}")
        }
    }

    AdapterModelListComponent(modelList, modifier, onItemAction)
}
Does the [G] in your name mean, you are working at google and with/in the compose team respectively?
m

matvei

06/14/2020, 4:54 PM
Yes, I work in the Compose team and I wrote the first version of the
Scaffold
component, so I'm happy to answer any questions or discuss feature requests around it 🙂 Yeah, I think you figures it out, nice! With proper memoization via
remember
,
state
,
onCommit
,
launchInComposition
and other tools it shouldn't matter how many times your @Composable function is being invoked. Broadly speaking, the big application and/or in reusable components its almost impossible to know how many times any particular components in the code will be recomposed.
l

Lilly

06/15/2020, 11:28 AM
Thank you for your investigation 🙂
6 Views