feat(gift): 선물함 목록 상태를 추가한다

This commit is contained in:
2026-10-01 21:17:49 +09:00
parent 4bb922b1f9
commit aa314822b4
2 changed files with 355 additions and 0 deletions
@@ -0,0 +1,133 @@
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.disposables.SerialDisposable
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.GiftInboxItemResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
sealed interface GiftInboxUiState {
data object Loading : GiftInboxUiState
data object Empty : GiftInboxUiState
data class Error(val message: String?) : GiftInboxUiState
data class Content(
val items: List<GiftInboxItemResponse>,
val isAppending: Boolean = false,
val isRefreshing: Boolean = false,
val appendError: Boolean = false,
val refreshError: Boolean = false
) : GiftInboxUiState
}
class GiftInboxViewModel(private val repository: GiftRepository) : BaseViewModel() {
private val mutableState = MutableLiveData<GiftInboxUiState>()
val state: LiveData<GiftInboxUiState> = mutableState
private val mutableToast = MutableLiveData<ToastMessage?>()
val toastLiveData: LiveData<ToastMessage?> = mutableToast
private val mutableInquiry = MutableLiveData(false)
val inquiryLiveData: LiveData<Boolean> = mutableInquiry
private val request = SerialDisposable()
private var loading = false
private var currentPage = 0
private var hasNext = false
private var failedPage: Int? = null
init {
compositeDisposable.add(request)
}
fun loadFirstPage() {
if (mutableState.value != null) return
load(0)
}
fun loadNextPage() {
if (loading || !hasNext || failedPage != null) return
load(currentPage + 1)
}
fun refresh() {
val current = mutableState.value
if (current == GiftInboxUiState.Loading || (current as? GiftInboxUiState.Content)?.isRefreshing == true) return
request.set(null)
loading = false
load(0)
}
fun retry() {
if (loading) return
load(failedPage ?: 0)
}
fun onCardClick(card: GiftInboxCardUiModel) {
mutableToast.value = card.status
}
fun consumeToast(): ToastMessage? = mutableToast.value.also { mutableToast.value = null }
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(page: Int) {
val previous = mutableState.value as? GiftInboxUiState.Content
loading = true
failedPage = null
mutableState.value = previous?.copy(
isAppending = page > 0,
isRefreshing = page == 0,
appendError = false,
refreshError = false
) ?: GiftInboxUiState.Loading
request.set(
repository.getGiftInbox(page, "Bearer ${SharedPreferenceManager.token}")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ response ->
val data = response.data
if (response.success && data != null) {
loading = false
currentPage = page
hasNext = data.hasNext
val items = if (page == 0) data.items else previous?.items.orEmpty() + data.items
mutableState.value = if (items.isEmpty()) GiftInboxUiState.Empty else GiftInboxUiState.Content(items)
} else {
fail(page, previous, response.message)
}
},
{ fail(page, previous, it.message) }
)
)
}
private fun fail(page: Int, previous: GiftInboxUiState.Content?, message: String?) {
loading = false
failedPage = page
mutableState.value = previous?.copy(
isAppending = false,
isRefreshing = false,
appendError = page > 0,
refreshError = page == 0
) ?: GiftInboxUiState.Error(message)
mutableToast.value = if (message.isNullOrBlank()) {
ToastMessage(resId = R.string.common_error_unknown)
} else {
ToastMessage(message = message)
}
}
}