계산은 바깥으로 빼서 remember로 해주는 것이 좋다.
package kr.co.fastcampus.part4plus.chapter2.ui.home
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kr.co.fastcampus.part4plus.chapter2.model.Memo
import kr.co.fastcampus.part4plus.chapter2.model.memos
import kr.co.fastcampus.part4plus.chapter2.ui.theme.MemoAppTheme
@Composable
fun HomeScreen(homeState: HomeState) {
MemoAppTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val memoList = remember { memos.sortedBy { it.id }.toMutableStateList() }
val onClickAction: (Int) -> Unit = {
homeState.showContent(
it
)
}
Column {
AddMemo(memoList)
MemoList(onClickAction, memoList)
}
}
}
}
@Composable
fun AddMemo(memoList: SnapshotStateList<Memo>) {
val inputValue = remember { mutableStateOf("") }
Row(
modifier = Modifier
.padding(all = 16.dp)
.height(100.dp),
horizontalArrangement = Arrangement.End
) {
TextField(
modifier = Modifier
.fillMaxHeight()
.weight(1f),
value = inputValue.value,
onValueChange = { textFieldValue -> inputValue.value = textFieldValue }
)
Button(
onClick = {
memoList.add(
index = 0,
Memo(memoList.size, inputValue.value)
)
inputValue.value = ""
},
modifier = Modifier
.wrapContentWidth()
.fillMaxHeight()
) {
Text("ADD")
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ColumnScope.MemoList(onClickAction: (Int) -> Unit, memoList: SnapshotStateList<Memo>) {
LazyColumn(
modifier = Modifier
.weight(1f)
) {
items(
items = memoList,
key = { it.id }
) { memo ->
Card(
modifier = Modifier
.height(100.dp)
.background(Color.White)
.padding(
horizontal = 16.dp,
vertical = 8.dp
)
.fillMaxWidth(),
backgroundColor = Color.LightGray,
onClick = {
onClickAction(memo.id)
}
) {
Text(
text = memo.text,
modifier = Modifier.fillMaxSize()
)
}
}
}
}