From e890564a9a62eca389bffcab4102093ea722e3e2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 6 Apr 2020 21:11:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/dao/BookDao.kt | 3 ++ .../ui/main/bookshelf/BookshelfFragment.kt | 38 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index 268398461..c91e01648 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -30,6 +30,9 @@ interface BookDao { @Query("select * from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0") fun observeNoGroup(): LiveData> + @Query("select count(bookUrl) from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0") + fun observeNoGroupSize(): LiveData + @Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'") fun liveDataSearch(key: String): LiveData> 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 9c24a721c..3e353cd20 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 @@ -54,8 +54,10 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b get() = getViewModel(BookshelfViewModel::class.java) private var bookGroupLiveData: LiveData>? = null + private var noGroupLiveData: LiveData? = null private val bookGroups = mutableListOf() private val fragmentMap = hashMapOf() + private var showGroupNone = false override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -104,11 +106,10 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b viewModel.checkGroup(it) launch { synchronized(this) { - + tab_layout.removeOnTabSelectedListener(this@BookshelfFragment) } withContext(IO) { synchronized(this@BookshelfFragment) { - tab_layout.removeOnTabSelectedListener(this@BookshelfFragment) bookGroups.clear() if (AppConfig.bookGroupAllShow) { bookGroups.add(AppConst.bookGroupAll) @@ -119,15 +120,31 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b if (AppConfig.bookGroupAudioShow) { bookGroups.add(AppConst.bookGroupAudio) } - if (App.db.bookDao().noGroupSize > 0) { + showGroupNone = if (App.db.bookDao().noGroupSize > 0) { 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) + synchronized(this@BookshelfFragment) { + view_pager_bookshelf.adapter?.notifyDataSetChanged() + tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select() + tab_layout.addOnTabSelectedListener(this@BookshelfFragment) + } + } + }) + noGroupLiveData?.removeObservers(viewLifecycleOwner) + noGroupLiveData = App.db.bookDao().observeNoGroupSize() + noGroupLiveData?.observe(viewLifecycleOwner, Observer { + if (it > 0 && !showGroupNone) { + showGroupNone = true + upGroup() + } else if (it == 0 && showGroupNone) { + showGroupNone = false + upGroup() } }) } @@ -149,8 +166,11 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b bookGroups.remove(AppConst.bookGroupLocal) bookGroups.remove(AppConst.bookGroupAudio) bookGroups.remove(AppConst.bookGroupNone) - if (App.db.bookDao().noGroupSize > 0) { + showGroupNone = if (App.db.bookDao().noGroupSize > 0) { bookGroups.add(0, AppConst.bookGroupNone) + true + } else { + false } if (AppConfig.bookGroupAudioShow) { bookGroups.add(0, AppConst.bookGroupAudio) @@ -163,7 +183,9 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b } } } - view_pager_bookshelf.adapter?.notifyDataSetChanged() + synchronized(this@BookshelfFragment) { + view_pager_bookshelf.adapter?.notifyDataSetChanged() + } } }