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 7334f26ad..b0c07339c 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 @@ -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) } \ No newline at end of file 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 f5a6d4b2c..66321596d 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 @@ -103,6 +103,7 @@ class BookshelfFragment : VMBaseFragment(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() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index d0ab47c0d..87d2b3e06 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -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) { + 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) + } + } + } + } + }