feat(creator): 커뮤니티 탭 상태 관리를 추가한다
This commit is contained in:
@@ -178,6 +178,7 @@ 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.community.CreatorChannelCommunityViewModel
|
||||
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
|
||||
@@ -414,6 +415,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { CreatorChannelLiveViewModel(get()) }
|
||||
viewModel { CreatorChannelAudioViewModel(get()) }
|
||||
viewModel { CreatorChannelSeriesViewModel(get()) }
|
||||
viewModel { CreatorChannelCommunityViewModel(get()) }
|
||||
viewModel { PushNotificationListViewModel(get()) }
|
||||
viewModel { CharacterTabViewModel(get()) }
|
||||
viewModel { CharacterDetailViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.community
|
||||
|
||||
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.creator.channel.community.data.CreatorChannelCommunityPostResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
|
||||
class CreatorChannelCommunityViewModel(
|
||||
private val repository: CreatorChannelRepository
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _communityStateLiveData = MutableLiveData<CreatorChannelCommunityUiState>()
|
||||
val communityStateLiveData: LiveData<CreatorChannelCommunityUiState>
|
||||
get() = _communityStateLiveData
|
||||
|
||||
private var creatorId: Long = 0L
|
||||
private var isOwner: Boolean = false
|
||||
private var viewMode: CreatorChannelCommunityViewMode = CreatorChannelCommunityViewMode.List
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadCommunity(creatorId: Long, isOwner: Boolean) {
|
||||
if (creatorId <= 0) return
|
||||
val shouldSkipReload = this.creatorId == creatorId && this.isOwner == isOwner && _communityStateLiveData.value != null
|
||||
if (shouldSkipReload) return
|
||||
|
||||
this.creatorId = creatorId
|
||||
this.isOwner = isOwner
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun toggleViewMode() {
|
||||
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
|
||||
viewMode = when (content.viewMode) {
|
||||
CreatorChannelCommunityViewMode.List -> CreatorChannelCommunityViewMode.Grid
|
||||
CreatorChannelCommunityViewMode.Grid -> CreatorChannelCommunityViewMode.List
|
||||
}
|
||||
_communityStateLiveData.value = content.copy(viewMode = viewMode)
|
||||
}
|
||||
|
||||
fun retryCommunity() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_communityStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestCommunity(page = content.page + 1, generation = generation) { response ->
|
||||
val data = response.data
|
||||
val current = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: content
|
||||
if (response.success && data != null) {
|
||||
_communityStateLiveData.value = current.copy(
|
||||
communityPosts = current.communityPosts + data.communityPosts,
|
||||
page = data.page,
|
||||
size = data.size,
|
||||
hasNext = data.hasNext,
|
||||
isLoadingMore = false
|
||||
)
|
||||
} else {
|
||||
_communityStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_communityStateLiveData.value = content.copy(paginationErrorMessage = null)
|
||||
}
|
||||
|
||||
private fun loadFirstPage() {
|
||||
val generation = ++requestGeneration
|
||||
_communityStateLiveData.value = CreatorChannelCommunityUiState.Loading
|
||||
requestCommunity(page = FIRST_PAGE, generation = generation) { response ->
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val communityPosts = data.communityPosts
|
||||
_communityStateLiveData.value = if (communityPosts.isEmpty() || data.communityPostCount == 0) {
|
||||
CreatorChannelCommunityUiState.Empty
|
||||
} else {
|
||||
data.toContentState(communityPosts = communityPosts)
|
||||
}
|
||||
} else {
|
||||
_communityStateLiveData.value = CreatorChannelCommunityUiState.Error(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestCommunity(
|
||||
page: Int,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<CreatorChannelCommunityTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getCommunity(
|
||||
creatorId = creatorId,
|
||||
page = page,
|
||||
size = DEFAULT_PAGE_SIZE,
|
||||
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 = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content
|
||||
_communityStateLiveData.value = if (current != null && page > FIRST_PAGE) {
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
} else {
|
||||
CreatorChannelCommunityUiState.Error(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun CreatorChannelCommunityTabResponse.toContentState(
|
||||
communityPosts: List<CreatorChannelCommunityPostResponse>
|
||||
) = CreatorChannelCommunityUiState.Content(
|
||||
communityPostCount = communityPostCount,
|
||||
communityPosts = communityPosts,
|
||||
viewMode = viewMode,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
|
||||
companion object {
|
||||
val DEFAULT_PAGE_SIZE = 20
|
||||
private const val FIRST_PAGE = 0
|
||||
}
|
||||
}
|
||||
|
||||
enum class CreatorChannelCommunityViewMode {
|
||||
List,
|
||||
Grid
|
||||
}
|
||||
|
||||
sealed interface CreatorChannelCommunityUiState {
|
||||
data object Loading : CreatorChannelCommunityUiState
|
||||
data object Empty : CreatorChannelCommunityUiState
|
||||
data class Error(val message: String?) : CreatorChannelCommunityUiState
|
||||
data class Content(
|
||||
val communityPostCount: Int,
|
||||
val communityPosts: List<CreatorChannelCommunityPostResponse>,
|
||||
val viewMode: CreatorChannelCommunityViewMode,
|
||||
val page: Int,
|
||||
val size: Int,
|
||||
val hasNext: Boolean,
|
||||
val isLoadingMore: Boolean = false,
|
||||
val paginationErrorMessage: String? = null
|
||||
) : CreatorChannelCommunityUiState
|
||||
}
|
||||
Reference in New Issue
Block a user