feat(creator): 라이브 탭 ViewModel을 추가한다
This commit is contained in:
@@ -179,6 +179,7 @@ import kr.co.vividnext.sodalive.user.signup.SignUpViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.CreatorChannelHomeViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelApi
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.MainV2ViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.ChatMainViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.data.ChatRoomApi
|
||||
@@ -409,6 +410,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { HomeCreatorRankingViewModel(get()) }
|
||||
viewModel { HomeRecommendationViewModel(get()) }
|
||||
viewModel { CreatorChannelHomeViewModel(get()) }
|
||||
viewModel { CreatorChannelLiveViewModel(get()) }
|
||||
viewModel { PushNotificationListViewModel(get()) }
|
||||
viewModel { CharacterTabViewModel(get()) }
|
||||
viewModel { CharacterDetailViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.live
|
||||
|
||||
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.base.BaseViewModel
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.common.data.ContentSort
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelAudioContentResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelLiveResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.data.CreatorChannelLiveTabResponse
|
||||
|
||||
class CreatorChannelLiveViewModel(
|
||||
private val repository: CreatorChannelRepository
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _liveStateLiveData = MutableLiveData<CreatorChannelLiveUiState>()
|
||||
val liveStateLiveData: LiveData<CreatorChannelLiveUiState>
|
||||
get() = _liveStateLiveData
|
||||
|
||||
private var creatorId: Long = 0L
|
||||
private var selectedSort: ContentSort = ContentSort.LATEST
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadLive(creatorId: Long) {
|
||||
if (creatorId <= 0) return
|
||||
if (this.creatorId == creatorId && _liveStateLiveData.value != null) return
|
||||
|
||||
this.creatorId = creatorId
|
||||
loadFirstPage(selectedSort)
|
||||
}
|
||||
|
||||
fun changeSort(sort: ContentSort) {
|
||||
if (sort == selectedSort) return
|
||||
if (creatorId <= 0) return
|
||||
|
||||
selectedSort = sort
|
||||
loadFirstPage(sort)
|
||||
}
|
||||
|
||||
fun retryLive() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage(selectedSort)
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _liveStateLiveData.value as? CreatorChannelLiveUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_liveStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestLive(page = content.page + 1, sort = content.selectedSort, generation = generation) { response ->
|
||||
val data = response.data
|
||||
val current = _liveStateLiveData.value as? CreatorChannelLiveUiState.Content ?: content
|
||||
if (response.success && data != null) {
|
||||
_liveStateLiveData.value = data.toContentState(
|
||||
liveReplayContents = current.liveReplayContents + data.liveReplayContents,
|
||||
isLoadingMore = false
|
||||
)
|
||||
} else {
|
||||
_liveStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadFirstPage(sort: ContentSort) {
|
||||
val generation = ++requestGeneration
|
||||
_liveStateLiveData.value = CreatorChannelLiveUiState.Loading
|
||||
requestLive(page = FIRST_PAGE, sort = sort, generation = generation) { response ->
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
_liveStateLiveData.value = if (data.currentLive == null && data.liveReplayContents.isEmpty()) {
|
||||
CreatorChannelLiveUiState.Empty
|
||||
} else {
|
||||
data.toContentState(liveReplayContents = data.liveReplayContents)
|
||||
}
|
||||
} else {
|
||||
_liveStateLiveData.value = CreatorChannelLiveUiState.Error(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestLive(
|
||||
page: Int,
|
||||
sort: ContentSort,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<CreatorChannelLiveTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getLive(
|
||||
creatorId = creatorId,
|
||||
page = page,
|
||||
size = DEFAULT_PAGE_SIZE,
|
||||
sort = sort,
|
||||
token = authToken()
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
if (generation == requestGeneration) {
|
||||
onSuccess(it)
|
||||
}
|
||||
},
|
||||
{
|
||||
if (generation != requestGeneration) return@subscribe
|
||||
|
||||
it.message?.let { message -> Logger.e(message) }
|
||||
val current = _liveStateLiveData.value as? CreatorChannelLiveUiState.Content
|
||||
_liveStateLiveData.value = if (current != null && page > FIRST_PAGE) {
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
} else {
|
||||
CreatorChannelLiveUiState.Error(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun CreatorChannelLiveTabResponse.toContentState(
|
||||
liveReplayContents: List<CreatorChannelAudioContentResponse>,
|
||||
isLoadingMore: Boolean = false
|
||||
) = CreatorChannelLiveUiState.Content(
|
||||
liveReplayContentCount = liveReplayContentCount,
|
||||
currentLive = currentLive,
|
||||
liveReplayContents = liveReplayContents,
|
||||
selectedSort = sort,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext,
|
||||
isLoadingMore = isLoadingMore
|
||||
)
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_PAGE_SIZE = 10
|
||||
private const val FIRST_PAGE = 0
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface CreatorChannelLiveUiState {
|
||||
data object Loading : CreatorChannelLiveUiState
|
||||
data object Empty : CreatorChannelLiveUiState
|
||||
data class Error(val message: String?) : CreatorChannelLiveUiState
|
||||
data class Content(
|
||||
val liveReplayContentCount: Int,
|
||||
val currentLive: CreatorChannelLiveResponse?,
|
||||
val liveReplayContents: List<CreatorChannelAudioContentResponse>,
|
||||
val selectedSort: ContentSort,
|
||||
val page: Int,
|
||||
val size: Int,
|
||||
val hasNext: Boolean,
|
||||
val isLoadingMore: Boolean = false,
|
||||
val paginationErrorMessage: String? = null
|
||||
) : CreatorChannelLiveUiState
|
||||
}
|
||||
Reference in New Issue
Block a user