@@ -8,16 +8,13 @@ import com.facebook.react.bridge.WritableMap
8
8
import com.pspdfkit.PSPDFKit
9
9
import com.pspdfkit.analytics.AnalyticsClient
10
10
import com.pspdfkit.annotations.Annotation
11
- import com.pspdfkit.annotations.AnnotationType
12
- import com.pspdfkit.annotations.WidgetAnnotation
13
11
import com.pspdfkit.forms.ChoiceFormElement
14
12
import com.pspdfkit.forms.ComboBoxFormElement
15
13
import com.pspdfkit.forms.EditableButtonFormElement
16
14
import com.pspdfkit.forms.FormElement
17
15
import com.pspdfkit.forms.FormField
18
16
import com.pspdfkit.forms.TextFormElement
19
- import com.pspdfkit.react.helper.JsonUtilities
20
- import org.json.JSONObject
17
+ import com.pspdfkit.react.helper.AnnotationUtils
21
18
22
19
class CustomAnalyticsClient : AnalyticsClient {
23
20
override fun onEvent (name : String , data : Bundle ? ) {
@@ -30,6 +27,7 @@ enum class NotificationEvent(val value: String) {
30
27
DOCUMENT_LOAD_FAILED (" documentLoadFailed" ),
31
28
DOCUMENT_PAGE_CHANGED (" documentPageChanged" ),
32
29
DOCUMENT_SCROLLED (" documentScrolled" ),
30
+ DOCUMENT_TAPPED (" documentTapped" ),
33
31
ANNOTATIONS_ADDED (" annotationsAdded" ),
34
32
ANNOTATION_CHANGED (" annotationChanged" ),
35
33
ANNOTATIONS_REMOVED (" annotationsRemoved" ),
@@ -102,18 +100,27 @@ object NutrientNotificationCenter {
102
100
sendEvent(NotificationEvent .DOCUMENT_SCROLLED .value, jsonData)
103
101
}
104
102
103
+ fun didTapDocument (pointF : PointF , documentID : String ) {
104
+ try {
105
+ val pointMap = mapOf (" x" to pointF.x, " y" to pointF.y)
106
+ val nativePointMap = Arguments .makeNativeMap(pointMap)
107
+
108
+ val jsonData = Arguments .createMap()
109
+ jsonData.putString(" event" , NotificationEvent .DOCUMENT_TAPPED .value)
110
+ jsonData.putMap(" point" , nativePointMap)
111
+ jsonData.putString(" documentID" , documentID)
112
+ sendEvent(NotificationEvent .DOCUMENT_TAPPED .value, jsonData)
113
+ } catch (e: Exception ) {
114
+ // Could not decode point data
115
+ }
116
+ }
117
+
105
118
fun annotationsChanged (changeType : String , annotation : Annotation , documentID : String ) {
106
119
when (changeType) {
107
120
" changed" -> {
108
121
try {
109
- val instantJson = JSONObject (annotation.toInstantJson())
110
122
val annotationsList = mutableListOf<Map <String , Any >>()
111
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
112
- annotationMap[" uuid" ] = annotation.uuid
113
- if (annotation.type == AnnotationType .WIDGET ) {
114
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
115
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
116
- }
123
+ val annotationMap = AnnotationUtils .processAnnotation(annotation)
117
124
annotationsList.add(annotationMap)
118
125
val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
119
126
@@ -139,18 +146,12 @@ object NutrientNotificationCenter {
139
146
jsonData.putString(" event" , NotificationEvent .ANNOTATIONS_REMOVED .value)
140
147
jsonData.putArray(" annotations" , nativeAnnotationsList)
141
148
jsonData.putString(" documentID" , documentID)
142
- sendEvent(NotificationEvent .ANNOTATION_CHANGED .value, jsonData)
149
+ sendEvent(NotificationEvent .ANNOTATIONS_REMOVED .value, jsonData)
143
150
}
144
151
" added" -> {
145
152
try {
146
- val instantJson = JSONObject (annotation.toInstantJson())
147
153
val annotationsList = mutableListOf<Map <String , Any >>()
148
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
149
- annotationMap[" uuid" ] = annotation.uuid
150
- if (annotation.type == AnnotationType .WIDGET ) {
151
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
152
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
153
- }
154
+ val annotationMap = AnnotationUtils .processAnnotation(annotation)
154
155
annotationsList.add(annotationMap)
155
156
val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
156
157
@@ -168,14 +169,8 @@ object NutrientNotificationCenter {
168
169
169
170
fun didSelectAnnotations (annotation : Annotation , documentID : String ) {
170
171
try {
171
- val instantJson = JSONObject (annotation.toInstantJson())
172
172
val annotationsList = mutableListOf<Map <String , Any >>()
173
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
174
- annotationMap[" uuid" ] = annotation.uuid
175
- if (annotation.type == AnnotationType .WIDGET ) {
176
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
177
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
178
- }
173
+ val annotationMap = AnnotationUtils .processAnnotation(annotation)
179
174
annotationsList.add(annotationMap)
180
175
val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
181
176
@@ -191,14 +186,8 @@ object NutrientNotificationCenter {
191
186
192
187
fun didDeselectAnnotations (annotation : Annotation , documentID : String ) {
193
188
try {
194
- val instantJson = JSONObject (annotation.toInstantJson())
195
189
val annotationsList = mutableListOf<Map <String , Any >>()
196
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
197
- annotationMap[" uuid" ] = annotation.uuid
198
- if (annotation.type == AnnotationType .WIDGET ) {
199
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
200
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
201
- }
190
+ val annotationMap = AnnotationUtils .processAnnotation(annotation)
202
191
annotationsList.add(annotationMap)
203
192
val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
204
193
@@ -214,13 +203,7 @@ object NutrientNotificationCenter {
214
203
215
204
fun didTapAnnotation (annotation : Annotation , pointF : PointF , documentID : String ) {
216
205
try {
217
- val instantJson = JSONObject (annotation.toInstantJson())
218
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
219
- annotationMap[" uuid" ] = annotation.uuid
220
- if (annotation.type == AnnotationType .WIDGET ) {
221
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
222
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
223
- }
206
+ val annotationMap = AnnotationUtils .processAnnotation(annotation)
224
207
val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
225
208
226
209
val pointMap = mapOf (" x" to pointF.x, " y" to pointF.y)
@@ -248,35 +231,8 @@ object NutrientNotificationCenter {
248
231
fun formFieldValuesUpdated (formField : FormField , documentID : String ) {
249
232
try {
250
233
val annotation = formField.formElement.annotation
251
- val instantJson = JSONObject (annotation.toInstantJson() )
234
+ val annotationMap = AnnotationUtils .processAnnotation (annotation).toMutableMap( )
252
235
val annotationsList = mutableListOf<Map <String , Any >>()
253
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
254
- annotationMap[" uuid" ] = annotation.uuid
255
- if (annotation.type == AnnotationType .WIDGET ) {
256
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
257
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
258
- }
259
-
260
- (formField.formElement as ? TextFormElement ).let { textFormElement ->
261
- if (textFormElement != null ) {
262
- annotationMap[" value" ] = textFormElement.text
263
- }
264
- }
265
- (formField.formElement as ? EditableButtonFormElement ).let { buttonFormElement ->
266
- if (buttonFormElement != null ) {
267
- annotationMap[" value" ] = if (buttonFormElement.isSelected) " selected" else " deselected"
268
- }
269
- }
270
- (formField.formElement as ? ComboBoxFormElement ).let { comboBoxFormElement ->
271
- if (comboBoxFormElement != null ) {
272
- annotationMap[" value" ] = if (comboBoxFormElement.isCustomTextSet) comboBoxFormElement.customText else comboBoxFormElement.selectedIndexes
273
- }
274
- }
275
- (formField.formElement as ? ChoiceFormElement ).let { choiceFormElement ->
276
- if (choiceFormElement != null ) {
277
- annotationMap[" value" ] = choiceFormElement.selectedIndexes
278
- }
279
- }
280
236
281
237
annotationsList.add(annotationMap)
282
238
val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
@@ -294,34 +250,7 @@ object NutrientNotificationCenter {
294
250
fun didSelectFormField (formElement : FormElement , documentID : String ) {
295
251
try {
296
252
val annotation = formElement.annotation
297
- val instantJson = JSONObject (annotation.toInstantJson())
298
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
299
- annotationMap[" uuid" ] = annotation.uuid
300
- if (annotation.type == AnnotationType .WIDGET ) {
301
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
302
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
303
- }
304
-
305
- (formElement as ? TextFormElement ).let { textFormElement ->
306
- if (textFormElement != null ) {
307
- annotationMap[" value" ] = textFormElement.text
308
- }
309
- }
310
- (formElement as ? EditableButtonFormElement ).let { buttonFormElement ->
311
- if (buttonFormElement != null ) {
312
- annotationMap[" value" ] = if (buttonFormElement.isSelected) " selected" else " deselected"
313
- }
314
- }
315
- (formElement as ? ComboBoxFormElement ).let { comboBoxFormElement ->
316
- if (comboBoxFormElement != null ) {
317
- annotationMap[" value" ] = if (comboBoxFormElement.isCustomTextSet) comboBoxFormElement.customText else comboBoxFormElement.selectedIndexes
318
- }
319
- }
320
- (formElement as ? ChoiceFormElement ).let { choiceFormElement ->
321
- if (choiceFormElement != null ) {
322
- annotationMap[" value" ] = choiceFormElement.selectedIndexes
323
- }
324
- }
253
+ val annotationMap = AnnotationUtils .processAnnotation(annotation).toMutableMap()
325
254
326
255
val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
327
256
val jsonData = Arguments .createMap()
@@ -337,34 +266,7 @@ object NutrientNotificationCenter {
337
266
fun didDeSelectFormField (formElement : FormElement , documentID : String ) {
338
267
try {
339
268
val annotation = formElement.annotation
340
- val instantJson = JSONObject (annotation.toInstantJson())
341
- val annotationMap = JsonUtilities .jsonObjectToMap(instantJson)
342
- annotationMap[" uuid" ] = annotation.uuid
343
- if (annotation.type == AnnotationType .WIDGET ) {
344
- val widgetAnnotation : WidgetAnnotation = annotation as WidgetAnnotation
345
- annotationMap[" isRequired" ] = widgetAnnotation.formElement?.isRequired
346
- }
347
-
348
- (formElement as ? TextFormElement ).let { textFormElement ->
349
- if (textFormElement != null ) {
350
- annotationMap[" value" ] = textFormElement.text
351
- }
352
- }
353
- (formElement as ? EditableButtonFormElement ).let { buttonFormElement ->
354
- if (buttonFormElement != null ) {
355
- annotationMap[" value" ] = if (buttonFormElement.isSelected) " selected" else " deselected"
356
- }
357
- }
358
- (formElement as ? ComboBoxFormElement ).let { comboBoxFormElement ->
359
- if (comboBoxFormElement != null ) {
360
- annotationMap[" value" ] = if (comboBoxFormElement.isCustomTextSet) comboBoxFormElement.customText else comboBoxFormElement.selectedIndexes
361
- }
362
- }
363
- (formElement as ? ChoiceFormElement ).let { choiceFormElement ->
364
- if (choiceFormElement != null ) {
365
- annotationMap[" value" ] = choiceFormElement.selectedIndexes
366
- }
367
- }
269
+ val annotationMap = AnnotationUtils .processAnnotation(annotation).toMutableMap()
368
270
369
271
val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
370
272
val jsonData = Arguments .createMap()
0 commit comments