Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. class BooksViewModel(bookDao: BookDao) : ViewModel() {
  2.  
  3. // the LiveData from Room won't be exposed to the view...
  4. private val dbBooks = bookDao.books()
  5.  
  6. // ...because this is what we'll want to expose
  7. val books = MediatorLiveData<List<Book>>()
  8.  
  9. private var currentOrder = ASCENDING
  10.  
  11. init {
  12. // here our MediatorLiveData is basically a proxy to dbBooks
  13. books.addSource(dbBooks) { result: List<Book>? ->
  14. result?.let { books.value = sortBooks(it, currentOrder) }
  15. }
  16. }
  17.  
  18. fun rearrangeBooks(order: BooksOrder) = dbBooks.value?.let {
  19. // and here we can set the value we want
  20. books.value = sortBooks(it, order)
  21. }.also { currentOrder = order }
  22. }
Add Comment
Please, Sign In to add comment