Skip to content

Instantly share code, notes, and snippets.

@Benjiko99
Created October 25, 2018 14:09
Show Gist options
  • Save Benjiko99/1ec1b59db70e76c06c732f4b426e0e23 to your computer and use it in GitHub Desktop.
Save Benjiko99/1ec1b59db70e76c06c732f4b426e0e23 to your computer and use it in GitHub Desktop.
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
}
}
@Benjiko99
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment