Skip to content

Instantly share code, notes, and snippets.

@antonicg
Created February 5, 2018 16:36
Show Gist options
  • Save antonicg/410fbe3b7d0366a946dfbf1c58feb6a1 to your computer and use it in GitHub Desktop.
Save antonicg/410fbe3b7d0366a946dfbf1c58feb6a1 to your computer and use it in GitHub Desktop.
package com.antonicastejon.cryptodata.presentation.main.crypto_list
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import com.antonicastejon.cryptodata.di.SCHEDULER_IO
import com.antonicastejon.cryptodata.di.SCHEDULER_MAIN_THREAD
import com.antonicastejon.cryptodata.domain.CryptoListUseCases
import com.antonicastejon.cryptodata.domain.CryptoViewModel
import com.antonicastejon.cryptodata.domain.LIMIT_CRYPTO_LIST
import io.reactivex.Scheduler
import javax.inject.Inject
import javax.inject.Named
/**
* Created by Antoni Castejón on 31/12/2017.
*/
private val TAG = CryptoListViewModel::class.java.name
class CryptoListViewModel
@Inject constructor(private val cryptoListUseCases: CryptoListUseCases, @Named(SCHEDULER_IO) val subscribeOnScheduler:Scheduler, @Named(SCHEDULER_MAIN_THREAD) val observeOnScheduler: Scheduler) : ViewModel() {
val stateLiveData = MutableLiveData<CryptoListState>()
init {
stateLiveData.value = DefaultState(0, false, emptyList())
}
fun updateCryptoList() {
val pageNum = obtainCurrentPageNum()
stateLiveData.value = if (pageNum == 0)
LoadingState(pageNum, false, obtainCurrentData())
else
PaginatingState(pageNum, false, obtainCurrentData())
getCryptoList(pageNum)
}
fun resetCryptoList() {
val pageNum = 0
stateLiveData.value = LoadingState(pageNum, false, emptyList())
updateCryptoList()
}
fun restoreCryptoList() {
val pageNum = obtainCurrentPageNum()
stateLiveData.value = DefaultState(pageNum, false, obtainCurrentData())
}
private fun getCryptoList(page:Int) {
cryptoListUseCases.getCryptoListBy(page)
.subscribeOn(subscribeOnScheduler)
.observeOn(observeOnScheduler)
.subscribe(this::onCryptoListReceived, this::onError)
}
private fun onCryptoListReceived(cryptoList: List<CryptoViewModel>) {
val currentCryptoList = obtainCurrentData().toMutableList()
val currentPageNum = obtainCurrentPageNum() + 1
val areAllItemsLoaded = cryptoList.size < LIMIT_CRYPTO_LIST
currentCryptoList.addAll(cryptoList)
stateLiveData.value = DefaultState(currentPageNum, areAllItemsLoaded, currentCryptoList)
}
private fun onError(error: Throwable) {
val pageNum = stateLiveData.value?.pageNum ?: 0
stateLiveData.value = ErrorState(error.message ?: "", pageNum, obtainCurrentLoadedAllItems(), obtainCurrentData())
}
private fun obtainCurrentPageNum() = stateLiveData.value?.pageNum ?: 0
private fun obtainCurrentData() = stateLiveData.value?.data ?: emptyList()
private fun obtainCurrentLoadedAllItems() = stateLiveData.value?.loadedAllItems ?: false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment