-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathCommonDao.kt
More file actions
120 lines (110 loc) · 4.72 KB
/
CommonDao.kt
File metadata and controls
120 lines (110 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.philkes.notallyx.data.dao
import android.content.ContextWrapper
import androidx.room.Dao
import androidx.room.Transaction
import com.philkes.notallyx.data.NotallyDatabase
import com.philkes.notallyx.data.dao.BaseNoteDao.Companion.MAX_BODY_CHAR_LENGTH
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.SpanRepresentation
import com.philkes.notallyx.data.model.Type
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
import com.philkes.notallyx.utils.NoteSplitUtils
@Dao
abstract class CommonDao(private val database: NotallyDatabase) {
@Transaction
open suspend fun deleteLabel(value: String) {
val labelsInBaseNotes =
database.getBaseNoteDao().getListOfBaseNotesByLabel(value).map { baseNote ->
val labels = ArrayList(baseNote.labels)
labels.remove(value)
LabelsInBaseNote(baseNote.id, labels)
}
database.getBaseNoteDao().update(labelsInBaseNotes)
database.getLabelDao().delete(value)
}
@Transaction
open suspend fun updateLabel(oldValue: String, newValue: String) {
val labelsInBaseNotes =
database.getBaseNoteDao().getListOfBaseNotesByLabel(oldValue).map { baseNote ->
val labels = ArrayList(baseNote.labels)
labels.remove(oldValue)
labels.add(newValue)
LabelsInBaseNote(baseNote.id, labels)
}
database.getBaseNoteDao().update(labelsInBaseNotes)
database.getLabelDao().update(oldValue, newValue)
}
@Transaction
open suspend fun importBackup(
context: ContextWrapper,
baseNotes: List<BaseNote>,
labels: List<Label>,
) {
val dao = database.getBaseNoteDao()
// Insert notes, splitting oversized text notes instead of truncating
baseNotes.forEach { note ->
if (note.type == Type.NOTE && note.body.length > MAX_BODY_CHAR_LENGTH) {
NoteSplitUtils.splitAndInsertForImport(note, dao)
} else {
dao.insert(note.copy(id = 0))
}
}
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()
// 1) Insert notes with splitting; build mapping from original id -> first-part new id
val idMap = HashMap<Long, Long>(originalIds.size)
// Keep all inserted note ids with their spans for remapping pass
val insertedParts = ArrayList<Pair<Long, List<SpanRepresentation>>>()
for (i in baseNotes.indices) {
val original = baseNotes[i]
val (firstId, parts) =
if (original.type == Type.NOTE && original.body.length > MAX_BODY_CHAR_LENGTH) {
NoteSplitUtils.splitAndInsertForImport(original, baseNoteDao)
} else {
val newId = baseNoteDao.insert(original.copy(id = 0))
Pair(newId, listOf(Pair(newId, original.spans)))
}
val oldId = originalIds.getOrNull(i)
if (oldId != null) idMap[oldId] = firstId
insertedParts.addAll(parts)
}
// 2) Remap note links in spans for all inserted notes
for ((noteId, spans) in insertedParts) {
var changed = false
val updated =
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(noteId, updated)
}
}
database.getLabelDao().insert(labels)
}
}