From 783060b8fa0dcc2e3507f35a61f0f704e7856440 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 14 Feb 2020 09:10:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/dao/BookDao.kt | 3 +++ .../app/ui/main/bookshelf/BookshelfFragment.kt | 1 + .../ui/main/bookshelf/BookshelfViewModel.kt | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) 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) + } + } + } + } + }