KotlinLeaner
01/13/2023, 5:08 PMNested Column
will be 100 times recompose
? If not can someone help me on this please?KotlinLeaner
01/13/2023, 5:08 PMclass ListViewComposableActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppBarScaffold(
displayHomeAsUpEnabled = true,
titleId = R.string.activity
) {
ListViewItemStateful()
}
}
}
}
ListViewItemStateful
@Composable
fun ListViewItemStateful(
viewModel: ListViewModel = koinViewModel(),
) {
ItemViewListStateless(
uiState = viewModel.uiState,
isEnable = viewModel.isEnable,
scanDeviceList = viewModel.scanResultList,
)
}
ItemViewListStateless
@Composable
fun ItemViewListStateless(
uiState: State,
isEnable: Boolean,
scanDeviceList: SnapshotStateList<ScanResults>,
) {
when (uiState) {
INITIAL,
FIRST -> {
ListContent(isEnable, scanDeviceList)
}
}
}
ListContent
@Composable
fun ListContent(isEnable: Boolean, scanDeviceList: SnapshotStateList<ScanResults>) {
AnimatedVisibility(true) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxSize()
.verticalScroll(rememberScrollState()),
) {
if (isEnable) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
DeviceList(
scanDeviceList,
modifier = Modifier.align(Alignment.Start),
)
}
}
}
}
}
DeviceList
@Composable
fun ColumnScope.DeviceList(
scanDeviceList: SnapshotStateList<ScanResults>,
modifier: Modifier = Modifier,
) {
Spacer(modifier = Modifier.height(32.dp))
AnimatedVisibility(
scanDeviceList.isNotEmpty(),
modifier = modifier
) {
Column {
Text(text = "Device List")
scanDeviceList.forEachIndexed { index, scanResults ->
Text(text = scanResults.device.name)
}
}
}
}
ListViewModel.kt
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import com.abc.app.common.BaseViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class ListViewModel : BaseViewModel() {
val scanResultList by lazy { mutableStateListOf<ScanResults>() }
var isEnable by mutableStateOf(false)
private set
var uiState by mutableStateOf<State>(State.INITIAL)
private set
init {
viewModelScope.launch {
(0..10).forEach {
delay(2000)
scanResultList.add(ScanResults(Device("item $it")))
}
}
isEnable = true
uiState = State.FIRST
}
}
data class ScanResults(val device: Device)
data class Device(val name: String)
enum class State {
INITIAL,
FIRST
}
KotlinLeaner
01/13/2023, 5:08 PMKotlinLeaner
01/13/2023, 5:08 PMKotlinLeaner
01/13/2023, 5:09 PMDeviceList
is recompose 10 times.
I checked in @Ben Trengrove [G] around 6:40 min he tried to solve recompose issue and there skipped recomposition count is clear. So why it's showing count in my component tree in recomposition and skipped section? Many thanksJakub Syty
01/16/2023, 9:32 AMKotlinLeaner
01/16/2023, 9:34 AMJakub Syty
01/16/2023, 9:36 AMKotlinLeaner
01/16/2023, 9:43 AMJakub Syty
01/16/2023, 10:01 AMitems
KotlinLeaner
01/16/2023, 10:03 AMKotlinLeaner
01/16/2023, 11:04 AMListContent
to like this what you mention above
@Composable
fun ListContent(isEnable: Boolean, scanDeviceList: SnapshotStateList<ScanResults>) {
AnimatedVisibility(true) {
LazyColumn(
modifier = Modifier
.padding(16.dp)
.fillMaxSize()
) {
if (isEnable) {
item {
Text(text = "Device List")
}
item {
Spacer(modifier = Modifier.height(32.dp))
}
items(scanDeviceList) {
Text(text = it.device.name)
}
}
}
}
}
KotlinLeaner
01/16/2023, 11:05 AMSaveableStateProvider
is recomposing ?Jakub Syty
01/16/2023, 11:06 AMJakub Syty
01/16/2023, 11:07 AMJakub Syty
01/16/2023, 11:07 AMKotlinLeaner
01/16/2023, 11:08 AM