Created
May 17, 2020 23:38
-
-
Save DenisBronx/016e58faa0ad72ecf0aab4dca1adf530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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