@@ -8,16 +8,13 @@ import com.facebook.react.bridge.WritableMap
88import com.pspdfkit.PSPDFKit
99import com.pspdfkit.analytics.AnalyticsClient
1010import com.pspdfkit.annotations.Annotation
11- import com.pspdfkit.annotations.AnnotationType
12- import com.pspdfkit.annotations.WidgetAnnotation
1311import com.pspdfkit.forms.ChoiceFormElement
1412import com.pspdfkit.forms.ComboBoxFormElement
1513import com.pspdfkit.forms.EditableButtonFormElement
1614import com.pspdfkit.forms.FormElement
1715import com.pspdfkit.forms.FormField
1816import com.pspdfkit.forms.TextFormElement
19- import com.pspdfkit.react.helper.JsonUtilities
20- import org.json.JSONObject
17+ import com.pspdfkit.react.helper.AnnotationUtils
2118
2219class CustomAnalyticsClient : AnalyticsClient {
2320 override fun onEvent (name : String , data : Bundle ? ) {
@@ -30,6 +27,7 @@ enum class NotificationEvent(val value: String) {
3027 DOCUMENT_LOAD_FAILED (" documentLoadFailed" ),
3128 DOCUMENT_PAGE_CHANGED (" documentPageChanged" ),
3229 DOCUMENT_SCROLLED (" documentScrolled" ),
30+ DOCUMENT_TAPPED (" documentTapped" ),
3331 ANNOTATIONS_ADDED (" annotationsAdded" ),
3432 ANNOTATION_CHANGED (" annotationChanged" ),
3533 ANNOTATIONS_REMOVED (" annotationsRemoved" ),
@@ -102,18 +100,27 @@ object NutrientNotificationCenter {
102100 sendEvent(NotificationEvent .DOCUMENT_SCROLLED .value, jsonData)
103101 }
104102
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+
105118 fun annotationsChanged (changeType : String , annotation : Annotation , documentID : String ) {
106119 when (changeType) {
107120 " changed" -> {
108121 try {
109- val instantJson = JSONObject (annotation.toInstantJson())
110122 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)
117124 annotationsList.add(annotationMap)
118125 val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
119126
@@ -139,18 +146,12 @@ object NutrientNotificationCenter {
139146 jsonData.putString(" event" , NotificationEvent .ANNOTATIONS_REMOVED .value)
140147 jsonData.putArray(" annotations" , nativeAnnotationsList)
141148 jsonData.putString(" documentID" , documentID)
142- sendEvent(NotificationEvent .ANNOTATION_CHANGED .value, jsonData)
149+ sendEvent(NotificationEvent .ANNOTATIONS_REMOVED .value, jsonData)
143150 }
144151 " added" -> {
145152 try {
146- val instantJson = JSONObject (annotation.toInstantJson())
147153 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)
154155 annotationsList.add(annotationMap)
155156 val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
156157
@@ -168,14 +169,8 @@ object NutrientNotificationCenter {
168169
169170 fun didSelectAnnotations (annotation : Annotation , documentID : String ) {
170171 try {
171- val instantJson = JSONObject (annotation.toInstantJson())
172172 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)
179174 annotationsList.add(annotationMap)
180175 val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
181176
@@ -191,14 +186,8 @@ object NutrientNotificationCenter {
191186
192187 fun didDeselectAnnotations (annotation : Annotation , documentID : String ) {
193188 try {
194- val instantJson = JSONObject (annotation.toInstantJson())
195189 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)
202191 annotationsList.add(annotationMap)
203192 val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
204193
@@ -214,13 +203,7 @@ object NutrientNotificationCenter {
214203
215204 fun didTapAnnotation (annotation : Annotation , pointF : PointF , documentID : String ) {
216205 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)
224207 val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
225208
226209 val pointMap = mapOf (" x" to pointF.x, " y" to pointF.y)
@@ -248,35 +231,8 @@ object NutrientNotificationCenter {
248231 fun formFieldValuesUpdated (formField : FormField , documentID : String ) {
249232 try {
250233 val annotation = formField.formElement.annotation
251- val instantJson = JSONObject (annotation.toInstantJson() )
234+ val annotationMap = AnnotationUtils .processAnnotation (annotation).toMutableMap( )
252235 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- }
280236
281237 annotationsList.add(annotationMap)
282238 val nativeAnnotationsList = Arguments .makeNativeArray(annotationsList)
@@ -294,34 +250,7 @@ object NutrientNotificationCenter {
294250 fun didSelectFormField (formElement : FormElement , documentID : String ) {
295251 try {
296252 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()
325254
326255 val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
327256 val jsonData = Arguments .createMap()
@@ -337,34 +266,7 @@ object NutrientNotificationCenter {
337266 fun didDeSelectFormField (formElement : FormElement , documentID : String ) {
338267 try {
339268 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()
368270
369271 val nativeAnnotationMap = Arguments .makeNativeMap(annotationMap)
370272 val jsonData = Arguments .createMap()
0 commit comments