pull/433/head
gedoor 4 years ago
parent 50f9c61887
commit 1dba9063d1
  1. 60
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  2. 20
      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 { bookGroups.clear()
synchronized(this) { bookGroups.addAll(it)
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment) upGroup()
}
var noGroupSize = 0
withContext(IO) {
if (AppConfig.bookGroupNoneShow) {
noGroupSize = App.db.bookDao().noGroupSize
}
}
synchronized(this@BookshelfFragment) {
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)
view_pager_bookshelf.adapter?.notifyDataSetChanged()
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,11 +169,19 @@ 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")
private fun configBookshelf() { private fun configBookshelf() {
requireContext().alert(titleResource = R.string.bookshelf_layout) { requireContext().alert(titleResource = R.string.bookshelf_layout) {
@ -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,18 +63,16 @@ 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 val idsSum = App.db.bookGroupDao().idsSum
val idsSum = App.db.bookGroupDao().idsSum while (id and idsSum != 0L) {
while (id and idsSum != 0L) { id = id.shl(1)
id = id.shl(1)
}
App.db.bookGroupDao().delete(group)
App.db.bookGroupDao().insert(group.copy(groupId = id))
App.db.bookDao().upGroup(group.groupId, id)
} }
App.db.bookGroupDao().delete(group)
App.db.bookGroupDao().insert(group.copy(groupId = id))
App.db.bookDao().upGroup(group.groupId, id)
} }
} }
} }

Loading…
Cancel
Save