feat(content): 전체 탭 ViewModel을 추가한다
This commit is contained in:
@@ -195,6 +195,7 @@ import kr.co.vividnext.sodalive.v2.main.chat.dm.DmChatRoomViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.data.DmChatApi
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.data.DmChatRepository
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.data.DmChatSocketClient
|
||||
import kr.co.vividnext.sodalive.v2.main.content.ContentAllTabViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.content.ContentMainViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.content.ContentRankingViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.content.data.AudioRecommendationsApi
|
||||
@@ -426,6 +427,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { HomeViewModel(get(), get()) }
|
||||
viewModel { ChatMainViewModel(get()) }
|
||||
viewModel { DmChatRoomViewModel(get()) }
|
||||
viewModel { ContentAllTabViewModel(get()) }
|
||||
viewModel { ContentMainViewModel(get()) }
|
||||
viewModel { ContentRankingViewModel(get()) }
|
||||
viewModel { HomeCreatorRankingViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.content
|
||||
|
||||
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.home.SeriesPublishedDaysOfWeek
|
||||
import kr.co.vividnext.sodalive.v2.common.data.ContentSort
|
||||
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllTabRepository
|
||||
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllType
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.MainContentAllTabUiState
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.currentDeviceDayOfWeek
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.toContent
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.toUiModel
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.usesDayOfWeekQuery
|
||||
import kr.co.vividnext.sodalive.v2.main.content.model.usesSeriesItems
|
||||
|
||||
class ContentAllTabViewModel(
|
||||
private val repository: MainContentAllTabRepository,
|
||||
private val currentDayOfWeekProvider: () -> SeriesPublishedDaysOfWeek = { currentDeviceDayOfWeek() }
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _allTabStateLiveData = MutableLiveData<MainContentAllTabUiState>()
|
||||
val allTabStateLiveData: LiveData<MainContentAllTabUiState>
|
||||
get() = _allTabStateLiveData
|
||||
|
||||
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: MainContentAllType = MainContentAllType.AUDIO
|
||||
private var selectedSort: ContentSort = ContentSort.LATEST
|
||||
private var selectedDayOfWeek: SeriesPublishedDaysOfWeek? = null
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadContents() {
|
||||
loadFirstPage(selectedType, selectedSort, selectedDayOfWeekFor(selectedType))
|
||||
}
|
||||
|
||||
fun loadInitial() {
|
||||
loadContents()
|
||||
}
|
||||
|
||||
fun changeType(type: MainContentAllType) {
|
||||
selectedType = type
|
||||
selectedDayOfWeek = selectedDayOfWeekFor(type)
|
||||
loadFirstPage(type, selectedSort, selectedDayOfWeek)
|
||||
}
|
||||
|
||||
fun changeSort(sort: ContentSort) {
|
||||
selectedSort = sort
|
||||
loadFirstPage(selectedType, sort, selectedDayOfWeekFor(selectedType))
|
||||
}
|
||||
|
||||
fun changeDayOfWeek(dayOfWeek: SeriesPublishedDaysOfWeek) {
|
||||
if (!selectedType.usesDayOfWeekQuery()) return
|
||||
|
||||
selectedDayOfWeek = dayOfWeek
|
||||
loadFirstPage(selectedType, selectedSort, dayOfWeek)
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _allTabStateLiveData.value as? MainContentAllTabUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_allTabStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestContents(
|
||||
type = content.selectedType,
|
||||
sort = content.selectedSort,
|
||||
page = content.page + 1,
|
||||
dayOfWeek = content.selectedDayOfWeek,
|
||||
generation = generation
|
||||
) { response ->
|
||||
val current = _allTabStateLiveData.value as? MainContentAllTabUiState.Content ?: content
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
_allTabStateLiveData.value = current.append(data)
|
||||
} else {
|
||||
_allTabStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun retry() {
|
||||
loadContents()
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = _allTabStateLiveData.value as? MainContentAllTabUiState.Content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_allTabStateLiveData.value = content.copy(paginationErrorMessage = null)
|
||||
}
|
||||
|
||||
private fun loadFirstPage(
|
||||
type: MainContentAllType,
|
||||
sort: ContentSort,
|
||||
dayOfWeek: SeriesPublishedDaysOfWeek?
|
||||
) {
|
||||
val generation = ++requestGeneration
|
||||
_isLoading.value = true
|
||||
_allTabStateLiveData.value = MainContentAllTabUiState.Loading(type, sort, dayOfWeek, totalCount = 0)
|
||||
requestContents(type, sort, FIRST_PAGE, dayOfWeek, generation) { response ->
|
||||
_isLoading.value = false
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val content = data.toContent()
|
||||
_allTabStateLiveData.value = if (content.isSelectedTypeEmpty()) {
|
||||
MainContentAllTabUiState.Empty(
|
||||
content.selectedType,
|
||||
content.selectedSort,
|
||||
content.selectedDayOfWeek,
|
||||
content.totalCount
|
||||
)
|
||||
} else {
|
||||
content
|
||||
}
|
||||
} else {
|
||||
showFirstPageError(type, sort, dayOfWeek, response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestContents(
|
||||
type: MainContentAllType,
|
||||
sort: ContentSort,
|
||||
page: Int,
|
||||
dayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<MainContentAllTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getContents(authToken(), type, sort, page, DEFAULT_PAGE_SIZE, dayOfWeek)
|
||||
.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 = _allTabStateLiveData.value as? MainContentAllTabUiState.Content
|
||||
if (current != null && page > FIRST_PAGE) {
|
||||
_allTabStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = it.message
|
||||
)
|
||||
} else {
|
||||
showFirstPageError(type, sort, dayOfWeek, it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun selectedDayOfWeekFor(type: MainContentAllType): SeriesPublishedDaysOfWeek? {
|
||||
return if (type.usesDayOfWeekQuery()) selectedDayOfWeek ?: currentDayOfWeekProvider() else null
|
||||
}
|
||||
|
||||
private fun MainContentAllTabUiState.Content.isSelectedTypeEmpty(): Boolean {
|
||||
return if (selectedType.usesSeriesItems()) seriesItems.isEmpty() else audioItems.isEmpty()
|
||||
}
|
||||
|
||||
private fun MainContentAllTabUiState.Content.append(
|
||||
data: MainContentAllTabResponse
|
||||
): MainContentAllTabUiState.Content {
|
||||
val mapped = data.toContent()
|
||||
return copy(
|
||||
selectedType = mapped.selectedType,
|
||||
selectedSort = mapped.selectedSort,
|
||||
selectedDayOfWeek = mapped.selectedDayOfWeek,
|
||||
totalCount = mapped.totalCount,
|
||||
audioItems = if (mapped.selectedType.usesSeriesItems()) {
|
||||
emptyList()
|
||||
} else {
|
||||
audioItems + data.audios.map { it.toUiModel() }
|
||||
},
|
||||
seriesItems = if (mapped.selectedType.usesSeriesItems()) {
|
||||
seriesItems + data.series.map {
|
||||
it.toUiModel(mapped.selectedType)
|
||||
}
|
||||
} else {
|
||||
emptyList()
|
||||
},
|
||||
page = mapped.page,
|
||||
size = mapped.size,
|
||||
hasNext = mapped.hasNext,
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = null
|
||||
)
|
||||
}
|
||||
|
||||
private fun showFirstPageError(
|
||||
type: MainContentAllType,
|
||||
sort: ContentSort,
|
||||
dayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
message: String?
|
||||
) {
|
||||
_allTabStateLiveData.value = MainContentAllTabUiState.Error(
|
||||
selectedType = type,
|
||||
selectedSort = sort,
|
||||
selectedDayOfWeek = dayOfWeek,
|
||||
totalCount = 0,
|
||||
message = 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
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,25 @@ import kr.co.vividnext.sodalive.v2.common.data.ContentSort
|
||||
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllType
|
||||
|
||||
sealed interface MainContentAllTabUiState {
|
||||
data object Loading : MainContentAllTabUiState
|
||||
val selectedType: MainContentAllType
|
||||
val selectedSort: ContentSort
|
||||
val selectedDayOfWeek: SeriesPublishedDaysOfWeek?
|
||||
val totalCount: Int
|
||||
|
||||
data class Loading(
|
||||
override val selectedType: MainContentAllType,
|
||||
override val selectedSort: ContentSort,
|
||||
override val selectedDayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
override val totalCount: Int
|
||||
) : MainContentAllTabUiState {
|
||||
companion object
|
||||
}
|
||||
|
||||
data class Content(
|
||||
val selectedType: MainContentAllType,
|
||||
val selectedSort: ContentSort,
|
||||
val selectedDayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
val totalCount: Int,
|
||||
override val selectedType: MainContentAllType,
|
||||
override val selectedSort: ContentSort,
|
||||
override val selectedDayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
override val totalCount: Int,
|
||||
val audioItems: List<MainContentAllAudioUiModel>,
|
||||
val seriesItems: List<MainContentAllSeriesUiModel>,
|
||||
val page: Int,
|
||||
@@ -21,7 +33,20 @@ sealed interface MainContentAllTabUiState {
|
||||
val paginationErrorMessage: String? = null
|
||||
) : MainContentAllTabUiState
|
||||
|
||||
data object Empty : MainContentAllTabUiState
|
||||
data class Empty(
|
||||
override val selectedType: MainContentAllType,
|
||||
override val selectedSort: ContentSort,
|
||||
override val selectedDayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
override val totalCount: Int
|
||||
) : MainContentAllTabUiState {
|
||||
companion object
|
||||
}
|
||||
|
||||
data class Error(val message: String? = null) : MainContentAllTabUiState
|
||||
data class Error(
|
||||
override val selectedType: MainContentAllType,
|
||||
override val selectedSort: ContentSort,
|
||||
override val selectedDayOfWeek: SeriesPublishedDaysOfWeek?,
|
||||
override val totalCount: Int,
|
||||
val message: String? = null
|
||||
) : MainContentAllTabUiState
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user