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

Step4 - 페이먼츠(카드 수정) #53

Open
wants to merge 18 commits into
base: oyj7677
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,4 @@ dependencies {
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
implementation(kotlin("script-runtime"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ class RegisteredCreditCardsScreenTest {
val registeredCreditCards = RegisteredCreditCards(
mutableListOf(
Card(
id = 1,
cardNumber = "1234-5678-1234-5678",
ownerName = "홍길동",
expiredDate = "12/24",
password = "123",
bankType = BankType.BC
),
Card(
id = 2,
cardNumber = "1234-5678-1234-5628",
ownerName = "홍길동",
expiredDate = "12/24",
Expand Down Expand Up @@ -120,6 +122,7 @@ class RegisteredCreditCardsScreenTest {
// given : 카드 등록이 되어있다.
PaymentCardsRepository.addCard(
Card(
id = 13,
cardNumber = "1234-5678-1234-5628",
ownerName = "홍길동",
expiredDate = "12/24",
Expand All @@ -130,6 +133,7 @@ class RegisteredCreditCardsScreenTest {
val registeredCreditCards = RegisteredCreditCards(
mutableListOf(
Card(
id = 4,
cardNumber = "1234-5678-1234-5628",
ownerName = "홍길동",
expiredDate = "12/24",
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/nextstep/payments/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import nextstep.payments.ui.card.registration.NewCardActivity
import nextstep.payments.ui.theme.PaymentsTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val viewModel by viewModels<CardListViewModel>()
private val viewModel: CardListViewModel by viewModels { CardListViewModel.Factory }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
Expand Down Expand Up @@ -52,6 +52,16 @@ class MainActivity : ComponentActivity() {
}
}
}

override fun onPause() {
super.onPause()
viewModel.saveCard()
}

override fun onResume() {
super.onResume()
viewModel.openCard()
}
}


2 changes: 1 addition & 1 deletion app/src/main/java/nextstep/payments/data/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.parcelize.Parcelize

@Parcelize
data class Card(
val id: Int,
var id: Int = 0,
val cardNumber: String,
val expiredDate: String,
val ownerName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package nextstep.payments.data
object PaymentCardsRepository {

private val _cards = mutableListOf<Card>()
val cards: List<Card> get() = _cards.toList()
val cards: List<Card> get() = _cards

fun addCard(card: Card) {
if (card.id == 0) card.id = createId()
_cards.add(card)
}

fun removeAllCard() {
_cards.clear()
}

fun editCard(oldCard: Card?, newCard: Card) {
val index = _cards.indexOfFirst { it.id == oldCard!!.id }
fun editCard(newCard: Card) {
val index = _cards.indexOfFirst { it.id == newCard.id }
_cards[index] = newCard
}

fun createId(): Int {
private fun createId(): Int {
return _cards.maxOfOrNull { it.id }?.plus(1) ?: 1
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package nextstep.payments.data

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import nextstep.payments.ui.card.CreditCardUiState

data class RegisteredCreditCards(val cardList: List<Card>) {
@Parcelize
data class RegisteredCreditCards(val cardList: List<Card>) : Parcelable {

fun getState(): CreditCardUiState {
return when (cardList.size) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/nextstep/payments/ui/PaymentCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fun PaymentCard(
@Composable
fun PaymentCardContents(
card: Card,
modifier: Modifier = Modifier,
onClick: (Card) -> Unit = {}
) {
Box(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package nextstep.payments.ui.card.list

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.createSavedStateHandle
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import nextstep.payments.data.PaymentCardsRepository
import nextstep.payments.data.RegisteredCreditCards

class CardListViewModel : ViewModel() {
class CardListViewModel(
val savedStateHandle: SavedStateHandle,
) : ViewModel() {

private val _registeredCreditCards = MutableStateFlow(RegisteredCreditCards(emptyList()))
val registeredCreditCards: StateFlow<RegisteredCreditCards> =
Expand All @@ -16,4 +23,25 @@ class CardListViewModel : ViewModel() {
fun fetchCards() {
_registeredCreditCards.value = RegisteredCreditCards(PaymentCardsRepository.cards)
}

fun saveCard() {
savedStateHandle[KEY_REGISTERED_CREDIT_CARDS] = _registeredCreditCards.value
}

fun openCard() {
_registeredCreditCards.value =
savedStateHandle[KEY_REGISTERED_CREDIT_CARDS] ?: RegisteredCreditCards(emptyList())
}

companion object {
private const val KEY_REGISTERED_CREDIT_CARDS = "registeredCreditCards"
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
val savedStateHandle = createSavedStateHandle()
CardListViewModel(
savedStateHandle = savedStateHandle
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ import nextstep.payments.ui.theme.PaymentsTheme

class NewCardActivity : ComponentActivity() {

private var card: Card? = null
private val viewModel: NewCardViewModel by viewModels { NewCardViewModel.Factory }

override fun onCreate(savedInstanceState: Bundle?) {
val viewModel by viewModels<NewCardViewModel>()
super.onCreate(savedInstanceState)

(intent?.getParcelableExtra("card") as Card?)?.let {
viewModel.setOldCard(it)
viewModel.setUiState(RegistrationUiState.EditCard)
}

setContent {
PaymentsTheme {
NewCardScreen(
Expand All @@ -35,6 +33,16 @@ class NewCardActivity : ComponentActivity() {
}
}
}

override fun onResume() {
super.onResume()
viewModel.openCardData()
}

override fun onPause() {
super.onPause()
viewModel.saveCardData()
}
Comment on lines +37 to +45
Copy link
Member

Choose a reason for hiding this comment

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

Activity와 AAC ViewModel의 LifeCycle은 별도로 굴러가기 때문에 이러한 코드는 작성하지 않으셔도 됩니다. 🙂

}


Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.payments.ui.card.registration

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand All @@ -25,6 +26,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import nextstep.payments.R
Expand All @@ -33,9 +35,9 @@ import nextstep.payments.ui.PaymentCard
import nextstep.payments.ui.card.registration.component.BankSelectRow
import nextstep.payments.ui.card.registration.component.NewCardTopBar
import nextstep.payments.ui.theme.PaymentsTheme
import kotlin.reflect.KFunction0

// Stateful
@SuppressLint("StateFlowValueCalledInComposition")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NewCardScreen(
Expand All @@ -52,7 +54,7 @@ fun NewCardScreen(
val selectedBankType by viewModel.selectedBankType.collectAsStateWithLifecycle()
var showCardCompanyBottomSheet by rememberSaveable { mutableStateOf(true) }
val modalBottomSheetState = rememberModalBottomSheetState(confirmValueChange = { false })
val uiState by viewModel.uiState.collectAsStateWithLifecycle()


LaunchedEffect(cardAdded) {
if (cardAdded) navigateToCardList()
Expand All @@ -77,15 +79,6 @@ fun NewCardScreen(
)
}
}
val saveFunction: KFunction0<Unit> = when (uiState) {
RegistrationUiState.NewCard -> {
viewModel::addCard
}

RegistrationUiState.EditCard -> {
viewModel::editCard
}
}

NewCardScreen(
modifier = modifier,
Expand All @@ -99,7 +92,7 @@ fun NewCardScreen(
setOwnerNamedNumber = viewModel::setOwnerName,
setPasswordNumber = viewModel::setPassword,
onBackClick = onBackClick,
onSaveClick = saveFunction,
onSaveClick = viewModel::saveCard,
)
}

Expand Down Expand Up @@ -181,10 +174,13 @@ private fun NewCardScreen(
@Preview
@Composable
private fun NewCardScreenPreview() {
val savedState = SavedStateHandle(mapOf("someIdArg" to 1))
PaymentsTheme {
NewCardScreen(modifier = Modifier,
navigateToCardList = {},
viewModel = NewCardViewModel().apply {
viewModel = NewCardViewModel(
savedStateHandle = savedState
).apply {
setCardNumber("0000 - 0000 - 0000 -0000")
setExpiredDate("02/26")
setOwnerName("김수현")
Expand Down
Loading