feat(gift): 배송지 입력 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.ToastMessage
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRecipientAddressRequest
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
|
||||
data class GiftRecipientAddressFormState(
|
||||
val recipientName: String = "",
|
||||
val recipientPhoneNumber: String = "",
|
||||
val recipientZipCode: String = "",
|
||||
val recipientAddress: String = "",
|
||||
val recipientAddressDetail: String = "",
|
||||
val recipientTermsAgreed: Boolean = false,
|
||||
val recipientPrivacyAgreed: Boolean = false
|
||||
) {
|
||||
val isSubmitEnabled: Boolean
|
||||
get() = recipientName.isNotBlank() &&
|
||||
recipientPhoneNumber.isNotBlank() &&
|
||||
recipientZipCode.isNotBlank() &&
|
||||
recipientAddress.isNotBlank() &&
|
||||
recipientTermsAgreed &&
|
||||
recipientPrivacyAgreed
|
||||
|
||||
fun toRequest() = GiftRecipientAddressRequest(
|
||||
recipientName = recipientName,
|
||||
recipientPhoneNumber = recipientPhoneNumber,
|
||||
recipientZipCode = recipientZipCode,
|
||||
recipientAddress = recipientAddress,
|
||||
recipientAddressDetail = recipientAddressDetail,
|
||||
recipientTermsAgreed = recipientTermsAgreed,
|
||||
recipientPrivacyAgreed = recipientPrivacyAgreed
|
||||
)
|
||||
}
|
||||
|
||||
class GiftRecipientAddressViewModel(private val repository: GiftRepository) : BaseViewModel() {
|
||||
private val mutableFormState = MutableLiveData(GiftRecipientAddressFormState())
|
||||
val formState: LiveData<GiftRecipientAddressFormState> = mutableFormState
|
||||
private val mutableSubmitting = MutableLiveData(false)
|
||||
val isSubmitting: LiveData<Boolean> = mutableSubmitting
|
||||
private val mutableToast = MutableLiveData<ToastMessage?>()
|
||||
val toastLiveData: LiveData<ToastMessage?> = mutableToast
|
||||
private val mutableRecipientAddressSuccess = MutableLiveData(false)
|
||||
val recipientAddressSuccessLiveData: LiveData<Boolean> = mutableRecipientAddressSuccess
|
||||
|
||||
fun updateForm(formState: GiftRecipientAddressFormState) {
|
||||
mutableFormState.value = formState
|
||||
}
|
||||
|
||||
fun submit(applicationNo: String) {
|
||||
val form = mutableFormState.value ?: return
|
||||
if (applicationNo.isBlank() || !form.isSubmitEnabled || mutableSubmitting.value == true) return
|
||||
mutableSubmitting.value = true
|
||||
compositeDisposable.add(
|
||||
repository.registerRecipientAddress(applicationNo, form.toRequest(), authToken())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
mutableSubmitting.value = false
|
||||
if (response.success && response.data != null) {
|
||||
mutableToast.value = ToastMessage(resId = R.string.gift_recipient_address_success)
|
||||
mutableRecipientAddressSuccess.value = true
|
||||
} else {
|
||||
showToast(response.message)
|
||||
}
|
||||
}, {
|
||||
mutableSubmitting.value = false
|
||||
showToast(null)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
fun consumeToast(): ToastMessage? = mutableToast.value.also { mutableToast.value = null }
|
||||
|
||||
fun consumeRecipientAddressSuccessEvent(): Boolean {
|
||||
if (mutableRecipientAddressSuccess.value != true) return false
|
||||
mutableRecipientAddressSuccess.value = false
|
||||
return true
|
||||
}
|
||||
|
||||
private fun showToast(message: String?) {
|
||||
mutableToast.value = if (message.isNullOrBlank()) {
|
||||
ToastMessage(resId = R.string.common_error_unknown)
|
||||
} else {
|
||||
ToastMessage(message = message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
}
|
||||
Reference in New Issue
Block a user