Skip to content

Commit 70b7d52

Browse files
committed
Refactor : CardListScreen 상태 호이스팅 적용
1. 비즈니스 로직이 들어가지 않는 CardListScreen 컴포넌트 추가 2. 파라미터 명 변경 (cards -> registeredCreditCards), cards는 카드 리스트를 의미함
1 parent 97ca6a1 commit 70b7d52

File tree

3 files changed

+95
-91
lines changed

3 files changed

+95
-91
lines changed

app/src/androidTest/java/nextstep/payments/screen/card/list/RegisteredCreditCardsScreenTest.kt

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.test.onNodeWithText
77
import nextstep.payments.data.BcCard
88
import nextstep.payments.data.Card
99
import nextstep.payments.data.PaymentCardsRepository
10+
import nextstep.payments.data.RegisteredCreditCards
1011
import nextstep.payments.ui.card.list.CardListScreen
1112
import org.junit.Before
1213
import org.junit.Rule
@@ -28,7 +29,7 @@ class RegisteredCreditCardsScreenTest {
2829
composeRule
2930
.setContent {
3031
CardListScreen(
31-
cards = PaymentCardsRepository.cards,
32+
registeredCreditCards = RegisteredCreditCards(mutableListOf()),
3233
onAddCard = {}
3334
)
3435
}
@@ -42,30 +43,29 @@ class RegisteredCreditCardsScreenTest {
4243
@Test
4344
fun 등록된_카드가_존재할_경우_해당_카드_수만큼_카드_이미지를_노출시키다() {
4445
// given : 두 개의 카드를 등록한다
45-
PaymentCardsRepository.addCard(
46-
Card(
47-
cardNumber = "1234-5678-1234-5678",
48-
ownerName = "홍길동",
49-
expiredDate = "12/24",
50-
password = "123",
51-
cardCompany = BcCard
52-
)
53-
)
54-
55-
PaymentCardsRepository.addCard(
56-
Card(
57-
cardNumber = "1234-5678-1234-5628",
58-
ownerName = "홍길동",
59-
expiredDate = "12/24",
60-
password = "123",
61-
cardCompany = BcCard
46+
val registeredCreditCards = RegisteredCreditCards(
47+
mutableListOf(
48+
Card(
49+
cardNumber = "1234-5678-1234-5678",
50+
ownerName = "홍길동",
51+
expiredDate = "12/24",
52+
password = "123",
53+
cardCompany = BcCard
54+
),
55+
Card(
56+
cardNumber = "1234-5678-1234-5628",
57+
ownerName = "홍길동",
58+
expiredDate = "12/24",
59+
password = "123",
60+
cardCompany = BcCard
61+
)
6262
)
6363
)
6464

6565
// when : 화면을 렌더링한다.
6666
composeRule.setContent {
6767
CardListScreen(
68-
cards = PaymentCardsRepository.cards
68+
registeredCreditCards = registeredCreditCards
6969
)
7070
}
7171
val actual = composeRule
@@ -81,7 +81,7 @@ class RegisteredCreditCardsScreenTest {
8181
// when : 화면을 렌더링한다.
8282
composeRule.setContent {
8383
CardListScreen(
84-
cards = PaymentCardsRepository.cards
84+
registeredCreditCards = RegisteredCreditCards(mutableListOf())
8585
)
8686
}
8787

@@ -100,7 +100,7 @@ class RegisteredCreditCardsScreenTest {
100100
// cardImage
101101
composeRule.setContent {
102102
CardListScreen(
103-
cards = PaymentCardsRepository.cards
103+
registeredCreditCards = RegisteredCreditCards(mutableListOf())
104104
)
105105
}
106106

@@ -122,11 +122,23 @@ class RegisteredCreditCardsScreenTest {
122122
cardCompany = BcCard
123123
)
124124
)
125+
val registeredCreditCards = RegisteredCreditCards(
126+
mutableListOf(
127+
Card(
128+
cardNumber = "1234-5678-1234-5628",
129+
ownerName = "홍길동",
130+
expiredDate = "12/24",
131+
password = "123",
132+
cardCompany = BcCard
133+
)
134+
)
135+
)
136+
125137

126138
// when : 화면을 렌더링한다.
127139
composeRule.setContent {
128140
CardListScreen(
129-
cards = PaymentCardsRepository.cards
141+
registeredCreditCards = registeredCreditCards
130142
)
131143
}
132144

app/src/main/java/nextstep/payments/ui/card/list/CardListScreen.kt

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
1515
import androidx.lifecycle.viewmodel.compose.viewModel
1616
import nextstep.payments.data.BcCard
1717
import nextstep.payments.data.Card
18+
import nextstep.payments.data.PaymentCardsRepository
1819
import nextstep.payments.data.RegisteredCreditCards
1920
import nextstep.payments.ui.card.CreditCardUiState
20-
import nextstep.payments.data.PaymentCardsRepository
2121
import nextstep.payments.ui.card.list.component.card.CardLazyColumn
2222
import nextstep.payments.ui.card.list.component.card.CardListTopBar
2323
import nextstep.payments.ui.card.list.component.card.CardListTopBarWithAdd
@@ -31,7 +31,19 @@ fun CardListScreen(
3131
) {
3232
val cards by viewModel.registeredCreditCards.collectAsStateWithLifecycle()
3333

34-
when (cards.getState()) {
34+
CardListScreen(
35+
registeredCreditCards = cards,
36+
onAddCard = onAddCard,
37+
)
38+
}
39+
40+
@Composable
41+
fun CardListScreen(
42+
registeredCreditCards: RegisteredCreditCards = RegisteredCreditCards(mutableListOf()),
43+
onAddCard: () -> Unit = {}
44+
) {
45+
46+
when (registeredCreditCards.getState()) {
3547
is CreditCardUiState.Empty -> {
3648
CardListScreenEmpty(
3749
onAddCard = onAddCard,
@@ -40,14 +52,14 @@ fun CardListScreen(
4052

4153
is CreditCardUiState.One -> {
4254
CardListScreenOne(
43-
cards = cards,
55+
cards = registeredCreditCards.cardList,
4456
onAddCard = onAddCard,
4557
)
4658
}
4759

4860
is CreditCardUiState.Many -> {
4961
CardListScreenMany(
50-
cards = cards,
62+
cards = registeredCreditCards.cardList,
5163
onAddCard = onAddCard,
5264
)
5365
}
@@ -58,9 +70,7 @@ fun CardListScreen(
5870
fun CardListScreenEmpty(
5971
onAddCard: () -> Unit = {}
6072
) {
61-
Scaffold(
62-
topBar = { CardListTopBar() }
63-
) { paddingValues ->
73+
Scaffold(topBar = { CardListTopBar() }) { paddingValues ->
6474
Column(
6575
modifier = Modifier.padding(paddingValues)
6676
) {
@@ -77,10 +87,7 @@ fun CardListScreenEmpty(
7787
modifier = Modifier
7888
.fillMaxWidth()
7989
.padding(
80-
start = 73.dp,
81-
end = 73.dp,
82-
top = 12.dp,
83-
bottom = 24.dp
90+
start = 73.dp, end = 73.dp, top = 12.dp, bottom = 24.dp
8491
)
8592
.size(width = 208.dp, height = 124.dp)
8693
)
@@ -90,12 +97,10 @@ fun CardListScreenEmpty(
9097

9198
@Composable
9299
fun CardListScreenOne(
93-
cards: RegisteredCreditCards,
100+
cards: List<Card>,
94101
onAddCard: () -> Unit = {}
95102
) {
96-
Scaffold(
97-
topBar = { CardListTopBar() }
98-
) { paddingValues ->
103+
Scaffold(topBar = { CardListTopBar() }) { paddingValues ->
99104
Column(
100105
modifier = Modifier.padding(paddingValues)
101106
) {
@@ -107,10 +112,7 @@ fun CardListScreenOne(
107112
modifier = Modifier
108113
.fillMaxWidth()
109114
.padding(
110-
start = 73.dp,
111-
end = 73.dp,
112-
top = 12.dp,
113-
bottom = 24.dp
115+
start = 73.dp, end = 73.dp, top = 12.dp, bottom = 24.dp
114116
)
115117
.size(width = 208.dp, height = 124.dp)
116118
)
@@ -120,16 +122,14 @@ fun CardListScreenOne(
120122

121123
@Composable
122124
fun CardListScreenMany(
123-
cards: RegisteredCreditCards,
125+
cards: List<Card>,
124126
onAddCard: () -> Unit = {}
125127
) {
126-
Scaffold(
127-
topBar = {
128-
CardListTopBarWithAdd(
129-
onClickAdd = onAddCard
130-
)
131-
}
132-
) { paddingValues ->
128+
Scaffold(topBar = {
129+
CardListTopBarWithAdd(
130+
onClickAdd = onAddCard
131+
)
132+
}) { paddingValues ->
133133
Column(
134134
modifier = Modifier.padding(paddingValues)
135135
) {
@@ -144,66 +144,57 @@ private fun CardListScreenEmptyPreview() {
144144
PaymentCardsRepository.removeAllCard()
145145
PaymentsTheme {
146146
CardListScreen(
147+
registeredCreditCards = RegisteredCreditCards(mutableListOf()),
147148
onAddCard = {}
148149
)
149150
}
150151
}
151152

152-
153153
@Preview
154154
@Composable
155155
private fun CardListScreenOnePreview() {
156-
PaymentCardsRepository.addCard(
157-
Card(
158-
cardNumber = "1234-5678-1234-5678",
159-
ownerName = "홍길동",
160-
expiredDate = "12/24",
161-
password = "123",
162-
cardCompany = BcCard
163-
)
164-
)
165-
166-
val viewModel = CardListViewModel()
167-
168156
PaymentsTheme {
169157
CardListScreen(
170-
viewModel = viewModel,
158+
registeredCreditCards = RegisteredCreditCards(
159+
cardList = listOf(
160+
Card(
161+
cardNumber = "1234-5678-1234-6654",
162+
ownerName = "홍길동",
163+
expiredDate = "12/24",
164+
password = "123",
165+
cardCompany = BcCard
166+
)
167+
)
168+
),
171169
onAddCard = {}
172170
)
173171
}
174172
}
175173

176-
177174
@Preview
178175
@Composable
179176
private fun CardListScreenManyPreview() {
180-
PaymentCardsRepository.addCard(
181-
Card(
182-
cardNumber = "1234-5678-1234-6654",
183-
ownerName = "홍길동",
184-
expiredDate = "12/24",
185-
password = "123",
186-
cardCompany = BcCard
187-
)
188-
)
189-
190-
PaymentCardsRepository.addCard(
191-
Card(
192-
cardNumber = "1234-5678-1234-1234",
193-
ownerName = "홍길동",
194-
expiredDate = "12/24",
195-
password = "123",
196-
cardCompany = BcCard
177+
val registeredCreditCards = RegisteredCreditCards(
178+
cardList = listOf(
179+
Card(
180+
cardNumber = "1234-5678-1234-6654",
181+
ownerName = "홍길동",
182+
expiredDate = "12/24",
183+
password = "123",
184+
cardCompany = BcCard
185+
),
186+
Card(
187+
cardNumber = "1234-5678-1234-1234",
188+
ownerName = "홍길동",
189+
expiredDate = "12/24",
190+
password = "123",
191+
cardCompany = BcCard
192+
)
197193
)
198194
)
199195

200-
val viewModel = CardListViewModel()
201-
202196
PaymentsTheme {
203-
CardListScreen(
204-
viewModel = viewModel,
205-
onAddCard = {}
206-
)
197+
CardListScreen(registeredCreditCards = registeredCreditCards, onAddCard = {})
207198
}
208199
}
209200

app/src/main/java/nextstep/payments/ui/card/list/component/card/CardLazyColumn.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import androidx.compose.runtime.Composable
66
import androidx.compose.ui.Modifier
77
import androidx.compose.ui.graphics.Color
88
import androidx.compose.ui.unit.dp
9+
import nextstep.payments.data.Card
910
import nextstep.payments.data.RegisteredCreditCards
1011
import nextstep.payments.data.PaymentCardsRepository
1112

1213
@Composable
13-
fun CardLazyColumn(cards: RegisteredCreditCards) {
14+
fun CardLazyColumn(cards: List<Card>) {
1415
LazyColumn {
1516
items(
16-
count = cards.cardList.size,
17-
key = { index -> PaymentCardsRepository.cards[index].cardNumber }
17+
count = cards.size,
18+
key = { index -> cards[index].cardNumber }
1819
) {
1920
CardImage(
20-
card = PaymentCardsRepository.cards[it],
21+
card = cards[it],
2122
cardColor = Color(0xFFE5E5E5),
2223
modifier = Modifier.padding(
2324
start = 73.dp,

0 commit comments

Comments
 (0)