pull/433/head
gedoor 4 years ago
parent 50f9c61887
commit 1dba9063d1
  1. 56
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  2. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -51,6 +51,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
get() = getViewModel(BookshelfViewModel::class.java) get() = getViewModel(BookshelfViewModel::class.java)
private val activityViewModel: MainViewModel private val activityViewModel: MainViewModel
get() = getViewModelOfActivity(MainViewModel::class.java) get() = getViewModelOfActivity(MainViewModel::class.java)
private lateinit var adapter: FragmentStatePagerAdapter
private var bookGroupLiveData: LiveData<List<BookGroup>>? = null private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private var noGroupLiveData: LiveData<Int>? = null private var noGroupLiveData: LiveData<Int>? = null
private val bookGroups = mutableListOf<BookGroup>() private val bookGroups = mutableListOf<BookGroup>()
@ -104,7 +105,8 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
tab_layout.setSelectedTabIndicatorColor(requireContext().accentColor) tab_layout.setSelectedTabIndicatorColor(requireContext().accentColor)
tab_layout.setupWithViewPager(view_pager_bookshelf) tab_layout.setupWithViewPager(view_pager_bookshelf)
view_pager_bookshelf.offscreenPageLimit = 1 view_pager_bookshelf.offscreenPageLimit = 1
view_pager_bookshelf.adapter = TabFragmentPageAdapter(childFragmentManager) adapter = TabFragmentPageAdapter(childFragmentManager)
view_pager_bookshelf.adapter = adapter
} }
private fun initBookGroupData() { private fun initBookGroupData() {
@ -112,39 +114,9 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, { bookGroupLiveData?.observe(viewLifecycleOwner, {
viewModel.checkGroup(it) viewModel.checkGroup(it)
launch {
synchronized(this) {
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment)
}
var noGroupSize = 0
withContext(IO) {
if (AppConfig.bookGroupNoneShow) {
noGroupSize = App.db.bookDao().noGroupSize
}
}
synchronized(this@BookshelfFragment) {
bookGroups.clear() bookGroups.clear()
if (AppConfig.bookGroupAllShow) {
bookGroups.add(AppConst.bookGroupAll)
}
if (AppConfig.bookGroupLocalShow) {
bookGroups.add(AppConst.bookGroupLocal)
}
if (AppConfig.bookGroupAudioShow) {
bookGroups.add(AppConst.bookGroupAudio)
}
showGroupNone = if (noGroupSize > 0 && it.isNotEmpty()) {
bookGroups.add(AppConst.bookGroupNone)
true
} else {
false
}
bookGroups.addAll(it) bookGroups.addAll(it)
view_pager_bookshelf.adapter?.notifyDataSetChanged() upGroup()
tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select()
tab_layout.addOnTabSelectedListener(this@BookshelfFragment)
}
}
}) })
noGroupLiveData?.removeObservers(viewLifecycleOwner) noGroupLiveData?.removeObservers(viewLifecycleOwner)
noGroupLiveData = App.db.bookDao().observeNoGroupSize() noGroupLiveData = App.db.bookDao().observeNoGroupSize()
@ -197,9 +169,17 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
bookGroups.add(0, AppConst.bookGroupAll) bookGroups.add(0, AppConst.bookGroupAll)
} }
view_pager_bookshelf.adapter?.notifyDataSetChanged() adapter.notifyDataSetChanged()
selectLastTab()
}
} }
} }
@Synchronized
private fun selectLastTab() {
tab_layout.removeOnTabSelectedListener(this)
tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select()
tab_layout.addOnTabSelectedListener(this)
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -252,14 +232,12 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
}.show().applyTint() }.show().applyTint()
} }
override fun onTabReselected(tab: TabLayout.Tab?) = Unit override fun onTabReselected(tab: TabLayout.Tab) = Unit
override fun onTabUnselected(tab: TabLayout.Tab?) = Unit override fun onTabUnselected(tab: TabLayout.Tab) = Unit
override fun onTabSelected(tab: TabLayout.Tab?) { override fun onTabSelected(tab: TabLayout.Tab) {
tab?.position?.let { putPrefInt(PreferKey.saveTabPosition, tab.position)
putPrefInt(PreferKey.saveTabPosition, it)
}
} }
fun gotoTop() { fun gotoTop() {

@ -63,7 +63,6 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
fun checkGroup(groups: List<BookGroup>) { fun checkGroup(groups: List<BookGroup>) {
execute {
groups.forEach { group -> groups.forEach { group ->
if (group.groupId and (group.groupId - 1) != 0L) { if (group.groupId and (group.groupId - 1) != 0L) {
var id = 1L var id = 1L
@ -77,6 +76,5 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
} }
} }
} }
}
} }

Loading…
Cancel
Save