-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[악어] 장바구니 미션 제출합니다. #6
base: main
Are you sure you want to change the base?
Changes from 1 commit
71a229c
e4c0cad
d417681
1038216
29b1d18
efdf548
bb07f93
f81c672
959a34d
0882ffd
f33d230
b03e67d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,46 @@ | ||
package nextstep.shoppingcart.data | ||
|
||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.snapshots.SnapshotStateList | ||
import androidx.compose.runtime.toMutableStateList | ||
import nextstep.shoppingcart.domain.model.CartItem | ||
import nextstep.shoppingcart.domain.model.Product | ||
|
||
object Cart { | ||
private val _items: MutableList<CartItem> = mutableListOf() | ||
val items: List<CartItem> get() = _items.toList() | ||
val items: SnapshotStateList<CartItem> = emptyList<CartItem>().toMutableStateList() | ||
|
||
val totalPrice: Int get() = _items.sumOf { it.totalPrice } | ||
|
||
private var maxCartItemId: Long = 0L | ||
|
||
fun addOne(product: Product): List<CartItem> { | ||
val item = _items.find { it.product == product } | ||
if (item == null) { | ||
_items.add( | ||
CartItem( | ||
id = fetchMaxCartItemId(), | ||
product = product, | ||
count = 1, | ||
) | ||
) | ||
val totalPrice = mutableIntStateOf(0) | ||
|
||
fun addOne(product: Product) { | ||
val item = items.find { it.product == product } | ||
if (item != null) { | ||
val index = items.indexOf(item) | ||
items[index] = item.copy(count = item.count + 1) | ||
} else { | ||
val index = _items.indexOf(item) | ||
_items[index] = item.copy(count = item.count + 1) | ||
items.add(CartItem(product = product, count = 1)) | ||
} | ||
return items | ||
updateState() | ||
} | ||
|
||
private fun fetchMaxCartItemId(): Long = maxCartItemId++ | ||
|
||
fun removeOne(product: Product): List<CartItem> { | ||
_items | ||
.find { it.product == product } | ||
?.let { item -> | ||
if (item.count > 1) { | ||
val index = _items.indexOf(item) | ||
_items[index] = item.copy(count = item.count - 1) | ||
} else { | ||
_items.remove(item) | ||
} | ||
fun removeOne(product: Product) { | ||
val item = items.find { it.product == product } | ||
if (item != null) { | ||
if (item.count > 1) { | ||
val index = items.indexOf(item) | ||
items[index] = item.copy(count = item.count - 1) | ||
} else { | ||
items.remove(item) | ||
} | ||
return items | ||
updateState() | ||
} | ||
} | ||
|
||
fun removeAll(product: Product) { | ||
items.removeAll { it.product == product } | ||
updateState() | ||
} | ||
|
||
fun removeAll(product: Product): List<CartItem> { | ||
_items.removeAll { it.product == product } | ||
return items | ||
private fun updateState() { | ||
totalPrice.value = items.sumOf { it.product.price * it.count } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package nextstep.shoppingcart.domain.model | ||
|
||
data class CartItem( | ||
val id: Long, | ||
val id: Long = maxCartItemId++, | ||
val product: Product, | ||
val count: Int | ||
) { | ||
val totalPrice: Int get() = product.price * count | ||
companion object { | ||
private var maxCartItemId: Long = 0L | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package nextstep.shoppingcart.presentation.components | |
import androidx.compose.foundation.border | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
|
@@ -41,6 +42,7 @@ fun CartItemList( | |
) { | ||
LazyColumn( | ||
modifier = modifier, | ||
contentPadding = PaddingValues(50.dp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scaffold의 innerPadding과는 어떤 차이점이 있나요? (반복 질문이죠?) 정확히 모르겠다고 하셨는데, 그럼 어떤 기준으로 이렇게 사용하셨나요? |
||
) { | ||
items( | ||
items = items, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,12 @@ import nextstep.signup.R | |
fun CartScreen( | ||
navigation: () -> Unit, | ||
cartItems: List<CartItem>, | ||
cart: Cart, | ||
totalPrice: Int, | ||
onIncrease: (CartItem) -> Unit = {}, | ||
onDecrease: (CartItem) -> Unit = {}, | ||
onClear: (CartItem) -> Unit = {}, | ||
modifier: Modifier = Modifier, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 여전히 compose api guideline과 일치하지 않네요. |
||
) { | ||
val totalPrice = cartItems.sumOf { it.product.price * it.count } | ||
AndroidshoppingcartTheme { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재는 큰 문제가 아닐 수 있지만 테마를 Activity에서 지정하지 않은 이유가 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코멘트를 반영하려고 모두 Screen 단위 안으로 Theme을 집어넣었습니다. |
||
Scaffold( | ||
modifier = modifier, | ||
|
@@ -93,5 +92,5 @@ private fun CartScreenPreview() { | |
count = 2, | ||
), | ||
) | ||
CartScreen(navigation = {}, cartItems = items, cart = Cart) | ||
CartScreen(navigation = {}, cartItems = items, totalPrice = items.sumOf { it.product.price * it.count }) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cart 에는 레포지토리 패턴을 적용하지 않으신 이유가 있을까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
product 때까지만 해도 확장성을 열어놓고 생각했었는데요,
만들고 나니 현재로서는 레포지토리 패턴으로 확장하지 않아도 되었겠다는 생각이 들더라구요.
그래서 주어진 Cart 코드로만 작성해봐야겠다 라는 의도로 적용하지 않았습니다...ㅎㅎ