@@ -9,6 +9,7 @@ import com.google.gson.JsonParser
9
9
import com.orgzly.android.App
10
10
import com.orgzly.android.data.DataRepository
11
11
import com.orgzly.android.db.entity.SavedSearch
12
+ import com.orgzly.android.external.types.ExternalHandlerFailure
12
13
import com.orgzly.android.external.types.Response
13
14
import com.orgzly.android.ui.NotePlace
14
15
import com.orgzly.android.ui.Place
@@ -25,12 +26,12 @@ abstract class ExternalAccessActionHandler {
25
26
App .appComponent.inject(this )
26
27
}
27
28
28
- abstract val actions: List <List <Pair <String , (Intent , Context ) - > Response >>>
29
+ abstract val actions: List <List <Pair <String , (Intent , Context ) - > Any >>>
29
30
private val fullNameActions by lazy {
30
31
actions.flatten().toMap().mapKeys { (key, _) -> " com.orgzly.android.$key " }
31
32
}
32
33
33
- fun Intent.getNotePayload (title : String? = null): NotePayload ? {
34
+ fun Intent.getNotePayload (title : String? = null): NotePayload {
34
35
val rawJson = getStringExtra(" NOTE_PAYLOAD" )
35
36
val json = try {
36
37
JsonParser .parseString(rawJson)
@@ -56,19 +57,27 @@ abstract class ExternalAccessActionHandler {
56
57
json[" properties" ]?.asMap?.forEach { (k, v) -> this [k] = v }
57
58
}
58
59
)
59
- } catch (e: NullPointerException ) { null }
60
+ } catch (e: NullPointerException ) {
61
+ throw ExternalHandlerFailure (" invalid payload" )
62
+ }
60
63
}
61
64
62
- fun Intent.getNotePlace () = getNote(prefix= " PARENT_" )?.let { noteView ->
63
- val place = try {
64
- Place .valueOf(getStringExtra(" PLACEMENT" ) ? : " " )
65
- } catch (e: IllegalArgumentException ) { Place .UNDER }
66
- dataRepository.getBook(noteView.bookName)?.let { book ->
67
- NotePlace (book.id, noteView.note.id, place)
65
+ fun Intent.getNotePlace () = try {
66
+ getNote(prefix= " PARENT_" ).let { noteView ->
67
+ val place = try {
68
+ Place .valueOf(getStringExtra(" PLACEMENT" ) ? : " " )
69
+ } catch (e: IllegalArgumentException ) { Place .UNDER }
70
+ dataRepository.getBook(noteView.bookName)?.let { book ->
71
+ NotePlace (book.id, noteView.note.id, place)
72
+ }
68
73
}
69
- } ? : getBook(prefix= " PARENT" )?.let { book -> NotePlace (book.id) }
74
+ } catch (e: ExternalHandlerFailure ) { null } ? : try {
75
+ NotePlace (getBook(prefix= " PARENT_" ).id)
76
+ } catch (e: ExternalHandlerFailure ) {
77
+ throw ExternalHandlerFailure (" could not find parent note/book" )
78
+ }
70
79
71
- fun Intent.getNoteIds (allowSingle : Boolean = true): Set <Long > {
80
+ fun Intent.getNoteIds (allowSingle : Boolean = true, allowEmpty : Boolean = false ): Set <Long > {
72
81
val id = if (allowSingle) getLongExtra(" NOTE_ID" , - 1 ) else null
73
82
val ids = getLongArrayExtra(" NOTE_IDS" )?.toTypedArray() ? : emptyArray()
74
83
val path =
@@ -79,26 +88,33 @@ abstract class ExternalAccessActionHandler {
79
88
val paths = (getStringArrayExtra(" NOTE_PATHS" ) ? : emptyArray())
80
89
.mapNotNull { dataRepository.getNoteAtPath(it)?.note?.id }
81
90
.toTypedArray()
82
- return listOfNotNull(id, * ids, path, * paths).filter { it >= 0 }.toSet()
91
+ return listOfNotNull(id, * ids, path, * paths).filter { it >= 0 }.toSet().also {
92
+ if (it.isEmpty() && ! allowEmpty)
93
+ throw ExternalHandlerFailure (" no notes specified" )
94
+ }
83
95
}
84
96
85
97
fun Intent.getNote (prefix : String = "") =
86
98
dataRepository.getNoteView(getLongExtra(" ${prefix} NOTE_ID" , - 1 ))
87
99
? : dataRepository.getNoteAtPath(getStringExtra(" ${prefix} NOTE_PATH" ) ? : " " )
100
+ ? : throw ExternalHandlerFailure (" couldn't find note" )
88
101
89
102
fun Intent.getBook (prefix : String = "") =
90
103
dataRepository.getBook(getLongExtra(" ${prefix} BOOK_ID" , - 1 ))
91
104
? : dataRepository.getBook(getStringExtra(" ${prefix} BOOK_NAME" ) ? : " " )
105
+ ? : throw ExternalHandlerFailure (" couldn't find book" )
92
106
93
107
fun Intent.getSavedSearch () =
94
108
dataRepository.getSavedSearch(getLongExtra(" SAVED_SEARCH_ID" , - 1 ))
95
109
? : dataRepository.getSavedSearches()
96
110
.find { it.name == getStringExtra(" SAVED_SEARCH_NAME" ) }
111
+ ? : throw ExternalHandlerFailure (" couldn't find saved search" )
97
112
98
- fun Intent.getNewSavedSearch (allowBlank : Boolean = false): SavedSearch ? {
113
+ fun Intent.getNewSavedSearch (allowBlank : Boolean = false): SavedSearch {
99
114
val name = getStringExtra(" SAVED_SEARCH_NEW_NAME" )
100
115
val query = getStringExtra(" SAVED_SEARCH_NEW_QUERY" )
101
- if (! allowBlank && (name.isNullOrBlank() || query.isNullOrBlank())) return null
116
+ if (! allowBlank && (name.isNullOrBlank() || query.isNullOrBlank()))
117
+ throw ExternalHandlerFailure (" invalid parameters for new saved search" )
102
118
return SavedSearch (0 , name ? : " " , query ? : " " , 0 )
103
119
}
104
120
@@ -120,20 +136,25 @@ abstract class ExternalAccessActionHandler {
120
136
.toMap()
121
137
} else null
122
138
123
- fun action (f : (Intent , Context ) -> Response , vararg names : String ) = names.map { it to f }
139
+ fun action (f : (Intent , Context ) -> Any , vararg names : String ) = names.map { it to f }
124
140
125
141
@JvmName(" intentAction" )
126
- fun action (f : (Intent ) -> Response , vararg names : String ) =
142
+ fun action (f : (Intent ) -> Any , vararg names : String ) =
127
143
action({ i, _ -> f(i) }, * names)
128
144
129
145
@JvmName(" contextAction" )
130
- fun action (f : (Context ) -> Response , vararg names : String ) =
146
+ fun action (f : (Context ) -> Any , vararg names : String ) =
131
147
action({ _, c -> f(c) }, * names)
132
148
133
- fun action (f : () -> Response , vararg names : String ) =
149
+ fun action (f : () -> Any , vararg names : String ) =
134
150
action({ _, _ -> f() }, * names)
135
151
136
152
137
- fun handle (intent : Intent , context : Context ) =
138
- fullNameActions[intent.action!! ]?.let { it(intent, context) }
153
+ fun handle (intent : Intent , context : Context ) = try {
154
+ fullNameActions[intent.action!! ]
155
+ ?.let { it(intent, context) }
156
+ ?.let { Response (true , if (it is Unit ) null else it) }
157
+ } catch (e: ExternalHandlerFailure ) {
158
+ Response (false , e.message)
159
+ }
139
160
}
0 commit comments