feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 32a0b2fbc2
commit 783060b8fa
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 1
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 18
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt

@ -64,4 +64,7 @@ interface BookDao {
@Query("update books set durChapterPos = :pos where bookUrl = :bookUrl")
fun upProgress(bookUrl: String, pos: Int)
@Query("update books set `group` = :newGroupId where `group` = :oldGroupId")
fun upGroup(oldGroupId: Int, newGroupId: Int)
}

@ -103,6 +103,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
bookGroupLiveData?.removeObservers(viewLifecycleOwner)
bookGroupLiveData = App.db.bookGroupDao().liveDataAll()
bookGroupLiveData?.observe(viewLifecycleOwner, Observer {
viewModel.checkGroup(it)
synchronized(this) {
tab_layout.removeOnTabSelectedListener(this)
bookGroups.clear()

@ -5,6 +5,7 @@ import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource
import io.legado.app.model.WebBook
import io.legado.app.utils.NetworkUtils
@ -62,4 +63,21 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
}
}
fun checkGroup(groups: List<BookGroup>) {
execute {
groups.forEach { group ->
if (group.groupId and (group.groupId - 1) != 0) {
var id = 1
val idsSum = App.db.bookGroupDao().idsSum
while (id and idsSum != 0) {
id = id.shl(1)
}
App.db.bookGroupDao().delete(group)
App.db.bookGroupDao().insert(group.copy(groupId = id))
App.db.bookDao().upGroup(group.groupId, id)
}
}
}
}
}

Loading…
Cancel
Save