Skip to content

Commit

Permalink
Use Flow instead of LiveData in the Test App (readium#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenzeck authored Feb 5, 2024
1 parent 2a3fbfe commit d5a53ff
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.readium.r2.shared.util.AbsoluteUrl
import org.readium.r2.testapp.Application
import org.readium.r2.testapp.R
Expand Down Expand Up @@ -103,9 +108,12 @@ class BookshelfFragment : Fragment() {
)
)
}

bookshelfViewModel.books.observe(viewLifecycleOwner) {
bookshelfAdapter.submitList(it)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
bookshelfViewModel.books.collectLatest {
bookshelfAdapter.submitList(it)
}
}
}

binding.bookshelfAddBookFab.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.readium.r2.testapp.R
import org.readium.r2.testapp.data.model.Catalog
import org.readium.r2.testapp.databinding.FragmentCatalogFeedListBinding
Expand Down Expand Up @@ -62,9 +67,13 @@ class CatalogFeedListFragment : Fragment() {
)
}

catalogFeedListViewModel.catalogs.observe(viewLifecycleOwner, {
catalogsAdapter.submitList(it)
})
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
catalogFeedListViewModel.catalogs.collectLatest {
catalogsAdapter.submitList(it)
}
}
}

val version = 2
val VERSION_KEY = "OPDS_CATALOG_VERSION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.readium.r2.testapp.data

import androidx.annotation.ColorInt
import androidx.lifecycle.LiveData
import java.io.File
import kotlinx.coroutines.flow.Flow
import org.joda.time.DateTime
Expand All @@ -25,7 +24,7 @@ import org.readium.r2.testapp.utils.extensions.readium.authorName
class BookRepository(
private val booksDao: BooksDao
) {
fun books(): LiveData<List<Book>> = booksDao.getAllBooks()
fun books(): Flow<List<Book>> = booksDao.getAllBooks()

suspend fun get(id: Long) = booksDao.get(id)

Expand All @@ -48,7 +47,7 @@ class BookRepository(
return booksDao.insertBookmark(bookmark)
}

fun bookmarksForBook(bookId: Long): LiveData<List<Bookmark>> =
fun bookmarksForBook(bookId: Long): Flow<List<Bookmark>> =
booksDao.getBookmarksForBook(bookId)

suspend fun deleteBookmark(bookmarkId: Long) = booksDao.deleteBookmark(bookmarkId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package org.readium.r2.testapp.data

import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow
import org.readium.r2.testapp.data.db.CatalogDao
import org.readium.r2.testapp.data.model.Catalog

Expand All @@ -16,7 +16,7 @@ class CatalogRepository(private val catalogDao: CatalogDao) {
return catalogDao.insertCatalog(catalog)
}

fun getCatalogsFromDatabase(): LiveData<List<Catalog>> = catalogDao.getCatalogModels()
fun getCatalogsFromDatabase(): Flow<List<Catalog>> = catalogDao.getCatalogModels()

suspend fun deleteCatalog(id: Long) = catalogDao.deleteCatalog(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.readium.r2.testapp.data.db

import androidx.annotation.ColorInt
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
Expand Down Expand Up @@ -43,18 +42,18 @@ interface BooksDao {

/**
* Retrieve all books
* @return List of books as LiveData
* @return List of books as Flow
*/
@Query("SELECT * FROM " + Book.TABLE_NAME + " ORDER BY " + Book.CREATION_DATE + " desc")
fun getAllBooks(): LiveData<List<Book>>
fun getAllBooks(): Flow<List<Book>>

/**
* Retrieve all bookmarks for a specific book
* @param bookId The ID of the book
* @return List of bookmarks for the book as LiveData
* @return List of bookmarks for the book as Flow
*/
@Query("SELECT * FROM " + Bookmark.TABLE_NAME + " WHERE " + Bookmark.BOOK_ID + " = :bookId")
fun getBookmarksForBook(bookId: Long): LiveData<List<Bookmark>>
fun getBookmarksForBook(bookId: Long): Flow<List<Bookmark>>

/**
* Retrieve all highlights for a specific book
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

package org.readium.r2.testapp.data.db

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow
import org.readium.r2.testapp.data.model.Catalog

@Dao
Expand All @@ -26,19 +26,19 @@ interface CatalogDao {

/**
* Retrieve list of Catalog models based on Catalog model
* @return List of Catalog models as LiveData
* @return List of Catalog models as Flow
*/
@Query(
"SELECT * FROM " + Catalog.TABLE_NAME + " WHERE " + Catalog.TITLE + " = :title AND " + Catalog.HREF + " = :href AND " + Catalog.TYPE + " = :type"
)
fun getCatalogModels(title: String, href: String, type: Int): LiveData<List<Catalog>>
fun getCatalogModels(title: String, href: String, type: Int): Flow<List<Catalog>>

/**
* Retrieve list of all Catalog models
* @return List of Catalog models as LiveData
* @return List of Catalog models as Flow
*/
@Query("SELECT * FROM " + Catalog.TABLE_NAME)
fun getCatalogModels(): LiveData<List<Catalog>>
fun getCatalogModels(): Flow<List<Catalog>>

/**
* Deletes an Catalog model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.roundToInt
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.readium.r2.shared.publication.Publication
Expand Down Expand Up @@ -73,9 +78,13 @@ class BookmarksFragment : Fragment() {
{ it.resourceIndex },
{ it.locator.locations.progression }
)
viewModel.getBookmarks().observe(viewLifecycleOwner) {
val bookmarks = it.sortedWith(comparator)
bookmarkAdapter.submitList(bookmarks)
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.getBookmarks().collectLatest {
val bookmarks = it.sortedWith(comparator)
bookmarkAdapter.submitList(bookmarks)
}
}
}
}

Expand Down

0 comments on commit d5a53ff

Please sign in to comment.