Created
May 17, 2020 22:54
-
-
Save DenisBronx/0f08d2fadfed14061cfe93b2ff118ed7 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 //interface | |
) : 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