Skip to content
Merged
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 @@ -34,7 +34,7 @@ interface BaseNoteDao {

@Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insert(baseNote: BaseNote): Long

@Insert suspend fun insert(baseNotes: List<BaseNote>)
@Insert suspend fun insert(baseNotes: List<BaseNote>): List<Long>

@Update(entity = BaseNote::class) suspend fun update(labelsInBaseNotes: List<LabelsInBaseNote>)

Expand Down Expand Up @@ -129,6 +129,12 @@ interface BaseNoteDao {
@Query("UPDATE BaseNote SET reminders = :reminders WHERE id = :id")
suspend fun updateReminders(id: Long, reminders: List<Reminder>)

@Query("UPDATE BaseNote SET spans = :spans WHERE id = :id")
suspend fun updateSpans(
id: Long,
spans: List<com.philkes.notallyx.data.model.SpanRepresentation>,
)

/**
* Both id and position can be invalid.
*
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/com/philkes/notallyx/data/dao/CommonDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import com.philkes.notallyx.data.NotallyDatabase
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.Label
import com.philkes.notallyx.data.model.LabelsInBaseNote
import com.philkes.notallyx.data.model.createNoteUrl
import com.philkes.notallyx.data.model.getNoteIdFromUrl
import com.philkes.notallyx.data.model.getNoteTypeFromUrl
import com.philkes.notallyx.data.model.isNoteUrl

@Dao
abstract class CommonDao(private val database: NotallyDatabase) {
Expand Down Expand Up @@ -40,4 +44,52 @@ abstract class CommonDao(private val database: NotallyDatabase) {
database.getBaseNoteDao().insert(baseNotes)
database.getLabelDao().insert(labels)
}

/**
* Import backup with remapping of note links inside spans. Uses a single bulk insert to obtain
* new IDs, builds an oldId->newId mapping based on [originalIds] order, then rewrites any
* note:// links in spans to reference the newly created IDs.
*/
@Transaction
open suspend fun importBackup(
baseNotes: List<BaseNote>,
originalIds: List<Long>,
labels: List<Label>,
) {
val baseNoteDao = database.getBaseNoteDao()
val newIds = baseNoteDao.insert(baseNotes)
// Build old->new mapping using positional correspondence
val idMap = HashMap<Long, Long>(originalIds.size)
val count = minOf(originalIds.size, newIds.size)
for (i in 0 until count) {
idMap[originalIds[i]] = newIds[i]
}

// Remap note links in spans where necessary
for (i in baseNotes.indices) {
val note = baseNotes[i]
val newId = newIds.getOrNull(i) ?: continue
var changed = false
val updatedSpans =
note.spans.map { span ->
if (span.link && span.linkData?.isNoteUrl() == true) {
val url = span.linkData!!
val oldTargetId = url.getNoteIdFromUrl()
val type = url.getNoteTypeFromUrl()
val newTargetId = idMap[oldTargetId]
if (newTargetId != null) {
changed = true
span.copy(linkData = newTargetId.createNoteUrl(type))
} else {
span
}
} else span
}
if (changed) {
baseNoteDao.updateSpans(newId, updatedSpans)
}
}

database.getLabelDao().insert(labels)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ suspend fun ContextWrapper.importZip(
var total = baseNoteCursor.count
var counter = 1
importingBackup?.postValue(ImportProgress(0, total))
val originalIds = ArrayList<Long>(baseNoteCursor.count)
val baseNotes =
baseNoteCursor.toList { cursor ->
val baseNote = cursor.toBaseNote()
val originalId = cursor.getLong(cursor.getColumnIndexOrThrow("id"))
originalIds.add(originalId)
importingBackup?.postValue(ImportProgress(counter++, total))
baseNote
}
Expand Down Expand Up @@ -192,7 +195,7 @@ suspend fun ContextWrapper.importZip(

val notallyDatabase =
NotallyDatabase.getDatabase(this@importZip, observePreferences = false).value
notallyDatabase.getCommonDao().importBackup(baseNotes, labels)
notallyDatabase.getCommonDao().importBackup(baseNotes, originalIds, labels)
val reminders = notallyDatabase.getBaseNoteDao().getAllReminders()
cancelNoteReminders(reminders)
scheduleNoteReminders(reminders)
Expand Down
Binary file modified app/translations.xlsx
Binary file not shown.