Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BooksViewModel(bookDao: BookDao) : ViewModel() {
- // the LiveData from Room won't be exposed to the view...
- private val dbBooks = bookDao.books()
- // ...because this is what we'll want to expose
- val books = MediatorLiveData<List<Book>>()
- private var currentOrder = ASCENDING
- init {
- // here our MediatorLiveData is basically a proxy to dbBooks
- books.addSource(dbBooks) { result: List<Book>? ->
- result?.let { books.value = sortBooks(it, currentOrder) }
- }
- }
- fun rearrangeBooks(order: BooksOrder) = dbBooks.value?.let {
- // and here we can set the value we want
- books.value = sortBooks(it, order)
- }.also { currentOrder = order }
- }
Add Comment
Please, Sign In to add comment