feat: 优化代码

pull/182/head
kunfei 5 years ago
parent 7cbbc0f057
commit e890564a9a
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 38
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt

@ -30,6 +30,9 @@ interface BookDao {
@Query("select * from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0") @Query("select * from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0")
fun observeNoGroup(): LiveData<List<Book>> fun observeNoGroup(): LiveData<List<Book>>
@Query("select count(bookUrl) from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0")
fun observeNoGroupSize(): LiveData<Int>
@Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'") @Query("SELECT * FROM books WHERE name like '%'||:key||'%' or author like '%'||:key||'%'")
fun liveDataSearch(key: String): LiveData<List<Book>> fun liveDataSearch(key: String): LiveData<List<Book>>

@ -54,8 +54,10 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
get() = getViewModel(BookshelfViewModel::class.java) get() = getViewModel(BookshelfViewModel::class.java)
private var bookGroupLiveData: LiveData<List<BookGroup>>? = null private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private var noGroupLiveData: LiveData<Int>? = null
private val bookGroups = mutableListOf<BookGroup>() private val bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Int, Fragment>() private val fragmentMap = hashMapOf<Int, Fragment>()
private var showGroupNone = false
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
setSupportToolbar(toolbar) setSupportToolbar(toolbar)
@ -104,11 +106,10 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
viewModel.checkGroup(it) viewModel.checkGroup(it)
launch { launch {
synchronized(this) { synchronized(this) {
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment)
} }
withContext(IO) { withContext(IO) {
synchronized(this@BookshelfFragment) { synchronized(this@BookshelfFragment) {
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment)
bookGroups.clear() bookGroups.clear()
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
bookGroups.add(AppConst.bookGroupAll) bookGroups.add(AppConst.bookGroupAll)
@ -119,15 +120,31 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(AppConst.bookGroupAudio) bookGroups.add(AppConst.bookGroupAudio)
} }
if (App.db.bookDao().noGroupSize > 0) { showGroupNone = if (App.db.bookDao().noGroupSize > 0) {
bookGroups.add(AppConst.bookGroupNone) bookGroups.add(AppConst.bookGroupNone)
true
} else {
false
} }
bookGroups.addAll(it) bookGroups.addAll(it)
} }
} }
view_pager_bookshelf.adapter?.notifyDataSetChanged() synchronized(this@BookshelfFragment) {
tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select() view_pager_bookshelf.adapter?.notifyDataSetChanged()
tab_layout.addOnTabSelectedListener(this@BookshelfFragment) 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<BookshelfViewModel>(R.layout.fragment_b
bookGroups.remove(AppConst.bookGroupLocal) bookGroups.remove(AppConst.bookGroupLocal)
bookGroups.remove(AppConst.bookGroupAudio) bookGroups.remove(AppConst.bookGroupAudio)
bookGroups.remove(AppConst.bookGroupNone) bookGroups.remove(AppConst.bookGroupNone)
if (App.db.bookDao().noGroupSize > 0) { showGroupNone = if (App.db.bookDao().noGroupSize > 0) {
bookGroups.add(0, AppConst.bookGroupNone) bookGroups.add(0, AppConst.bookGroupNone)
true
} else {
false
} }
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(0, AppConst.bookGroupAudio) bookGroups.add(0, AppConst.bookGroupAudio)
@ -163,7 +183,9 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
} }
} }
} }
view_pager_bookshelf.adapter?.notifyDataSetChanged() synchronized(this@BookshelfFragment) {
view_pager_bookshelf.adapter?.notifyDataSetChanged()
}
} }
} }

Loading…
Cancel
Save