feat(content): 전체보기 ViewModel을 추가한다

This commit is contained in:
2026-06-27 08:29:44 +09:00
parent 0a4b88fabe
commit 0134942172
2 changed files with 439 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
package kr.co.vividnext.sodalive.v2.main.content.overview
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.orhanobut.logger.Logger
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.ApiResponse
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.common.ToastMessage
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewPageResponse
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewRepository
import kr.co.vividnext.sodalive.v2.main.content.overview.data.ContentOverviewType
import kr.co.vividnext.sodalive.v2.main.content.overview.model.ContentOverviewUiState
import kr.co.vividnext.sodalive.v2.main.content.overview.model.toContent
class ContentOverviewViewModel(
private val repository: ContentOverviewRepository
) : BaseViewModel() {
private val _overviewStateLiveData = MutableLiveData<ContentOverviewUiState>()
val overviewStateLiveData: LiveData<ContentOverviewUiState>
get() = _overviewStateLiveData
private val _isLoading = MutableLiveData(false)
val isLoading: LiveData<Boolean>
get() = _isLoading
private val _toastLiveData = MutableLiveData<ToastMessage?>()
val toastLiveData: LiveData<ToastMessage?>
get() = _toastLiveData
private var selectedType: ContentOverviewType = ContentOverviewType.NEW_AND_HOT_AUDIO
private var requestGeneration: Int = 0
fun loadFirstPage(type: ContentOverviewType) {
selectedType = type
val generation = ++requestGeneration
_isLoading.value = true
_overviewStateLiveData.value = ContentOverviewUiState.Loading(type)
requestContents(type, FIRST_PAGE, generation) { response ->
_isLoading.value = false
val data = response.data
if (response.success && data != null) {
val content = data.toContent()
_overviewStateLiveData.value = if (content.items.isEmpty()) {
ContentOverviewUiState.Empty(content.type, content.totalCount)
} else {
content
}
} else {
showFirstPageError(type, response.message)
}
}
}
fun loadMore() {
val content = _overviewStateLiveData.value as? ContentOverviewUiState.Content ?: return
if (!content.hasNext || content.isLoadingMore) return
val generation = requestGeneration
_overviewStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
requestContents(content.type, content.page + 1, generation) { response ->
val current = _overviewStateLiveData.value as? ContentOverviewUiState.Content ?: content
val data = response.data
if (response.success && data != null) {
val mapped = data.toContent()
_overviewStateLiveData.value = current.copy(
type = mapped.type,
totalCount = mapped.totalCount,
items = current.items + mapped.items,
page = mapped.page,
size = mapped.size,
hasNext = mapped.hasNext,
isLoadingMore = false,
paginationErrorMessage = null
)
} else {
_overviewStateLiveData.value = current.copy(
isLoadingMore = false,
paginationErrorMessage = response.message
)
}
}
}
fun retry() {
loadFirstPage(selectedType)
}
fun consumePaginationErrorMessage() {
val content = _overviewStateLiveData.value as? ContentOverviewUiState.Content ?: return
if (content.paginationErrorMessage == null) return
_overviewStateLiveData.value = content.copy(paginationErrorMessage = null)
}
private fun requestContents(
type: ContentOverviewType,
page: Int,
generation: Int,
onSuccess: (ApiResponse<ContentOverviewPageResponse>) -> Unit
) {
compositeDisposable.add(
repository.getContents(authToken(), page, DEFAULT_PAGE_SIZE, type)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
if (generation == requestGeneration) {
onSuccess(it)
}
},
{
if (generation != requestGeneration) return@subscribe
it.message?.let { message -> Logger.e(message) }
_isLoading.value = false
val current = _overviewStateLiveData.value as? ContentOverviewUiState.Content
if (current != null && page > FIRST_PAGE) {
_overviewStateLiveData.value = current.copy(
isLoadingMore = false,
paginationErrorMessage = it.message
)
} else {
showFirstPageError(type, it.message)
}
}
)
)
}
private fun showFirstPageError(type: ContentOverviewType, message: String?) {
_overviewStateLiveData.value = ContentOverviewUiState.Error(type, message)
_toastLiveData.value = ToastMessage(resId = R.string.common_error_unknown)
}
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
companion object {
const val DEFAULT_PAGE_SIZE = 20
private const val FIRST_PAGE = 0
}
}