feat: 优化代码

pull/182/head
kunfei 5 years ago
parent a6937bd434
commit 11f0e73476
  1. 16
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt

@ -108,7 +108,10 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
synchronized(this) { synchronized(this) {
tab_layout.removeOnTabSelectedListener(this@BookshelfFragment) tab_layout.removeOnTabSelectedListener(this@BookshelfFragment)
} }
var noGroupSize = 0
withContext(IO) { withContext(IO) {
noGroupSize = App.db.bookDao().noGroupSize
}
synchronized(this@BookshelfFragment) { synchronized(this@BookshelfFragment) {
bookGroups.clear() bookGroups.clear()
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
@ -120,16 +123,13 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(AppConst.bookGroupAudio) bookGroups.add(AppConst.bookGroupAudio)
} }
showGroupNone = if (App.db.bookDao().noGroupSize > 0 && it.isNotEmpty()) { showGroupNone = if (noGroupSize > 0 && it.isNotEmpty()) {
bookGroups.add(AppConst.bookGroupNone) bookGroups.add(AppConst.bookGroupNone)
true true
} else { } else {
false false
} }
bookGroups.addAll(it) bookGroups.addAll(it)
}
}
synchronized(this@BookshelfFragment) {
view_pager_bookshelf.adapter?.notifyDataSetChanged() view_pager_bookshelf.adapter?.notifyDataSetChanged()
tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select() tab_layout.getTabAt(getPrefInt(PreferKey.saveTabPosition, 0))?.select()
tab_layout.addOnTabSelectedListener(this@BookshelfFragment) tab_layout.addOnTabSelectedListener(this@BookshelfFragment)
@ -160,14 +160,17 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
override fun upGroup() { override fun upGroup() {
launch { launch {
var noGroupSize = 0
withContext(IO) { withContext(IO) {
noGroupSize = App.db.bookDao().noGroupSize
}
synchronized(this@BookshelfFragment) { synchronized(this@BookshelfFragment) {
bookGroups.remove(AppConst.bookGroupAll) bookGroups.remove(AppConst.bookGroupAll)
bookGroups.remove(AppConst.bookGroupLocal) bookGroups.remove(AppConst.bookGroupLocal)
bookGroups.remove(AppConst.bookGroupAudio) bookGroups.remove(AppConst.bookGroupAudio)
bookGroups.remove(AppConst.bookGroupNone) bookGroups.remove(AppConst.bookGroupNone)
showGroupNone = showGroupNone =
if (App.db.bookDao().noGroupSize > 0 && bookGroups.isNotEmpty()) { if (noGroupSize > 0 && bookGroups.isNotEmpty()) {
bookGroups.add(0, AppConst.bookGroupNone) bookGroups.add(0, AppConst.bookGroupNone)
true true
} else { } else {
@ -182,9 +185,6 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
if (AppConfig.bookGroupAllShow) { if (AppConfig.bookGroupAllShow) {
bookGroups.add(0, AppConst.bookGroupAll) bookGroups.add(0, AppConst.bookGroupAll)
} }
}
}
synchronized(this@BookshelfFragment) {
view_pager_bookshelf.adapter?.notifyDataSetChanged() view_pager_bookshelf.adapter?.notifyDataSetChanged()
} }
} }

Loading…
Cancel
Save