feat(home): 크리에이터 랭킹 ViewModel을 추가한다
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.home
|
||||||
|
|
||||||
|
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.SharedPreferenceManager
|
||||||
|
import kr.co.vividnext.sodalive.common.ToastMessage
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.home.data.HomeCreatorRankingRepository
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.home.model.HomeCreatorRankingUiState
|
||||||
|
import kr.co.vividnext.sodalive.v2.main.home.model.toCreatorRankingItems
|
||||||
|
|
||||||
|
class HomeCreatorRankingViewModel(
|
||||||
|
private val repository: HomeCreatorRankingRepository
|
||||||
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
private val _rankingStateLiveData = MutableLiveData<HomeCreatorRankingUiState>()
|
||||||
|
val rankingStateLiveData: LiveData<HomeCreatorRankingUiState>
|
||||||
|
get() = _rankingStateLiveData
|
||||||
|
|
||||||
|
private val _toastLiveData = MutableLiveData<ToastMessage?>()
|
||||||
|
val toastLiveData: LiveData<ToastMessage?>
|
||||||
|
get() = _toastLiveData
|
||||||
|
|
||||||
|
private val _isLoading = MutableLiveData(false)
|
||||||
|
val isLoading: LiveData<Boolean>
|
||||||
|
get() = _isLoading
|
||||||
|
|
||||||
|
fun loadCreatorRankings() {
|
||||||
|
_isLoading.value = true
|
||||||
|
_rankingStateLiveData.value = HomeCreatorRankingUiState.Loading
|
||||||
|
|
||||||
|
compositeDisposable.add(
|
||||||
|
repository.getCreatorRankings(token = authToken())
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(
|
||||||
|
{
|
||||||
|
_isLoading.value = false
|
||||||
|
val data = it.data
|
||||||
|
if (it.success && data != null) {
|
||||||
|
val items = data.toCreatorRankingItems()
|
||||||
|
_rankingStateLiveData.value = if (items.isEmpty()) {
|
||||||
|
HomeCreatorRankingUiState.Empty
|
||||||
|
} else {
|
||||||
|
HomeCreatorRankingUiState.Content(items = items)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
showUnknownError(it.message)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_isLoading.value = false
|
||||||
|
it.message?.let { message -> Logger.e(message) }
|
||||||
|
showUnknownError(it.message)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showUnknownError(message: String?) {
|
||||||
|
_rankingStateLiveData.value = HomeCreatorRankingUiState.Error(message = message)
|
||||||
|
_toastLiveData.postValue(ToastMessage(resId = R.string.common_error_unknown))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.main.home.model
|
||||||
|
|
||||||
|
import kr.co.vividnext.sodalive.v2.widget.creatorranking.CreatorRankingItem
|
||||||
|
|
||||||
|
sealed interface HomeCreatorRankingUiState {
|
||||||
|
data object Loading : HomeCreatorRankingUiState
|
||||||
|
|
||||||
|
data class Content(
|
||||||
|
val items: List<CreatorRankingItem>
|
||||||
|
) : HomeCreatorRankingUiState
|
||||||
|
|
||||||
|
data object Empty : HomeCreatorRankingUiState
|
||||||
|
|
||||||
|
data class Error(
|
||||||
|
val message: String? = null
|
||||||
|
) : HomeCreatorRankingUiState
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user