feat: 优化代码

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

@ -108,28 +108,28 @@ 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) {
synchronized(this@BookshelfFragment) { noGroupSize = App.db.bookDao().noGroupSize
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 (App.db.bookDao().noGroupSize > 0 && it.isNotEmpty()) {
bookGroups.add(AppConst.bookGroupNone)
true
} else {
false
}
bookGroups.addAll(it)
}
} }
synchronized(this@BookshelfFragment) { 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() 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,31 +160,31 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
override fun upGroup() { override fun upGroup() {
launch { launch {
var noGroupSize = 0
withContext(IO) { withContext(IO) {
synchronized(this@BookshelfFragment) { noGroupSize = App.db.bookDao().noGroupSize
bookGroups.remove(AppConst.bookGroupAll) }
bookGroups.remove(AppConst.bookGroupLocal) synchronized(this@BookshelfFragment) {
bookGroups.remove(AppConst.bookGroupAudio) bookGroups.remove(AppConst.bookGroupAll)
bookGroups.remove(AppConst.bookGroupNone) bookGroups.remove(AppConst.bookGroupLocal)
showGroupNone = bookGroups.remove(AppConst.bookGroupAudio)
if (App.db.bookDao().noGroupSize > 0 && bookGroups.isNotEmpty()) { bookGroups.remove(AppConst.bookGroupNone)
showGroupNone =
if (noGroupSize > 0 && bookGroups.isNotEmpty()) {
bookGroups.add(0, AppConst.bookGroupNone) bookGroups.add(0, AppConst.bookGroupNone)
true true
} else { } else {
false false
} }
if (AppConfig.bookGroupAudioShow) { if (AppConfig.bookGroupAudioShow) {
bookGroups.add(0, AppConst.bookGroupAudio) bookGroups.add(0, AppConst.bookGroupAudio)
} }
if (AppConfig.bookGroupLocalShow) { if (AppConfig.bookGroupLocalShow) {
bookGroups.add(0, AppConst.bookGroupLocal) bookGroups.add(0, AppConst.bookGroupLocal)
} }
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