-
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 |
---|---|---|
@@ -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( | ||
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. BottomBar를 재사용하기 좋도록 잘 분리하셨네요. 👍 |
||
text: String = stringResource(R.string.add_to_cart), | ||
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. 이 부분도 옵셔널 파라미터가 아니어도 괜찮을 것 같아요. |
||
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 | ||
|
@@ -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 | ||
|
@@ -36,76 +31,44 @@ import coil.compose.AsyncImage | |
import nextstep.shoppingcart.domain.model.CartItem | ||
import nextstep.shoppingcart.domain.model.Price | ||
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. Price는 도메인 모델로 구분하셨네요. |
||
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 | ||
) { | ||
|
@@ -191,5 +154,5 @@ private fun ShoppingCartPreview() { | |
count = 2 | ||
) | ||
) | ||
ShoppingCartScreen(items) | ||
CartItemList(items) | ||
} |
This file was deleted.
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.
액티비티와 컴포저블 관련부를 나눠도 좋겠다는 생각이 드는데 악어는 어떻게 생각하세요?
가독성이 좋아지고 UI 관련은 컴포저블 함수를 다루는 클래스에서만 작업하면 편리할 것 같아서요.
데이터바인딩을 사용하면 액티비티에서 뷰 관련 코드가 많이 줄어들기도 하잖아요. 그런 관점에서 뷰(=컴포저블 관련 부분)으로 생각해 볼 수 있지 않을까요?
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.
오 그렇네요! 알려주셔서 감사합니다!
해당내용 반영했습니다!
(컴포저블 관련부를 분리하는 것도 사실상 기본적인 부분이었던 것 같은데,
새로운 것을 배울 때마다 기본적인 부분을 잘 생각하지 못한 것 같습니다...ㅎㅎ 😅 )