Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/edit position #960

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,20 @@ open class BookmarkControl @Inject constructor(

fun allBookmarksWithNotes(orderBy: BookmarkSortOrder): List<Bookmark> = dao.allBookmarksWithNotes(orderBy)

fun addOrUpdateBookmark(bookmark: Bookmark, labels: List<Long>?=null): Bookmark {
fun addOrUpdateBookmark(bookmark: Bookmark, labelIds: List<Long>?=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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

10 changes: 10 additions & 0 deletions app/src/main/java/net/bible/service/db/DatabaseContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion db/src/main/java/net/bible/android/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import java.io.ObjectOutputStream

import java.util.*

const val DATABASE_VERSION = 44
const val DATABASE_VERSION = 45

class Converters {
@TypeConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ interface BookmarkDao {

@Update fun updateBookmarkToLabels(bookmarkToLabels: List<BookmarkToLabel>)
@Update fun updateJournalTextEntries(studyPadTextEntries: List<BookmarkEntities.StudyPadTextEntry>)

@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)
}
}