I have stateFlow of objects in viewmodel, in ui I draw them in forEach loop, I don’t need lazy column in my case, because all my items in zoomable and drawable. when I remove certain item from list by id it removes correctly but on ui I always see that disappear not object I have delete but last object. how to fix that.
objects in state have @Stable annotation
Box(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Black)
.pointerInput(Unit) {
detectTapGestures(
onTap = {
viewModel.clickEmptySpace()
}
)
}
) {
allObjects.forEachIndexed {i, it->
FrameItem(
it,
it.id() == selectedObj,
click = { id ->
viewModel.objectClick(id)
},
delete = { id ->
viewModel.deleteObject(id)
},
)
}
}
@Composable
fun FrameItem(
obj: CanvasObject,
isSelected: Boolean,
click:(Int)->Unit,
delete:(Int)->Unit,
) {
val frameObj = remember { obj as Frame }
var scale by remember { mutableStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
var rotation by remember { mutableStateOf(0f) }
val transformabeState = rememberTransformableState { zoomChange, panChange, rotationChange ->
scale = (scale * zoomChange).coerceIn(0.3f, 5f)
offset += panChange
rotation += rotationChange
}
val frame = painterResource(Res.drawable.iphone_15_frame)
Box(
modifier = Modifier
.fillMaxSize()
.aspectRatio(frame.intrinsicSize.width / frame.intrinsicSize.height)
.graphicsLayer {
scaleX = scale
scaleY = scale
translationX = offset.x
translationY = offset.y
rotationZ = rotation
}
.transformable(transformabeState, enabled = isSelected),
contentAlignment = Alignment.TopEnd
) {
Image(
painter = painterResource(Res.drawable.shot),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.fillMaxSize()
)
}
}
how to delete from state (work correctly)
fun deleteObject(id: Int){
val currentList = emptyList<CanvasObject>().toMutableList()
currentList.addAll(allObjects.value)
currentList.removeAll { it.id()==id }
allObjects.value = currentList
}
——————-