feat(gift): 운송장 등록 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
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.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
|
||||
sealed interface GiftTrackingUiState {
|
||||
data object Loading : GiftTrackingUiState
|
||||
data class Content(val detail: GiftTrackingUiModel) : GiftTrackingUiState
|
||||
data class Error(val message: String?) : GiftTrackingUiState
|
||||
}
|
||||
|
||||
class GiftTrackingViewModel(private val repository: GiftRepository) : BaseViewModel() {
|
||||
private val mutableState = MutableLiveData<GiftTrackingUiState>()
|
||||
val state: LiveData<GiftTrackingUiState> = mutableState
|
||||
private val mutableSubmitting = MutableLiveData(false)
|
||||
val isSubmitting: LiveData<Boolean> = mutableSubmitting
|
||||
private val mutableToast = MutableLiveData<ToastMessage?>()
|
||||
val toastLiveData: LiveData<ToastMessage?> = mutableToast
|
||||
private val mutableIneligible = MutableLiveData(false)
|
||||
val ineligibleLiveData: LiveData<Boolean> = mutableIneligible
|
||||
private val mutableSubmitSuccess = MutableLiveData(false)
|
||||
val submitSuccessLiveData: LiveData<Boolean> = mutableSubmitSuccess
|
||||
private var applicationNo: String? = null
|
||||
var loadedDetail: GiftDetailResponse? = null
|
||||
private set
|
||||
|
||||
fun loadInitial(applicationNo: String) {
|
||||
if (this.applicationNo != null) return
|
||||
this.applicationNo = applicationNo
|
||||
if (applicationNo.isBlank()) {
|
||||
mutableIneligible.value = true
|
||||
return
|
||||
}
|
||||
load(applicationNo)
|
||||
}
|
||||
|
||||
fun retry() {
|
||||
if (mutableState.value !is GiftTrackingUiState.Error) return
|
||||
val number = applicationNo ?: return
|
||||
load(number)
|
||||
}
|
||||
|
||||
fun submitTracking(courier: String?, trackingNumber: String) {
|
||||
val detail = (mutableState.value as? GiftTrackingUiState.Content)?.detail ?: return
|
||||
if (!detail.isEligible || mutableSubmitting.value == true) return
|
||||
if (courier == null || !GiftTrackingPresenter.isSubmitEnabled(courier, trackingNumber)) return
|
||||
val number = applicationNo ?: return
|
||||
mutableSubmitting.value = true
|
||||
compositeDisposable.add(
|
||||
repository.registerGiftTracking(number, GiftTrackingPresenter.toRequest(courier, trackingNumber), authToken())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
mutableSubmitting.value = false
|
||||
if (response.success && response.data != null) {
|
||||
mutableSubmitSuccess.value = true
|
||||
} else {
|
||||
showToast(response.message)
|
||||
}
|
||||
}, {
|
||||
mutableSubmitting.value = false
|
||||
showToast(null)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
fun consumeToast(): ToastMessage? = mutableToast.value.also { mutableToast.value = null }
|
||||
|
||||
fun consumeIneligibleEvent(): Boolean {
|
||||
if (mutableIneligible.value != true) return false
|
||||
mutableIneligible.value = false
|
||||
return true
|
||||
}
|
||||
|
||||
fun consumeSubmitSuccessEvent(): Boolean {
|
||||
if (mutableSubmitSuccess.value != true) return false
|
||||
mutableSubmitSuccess.value = false
|
||||
return true
|
||||
}
|
||||
|
||||
private fun load(number: String) {
|
||||
loadedDetail = null
|
||||
mutableState.value = GiftTrackingUiState.Loading
|
||||
compositeDisposable.add(
|
||||
repository.getGiftDetail(number, authToken())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
val detail = response.data
|
||||
if (response.success && detail != null) {
|
||||
val model = GiftTrackingPresenter.present(detail)
|
||||
loadedDetail = detail.takeIf { model.isEligible }
|
||||
mutableState.value = GiftTrackingUiState.Content(model)
|
||||
if (!model.isEligible) mutableIneligible.value = true
|
||||
} else {
|
||||
mutableState.value = GiftTrackingUiState.Error(response.message)
|
||||
}
|
||||
}, { mutableState.value = GiftTrackingUiState.Error(null) })
|
||||
)
|
||||
}
|
||||
|
||||
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