diff --git a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt index af15188bb..eafff6b0e 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt @@ -13,8 +13,8 @@ interface BookGroupDao { @Query("SELECT * FROM book_groups ORDER BY `order`") fun liveDataAll(): LiveData> - @get:Query("SELECT MAX(groupId) FROM book_groups") - val maxId: Int + @get:Query("SELECT count(groupId) FROM book_groups") + val idsCount: Int @get:Query("SELECT MAX(`order`) FROM book_groups") val maxOrder: Int diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt index 610eff778..e0fdc2b09 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt @@ -9,8 +9,13 @@ class GroupViewModel(application: Application) : BaseViewModel(application) { fun addGroup(groupName: String) { execute { + var id = 1 + val idsCount = App.db.bookGroupDao().idsCount + while (id and idsCount != 0) { + id *= 2 + } val bookGroup = BookGroup( - groupId = App.db.bookGroupDao().maxId.plus(1), + groupId = id, groupName = groupName, order = App.db.bookGroupDao().maxOrder.plus(1) )