feat(gift): 선물 신청 취소 상태를 추가한다
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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.GiftCancelResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
|
||||
class GiftCancelViewModel(private val repository: GiftRepository) : BaseViewModel() {
|
||||
private val mutableSubmitting = MutableLiveData(false)
|
||||
val isSubmitting: LiveData<Boolean> = mutableSubmitting
|
||||
private val mutableToast = MutableLiveData<ToastMessage?>()
|
||||
val toastLiveData: LiveData<ToastMessage?> = mutableToast
|
||||
private val mutableCancelSuccess = MutableLiveData<GiftCancelResponse?>()
|
||||
val cancelSuccessLiveData: LiveData<GiftCancelResponse?> = mutableCancelSuccess
|
||||
|
||||
fun cancelGift(detail: GiftDetailResponse) {
|
||||
if (detail.direction != "SENT" || detail.status != "RECEIVED") return
|
||||
if (detail.applicationNo.isBlank() || mutableSubmitting.value == true) return
|
||||
mutableSubmitting.value = true
|
||||
compositeDisposable.add(
|
||||
repository.cancelGift(detail.applicationNo, "Bearer ${SharedPreferenceManager.token}")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe({ response ->
|
||||
mutableSubmitting.value = false
|
||||
if (response.success && response.data != null) {
|
||||
mutableCancelSuccess.value = response.data
|
||||
} else {
|
||||
showToast(response.message)
|
||||
}
|
||||
}, {
|
||||
mutableSubmitting.value = false
|
||||
showToast(null)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
fun consumeToast(): ToastMessage? = mutableToast.value.also { mutableToast.value = null }
|
||||
|
||||
fun consumeCancelSuccessEvent(): GiftCancelResponse? =
|
||||
mutableCancelSuccess.value.also { mutableCancelSuccess.value = null }
|
||||
|
||||
private fun showToast(message: String?) {
|
||||
mutableToast.value = if (message.isNullOrBlank()) {
|
||||
ToastMessage(resId = R.string.common_error_unknown)
|
||||
} else {
|
||||
ToastMessage(message = message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.v2.components.modal.V2ModalDialog
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
|
||||
|
||||
internal fun Activity.giftCancelDialog(
|
||||
creatorNickname: String?,
|
||||
onConfirm: () -> Unit,
|
||||
onExit: () -> Unit
|
||||
): V2ModalDialog = V2ModalDialog(
|
||||
activity = this,
|
||||
layoutInflater = layoutInflater,
|
||||
title = getString(R.string.gift_cancel_title),
|
||||
desc = getString(R.string.gift_cancel_body, creatorNickname ?: getString(R.string.gift_send_recipient)),
|
||||
confirmButtonTitle = getString(R.string.gift_cancel_confirm),
|
||||
confirmButtonClick = onConfirm,
|
||||
cancelButtonTitle = getString(R.string.gift_cancel_exit),
|
||||
cancelButtonClick = onExit,
|
||||
dismissOnConfirm = false
|
||||
)
|
||||
|
||||
internal object GiftCancelResult {
|
||||
private const val EXTRA_CANCELED_AT = "EXTRA_CANCELED_AT"
|
||||
private const val EXTRA_PRICE_CAN = "EXTRA_PRICE_CAN"
|
||||
private const val EXTRA_STATUS = "EXTRA_CANCEL_STATUS"
|
||||
private const val EXTRA_STATUS_NAME = "EXTRA_CANCEL_STATUS_NAME"
|
||||
|
||||
fun toIntent(applicationNo: String, cancel: GiftCancelResponse): Intent = Intent()
|
||||
.putExtra(GiftDetailActivity.EXTRA_APPLICATION_NO, applicationNo)
|
||||
.putExtra(EXTRA_CANCELED_AT, cancel.canceledAt)
|
||||
.putExtra(EXTRA_PRICE_CAN, cancel.priceCan)
|
||||
.putExtra(EXTRA_STATUS, cancel.status)
|
||||
.putExtra(EXTRA_STATUS_NAME, cancel.statusName)
|
||||
|
||||
fun fromIntent(intent: Intent?, applicationNo: String): GiftCancelResponse? {
|
||||
if (intent?.getStringExtra(GiftDetailActivity.EXTRA_APPLICATION_NO) != applicationNo) return null
|
||||
val canceledAt = intent.getStringExtra(EXTRA_CANCELED_AT) ?: return null
|
||||
if (!intent.hasExtra(EXTRA_PRICE_CAN)) return null
|
||||
val status = intent.getStringExtra(EXTRA_STATUS) ?: return null
|
||||
val statusName = intent.getStringExtra(EXTRA_STATUS_NAME) ?: return null
|
||||
return GiftCancelResponse(applicationNo, status, statusName, intent.getIntExtra(EXTRA_PRICE_CAN, 0), canceledAt)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user