-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remaining "essential" external actions
- Loading branch information
1 parent
1314430
commit 8917bfc
Showing
9 changed files
with
215 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 45 additions & 67 deletions
112
app/src/main/java/com/orgzly/android/external/actionhandlers/EditNotes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,63 @@ | ||
package com.orgzly.android.external.actionhandlers | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParseException | ||
import com.google.gson.JsonParser | ||
import com.orgzly.android.external.types.Response | ||
import com.orgzly.android.ui.NotePlace | ||
import com.orgzly.android.ui.Place | ||
import com.orgzly.android.ui.note.NotePayload | ||
import com.orgzly.org.OrgProperties | ||
|
||
class EditNotes : ExternalAccessActionHandler() { | ||
override val actions = listOf( | ||
action(::addNote, "ADD_NOTE", "ADD_NOTES"), | ||
action(::deleteNote, "DELETE_NOTE", "DELETE_NOTES") | ||
action(::addNote, "ADD_NOTE", "ADD_NOTES"), | ||
action(::editNote, "EDIT_NOTE"), | ||
action(::refileNote, "REFILE_NOTE", "REFILE_NOTES"), | ||
action(::moveNote, "MOVE_NOTE", "MOVE_NOTES"), | ||
action(::deleteNote, "DELETE_NOTE", "DELETE_NOTES") | ||
) | ||
|
||
fun addNote(intent: Intent): Response { | ||
val book = getBook(intent) ?: return Response(false, "Couldn't find specified book") | ||
val newNote = notePayloadFromJson(intent.getStringExtra("PAYLOAD") ?: "") | ||
private fun addNote(intent: Intent): Response { | ||
val place = intent.getNotePlace() | ||
?: return Response(false, "Could not find parent note") | ||
val newNote = intent.getNotePayload() | ||
?: return Response(false, "Invalid payload") | ||
val path = intent.getStringExtra("PATH") ?: "" | ||
|
||
val place = if (path.split("/").any { it.isNotEmpty() }) { | ||
dataRepository.getNoteAtPath(book.name, path)?.let { | ||
NotePlace(book.id, it.note.id, Place.UNDER) | ||
} | ||
} else null | ||
place ?: return Response(false, "Couldn't find parent note at path") | ||
dataRepository.createNote(newNote, place) | ||
return Response(true, null) | ||
val note = dataRepository.createNote(newNote, place) | ||
return Response(true, "${note.id}") | ||
} | ||
|
||
// <editor-fold desc="Helpers"> | ||
|
||
private fun JsonObject.getString(name: String) = this[name]?.let { | ||
if (it.isJsonPrimitive && it.asJsonPrimitive.isString) | ||
it.asJsonPrimitive.asString | ||
else null | ||
private fun editNote(intent: Intent): Response { | ||
val noteView = intent.getNote() | ||
?: return Response(false, "Couldn't find note") | ||
val newNote = intent.getNotePayload(title=noteView.note.title) | ||
?: return Response(false, "Invalid payload") | ||
dataRepository.updateNote(noteView.note.id, newNote) | ||
return Response() | ||
} | ||
|
||
private val JsonElement.asMap: Map<String, String>? | ||
get() = if (this.isJsonObject) { | ||
this.asJsonObject | ||
.entrySet() | ||
.map { | ||
if (it.value.isJsonPrimitive) | ||
it.key to it.value.asJsonPrimitive.asString | ||
else return null | ||
} | ||
.toMap() | ||
} else null | ||
private fun refileNote(intent: Intent): Response { | ||
val notes = intent.getNoteIds() | ||
if (notes.isEmpty()) | ||
return Response(false, "No notes specified") | ||
val place = intent.getNotePlace() | ||
?: return Response(false, "Couldn't find note") | ||
dataRepository.refileNotes(notes, place) | ||
return Response() | ||
} | ||
|
||
private fun notePayloadFromJson(rawJson: String): NotePayload? { | ||
val json = try { | ||
JsonParser.parseString(rawJson) | ||
.let { if (it.isJsonObject) it.asJsonObject else null } | ||
} catch (e: JsonParseException) { | ||
null | ||
} | ||
return try { | ||
json!! | ||
NotePayload( | ||
json.getString("title")!!, | ||
json.getString("content"), | ||
json.getString("state"), | ||
json.getString("priority"), | ||
json.getString("scheduled"), | ||
json.getString("deadline"), | ||
json.getString("closed"), | ||
(json.getString("tags") ?: "") | ||
.split(" +".toRegex()) | ||
.filter { it.isNotEmpty() }, | ||
OrgProperties().apply { | ||
json["properties"]?.asMap?.forEach { (k, v) -> this[k] = v } | ||
} | ||
) | ||
} catch (e: NullPointerException) { null } | ||
private fun moveNote(intent: Intent): Response { | ||
val notes = intent.getNoteIds() | ||
if (notes.isEmpty()) return Response(false, "No notes specified") | ||
with(dataRepository) { when (intent.getStringExtra("DIRECTION")) { | ||
"UP" -> intent.getBook()?.id?.let { moveNote(it, notes, -1) } | ||
"DOWN" -> intent.getBook()?.id?.let { moveNote(it, notes, 1) } | ||
"LEFT" -> promoteNotes(notes) | ||
"RIGHT" -> demoteNotes(notes) | ||
else -> return Response(false, "Invalid direction") | ||
} } | ||
return Response() | ||
} | ||
|
||
// </editor-fold> | ||
private fun deleteNote(intent: Intent): Response { | ||
val book = intent.getBook() ?: return Response(false, "Couldn't find specified book") | ||
val notes = intent.getNoteIds() | ||
if (notes.isEmpty()) return Response(false, "No notes specified") | ||
dataRepository.deleteNotes(book.id, notes) | ||
return Response() | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/orgzly/android/external/actionhandlers/EditSavedSearches.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.orgzly.android.external.actionhandlers | ||
|
||
import android.content.Intent | ||
import com.orgzly.android.db.entity.SavedSearch | ||
import com.orgzly.android.external.types.Response | ||
|
||
class EditSavedSearches : ExternalAccessActionHandler() { | ||
override val actions = listOf( | ||
action(::addSavedSearch, "ADD_SAVED_SEARCH"), | ||
action(::editSavedSearch, "EDIT_SAVED_SEARCH"), | ||
action(::moveSavedSearch, "MOVE_SAVED_SEARCH"), | ||
action(::deleteSavedSearch, "DELETE_SAVED_SEARCH"), | ||
) | ||
|
||
private fun addSavedSearch(intent: Intent) = | ||
intent.getNewSavedSearch()?.let { | ||
val id = dataRepository.createSavedSearch(it) | ||
Response(true, "$id") | ||
} ?: Response(false, "Invalid saved search details") | ||
|
||
private fun editSavedSearch(intent: Intent) = intent.getSavedSearch()?.let { savedSearch -> | ||
intent.getNewSavedSearch(allowBlank = true)?.let { newSavedSearch -> | ||
dataRepository.updateSavedSearch(SavedSearch( | ||
savedSearch.id, | ||
(if (newSavedSearch.name.isBlank()) savedSearch.name | ||
else newSavedSearch.name), | ||
(if (newSavedSearch.query.isBlank()) savedSearch.query | ||
else newSavedSearch.query), | ||
savedSearch.position | ||
)) | ||
return Response() | ||
} ?: Response(false, "Invalid saved search details") | ||
} ?: Response(false, "Couldn't find saved search") | ||
|
||
private fun moveSavedSearch(intent: Intent) = intent.getSavedSearch()?.let { savedSearch -> | ||
when (intent.getStringExtra("DIRECTION")) { | ||
"UP" -> dataRepository.moveSavedSearchUp(savedSearch.id) | ||
"DOWN" -> dataRepository.moveSavedSearchDown(savedSearch.id) | ||
else -> return Response(false, "Invalid direction") | ||
} | ||
return Response() | ||
} ?: Response(false, "Couldn't find saved search") | ||
|
||
private fun deleteSavedSearch(intent: Intent) = intent.getSavedSearch()?.let { savedSearch -> | ||
dataRepository.deleteSavedSearches(setOf(savedSearch.id)) | ||
return Response() | ||
} ?: Response(false, "Couldn't find saved search") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.