Conversation
kimdoyeon1234
left a comment
There was a problem hiding this comment.
수고하셨습니다!!
LazyVerticalGrid + items() DSL을 올바르게 사용하신 점, key를 명시적으로 지정해주신 점 좋았습니다! 다만 현재 key = { product -> product.name }으로 지정하고 계신데, name은 중복되거나 변경될 수 있어서 key의 조건인 고유(unique)하고 안정적(stable)을 만족하지 못할 수 있습니다! ProductData에 id 필드를 추가하고 key = { product -> product.id }로 사용하는 걸 추천드립니다!
그리고 HomeFragment, WishlistFragment는 아이템이 2개로 고정되어 있는데, 이런 경우엔 LazyVerticalGrid 대신 Column + Row 조합이 더 가볍습니다! Lazy 계열은 아이템 수가 많거나 동적으로 변하는 경우에 사용할 때 좋습니다!
id 필드만 추가해주시면 indexOf 문제와 key 문제가 한 번에 해결되니 꼭 적용해보세요! 수고하셨습니다!
| key = { product -> product.name } | ||
| ) { product -> | ||
| ProductCard( |
There was a problem hiding this comment.
현재 key = { product -> product.name }으로 지정하고 계신데, name은 중복되거나 변경될 수 있어서 key의 조건인 고유하고 안정적을 만족하지 못할 수 있습니다! ProductData에 id 필드를 추가하고 key = { product -> product.id }로 사용하는 걸 추천드립니다!
| onFavoriteClick = { | ||
| val idx = products.indexOf(product) | ||
| if (idx >= 0) { | ||
| products[idx] = product.copy( | ||
| isFavorite = !product.isFavorite |
There was a problem hiding this comment.
products.indexOf(product)는 동일한 name/price를 가진 아이템이 있을 경우 의도치 않은 인덱스가 반환될 수 있습니다! 1번 피드백처럼 id 필드 추가 후 products.indexOfFirst { it.id == product.id }로 변경하시면 더 안전합니다!
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
|
|
||
| binding.composeWishlist.apply { | ||
| setViewCompositionStrategy( | ||
| ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed | ||
| ) | ||
| setContent { | ||
| LazyVerticalGrid( | ||
| columns = GridCells.Fixed(2), | ||
| contentPadding = PaddingValues(bottom = 16.dp) | ||
| ) { | ||
| items( | ||
| items = wishList, | ||
| key = { product -> product.name } | ||
| ) { product -> | ||
| ProductCard( | ||
| imageResId = product.imageResId, | ||
| category = product.category, | ||
| name = product.name, | ||
| price = product.price | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
아이템이 2개로 고정되어 있는 경우엔 LazyVerticalGrid 대신 Column + Row 조합이 더 가볍습니다! Lazy 계열은 아이템 수가 많거나 동적으로 변하는 경우에 사용할 때 더 좋습니다!
📝 작업 내용
items()+key적용📸 스크린샷
3주차 화면 그대로
🙏 리뷰 요구사항 (선택)
mutableStateListOf+product.copy()로 상태를 관리했는데,indexOf로 위치를 찾는 방식이 적절한지 궁금합니다. 더 나은 패턴이 있다면 피드백 부탁드립니다.key를product.name으로 잡았는데, ProductData에 id 필드를 추가하는 게 정석일지 의견 듣고 싶습니다.