feat(live): 온에어 라이브 ViewModel을 연결한다
This commit is contained in:
@@ -187,6 +187,9 @@ import kr.co.vividnext.sodalive.v2.creator.channel.donation.CreatorChannelDonati
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.CreatorChannelFanTalkViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.series.CreatorChannelSeriesViewModel
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.HomeOnAirLiveViewModel
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.data.HomeOnAirLiveApi
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.data.HomeOnAirLiveRepository
|
||||
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
|
||||
@@ -332,6 +335,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
single { ApiBuilder().build(get(), HomeFollowingApi::class.java) }
|
||||
single { ApiBuilder().build(get(), HomeRecommendationApi::class.java) }
|
||||
single { ApiBuilder().build(get(), CreatorChannelApi::class.java) }
|
||||
single { ApiBuilder().build(get(), HomeOnAirLiveApi::class.java) }
|
||||
single { ApiBuilder().build(get(), CharacterApi::class.java) }
|
||||
single { ApiBuilder().build(get(), TalkApi::class.java) }
|
||||
single { ApiBuilder().build(get(), CharacterCommentApi::class.java) }
|
||||
@@ -437,6 +441,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { HomeCreatorRankingViewModel(get()) }
|
||||
viewModel { HomeFollowingViewModel(get(), get()) }
|
||||
viewModel { HomeRecommendationViewModel(get()) }
|
||||
viewModel { HomeOnAirLiveViewModel(get()) }
|
||||
viewModel { CreatorChannelHomeViewModel(get()) }
|
||||
viewModel { CreatorChannelLiveViewModel(get()) }
|
||||
viewModel { CreatorChannelAudioViewModel(get()) }
|
||||
@@ -502,6 +507,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
factory { HomeCreatorRankingRepository(get()) }
|
||||
factory { HomeFollowingRepository(get()) }
|
||||
factory { HomeRecommendationRepository(get()) }
|
||||
factory { HomeOnAirLiveRepository(get()) }
|
||||
factory {
|
||||
CreatorChannelRepository(
|
||||
api = get(),
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package kr.co.vividnext.sodalive.v2.live.onair
|
||||
|
||||
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.live.onair.data.HomeOnAirLivePageResponse
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.data.HomeOnAirLiveRepository
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.model.HomeOnAirLivePageUiState
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.model.homeOnAirLiveAuthHeader
|
||||
import kr.co.vividnext.sodalive.v2.live.onair.model.toUiState
|
||||
|
||||
class HomeOnAirLiveViewModel(
|
||||
private val repository: HomeOnAirLiveRepository
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _onAirLiveStateLiveData = MutableLiveData<HomeOnAirLivePageUiState>()
|
||||
val onAirLiveStateLiveData: LiveData<HomeOnAirLivePageUiState>
|
||||
get() = _onAirLiveStateLiveData
|
||||
|
||||
private val _isLoading = MutableLiveData(false)
|
||||
val isLoading: LiveData<Boolean>
|
||||
get() = _isLoading
|
||||
|
||||
private val _toastLiveData = MutableLiveData<ToastMessage?>()
|
||||
val toastLiveData: LiveData<ToastMessage?>
|
||||
get() = _toastLiveData
|
||||
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadFirstPage() {
|
||||
val generation = ++requestGeneration
|
||||
_isLoading.value = true
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Loading
|
||||
requestOnAirLives(page = FIRST_PAGE, generation = generation) { response ->
|
||||
_isLoading.value = false
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val state = data.toUiState()
|
||||
_onAirLiveStateLiveData.value = if (state.items.isEmpty()) {
|
||||
HomeOnAirLivePageUiState.Empty
|
||||
} else {
|
||||
HomeOnAirLivePageUiState.Content(state)
|
||||
}
|
||||
} else {
|
||||
showFirstPageError(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun loadNextPage() {
|
||||
val content = (_onAirLiveStateLiveData.value as? HomeOnAirLivePageUiState.Content)?.content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Content(
|
||||
content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
)
|
||||
requestOnAirLives(page = content.page + 1, generation = generation) { response ->
|
||||
val current = (_onAirLiveStateLiveData.value as? HomeOnAirLivePageUiState.Content)?.content ?: content
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val next = data.toUiState()
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Content(
|
||||
current.copy(
|
||||
items = current.items + next.items,
|
||||
page = next.page,
|
||||
size = next.size,
|
||||
hasNext = next.hasNext,
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = null
|
||||
)
|
||||
)
|
||||
} else {
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Content(
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = response.message)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = (_onAirLiveStateLiveData.value as? HomeOnAirLivePageUiState.Content)?.content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Content(
|
||||
content.copy(paginationErrorMessage = null)
|
||||
)
|
||||
}
|
||||
|
||||
private fun requestOnAirLives(
|
||||
page: Int,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<HomeOnAirLivePageResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getOnAirLives(authHeader = authHeader(), page = page)
|
||||
.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 = (_onAirLiveStateLiveData.value as? HomeOnAirLivePageUiState.Content)?.content
|
||||
if (current != null && page > FIRST_PAGE) {
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Content(
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
)
|
||||
} else {
|
||||
showFirstPageError(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun showFirstPageError(message: String?) {
|
||||
_onAirLiveStateLiveData.value = HomeOnAirLivePageUiState.Error(message)
|
||||
_toastLiveData.value = ToastMessage(resId = R.string.common_error_unknown)
|
||||
}
|
||||
|
||||
private fun authHeader(): String? = homeOnAirLiveAuthHeader(SharedPreferenceManager.token)
|
||||
|
||||
companion object {
|
||||
private const val FIRST_PAGE = 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user