Created
October 25, 2018 14:09
-
-
Save Benjiko99/1ec1b59db70e76c06c732f4b426e0e23 to your computer and use it in GitHub Desktop.
This file contains 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 TransactionListViewModel @Inject constructor( | |
private val repository: TransactionListRepository | |
) : ViewModel() { | |
val transactions = MediatorLiveData<Resource<List<Transaction>>>() | |
private val retryTransactionsTrigger = MutableLiveData<Nothing>() | |
private val transactionListFilter = MutableLiveData<TransactionListFilter>() | |
init { | |
transactionListFilter.value = TransactionListFilter() | |
val filterChanged = Transformations.switchMap(transactionListFilter) { filter -> | |
repository.loadTransactionList(filter) | |
} | |
val retryTriggered = Transformations.switchMap(retryTransactionsTrigger) { | |
repository.loadTransactionList(transactionListFilter.value!!) | |
} | |
transactions.addSource(filterChanged, transactions::setValue) | |
transactions.addSource(retryTriggered, transactions::setValue) | |
} | |
fun setTransactionDirectionFilter(transactionDirectionFilter: TransactionDirectionFilter) { | |
transactionListFilter.value?.transactionDirectionFilter = transactionDirectionFilter | |
transactionListFilter.value = transactionListFilter.value | |
} | |
fun retry() { | |
retryTransactionsTrigger.value = retryTransactionsTrigger.value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone is using this as a reference, know that it is not the correct way to do it, and this solution is better: https://github.com/Benjiko99/AirBankInterview/blob/master/app/src/main/java/com/spiraclesoftware/airbankinterview/transaction/list/ui/TransactionListViewModel.kt