diff --git a/app/src/main/java/net/bible/android/control/bookmark/BookmarkControl.kt b/app/src/main/java/net/bible/android/control/bookmark/BookmarkControl.kt index b954df0dc9..0061eb8f72 100644 --- a/app/src/main/java/net/bible/android/control/bookmark/BookmarkControl.kt +++ b/app/src/main/java/net/bible/android/control/bookmark/BookmarkControl.kt @@ -100,16 +100,20 @@ open class BookmarkControl @Inject constructor( fun allBookmarksWithNotes(orderBy: BookmarkSortOrder): List = dao.allBookmarksWithNotes(orderBy) - fun addOrUpdateBookmark(bookmark: Bookmark, labels: List?=null): Bookmark { + fun addOrUpdateBookmark(bookmark: Bookmark, labelIds: List?=null): Bookmark { if(bookmark.id != 0L) { dao.update(bookmark) } else { bookmark.id = dao.insert(bookmark) } - if(labels != null) { + if(labelIds != null) { dao.deleteLabels(bookmark.id) - dao.insert(labels.filter { it > 0 }.map { BookmarkToLabel(bookmark.id, it, orderNumber = dao.countJournalEntities(it)) }) + val labels = labelIds.filter { it > 0 }.mapNotNull { dao.labelById(it) } + for (it in labels) { + dao.makeSpace(it.id, it.editPosition) + dao.insert(BookmarkToLabel(bookmark.id, it.id, orderNumber = it.editPosition)) + } } addText(bookmark) diff --git a/app/src/main/java/net/bible/android/control/page/ClientPageObjects.kt b/app/src/main/java/net/bible/android/control/page/ClientPageObjects.kt index 0846f216c4..a5e4e654ce 100644 --- a/app/src/main/java/net/bible/android/control/page/ClientPageObjects.kt +++ b/app/src/main/java/net/bible/android/control/page/ClientPageObjects.kt @@ -210,12 +210,13 @@ class ClientBookmark(val bookmark: BookmarkEntities.Bookmark, val v11n: Versific data class ClientBookmarkStyle(val color: Int, val icon: String?, val noHighlight: Boolean) @Serializable -data class ClientBookmarkLabel(val id: Long, val name: String, val style: ClientBookmarkStyle) { +data class ClientBookmarkLabel(val id: Long, val name: String, val style: ClientBookmarkStyle, val editPosition: Int) { constructor(label: BookmarkEntities.Label): this( label.id, label.displayName, label.color.let {v -> ClientBookmarkStyle(v, if(label.isSpeakLabel) "headphones" else null, label.isSpeakLabel) - } + }, + label.editPosition, ) } diff --git a/app/src/main/java/net/bible/service/db/DatabaseContainer.kt b/app/src/main/java/net/bible/service/db/DatabaseContainer.kt index 75a388d402..5e96b6fa82 100644 --- a/app/src/main/java/net/bible/service/db/DatabaseContainer.kt +++ b/app/src/main/java/net/bible/service/db/DatabaseContainer.kt @@ -780,6 +780,15 @@ private val MIGRATION_43_44_topMargin = object : Migration(43, 44) { } } +private val MIGRATION_44_45_journal_edit_position = object : Migration(44, 45) { + override fun doMigrate(db: SupportSQLiteDatabase) { + db.apply { + execSQL("ALTER TABLE `Label` ADD COLUMN `editPosition` INTEGER NOT NULL DEFAULT 0") + execSQL("UPDATE `Label` SET editPosition=(SELECT count(*) from BookmarkToLabel WHERE labelId=id) + (SELECT count(*) from JournalTextEntry WHERE labelId=id)") + } + } +} + object DatabaseContainer { private var instance: AppDatabase? = null @@ -837,6 +846,7 @@ object DatabaseContainer { MIGRATION_41_42_cipherKey, MIGRATION_42_43_expandContent, MIGRATION_43_44_topMargin, + MIGRATION_44_45_journal_edit_position, // When adding new migrations, remember to increment DATABASE_VERSION too ) .build() diff --git a/db/src/main/java/net/bible/android/database/AppDatabase.kt b/db/src/main/java/net/bible/android/database/AppDatabase.kt index 6245be36ac..b09bedb890 100644 --- a/db/src/main/java/net/bible/android/database/AppDatabase.kt +++ b/db/src/main/java/net/bible/android/database/AppDatabase.kt @@ -44,7 +44,7 @@ import java.io.ObjectOutputStream import java.util.* -const val DATABASE_VERSION = 44 +const val DATABASE_VERSION = 45 class Converters { @TypeConverter diff --git a/db/src/main/java/net/bible/android/database/bookmarks/BookmarkEntities.kt b/db/src/main/java/net/bible/android/database/bookmarks/BookmarkEntities.kt index 3f726a14bd..bd6317a80f 100644 --- a/db/src/main/java/net/bible/android/database/bookmarks/BookmarkEntities.kt +++ b/db/src/main/java/net/bible/android/database/bookmarks/BookmarkEntities.kt @@ -248,7 +248,8 @@ class BookmarkEntities { @PrimaryKey(autoGenerate = true) var id: Long = 0, var name: String = "", @ColumnInfo(name = "bookmarkStyle") var bookmarkStyleDeprecated: BookmarkStyle? = null, - @ColumnInfo(defaultValue = "0") var color: Int = defaultLabelColor + @ColumnInfo(defaultValue = "0") var color: Int = defaultLabelColor, + @ColumnInfo(defaultValue = "0") var editPosition: Int = 0, ) { override fun toString() = name val isSpeakLabel get() = name == SPEAK_LABEL_NAME diff --git a/db/src/main/java/net/bible/android/database/bookmarks/BookmarksDao.kt b/db/src/main/java/net/bible/android/database/bookmarks/BookmarksDao.kt index c36a07ead8..9fa55961da 100644 --- a/db/src/main/java/net/bible/android/database/bookmarks/BookmarksDao.kt +++ b/db/src/main/java/net/bible/android/database/bookmarks/BookmarksDao.kt @@ -226,4 +226,15 @@ interface BookmarkDao { @Update fun updateBookmarkToLabels(bookmarkToLabels: List) @Update fun updateJournalTextEntries(studyPadTextEntries: List) + + @Query("UPDATE BookmarkToLabel SET orderNumber=orderNumber + 1 WHERE labelId=:labelId AND orderNumber >= :startPosition") + fun makeSpaceBookmarkToLabels(labelId: Long, startPosition: Int) + + @Query("UPDATE JournalTextEntry SET orderNumber=orderNumber + 1 WHERE labelId=:labelId AND orderNumber >= :startPosition") + fun makeSpaceTextEntries(labelId: Long, startPosition: Int) + + fun makeSpace(id: Long, editPosition: Int) { + makeSpaceBookmarkToLabels(id, editPosition) + makeSpaceTextEntries(id, editPosition) + } }