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

[악어] 장바구니 미션 제출합니다. #6

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import nextstep.shoppingcart.data.Cart
import nextstep.shoppingcart.domain.model.CartItem
import nextstep.shoppingcart.presentation.components.CartLayout
import nextstep.shoppingcart.presentation.components.screens.CartScreen
import nextstep.shoppingcart.presentation.ui.theme.AndroidshoppingcartTheme

class CartActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var cart by remember { mutableStateOf(Cart) }
var cartItems by remember { mutableStateOf(Cart.items) }
AndroidshoppingcartTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
CartLayout(
CartScreen(
navigation = { finish() },
modifier = Modifier.padding(innerPadding),
cartItems = cartItems,
cart = cart,
onIncrease = {
onIncrease(it)
cartItems = Cart.items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import nextstep.shoppingcart.data.CachedProductDataSource
import nextstep.shoppingcart.data.CachedProductRepository
import nextstep.shoppingcart.data.Cart
import nextstep.shoppingcart.domain.model.Product
import nextstep.shoppingcart.presentation.components.DetailLayout
import nextstep.shoppingcart.presentation.components.screens.DetailScreen
import nextstep.shoppingcart.presentation.ui.theme.AndroidshoppingcartTheme

class DetailActivity : ComponentActivity() {

Choose a reason for hiding this comment

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

액티비티와 컴포저블 관련부를 나눠도 좋겠다는 생각이 드는데 악어는 어떻게 생각하세요?
가독성이 좋아지고 UI 관련은 컴포저블 함수를 다루는 클래스에서만 작업하면 편리할 것 같아서요.
데이터바인딩을 사용하면 액티비티에서 뷰 관련 코드가 많이 줄어들기도 하잖아요. 그런 관점에서 뷰(=컴포저블 관련 부분)으로 생각해 볼 수 있지 않을까요?

Copy link
Author

Choose a reason for hiding this comment

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

오 그렇네요! 알려주셔서 감사합니다!
해당내용 반영했습니다!

(컴포저블 관련부를 분리하는 것도 사실상 기본적인 부분이었던 것 같은데,
새로운 것을 배울 때마다 기본적인 부분을 잘 생각하지 못한 것 같습니다...ㅎㅎ 😅 )

Expand All @@ -18,7 +18,7 @@ class DetailActivity : ComponentActivity() {
val product = fetchProduct()
setContent {
AndroidshoppingcartTheme {
DetailLayout(
DetailScreen(
product = product,
navigation = { navigateBack() },
action = { addToCart(product = product) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import nextstep.shoppingcart.data.CachedProductDataSource
import nextstep.shoppingcart.data.CachedProductRepository
import nextstep.shoppingcart.presentation.components.ListLayout
import nextstep.shoppingcart.presentation.components.screens.HomeScreen
import nextstep.shoppingcart.ui.theme.ShoppingCartTheme

class HomeActivity : ComponentActivity() {
Expand All @@ -15,7 +15,7 @@ class HomeActivity : ComponentActivity() {

setContent {
ShoppingCartTheme {
ListLayout(
HomeScreen(
products = repository.fetchProducts(),
action = { navigateToCart() },
onItemClick = { productId -> navigateToDetail(productId) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextstep.shoppingcart.presentation.components

import androidx.compose.foundation.layout.Box
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import nextstep.signup.R

@Composable
fun BottomBar(

Choose a reason for hiding this comment

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

BottomBar를 재사용하기 좋도록 잘 분리하셨네요. 👍

text: String = stringResource(R.string.add_to_cart),

Choose a reason for hiding this comment

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

이 부분도 옵셔널 파라미터가 아니어도 괜찮을 것 같아요.

modifier: Modifier = Modifier
) {
Box(
contentAlignment = Alignment.Center,
modifier = modifier
) {
Text(
text = text,
fontSize = 20.sp,
style =
TextStyle(
color = Color.White,
fontWeight = FontWeight.Bold
)
)
}
}

@Preview
@Composable
private fun BottomBarPreview() {
BottomBar()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package nextstep.shoppingcart.presentation.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
Expand All @@ -19,13 +16,11 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
Expand All @@ -36,76 +31,44 @@ import coil.compose.AsyncImage
import nextstep.shoppingcart.domain.model.CartItem
import nextstep.shoppingcart.domain.model.Price

Choose a reason for hiding this comment

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

Price는 도메인 모델로 구분하셨네요.
그런데 UI를 위한 포맷팅 작업을 하고 있습니다.
이 작업 UI 관련으로 볼 수도 있는데 도메인 모델에 적절할까요? 참으로 애매한 문제죠? 🤔
참고로 xml을 통해서 해결할 수 있는 방법도 있습니다. ("%,d원")

import nextstep.shoppingcart.domain.model.Product
import nextstep.signup.R

@Composable
fun ShoppingCartScreen(
fun CartItemList(
items: List<CartItem> = emptyList(),
onIncrease: (CartItem) -> Unit = {},
onDecrease: (CartItem) -> Unit = {},
onClear: (CartItem) -> Unit = {},
modifier: Modifier = Modifier
modifier: Modifier = Modifier,
) {
val totalPrice = items.sumOf { it.product.price * it.count }

Scaffold(
bottomBar = {
Box(
modifier =
Modifier
LazyColumn(
modifier = modifier
) {
items(items) { item ->
CartItem(
item = item,
onIncrease = onIncrease,
onDecrease = onDecrease,
onClear = onClear,
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFF2196F3))
.padding(16.dp),
contentAlignment = Alignment.Center
) {
Text(
text = stringResource(R.string.order, Price(totalPrice).format()),
fontSize = 20.sp,
style =
TextStyle(
color = Color.White,
fontWeight = FontWeight.Bold
)
)
}
}
) { innerPadding ->
Column(
modifier =
Modifier
.fillMaxSize()
.padding(innerPadding)
) {
LazyColumn {
items(items) { item ->
CartItemView(
item = item,
onIncrease = onIncrease,
onDecrease = onDecrease,
onClear = onClear
)
}
}

Spacer(modifier = Modifier.weight(1f))
.padding(8.dp)
.border(1.dp, Color.Gray, shape = RoundedCornerShape(4.dp)),
)
}
}
}


@Composable
fun CartItemView(
fun CartItem(
item: CartItem,
onIncrease: (CartItem) -> Unit = {},
onDecrease: (CartItem) -> Unit = {},
onClear: (CartItem) -> Unit = {},
modifier: Modifier = Modifier
) {
Row(
modifier =
Modifier
.fillMaxWidth()
.padding(8.dp)
.border(1.dp, Color.Gray, shape = RoundedCornerShape(4.dp)),
modifier = modifier,
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Expand Down Expand Up @@ -191,5 +154,5 @@ private fun ShoppingCartPreview() {
count = 2
)
)
ShoppingCartScreen(items)
CartItemList(items)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fun CounterForm(
onDecrease: () -> Unit,
modifier: Modifier = Modifier
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier
) {
Box(
modifier =
Modifier
Expand Down
Loading