pull/1625/head
kunfei 3 years ago
parent b9d37d2a7f
commit 775426c908
  1. 12
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt
  2. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt

@ -23,14 +23,17 @@ import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.main.MainViewModel import io.legado.app.ui.main.MainViewModel
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.math.max import kotlin.math.max
/** /**
* 书架界面 * 书架界面
*/ */
@ExperimentalCoroutinesApi
class BooksFragment() : BaseFragment(R.layout.fragment_books), class BooksFragment() : BaseFragment(R.layout.fragment_books),
BaseBooksAdapter.CallBack { BaseBooksAdapter.CallBack {
@ -109,9 +112,8 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
else -> appDb.bookDao.flowByGroup(groupId) else -> appDb.bookDao.flowByGroup(groupId)
}.catch { }.catch {
AppLog.put("书架更新出错", it) AppLog.put("书架更新出错", it)
}.collect { list -> }.mapLatest { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty() when (getPrefInt(PreferKey.bookshelfSort)) {
val books = when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { it.latestChapterTime } 1 -> list.sortedByDescending { it.latestChapterTime }
2 -> list.sortedWith { o1, o2 -> 2 -> list.sortedWith { o1, o2 ->
o1.name.cnCompare(o2.name) o1.name.cnCompare(o2.name)
@ -119,7 +121,9 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
3 -> list.sortedBy { it.order } 3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime } else -> list.sortedByDescending { it.durChapterTime }
} }
booksAdapter.setItems(books) }.collect { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty()
booksAdapter.setItems(list)
} }
} }
} }

@ -25,15 +25,18 @@ import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.math.max import kotlin.math.max
/** /**
* 书架界面 * 书架界面
*/ */
@ExperimentalCoroutinesApi
class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
SearchView.OnQueryTextListener, SearchView.OnQueryTextListener,
BaseBooksAdapter.CallBack { BaseBooksAdapter.CallBack {
@ -123,8 +126,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
else -> appDb.bookDao.flowByGroup(groupId) else -> appDb.bookDao.flowByGroup(groupId)
}.catch { }.catch {
AppLog.put("书架更新出错", it) AppLog.put("书架更新出错", it)
}.collectLatest { list -> }.mapLatest { list ->
books = when (getPrefInt(PreferKey.bookshelfSort)) { when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> list.sortedByDescending { 1 -> list.sortedByDescending {
it.latestChapterTime it.latestChapterTime
} }
@ -138,6 +141,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
it.durChapterTime it.durChapterTime
} }
} }
}.collectLatest { list ->
books = list
booksAdapter.notifyDataSetChanged() booksAdapter.notifyDataSetChanged()
binding.tvEmptyMsg.isGone = getItemCount() > 0 binding.tvEmptyMsg.isGone = getItemCount() > 0
} }

Loading…
Cancel
Save