Skip to content

Commit

Permalink
Support properties with the same name
Browse files Browse the repository at this point in the history
Resolves orgzly#507.
  • Loading branch information
nevenz committed Apr 1, 2019
1 parent d5f47e0 commit b1ab17a
Show file tree
Hide file tree
Showing 8 changed files with 1,415 additions and 51 deletions.
1,360 changes: 1,360 additions & 0 deletions app/schemas/com.orgzly.android.db.OrgzlyDatabase/152.json

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions app/src/main/java/com/orgzly/android/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ import com.orgzly.android.ui.NotePlace
import com.orgzly.android.ui.Place
import com.orgzly.android.ui.note.NoteBuilder
import com.orgzly.android.ui.note.NotePayload
import com.orgzly.android.usecase.BookDelete
import com.orgzly.android.usecase.RepoCreate
import com.orgzly.android.util.*
import com.orgzly.org.OrgFile
import com.orgzly.org.OrgFileSettings
import com.orgzly.org.OrgActiveTimestamps
import com.orgzly.org.*
import com.orgzly.org.datetime.OrgDateTime
import com.orgzly.org.datetime.OrgRange
import com.orgzly.org.parser.*
Expand Down Expand Up @@ -604,7 +601,7 @@ class DataRepository @Inject constructor(
place: Place,
targetNoteId: Long): Int {

var pastedNoteIds = mutableSetOf<Long>()
val pastedNoteIds = mutableSetOf<Long>()

val targetNote = db.note().get(targetNoteId) ?: return 0

Expand Down Expand Up @@ -666,7 +663,13 @@ class DataRepository @Inject constructor(

lastNoteId = db.note().insert(note)

insertNoteProperties(lastNoteId, entry.properties.map { Pair(it.name, it.value) }.toMap())
val properties = OrgProperties().apply {
entry.properties.forEach {
put(it.name, it.value)
}
}

insertNoteProperties(lastNoteId, properties)
insertNoteEvents(lastNoteId, note.title, note.content)

idsMap[entry.note.id] = lastNoteId
Expand Down Expand Up @@ -1122,18 +1125,19 @@ class DataRepository @Inject constructor(
db.noteProperty().upsert(noteId, name, value)
}

private fun replaceNoteProperties(noteId: Long, properties: Map<String, String>) {
private fun replaceNoteProperties(noteId: Long, properties: OrgProperties) {
db.noteProperty().delete(noteId)

insertNoteProperties(noteId, properties)
}

fun insertNoteProperties(noteId: Long, properties: Map<String, String>) {
fun insertNoteProperties(noteId: Long, properties: OrgProperties) {
var position = 1

properties.forEach { (name, value) ->
val property = NoteProperty(noteId, position++, name, value)
db.noteProperty().insert(property)
properties.all.forEach { property ->
NoteProperty(noteId, position++, property.name, property.value).let {
db.noteProperty().insert(it)
}
}
}

Expand All @@ -1157,10 +1161,15 @@ class DataRepository @Inject constructor(

val payload = if (AppPreferences.createdAt(context)) {
// Set created-at property

val propName = AppPreferences.createdAtProperty(context)
val propValue = OrgDateTime(createdAt, false).toString()
val property = Pair(propName, propValue)
notePayload.copy(properties = notePayload.properties + property)

val properties = notePayload.properties.apply {
put(propName, propValue)
}

notePayload.copy(properties = properties)

} else {
notePayload
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/com/orgzly/android/data/mappers/OrgMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ object OrgMapper {
val to = OrgProperties()

from.forEach { property ->
to[property.name] = property.value
}

return to
}

@JvmStatic
fun toOrgProperties(from: Map<String, String>): OrgProperties {
val to = OrgProperties()

from.forEach { (name, value) ->
to[name] = value
to.put(property.name, property.value)
}

return to
Expand Down Expand Up @@ -69,7 +58,7 @@ object OrgMapper {
deadline = notePayload.deadline?.let { OrgRange.parse(it) }
closed = notePayload.closed?.let { OrgRange.parse(it) }

properties = toOrgProperties(notePayload.properties)
properties = notePayload.properties

content = notePayload.content
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/orgzly/android/db/OrgzlyDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.*
VersionedRook::class
],

version = 151
version = 152
)
@TypeConverters(com.orgzly.android.db.TypeConverters::class)
abstract class OrgzlyDatabase : RoomDatabase() {
Expand Down Expand Up @@ -103,7 +103,8 @@ abstract class OrgzlyDatabase : RoomDatabase() {
PreRoomMigration.MIGRATION_148_149,

MIGRATION_149_150, // Switch to Room
MIGRATION_150_151
MIGRATION_150_151,
MIGRATION_151_152
)
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
Expand Down Expand Up @@ -451,5 +452,12 @@ abstract class OrgzlyDatabase : RoomDatabase() {
return db.insert("org_timestamps", SQLiteDatabase.CONFLICT_ROLLBACK, values)
}
}

private val MIGRATION_151_152 = object : Migration(151, 152) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("DROP INDEX index_note_properties_note_id_name")
db.execSQL("CREATE INDEX `index_note_properties_note_id` ON `note_properties` (`note_id`)")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.room.*
],

indices = [
Index("note_id", "name", unique = true),
Index("note_id"),
Index("position"),
Index("name"),
Index("value")
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/com/orgzly/android/ui/note/NoteBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.ui.NoteStates
import com.orgzly.android.util.EventsInNote
import com.orgzly.android.util.OrgFormatter
import com.orgzly.org.OrgProperties
import com.orgzly.org.datetime.OrgDateTime
import com.orgzly.org.datetime.OrgRange
import com.orgzly.org.utils.StateChangeLogic
Expand All @@ -24,7 +25,7 @@ class NoteBuilder {

var title = notePayload.title
var content = notePayload.content
val properties = notePayload.properties.toMutableMap()
val properties = notePayload.properties

val eventsInNote = EventsInNote(title, content)

Expand All @@ -48,7 +49,7 @@ class NoteBuilder {

// Add last-repeat time
if (AppPreferences.setLastRepeatOnTimeShift(context)) {
properties[OrgFormatter.LAST_REPEAT_PROPERTY] = now
properties.put(OrgFormatter.LAST_REPEAT_PROPERTY, now)
}

// Log state change
Expand Down Expand Up @@ -94,8 +95,7 @@ class NoteBuilder {
noteView.deadlineRangeString,
noteView.closedRangeString,
noteView.note.getTagsList(),
linkedMapOf(*properties.map { Pair(it.name, it.value) }.toTypedArray())
)
OrgProperties().apply { properties.forEach { put(it.name, it.value) } })
}

@JvmStatic
Expand Down Expand Up @@ -141,8 +141,6 @@ class NoteBuilder {
}
}


private val TAG = NoteBuilder::class.java.name
}

}
15 changes: 6 additions & 9 deletions app/src/main/java/com/orgzly/android/ui/note/NoteFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.orgzly.android.util.LogUtils
import com.orgzly.android.util.MiscUtils
import com.orgzly.android.util.SpaceTokenizer
import com.orgzly.android.util.UserTimeFormatter
import com.orgzly.org.OrgProperties
import com.orgzly.org.datetime.OrgDateTime
import com.orgzly.org.datetime.OrgRange
import com.orgzly.org.parser.OrgParserWriter
Expand Down Expand Up @@ -409,12 +410,8 @@ class NoteFragment : DaggerFragment(), View.OnClickListener, TimestampDialogFrag

/* Properties. */
propertiesContainer.removeAllViews()
if (!payload.properties.isEmpty()) {
val properties = OrgMapper.toOrgProperties(payload.properties)
for (name in properties.keys) {
val value = properties[name]
addPropertyToList(name, value)
}
for (property in payload.properties.all) {
addPropertyToList(property.name, property.value)
}
addPropertyToList(null, null)

Expand Down Expand Up @@ -482,7 +479,7 @@ class NoteFragment : DaggerFragment(), View.OnClickListener, TimestampDialogFrag
}

private fun updatePayloadFromViews() {
val properties = LinkedHashMap<String, String>()
val properties = OrgProperties()

for (i in 0 until propertiesContainer.childCount) {
val property = propertiesContainer.getChildAt(i)
Expand All @@ -491,7 +488,7 @@ class NoteFragment : DaggerFragment(), View.OnClickListener, TimestampDialogFrag
val value = (property.findViewById<View>(R.id.value) as TextView).text

if (!TextUtils.isEmpty(name)) { // Ignore property with no name
properties[name.toString()] = value.toString()
properties.put(name.toString(), value.toString())
}
}

Expand Down Expand Up @@ -994,7 +991,7 @@ class NoteFragment : DaggerFragment(), View.OnClickListener, TimestampDialogFrag
.show()
}

fun cancelWithConfirmation() {
private fun cancelWithConfirmation() {
if (!isAskingForConfirmationForModifiedNote()) {
cancel()
}
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/com/orgzly/android/ui/note/NotePayload.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.orgzly.android.ui.note

import android.os.Parcel
import android.os.Parcelable
import com.orgzly.org.OrgProperties

data class NotePayload @JvmOverloads constructor(
val title: String = "",
Expand All @@ -12,7 +13,7 @@ data class NotePayload @JvmOverloads constructor(
val deadline: String? = null,
val closed: String? = null,
val tags: List<String> = emptyList(),
val properties: Map<String, String> = LinkedHashMap()
val properties: OrgProperties = OrgProperties()
) : Parcelable {

override fun describeContents(): Int {
Expand All @@ -32,10 +33,12 @@ data class NotePayload @JvmOverloads constructor(

out.writeStringList(tags)

out.writeInt(properties.size)
for ((key, value) in properties) {
out.writeString(key)
out.writeString(value)
out.writeInt(properties.size())
properties.all.let {
for (property in it) {
out.writeString(property.name)
out.writeString(property.value)
}
}
}

Expand All @@ -59,11 +62,11 @@ data class NotePayload @JvmOverloads constructor(
val tags = mutableListOf<String>()
parcel.readStringList(tags)

val properties = LinkedHashMap<String, String>()
val properties = OrgProperties()
repeat(parcel.readInt()) {
val name = parcel.readString()
val value = parcel.readString()
properties[name!!] = value!!
properties.put(name!!, value!!)
}

return NotePayload(
Expand Down

0 comments on commit b1ab17a

Please sign in to comment.