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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift
|
||||
|
||||
import android.app.Application
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.SingleSubject
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.AppPreferencesDataStoreProvider
|
||||
import kr.co.vividnext.sodalive.common.Constants
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.ToastMessage
|
||||
import kr.co.vividnext.sodalive.v2.components.modal.V2ModalDialog
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftApi
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDeliveryResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailInfoResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
|
||||
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class GiftCancelViewModelTest {
|
||||
private lateinit var api: CancelFakeApi
|
||||
private lateinit var viewModel: GiftCancelViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
SharedPreferenceManager.resetForTest()
|
||||
val application = ApplicationProvider.getApplicationContext<Application>()
|
||||
runBlocking {
|
||||
AppPreferencesDataStoreProvider.get(application).edit {
|
||||
it[stringPreferencesKey(Constants.PREF_TOKEN)] = "test-token"
|
||||
}
|
||||
}
|
||||
SharedPreferenceManager.init(application)
|
||||
api = CancelFakeApi()
|
||||
viewModel = GiftCancelViewModel(GiftRepository(api))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
viewModel.compositeDisposable.clear()
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Activity 계약상 dialog 열기와 뒤로가기와 나가기는 API를 호출하지 않고 확인만 호출한다`() {
|
||||
val controller = Robolectric.buildActivity(FragmentActivity::class.java).setup()
|
||||
val activity = controller.get()
|
||||
fun openDialog() = V2ModalDialog(
|
||||
activity,
|
||||
activity.layoutInflater,
|
||||
"신청 취소",
|
||||
"선물을 취소할까요?",
|
||||
"취소하기",
|
||||
{ viewModel.cancelGift(detail()) },
|
||||
"나가기",
|
||||
dismissOnConfirm = false
|
||||
).also { it.show(width = 360) }
|
||||
try {
|
||||
val dialog = openDialog()
|
||||
assertTrue(api.requests.isEmpty())
|
||||
val field = V2ModalDialog::class.java.getDeclaredField("alertDialog").apply { isAccessible = true }
|
||||
assertTrue((field.get(dialog) as AlertDialog).isShowing)
|
||||
(field.get(dialog) as AlertDialog).onBackPressed()
|
||||
assertTrue(api.requests.isEmpty())
|
||||
val exitDialog = openDialog()
|
||||
assertTrue((field.get(exitDialog) as AlertDialog).isShowing)
|
||||
exitDialog.dialogView.tvCancel.performClick()
|
||||
assertFalse((field.get(exitDialog) as AlertDialog).isShowing)
|
||||
assertTrue(api.requests.isEmpty())
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
val confirmedDialog = openDialog()
|
||||
assertTrue((field.get(confirmedDialog) as AlertDialog).isShowing)
|
||||
confirmedDialog.dialogView.tvConfirm.performClick()
|
||||
assertEquals(listOf("test-gift" to "Bearer test-token"), api.requests)
|
||||
confirmedDialog.dismiss()
|
||||
} finally {
|
||||
controller.pause().stop().destroy()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `보낸 접수 상태 이외의 모든 방향과 상태 조합은 제출하지 않는다`() {
|
||||
val directions = listOf("SENT", "RECEIVED", "UNKNOWN", "", "sent", " SENT ")
|
||||
val statuses = listOf(
|
||||
"RECEIVED", "TRACKING_REGISTERED", "ARRIVED_AT_MAILBOX", "INSPECTION_COMPLETED",
|
||||
"DELIVERED", "UNDELIVERABLE", "CANCELED", "UNKNOWN", "", "received", " RECEIVED "
|
||||
)
|
||||
directions.forEach { direction ->
|
||||
statuses.forEach { status ->
|
||||
if (direction != "SENT" || status != "RECEIVED") {
|
||||
viewModel.cancelGift(detail().copy(direction = direction, status = status))
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(api.requests.isEmpty())
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.consumeToast())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `빈 신청번호와 공백 신청번호는 제출하지 않는다`() {
|
||||
listOf("", " \n\t ").forEach { viewModel.cancelGift(detail().copy(applicationNo = it)) }
|
||||
assertTrue(api.requests.isEmpty())
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `대기 중 중복 확인은 같은 신청번호와 인증으로 한 번만 제출한다`() {
|
||||
viewModel.cancelGift(detail())
|
||||
viewModel.cancelGift(detail())
|
||||
assertEquals(true, viewModel.isSubmitting.value)
|
||||
assertEquals(listOf("test-gift" to "Bearer test-token"), api.requests)
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `성공은 로딩을 해제하고 응답 전체를 한 번만 소비한다`() {
|
||||
viewModel.cancelGift(detail())
|
||||
api.response.onSuccess(ApiResponse(true, canceled(), null))
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
assertEquals(canceled(), viewModel.cancelSuccessLiveData.value)
|
||||
assertEquals(canceled(), viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.cancelSuccessLiveData.value)
|
||||
assertNull(viewModel.consumeToast())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `실패 응답은 서버 안내를 한 번 표시하고 같은 신청번호로 재시도한다`() =
|
||||
assertFailure(ApiResponse(false, canceled(), "취소 실패"), ToastMessage(message = "취소 실패"))
|
||||
|
||||
@Test
|
||||
fun `빈 실패 안내는 공통 오류를 한 번 표시하고 재시도한다`() =
|
||||
assertFailure(ApiResponse(false, null, " \t"), ToastMessage(resId = R.string.common_error_unknown))
|
||||
|
||||
@Test
|
||||
fun `성공 데이터 누락은 공통 오류를 한 번 표시하고 재시도한다`() =
|
||||
assertFailure(ApiResponse(true, null, null), ToastMessage(resId = R.string.common_error_unknown))
|
||||
|
||||
@Test
|
||||
fun `전송 오류는 내부 내용을 숨기고 공통 오류를 한 번 표시하고 재시도한다`() =
|
||||
assertFailure(null, ToastMessage(resId = R.string.common_error_unknown))
|
||||
|
||||
@Test
|
||||
fun `소비된 실패 안내 이후 새 실패 안내는 다시 한 번 소비된다`() {
|
||||
listOf("첫 실패", "다음 실패").forEach { message ->
|
||||
api.response = SingleSubject.create()
|
||||
viewModel.cancelGift(detail())
|
||||
api.response.onSuccess(ApiResponse(false, null, message))
|
||||
assertEquals(ToastMessage(message = message), viewModel.consumeToast())
|
||||
assertNull(viewModel.consumeToast())
|
||||
}
|
||||
assertEquals(2, api.requests.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `요청 해제 뒤 늦은 성공은 이벤트를 만들지 않는다`() {
|
||||
viewModel.cancelGift(detail())
|
||||
viewModel.compositeDisposable.clear()
|
||||
assertFalse(api.response.hasObservers())
|
||||
api.response.onSuccess(ApiResponse(true, canceled(), null))
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.consumeToast())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `요청 해제 뒤 늦은 실패는 안내를 만들지 않는다`() {
|
||||
viewModel.cancelGift(detail())
|
||||
viewModel.compositeDisposable.clear()
|
||||
api.response.onSuccess(ApiResponse(false, null, "늦은 실패"))
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.consumeToast())
|
||||
}
|
||||
|
||||
private fun assertFailure(response: ApiResponse<GiftCancelResponse>?, toast: ToastMessage) {
|
||||
viewModel.cancelGift(detail())
|
||||
if (response != null) {
|
||||
api.response.onSuccess(response)
|
||||
} else {
|
||||
api.response.onError(IllegalStateException("private transport detail"))
|
||||
}
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
assertNull(viewModel.cancelSuccessLiveData.value)
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
assertEquals(toast, viewModel.toastLiveData.value)
|
||||
assertEquals(toast, viewModel.consumeToast())
|
||||
assertNull(viewModel.consumeToast())
|
||||
assertNull(viewModel.toastLiveData.value)
|
||||
api.response = SingleSubject.create()
|
||||
viewModel.cancelGift(detail())
|
||||
assertEquals(true, viewModel.isSubmitting.value)
|
||||
assertEquals(List(2) { "test-gift" to "Bearer test-token" }, api.requests)
|
||||
api.response.onSuccess(ApiResponse(true, canceled(), null))
|
||||
assertEquals(false, viewModel.isSubmitting.value)
|
||||
assertEquals(canceled(), viewModel.consumeCancelSuccessEvent())
|
||||
assertNull(viewModel.consumeCancelSuccessEvent())
|
||||
}
|
||||
|
||||
private fun detail() = GiftDetailResponse(
|
||||
"test-gift", "SENT", "RECEIVED", "접수 완료",
|
||||
GiftDetailInfoResponse("가상 크리에이터", null, "소형", "도서", "test-gift", 100, null, null),
|
||||
null, null, null, false, false, null, GiftDeliveryResponse(null, null, null), emptyList()
|
||||
)
|
||||
|
||||
private fun canceled() = GiftCancelResponse("test-gift", "CANCELED", "신청 취소", 100, "2026-10-02T00:00:00Z")
|
||||
|
||||
private class CancelFakeApi : GiftApi by InboxFakeApi() {
|
||||
val requests = mutableListOf<Pair<String, String>>()
|
||||
var response = SingleSubject.create<ApiResponse<GiftCancelResponse>>()
|
||||
override fun cancelGift(applicationNo: String, authHeader: String): Single<ApiResponse<GiftCancelResponse>> {
|
||||
requests.add(applicationNo to authHeader)
|
||||
return response
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user