Skip to content

Instantly share code, notes, and snippets.

@DenisBronx
Created May 17, 2020 23:38
Show Gist options
  • Save DenisBronx/016e58faa0ad72ecf0aab4dca1adf530 to your computer and use it in GitHub Desktop.
Save DenisBronx/016e58faa0ad72ecf0aab4dca1adf530 to your computer and use it in GitHub Desktop.
class TransactionListViewModelImpl(
private val getTransactionsUseCase: GetTransactionsUseCase //typealias
) : TransactionListViewModel, ViewModel() {
private val compositeDisposable = CompositeDisposable()
override val state = MutableLiveData<State>()
override fun loadTransactions() {
state.postValue(State.Loading)
getTransactionsUseCase().subscribeBy { handleResult(it) }
.addTo(compositeDisposable)
}
private fun handleResult(getTransactionsResult: SimpleResult<List<Transaction>>) {
getTransactionsResult.fold(
success = { transactions -> state.postValue(State.Content(transactions)) },
failure = { throwable -> state.postValue(State.Error(throwable.message.orEmpty())) }
)
}
override fun onCleared() {
compositeDisposable.clear()
super.onCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment