feat(creator): 오디오 탭 상태 관리를 추가한다
This commit is contained in:
@@ -177,6 +177,7 @@ import kr.co.vividnext.sodalive.user.find_password.FindPasswordViewModel
|
||||
import kr.co.vividnext.sodalive.user.login.LoginViewModel
|
||||
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.audio.CreatorChannelAudioViewModel
|
||||
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
|
||||
@@ -410,6 +411,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { HomeRecommendationViewModel(get()) }
|
||||
viewModel { CreatorChannelHomeViewModel(get()) }
|
||||
viewModel { CreatorChannelLiveViewModel(get()) }
|
||||
viewModel { CreatorChannelAudioViewModel(get()) }
|
||||
viewModel { PushNotificationListViewModel(get()) }
|
||||
viewModel { CharacterTabViewModel(get()) }
|
||||
viewModel { CharacterDetailViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.audio
|
||||
|
||||
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.audio.data.CreatorChannelAudioTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelAudioContentResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
|
||||
class CreatorChannelAudioViewModel(
|
||||
private val repository: CreatorChannelRepository
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _audioStateLiveData = MutableLiveData<CreatorChannelAudioUiState>()
|
||||
val audioStateLiveData: LiveData<CreatorChannelAudioUiState>
|
||||
get() = _audioStateLiveData
|
||||
|
||||
private var creatorId: Long = 0L
|
||||
private var isOwner: Boolean = false
|
||||
private var selectedSort: ContentSort = ContentSort.LATEST
|
||||
private var selectedThemeId: Long? = null
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadAudio(creatorId: Long, isOwner: Boolean) {
|
||||
if (creatorId <= 0) return
|
||||
if (this.creatorId == creatorId && _audioStateLiveData.value != null) return
|
||||
|
||||
this.creatorId = creatorId
|
||||
this.isOwner = isOwner
|
||||
loadFirstPage(selectedSort, selectedThemeId)
|
||||
}
|
||||
|
||||
fun changeSort(sort: ContentSort) {
|
||||
if (sort == selectedSort) return
|
||||
if (creatorId <= 0) return
|
||||
|
||||
selectedSort = sort
|
||||
loadFirstPage(sort, selectedThemeId)
|
||||
}
|
||||
|
||||
fun changeTheme(themeId: Long?) {
|
||||
if (themeId == selectedThemeId) return
|
||||
if (creatorId <= 0) return
|
||||
|
||||
selectedThemeId = themeId
|
||||
loadFirstPage(selectedSort, themeId)
|
||||
}
|
||||
|
||||
fun retryAudio() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage(selectedSort, selectedThemeId)
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_audioStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestAudio(
|
||||
page = content.page + 1,
|
||||
sort = content.selectedSort,
|
||||
themeId = content.selectedThemeId,
|
||||
generation = generation
|
||||
) { response ->
|
||||
val data = response.data
|
||||
val current = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: content
|
||||
if (response.success && data != null) {
|
||||
_audioStateLiveData.value = current.copy(
|
||||
audioContents = current.audioContents + data.displayableAudioContents(),
|
||||
page = data.page,
|
||||
size = data.size,
|
||||
hasNext = data.hasNext,
|
||||
isLoadingMore = false
|
||||
)
|
||||
} else {
|
||||
_audioStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_audioStateLiveData.value = content.copy(paginationErrorMessage = null)
|
||||
}
|
||||
|
||||
private fun loadFirstPage(sort: ContentSort, themeId: Long?) {
|
||||
val generation = ++requestGeneration
|
||||
_audioStateLiveData.value = CreatorChannelAudioUiState.Loading
|
||||
requestAudio(page = FIRST_PAGE, sort = sort, themeId = themeId, generation = generation) { response ->
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val audioContents = data.displayableAudioContents()
|
||||
_audioStateLiveData.value = if (audioContents.isEmpty() || data.audioContentCount == 0) {
|
||||
CreatorChannelAudioUiState.Empty
|
||||
} else {
|
||||
data.toContentState(audioContents = audioContents)
|
||||
}
|
||||
} else {
|
||||
_audioStateLiveData.value = CreatorChannelAudioUiState.Error(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestAudio(
|
||||
page: Int,
|
||||
sort: ContentSort,
|
||||
themeId: Long?,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<CreatorChannelAudioTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getAudio(
|
||||
creatorId = creatorId,
|
||||
page = page,
|
||||
size = DEFAULT_PAGE_SIZE,
|
||||
sort = sort,
|
||||
themeId = themeId,
|
||||
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 = _audioStateLiveData.value as? CreatorChannelAudioUiState.Content
|
||||
_audioStateLiveData.value = if (current != null && page > FIRST_PAGE) {
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
} else {
|
||||
CreatorChannelAudioUiState.Error(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun CreatorChannelAudioTabResponse.displayableAudioContents(): List<CreatorChannelAudioContentResponse> =
|
||||
audioContents.filter { it.duration != null }
|
||||
|
||||
private fun CreatorChannelAudioTabResponse.toContentState(
|
||||
audioContents: List<CreatorChannelAudioContentResponse>,
|
||||
isLoadingMore: Boolean = false
|
||||
) = CreatorChannelAudioUiState.Content(
|
||||
audioContentCount = audioContentCount,
|
||||
themes = toThemeUiModels(),
|
||||
selectedSort = sort,
|
||||
selectedThemeId = themeId,
|
||||
rate = toRateUiModel(),
|
||||
audioContents = audioContents,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext,
|
||||
isLoadingMore = isLoadingMore
|
||||
)
|
||||
|
||||
private fun CreatorChannelAudioTabResponse.toThemeUiModels(): List<CreatorChannelAudioThemeUiModel> =
|
||||
listOf(CreatorChannelAudioThemeUiModel(themeId = null, title = ALL_THEME_TITLE, isSelected = themeId == null)) +
|
||||
themes.map { theme ->
|
||||
CreatorChannelAudioThemeUiModel(
|
||||
themeId = theme.themeId,
|
||||
title = theme.themeName,
|
||||
isSelected = theme.themeId == themeId
|
||||
)
|
||||
}
|
||||
|
||||
private fun CreatorChannelAudioTabResponse.toRateUiModel(): CreatorChannelAudioRateUiModel? =
|
||||
if (!isOwner && themeId == null) {
|
||||
CreatorChannelAudioRateUiModel(
|
||||
ratePercent = purchasedAudioContentRate,
|
||||
purchasedCount = purchasedAudioContentCount,
|
||||
paidCount = paidAudioContentCount
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
|
||||
companion object {
|
||||
val DEFAULT_PAGE_SIZE = 20
|
||||
private const val FIRST_PAGE = 0
|
||||
private const val ALL_THEME_TITLE = "전체"
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface CreatorChannelAudioUiState {
|
||||
data object Loading : CreatorChannelAudioUiState
|
||||
data object Empty : CreatorChannelAudioUiState
|
||||
data class Error(val message: String?) : CreatorChannelAudioUiState
|
||||
data class Content(
|
||||
val audioContentCount: Int,
|
||||
val themes: List<CreatorChannelAudioThemeUiModel>,
|
||||
val selectedSort: ContentSort,
|
||||
val selectedThemeId: Long?,
|
||||
val rate: CreatorChannelAudioRateUiModel?,
|
||||
val audioContents: List<CreatorChannelAudioContentResponse>,
|
||||
val page: Int,
|
||||
val size: Int,
|
||||
val hasNext: Boolean,
|
||||
val isLoadingMore: Boolean = false,
|
||||
val paginationErrorMessage: String? = null
|
||||
) : CreatorChannelAudioUiState
|
||||
}
|
||||
|
||||
data class CreatorChannelAudioThemeUiModel(
|
||||
val themeId: Long?,
|
||||
val title: String,
|
||||
val isSelected: Boolean
|
||||
)
|
||||
|
||||
data class CreatorChannelAudioRateUiModel(
|
||||
val ratePercent: Double,
|
||||
val purchasedCount: Int,
|
||||
val paidCount: Int
|
||||
)
|
||||
Reference in New Issue
Block a user