feat(gift): 선물 상세 조회 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
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.GiftCancelResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
|
||||
sealed interface GiftDetailUiState {
|
||||
data object Loading : GiftDetailUiState
|
||||
data class Content(val detail: GiftDetailResponse) : GiftDetailUiState
|
||||
data class Canceled(val detail: GiftDetailResponse, val cancel: GiftCancelResponse?) : GiftDetailUiState
|
||||
data class Error(val message: String?) : GiftDetailUiState
|
||||
}
|
||||
|
||||
class GiftDetailViewModel(private val repository: GiftRepository) : BaseViewModel() {
|
||||
private val mutableState = MutableLiveData<GiftDetailUiState>()
|
||||
val state: LiveData<GiftDetailUiState> = mutableState
|
||||
private val mutableFinishError = MutableLiveData<ToastMessage?>()
|
||||
val finishErrorLiveData: LiveData<ToastMessage?> = mutableFinishError
|
||||
private val mutableInquiry = MutableLiveData(false)
|
||||
val inquiryLiveData: LiveData<Boolean> = mutableInquiry
|
||||
private var applicationNo: String? = null
|
||||
private var pendingCancellation: GiftCancelResponse? = null
|
||||
|
||||
fun loadInitial(applicationNo: String) {
|
||||
if (this.applicationNo != null) return
|
||||
this.applicationNo = applicationNo
|
||||
if (applicationNo.isBlank()) {
|
||||
mutableFinishError.value = ToastMessage(resId = R.string.gift_send_invalid_access)
|
||||
return
|
||||
}
|
||||
load(applicationNo)
|
||||
}
|
||||
|
||||
fun retry() {
|
||||
val number = applicationNo?.takeIf { it.isNotBlank() } ?: return
|
||||
if (mutableState.value == GiftDetailUiState.Loading) return
|
||||
load(number)
|
||||
}
|
||||
|
||||
fun consumeFinishError(): ToastMessage? = mutableFinishError.value.also { mutableFinishError.value = null }
|
||||
|
||||
fun applyCancellation(cancel: GiftCancelResponse) {
|
||||
if (applicationNo != cancel.applicationNo) return
|
||||
pendingCancellation = cancel
|
||||
val detail = when (val current = mutableState.value) {
|
||||
is GiftDetailUiState.Content -> current.detail
|
||||
is GiftDetailUiState.Canceled -> current.detail
|
||||
else -> return
|
||||
}
|
||||
if (detail.applicationNo == applicationNo) {
|
||||
mutableState.value = GiftDetailUiState.Canceled(detail, cancel)
|
||||
}
|
||||
}
|
||||
|
||||
fun onInquiryClick() {
|
||||
mutableInquiry.value = true
|
||||
}
|
||||
|
||||
fun consumeInquiryIntent(): Intent? {
|
||||
if (mutableInquiry.value != true) return null
|
||||
mutableInquiry.value = false
|
||||
return Intent(Intent.ACTION_VIEW, Uri.parse("http://pf.kakao.com/_lkxgxhG/chat"))
|
||||
}
|
||||
|
||||
private fun load(number: String) {
|
||||
mutableState.value = GiftDetailUiState.Loading
|
||||
compositeDisposable.add(
|
||||
repository.getGiftDetail(number, "Bearer ${SharedPreferenceManager.token}")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
val detail = response.data
|
||||
mutableState.value = if (response.success && detail != null) {
|
||||
val cancel = pendingCancellation?.takeIf { it.applicationNo == applicationNo }
|
||||
if (detail.status == "CANCELED" || cancel != null) {
|
||||
GiftDetailUiState.Canceled(detail, cancel)
|
||||
} else {
|
||||
GiftDetailUiState.Content(detail)
|
||||
}
|
||||
} else {
|
||||
GiftDetailUiState.Error(response.message)
|
||||
}
|
||||
}, { mutableState.value = GiftDetailUiState.Error(null) })
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user