feat(creator): 후원 탭 상태 관리를 추가한다
This commit is contained in:
@@ -183,6 +183,7 @@ import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioView
|
||||
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.donation.CreatorChannelDonationViewModel
|
||||
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
|
||||
@@ -421,6 +422,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { CreatorChannelSeriesViewModel(get()) }
|
||||
viewModel { CreatorChannelCommunityViewModel(get(), get()) }
|
||||
viewModel { CreatorChannelFanTalkViewModel(get(), get()) }
|
||||
viewModel { CreatorChannelDonationViewModel(get(), get()) }
|
||||
viewModel { PushNotificationListViewModel(get()) }
|
||||
viewModel { CharacterTabViewModel(get()) }
|
||||
viewModel { CharacterDetailViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.donation
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
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.common.SodaLiveApplicationHolder
|
||||
import kr.co.vividnext.sodalive.common.UtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.data.CreatorChannelDonationTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.CreatorChannelDonationRankingUiModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.CreatorChannelDonationUiModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.toDonationRankingUiModels
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.donation.model.toDonationUiModels
|
||||
|
||||
class CreatorChannelDonationViewModel(
|
||||
private val repository: CreatorChannelRepository,
|
||||
private val relativeTimeTextFormatter: UtcRelativeTimeTextFormatter
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _donationStateLiveData = MutableLiveData<CreatorChannelDonationUiState>()
|
||||
val donationStateLiveData: LiveData<CreatorChannelDonationUiState>
|
||||
get() = _donationStateLiveData
|
||||
|
||||
private val context = SodaLiveApplicationHolder.get()
|
||||
private var creatorId: Long = 0L
|
||||
private var isOwner: Boolean = false
|
||||
private var requestGeneration: Int = 0
|
||||
private var isPostChannelDonationInProgress: Boolean = false
|
||||
private var donationSuccessEvent: Boolean = false
|
||||
|
||||
fun loadDonations(creatorId: Long, isOwner: Boolean) {
|
||||
if (creatorId <= 0) return
|
||||
val shouldSkipReload = this.creatorId == creatorId && this.isOwner == isOwner && _donationStateLiveData.value != null
|
||||
if (shouldSkipReload) return
|
||||
|
||||
this.creatorId = creatorId
|
||||
this.isOwner = isOwner
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun retryDonations() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun refreshDonations() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_donationStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestDonations(page = content.page + 1, generation = generation) { response ->
|
||||
val data = response.data
|
||||
val current = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: content
|
||||
if (response.success && data != null) {
|
||||
_donationStateLiveData.value = current.copy(
|
||||
donationCount = data.donationCount,
|
||||
rankings = data.toDonationRankingUiModels(),
|
||||
donations = current.donations + data.toDonationUiModels(),
|
||||
page = data.page,
|
||||
size = data.size,
|
||||
hasNext = data.hasNext,
|
||||
isLoadingMore = false
|
||||
)
|
||||
} else {
|
||||
_donationStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun postChannelDonation(can: Int, isSecret: Boolean, message: String) {
|
||||
if (creatorId <= 0 || isPostChannelDonationInProgress) return
|
||||
|
||||
isPostChannelDonationInProgress = true
|
||||
compositeDisposable.add(
|
||||
repository.postChannelDonation(
|
||||
creatorId = creatorId,
|
||||
can = can,
|
||||
isSecret = isSecret,
|
||||
message = message,
|
||||
token = authToken()
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
isPostChannelDonationInProgress = false
|
||||
if (it.success) {
|
||||
SharedPreferenceManager.can = (SharedPreferenceManager.can - can).coerceAtLeast(0)
|
||||
donationSuccessEvent = true
|
||||
loadFirstPage()
|
||||
} else {
|
||||
setActionToastMessage(it.message)
|
||||
}
|
||||
},
|
||||
{
|
||||
isPostChannelDonationInProgress = false
|
||||
setActionToastMessage(it.message)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_donationStateLiveData.value = content.copy(paginationErrorMessage = null)
|
||||
}
|
||||
|
||||
fun consumeActionToastMessage() {
|
||||
val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return
|
||||
if (content.actionToastMessage == null) return
|
||||
|
||||
_donationStateLiveData.value = content.copy(actionToastMessage = null)
|
||||
}
|
||||
|
||||
fun consumeDonationSuccessEvent(): Boolean {
|
||||
val event = donationSuccessEvent
|
||||
donationSuccessEvent = false
|
||||
return event
|
||||
}
|
||||
|
||||
private fun loadFirstPage() {
|
||||
val generation = ++requestGeneration
|
||||
_donationStateLiveData.value = CreatorChannelDonationUiState.Loading
|
||||
requestDonations(page = FIRST_PAGE, generation = generation) { response ->
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val donations = data.toDonationUiModels()
|
||||
_donationStateLiveData.value = if (donations.isEmpty() || data.donationCount == 0) {
|
||||
CreatorChannelDonationUiState.Empty(data.donationCount, isOwner)
|
||||
} else {
|
||||
data.toContentState(donations)
|
||||
}
|
||||
} else {
|
||||
_donationStateLiveData.value = CreatorChannelDonationUiState.Error(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestDonations(
|
||||
page: Int,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<CreatorChannelDonationTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getDonations(
|
||||
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
|
||||
|
||||
val current = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content
|
||||
_donationStateLiveData.value = if (current != null && page > FIRST_PAGE) {
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
} else {
|
||||
CreatorChannelDonationUiState.Error(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun setActionToastMessage(message: String?) {
|
||||
val content = _donationStateLiveData.value as? CreatorChannelDonationUiState.Content ?: return
|
||||
_donationStateLiveData.value = content.copy(actionToastMessage = message)
|
||||
}
|
||||
|
||||
private fun CreatorChannelDonationTabResponse.toContentState(
|
||||
donations: List<CreatorChannelDonationUiModel>
|
||||
) = CreatorChannelDonationUiState.Content(
|
||||
donationCount = donationCount,
|
||||
rankings = toDonationRankingUiModels(),
|
||||
donations = donations,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext,
|
||||
isOwner = isOwner
|
||||
)
|
||||
|
||||
private fun CreatorChannelDonationTabResponse.toDonationRankingUiModels(): List<CreatorChannelDonationRankingUiModel> =
|
||||
rankings.toDonationRankingUiModels()
|
||||
|
||||
private fun CreatorChannelDonationTabResponse.toDonationUiModels(): List<CreatorChannelDonationUiModel> =
|
||||
donations.toDonationUiModels(context, relativeTimeTextFormatter)
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_PAGE_SIZE = 20
|
||||
private const val FIRST_PAGE = 0
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface CreatorChannelDonationUiState {
|
||||
data object Loading : CreatorChannelDonationUiState
|
||||
data class Empty(val donationCount: Int, val isOwner: Boolean) : CreatorChannelDonationUiState
|
||||
data class Error(val message: String?) : CreatorChannelDonationUiState
|
||||
data class Content(
|
||||
val donationCount: Int,
|
||||
val rankings: List<CreatorChannelDonationRankingUiModel>,
|
||||
val donations: List<CreatorChannelDonationUiModel>,
|
||||
val page: Int,
|
||||
val size: Int,
|
||||
val hasNext: Boolean,
|
||||
val isOwner: Boolean,
|
||||
val isLoadingMore: Boolean = false,
|
||||
val paginationErrorMessage: String? = null,
|
||||
val actionToastMessage: String? = null
|
||||
) : CreatorChannelDonationUiState
|
||||
}
|
||||
Reference in New Issue
Block a user