Skip to content
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

๐Ÿš€ 4๋‹จ๊ณ„ - ์žฅ๋ฐ”๊ตฌ๋‹ˆ(์ˆ˜๋Ÿ‰) #125

Open
wants to merge 12 commits into
base: dothebestmayb
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
629e84c
docs: TODO ๋ฆฌ์ŠคํŠธ ์ž‘์„ฑ
DoTheBestMayB Feb 24, 2025
5b0959f
fix: ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋กœ์ง ์˜ค๋ฅ˜ ์ˆ˜์ •
DoTheBestMayB Feb 24, 2025
f07f842
refactor: ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์•„์ดํ…œ์„ ProductRepository์—์„œ ๊ด€๋ฆฌํ•˜๋„๋ก ์ˆ˜์ •
DoTheBestMayB Mar 5, 2025
3c62ca3
chore: ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” import ๋ฌธ ์‚ญ์ œ ๋ฐ ์ฝ”๋“œ ์ •๋ ฌ
DoTheBestMayB Mar 5, 2025
23ee0cd
feat: ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์ˆ˜๋Ÿ‰ ๋ฑƒ์ง€๋กœ ํ‘œ์‹œํ•˜๊ธฐ
DoTheBestMayB Mar 5, 2025
725c508
feat: + ๋งŒ ์กด์žฌํ•˜๋Š” ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ Composable ๊ตฌํ˜„
DoTheBestMayB Mar 5, 2025
0460aa6
feat: ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ UI ๊ตฌํ˜„
DoTheBestMayB Mar 5, 2025
12570fb
chore: ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” import๋ฌธ ์‚ญ์ œ ๋ฐ ์ฝ”๋“œ ์ •๋ ฌ
DoTheBestMayB Mar 5, 2025
c1bded6
refactor: ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์•„์ดํ…œ์„ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก ์ˆ˜์ •
DoTheBestMayB Mar 10, 2025
0fd44a7
chore: ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜, import ๋ฌธ ์‚ญ์ œ
DoTheBestMayB Mar 10, 2025
59ef046
refactor: ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์•„์ดํ…œ ์ˆ˜๋Ÿ‰์— ๋”ฐ๋ผ QuantityHandler UI ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝํ•˜๋„๋ก ์ˆ˜์ •
DoTheBestMayB Mar 10, 2025
b176b37
chore: ์ฝ”๋“œ ์ •๋ ฌ ๋ฐ import ๋ฌธ ์ •๋ฆฌ
DoTheBestMayB Mar 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@
- [x] ๋‹ด๊ธด ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
- [x] ์ˆ˜๋Ÿ‰์„ 1๋ณด๋‹ค ์ž‘๊ฒŒ ํ•˜๋ฉด ์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์ƒํ’ˆ์ด ์ œ๊ฑฐ๋œ๋‹ค.
- [x] ๋‹ด๊ธด ์ƒํ’ˆ ๊ฐ€๊ฒฉ์˜ ์ดํ•ฉ์ด ์ฃผ๋ฌธํ•˜๊ธฐ ๋ฒ„ํŠผ์— ํ‘œ์‹œ๋œ๋‹ค.
- [ ] ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํ™”๋ฉด์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
- [x] ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํ™”๋ฉด์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

๐Ÿš€ 4๋‹จ๊ณ„ - ์žฅ๋ฐ”๊ตฌ๋‹ˆ(์ˆ˜๋Ÿ‰)
- [x] ๋ฒ„๊ทธ ์ˆ˜์ • : ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์•„์ดํ…œ์ด ๋‹ด๊ธฐ์ง€ ์•Š๋Š” ๋ฌธ์ œ ์ˆ˜์ •ํ•˜๊ธฐ
- [x] ์ด์ „ ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ : CartItem์˜ totalPrice get์œผ๋กœ ๋งค๋ฒˆ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ , state๋กœ ์ €์žฅํ•˜๋„๋ก ์ˆ˜์ •
- [x ์ƒํ’ˆ ๋ชฉ๋ก์—์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์„/๋‹ด๊ธด ์ƒํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ๋‹ค
- [x] ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธฐ์ง€ ์•Š์€ ์•„์ดํ…œ์— ๋Œ€ํ•ด ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” + ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
- [x] ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์•„์ดํ…œ์€ ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
- [ ] (optional) + ๋ฒ„ํŠผ์—์„œ ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ UI๋กœ ๋ณ€๊ฒฝํ•  ๋•Œ B๋งˆํŠธ์ฒ˜๋Ÿผ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ด์šฉํ•œ๋‹ค.
- [x] ์ƒํ’ˆ ๋ชฉ๋ก ํ™”๋ฉด์—์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์ˆ˜๋Ÿ‰ ๋งŒํผ ์šฐ์ธก ์ƒ๋‹จ์— ์ˆ˜๋Ÿ‰์„ ๋ฑƒ์ง€๋กœ ํ‘œ์‹œํ•œ๋‹ค.
22 changes: 13 additions & 9 deletions app/src/androidTest/java/nextstep/shoppingcart/BasketScreenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ internal class BasketScreenTest {
onRemoveCartItemClick = { removeItem ->
state = state.copy(
cartItems = state.cartItems.filterNot { item ->
item == removeItem
item.product == removeItem
}
)
},
onIncreaseQuantityClick = { increaseItem ->
state = state.copy(
cartItems = state.cartItems.map { item ->
if (item == increaseItem) {
item.copy(count = item.count + 1)
if (item.product == increaseItem) {
item.copy(
product = item.product.copy(cartQuantity = item.quantity + 1),
quantity = item.quantity + 1,
)
} else {
item
}
Expand All @@ -66,17 +69,17 @@ internal class BasketScreenTest {
},
onDecreaseQuantityClick = { decreaseItem ->
val newCartItems = state.cartItems.let { items ->
items.find { it.product == decreaseItem.product }?.let { item ->
if (item.count > 1) {
items.find { it.product == decreaseItem }?.let { item ->
if (item.quantity > 1) {
items.map { current ->
if (current.product == decreaseItem.product) {
current.copy(count = current.count - 1)
if (current.product == decreaseItem) {
current.copy(quantity = current.quantity - 1)
} else {
current
}
}
} else {
items.filterNot { it.product == decreaseItem.product }
items.filterNot { it.product == decreaseItem }
}
} ?: items
}
Expand Down Expand Up @@ -158,8 +161,9 @@ internal class BasketScreenTest {
imageUrl = "",
name = "Item $it",
price = 100,
cartQuantity = 1,
),
count = 1,
quantity = 1,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,22 @@ package nextstep.shoppingcart.data.datasource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import nextstep.shoppingcart.data.model.CartItemEntity
import nextstep.shoppingcart.data.model.ProductEntity

/**
* Room๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
*/
class CartLocalDataSource {

// MutableList๋กœ ๊ตฌํ˜„ํ•˜๋ฉด Flow<List<CartItemEntity>>๋กœ ๋ฐ˜ํ™˜ํ•  ๋ฐฉ๋ฒ•์ด ์—†์–ด์„œ MutableStateFlow๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.
private val _itemsFlow = MutableStateFlow<List<CartItemEntity>>(emptyList())
val itemsFlow: Flow<List<CartItemEntity>> = _itemsFlow.asStateFlow()
private val _itemsFlow = MutableStateFlow<LinkedHashMap<String, CartItemEntity>>(linkedMapOf())
val items: Flow<LinkedHashMap<String, CartItemEntity>> = _itemsFlow.asStateFlow()

fun addOne(product: ProductEntity) {
_itemsFlow.update { items ->
val isExistingItem = items.any { it.product == product }
if (isExistingItem) {
items + CartItemEntity(product, 1)
} else {
items.map { item ->
if (item.product == product) {
item.copy(count = item.count + 1)
} else {
item
}
}
}
fun update(itemEntity: CartItemEntity) {
val currentMap = _itemsFlow.value.toMutableMap()
val productId = itemEntity.product.id
if (itemEntity.quantity < 1) {
currentMap.remove(productId)
} else {
currentMap[productId] = itemEntity
}

}

fun removeOne(product: ProductEntity) {
_itemsFlow.update { items ->
items.find { it.product == product }?.let { item ->
if (item.count > 1) {
items.map { current ->
if (current.product == product) {
current.copy(count = current.count - 1)
} else {
current
}
}
} else {
items.filterNot { it.product == product }
}
} ?: items
}

}

fun removeAll(product: ProductEntity) {
_itemsFlow.update { items ->
items.filterNot { it.product == product }
}
_itemsFlow.value = LinkedHashMap(currentMap)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nextstep.shoppingcart.data.datasource

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import nextstep.shoppingcart.data.model.ProductEntity

/**
* Room๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.
*/
class ProductLocalDataSource {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CartRepository์—์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ์•„์ดํ…œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ CartLocalDataSource๋ฅผ ProductLocalDataSource๋กœ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ  ProdcutRepository๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค.


private val _itemsFlow = MutableStateFlow<List<ProductEntity>>(emptyList())
val itemsFlow: Flow<List<ProductEntity>> = _itemsFlow.asStateFlow()
Comment on lines +13 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ผ๋ฐ˜ Flow๋กœ ์„ ์–ธํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์–ด์„œ StateFlow๋กœ ์„ ์–ธํ–ˆ์Šต๋‹ˆ๋‹ค.
StateFlow ๋ง๊ณ  ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฐฉ๋ฒ• ๋ง๊ณ  ๋– ์˜ค๋ฅด๋Š”๊ฒŒ ์ง€๊ธˆ์€ ์—†๋„ค์š” ๐Ÿ˜…


fun replaceAll(items: List<ProductEntity>) {
_itemsFlow.value = items
}
}
Original file line number Diff line number Diff line change
@@ -1,137 +1,137 @@
package nextstep.shoppingcart.data.datasource

import kotlinx.coroutines.delay
import nextstep.shoppingcart.data.model.ProductEntity
import nextstep.shoppingcart.data.model.ProductResponse
import java.util.UUID
import kotlin.random.Random

class ProductRemoteDataSource {

private val random = Random(System.currentTimeMillis())

suspend fun fetch(): List<ProductEntity> {
suspend fun fetch(): List<ProductResponse> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐ์ดํ„ฐ๋Š” XxxResponse๋กœ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ 
API๋กœ ๋ถˆ๋Ÿฌ์™€์„œ Local์— ๋“ค๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ XxxEntity๋กœ ๋ช…๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค.

delay(random.nextLong(300L, 1000L))

return dummyProducts
}

companion object {
internal val dummyProducts = listOf(
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/200/300",
name = "์น˜์ฆˆ๋ฒ„๊ฑฐ",
price = 5500
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/250/350",
name = "ํŽ˜ํผ๋กœ๋‹ˆ ํ”ผ์ž",
price = 13900
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/180/280",
name = "์ดˆ์ฝœ๋ฆฟ ์ผ€์ดํฌ",
price = 8900
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/220/320",
name = "์•„์ด์Šค ์•„๋ฉ”๋ฆฌ์นด๋…ธ",
price = 4100
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/240/360",
name = "ํ›„๋ผ์ด๋“œ ์น˜ํ‚จ",
price = 16900
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/190/290",
name = "๋”ธ๊ธฐ ํƒ€๋ฅดํŠธ",
price = 7500
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/210/310",
name = "ํฌ๋กœ์™€์ƒ",
price = 3200
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/230/330",
name = "์—ฐ์–ด ๋ฎ๋ฐฅ",
price = 13500
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/200/250",
name = "์Šคํ…Œ์ดํฌ",
price = 28900
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/260/360",
name = "๋งˆ์นด๋กฑ ์„ธํŠธ",
price = 9600
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/180/300",
name = "์ดˆ๋ฐฅ ๋ชจ๋‘ ",
price = 19800
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/240/340",
name = "๋ž์Šคํ„ฐ ์š”๋ฆฌ",
price = 49900
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/210/290",
name = "๋ชจ์นด ๋ผ๋–ผ",
price = 4600
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/220/350",
name = "๋ถˆ๊ณ ๊ธฐ ๋ฎ๋ฐฅ",
price = 12500
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/250/320",
name = "๋ง๊ณ  ๋น™์ˆ˜",
price = 11000
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/230/300",
name = "์น˜์ฆˆ ์ผ€์ดํฌ",
price = 7200
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/200/280",
name = "๋ฐ”๋ฒ ํ ํญ๋ฆฝ",
price = 27800
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/260/340",
name = "์ƒˆ์šฐ ํŒŒ์Šคํƒ€",
price = 15800
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/190/310",
name = "ํ‹ฐ๋ผ๋ฏธ์ˆ˜",
price = 8800
),
ProductEntity(
ProductResponse(
id = UUID.randomUUID().toString(),
imageUrl = "https://picsum.photos/220/300",
name = "ํŒ์ฝ˜ ์น˜ํ‚จ",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nextstep.shoppingcart.data.mapper

import nextstep.shoppingcart.data.model.ProductEntity
import nextstep.shoppingcart.data.model.ProductResponse

fun ProductResponse.toEntity(): ProductEntity {
return ProductEntity(
id = id,
imageUrl = imageUrl,
name = name,
price = price,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package nextstep.shoppingcart.data.model

data class CartItemEntity(
val product: ProductEntity,
val count: Int,
val quantity: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package nextstep.shoppingcart.data.model

data class ProductResponse(
val id: String,
val imageUrl: String,
val name: String,
val price: Int,
)
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package nextstep.shoppingcart.data.repository

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import nextstep.shoppingcart.data.datasource.CartLocalDataSource
import nextstep.shoppingcart.data.model.CartItemEntity
import nextstep.shoppingcart.data.model.ProductEntity
import nextstep.shoppingcart.data.util.SingletonHolder

/**
* ๊ตฌํ˜„์„ ๊ฐ„์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด interface ๋Œ€์‹  ๊ตฌํ˜„์ฒด ๋ฐ”๋กœ ์‚ฌ์šฉ
*/
class CartRepository private constructor(
private val cartLocalDataSource: CartLocalDataSource,
) {

fun getItems(): Flow<List<CartItemEntity>> = cartLocalDataSource.itemsFlow.map { items ->
items
}

fun addOne(product: ProductEntity) {
cartLocalDataSource.addOne(product)
}

fun removeOne(product: ProductEntity) {
cartLocalDataSource.removeOne(product)
}
val items: Flow<LinkedHashMap<String, CartItemEntity>> = cartLocalDataSource.items

fun removeAll(product: ProductEntity) {
cartLocalDataSource.removeAll(product)
fun update(cartItem: CartItemEntity) {
cartLocalDataSource.update(cartItem)
}

companion object : SingletonHolder<CartRepository>(
Expand Down
Loading