pull/1395/head
gedoor 3 years ago
parent 3ad8aba2d5
commit 98492c3249
  1. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/BookshelfFragment1.kt
  2. 17
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt
  3. 22
      app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt
  4. 4
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt

@ -27,6 +27,7 @@ import io.legado.app.utils.putPrefInt
import io.legado.app.utils.setEdgeEffectColor import io.legado.app.utils.setEdgeEffectColor
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -44,7 +45,7 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
} }
private val bookGroups = mutableListOf<BookGroup>() private val bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Long, BooksFragment>() private val fragmentMap = hashMapOf<Long, BooksFragment>()
private var groupsFlowJob: Job? = null
override val groupId: Long get() = selectedGroup.groupId override val groupId: Long get() = selectedGroup.groupId
override val books: List<Book> override val books: List<Book>
@ -73,7 +74,8 @@ class BookshelfFragment1 : BaseBookshelfFragment(R.layout.fragment_bookshelf),
} }
private fun initBookGroupData() { private fun initBookGroupData() {
launch { groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.bookGroupDao.flowShow().collect { appDb.bookGroupDao.flowShow().collect {
upGroup(it) upGroup(it)
} }

@ -45,9 +45,17 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
} }
private val binding by viewBinding(FragmentBooksBinding::bind) private val binding by viewBinding(FragmentBooksBinding::bind)
private val activityViewModel: MainViewModel private val activityViewModel by activityViewModels<MainViewModel>()
by activityViewModels() private val bookshelfLayout by lazy {
private lateinit var booksAdapter: BaseBooksAdapter<*> getPrefInt(PreferKey.bookshelfLayout)
}
private val booksAdapter: BaseBooksAdapter<*> by lazy {
if (bookshelfLayout == 0) {
BooksAdapterList(requireContext(), this)
} else {
BooksAdapterGrid(requireContext(), this)
}
}
private var booksFlowJob: Job? = null private var booksFlowJob: Job? = null
private var position = 0 private var position = 0
private var groupId = -1L private var groupId = -1L
@ -68,13 +76,10 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
binding.refreshLayout.isRefreshing = false binding.refreshLayout.isRefreshing = false
activityViewModel.upToc(booksAdapter.getItems()) activityViewModel.upToc(booksAdapter.getItems())
} }
val bookshelfLayout = getPrefInt(PreferKey.bookshelfLayout)
if (bookshelfLayout == 0) { if (bookshelfLayout == 0) {
binding.rvBookshelf.layoutManager = LinearLayoutManager(context) binding.rvBookshelf.layoutManager = LinearLayoutManager(context)
booksAdapter = BooksAdapterList(requireContext(), this)
} else { } else {
binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2) binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2)
booksAdapter = BooksAdapterGrid(requireContext(), this)
} }
binding.rvBookshelf.adapter = booksAdapter binding.rvBookshelf.adapter = booksAdapter
booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {

@ -41,10 +41,20 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
BaseBooksAdapter.CallBack { BaseBooksAdapter.CallBack {
private val binding by viewBinding(FragmentBookshelf1Binding::bind) private val binding by viewBinding(FragmentBookshelf1Binding::bind)
private lateinit var booksAdapter: BaseBooksAdapter<*> private val bookshelfLayout by lazy {
override var groupId = AppConst.bookGroupNoneId getPrefInt(PreferKey.bookshelfLayout)
private var booksFlowJob: Job? = null }
private val booksAdapter: BaseBooksAdapter<*> by lazy {
if (bookshelfLayout == 0) {
BooksAdapterList(requireContext(), this)
} else {
BooksAdapterGrid(requireContext(), this)
}
}
private var bookGroups: List<BookGroup> = emptyList() private var bookGroups: List<BookGroup> = emptyList()
private var groupsFlowJob: Job? = null
private var booksFlowJob: Job? = null
override var groupId = AppConst.bookGroupNoneId
override var books: List<Book> = emptyList() override var books: List<Book> = emptyList()
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
@ -61,13 +71,10 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
binding.refreshLayout.isRefreshing = false binding.refreshLayout.isRefreshing = false
activityViewModel.upToc(books) activityViewModel.upToc(books)
} }
val bookshelfLayout = getPrefInt(PreferKey.bookshelfLayout)
if (bookshelfLayout == 0) { if (bookshelfLayout == 0) {
binding.rvBookshelf.layoutManager = LinearLayoutManager(context) binding.rvBookshelf.layoutManager = LinearLayoutManager(context)
booksAdapter = BooksAdapterList(requireContext(), this)
} else { } else {
binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2) binding.rvBookshelf.layoutManager = GridLayoutManager(context, bookshelfLayout + 2)
booksAdapter = BooksAdapterGrid(requireContext(), this)
} }
binding.rvBookshelf.adapter = booksAdapter binding.rvBookshelf.adapter = booksAdapter
booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { booksAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
@ -91,7 +98,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
private fun initGroupData() { private fun initGroupData() {
launch { groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.bookGroupDao.flowShow().collect { appDb.bookGroupDao.flowShow().collect {
if (it != bookGroups) { if (it != bookGroups) {
bookGroups = it bookGroups = it

@ -41,6 +41,7 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
private val searchView: SearchView by lazy { private val searchView: SearchView by lazy {
binding.titleBar.findViewById(R.id.search_view) binding.titleBar.findViewById(R.id.search_view)
} }
private var groupsFlowJob: Job? = null
private var rssFlowJob: Job? = null private var rssFlowJob: Job? = null
private val groups = linkedSetOf<String>() private val groups = linkedSetOf<String>()
private var groupsMenu: SubMenu? = null private var groupsMenu: SubMenu? = null
@ -117,7 +118,8 @@ class RssFragment : VMBaseFragment<RssSourceViewModel>(R.layout.fragment_rss),
} }
private fun initGroupData() { private fun initGroupData() {
launch { groupsFlowJob?.cancel()
groupsFlowJob = launch {
appDb.rssSourceDao.flowGroup().collect { appDb.rssSourceDao.flowGroup().collect {
groups.clear() groups.clear()
it.map { group -> it.map { group ->

Loading…
Cancel
Save