diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index af82c14f1..56bf439f6 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -51,6 +51,7 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b get() = getViewModel(BookshelfViewModel::class.java) private val activityViewModel: MainViewModel get() = getViewModelOfActivity(MainViewModel::class.java) + private lateinit var adapter: FragmentStatePagerAdapter private var bookGroupLiveData: LiveData>? = null private var noGroupLiveData: LiveData? = null private val bookGroups = mutableListOf() @@ -104,7 +105,8 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b tab_layout.setSelectedTabIndicatorColor(requireContext().accentColor) tab_layout.setupWithViewPager(view_pager_bookshelf) view_pager_bookshelf.offscreenPageLimit = 1 - view_pager_bookshelf.adapter = TabFragmentPageAdapter(childFragmentManager) + adapter = TabFragmentPageAdapter(childFragmentManager) + view_pager_bookshelf.adapter = adapter } private fun initBookGroupData() { @@ -112,39 +114,9 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b bookGroupLiveData = App.db.bookGroupDao().liveDataAll() bookGroupLiveData?.observe(viewLifecycleOwner, { 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() - 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) - } - } + bookGroups.clear() + bookGroups.addAll(it) + upGroup() }) noGroupLiveData?.removeObservers(viewLifecycleOwner) noGroupLiveData = App.db.bookDao().observeNoGroupSize() @@ -197,11 +169,19 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b if (AppConfig.bookGroupAllShow) { 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") private fun configBookshelf() { requireContext().alert(titleResource = R.string.bookshelf_layout) { @@ -252,14 +232,12 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b }.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?) { - tab?.position?.let { - putPrefInt(PreferKey.saveTabPosition, it) - } + override fun onTabSelected(tab: TabLayout.Tab) { + putPrefInt(PreferKey.saveTabPosition, tab.position) } fun gotoTop() { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 298b84cca..e9fb4cd4c 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -63,18 +63,16 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } fun checkGroup(groups: List) { - execute { - groups.forEach { group -> - if (group.groupId and (group.groupId - 1) != 0L) { - var id = 1L - val idsSum = App.db.bookGroupDao().idsSum - while (id and idsSum != 0L) { - id = id.shl(1) - } - App.db.bookGroupDao().delete(group) - App.db.bookGroupDao().insert(group.copy(groupId = id)) - App.db.bookDao().upGroup(group.groupId, id) + groups.forEach { group -> + if (group.groupId and (group.groupId - 1) != 0L) { + var id = 1L + val idsSum = App.db.bookGroupDao().idsSum + while (id and idsSum != 0L) { + id = id.shl(1) } + App.db.bookGroupDao().delete(group) + App.db.bookGroupDao().insert(group.copy(groupId = id)) + App.db.bookDao().upGroup(group.groupId, id) } } }