@@ -17,6 +17,7 @@ package com.pspdfkit.flutter.pspdfkit
17
17
// /
18
18
import android.content.Context
19
19
import android.graphics.PointF
20
+ import android.util.Log
20
21
import android.view.MotionEvent
21
22
import androidx.fragment.app.Fragment
22
23
import androidx.fragment.app.FragmentManager
@@ -31,6 +32,8 @@ import com.pspdfkit.ui.PdfFragment
31
32
import io.flutter.plugin.common.BinaryMessenger
32
33
import io.flutter.plugin.common.MethodChannel
33
34
35
+ private const val LOG_TAG = " FlutterPdfUiCallbacks"
36
+
34
37
/* *
35
38
* Callbacks for the FlutterPdfUiFragment.
36
39
* This class is responsible for notifying the Flutter side about document loading events.
@@ -41,8 +44,8 @@ import io.flutter.plugin.common.MethodChannel
41
44
* @param flutterWidgetCallback The callback to notify the Flutter side about document loading events.
42
45
*/
43
46
class FlutterPdfUiFragmentCallbacks (
44
- private val methodChannel : MethodChannel , private val measurementConfigurations :
45
- List <Map <String , Any >>? ,
47
+ private val methodChannel : MethodChannel ,
48
+ private val measurementConfigurations : List <Map <String , Any >>? ,
46
49
private val binaryMessenger : BinaryMessenger ,
47
50
private val flutterWidgetCallback : FlutterWidgetCallback
48
51
) : FragmentManager.FragmentLifecycleCallbacks(), DocumentListener {
@@ -55,9 +58,10 @@ class FlutterPdfUiFragmentCallbacks(
55
58
f : Fragment ,
56
59
context : Context
57
60
) {
58
- if (f.tag?.contains(" PSPDFKit .Fragment" ) == true ) {
61
+ if (f.tag?.contains(" Nutrient .Fragment" ) == true ) {
59
62
EventDispatcher .getInstance().notifyPdfFragmentAdded()
60
63
if (f !is PdfFragment ) {
64
+ Log .w(LOG_TAG , " Fragment is not a PdfFragment: ${f::class .java.simpleName} " )
61
65
return
62
66
}
63
67
if (pdfFragment != null ) {
@@ -69,45 +73,82 @@ class FlutterPdfUiFragmentCallbacks(
69
73
}
70
74
71
75
override fun onDocumentLoaded (document : PdfDocument ) {
76
+ // Apply measurement configurations if available
72
77
measurementConfigurations?.forEach {
73
- MeasurementHelper .addMeasurementConfiguration(pdfFragment!! , it)
78
+ try {
79
+ MeasurementHelper .addMeasurementConfiguration(pdfFragment!! , it)
80
+ } catch (e: Exception ) {
81
+ Log .e(LOG_TAG , " Failed to apply measurement configuration" , e)
82
+ }
83
+ }
84
+
85
+ // Send event through method channel
86
+ try {
87
+ val eventData = mapOf (" documentId" to document.uid)
88
+ methodChannel.invokeMethod(" onDocumentLoaded" , eventData)
89
+ } catch (e: Exception ) {
90
+ Log .e(LOG_TAG , " Error sending onDocumentLoaded via method channel" , e)
91
+ }
92
+
93
+ // Send event through widget callbacks
94
+ try {
95
+ flutterWidgetCallback.onDocumentLoaded(document)
96
+ } catch (e: Exception ) {
97
+ Log .e(LOG_TAG , " Error sending onDocumentLoaded via widget callbacks" , e)
98
+ }
99
+
100
+ // Set up document API for Flutter access
101
+ try {
102
+ flutterPdfDocument = FlutterPdfDocument (document)
103
+ PdfDocumentApi .setUp(binaryMessenger, flutterPdfDocument, document.uid)
104
+ } catch (e: Exception ) {
105
+ Log .e(LOG_TAG , " Error setting up FlutterPdfDocument" , e)
106
+ }
107
+
108
+ // Additional direct channel sending
109
+ try {
110
+ EventDispatcher .getInstance().notifyDocumentLoaded(document)
111
+ } catch (e: Exception ) {
112
+ Log .e(LOG_TAG , " Error sending direct event notification" , e)
74
113
}
75
- methodChannel.invokeMethod(
76
- " onDocumentLoaded" , mapOf (
77
- " documentId" to document.uid
78
- )
79
- )
80
- flutterWidgetCallback.onDocumentLoaded(document)
81
- flutterPdfDocument =
82
- FlutterPdfDocument (document);
83
- PdfDocumentApi .setUp(binaryMessenger, flutterPdfDocument, document.uid)
84
114
}
85
115
86
116
override fun onPageChanged (document : PdfDocument , pageIndex : Int ) {
87
117
super .onPageChanged(document, pageIndex)
88
- flutterWidgetCallback.onPageChanged(document, pageIndex)
89
- methodChannel.invokeMethod(
90
- " onPageChanged" ,
91
- mapOf (
92
- " documentId" to document.uid,
93
- " pageIndex" to pageIndex
118
+
119
+ try {
120
+ flutterWidgetCallback.onPageChanged(document, pageIndex)
121
+ methodChannel.invokeMethod(
122
+ " onPageChanged" ,
123
+ mapOf (
124
+ " documentId" to document.uid,
125
+ " pageIndex" to pageIndex
126
+ )
94
127
)
95
- )
128
+ } catch (e: Exception ) {
129
+ Log .e(LOG_TAG , " Error sending onPageChanged event" , e)
130
+ }
96
131
}
97
132
98
133
override fun onDocumentLoadFailed (exception : Throwable ) {
99
134
super .onDocumentLoadFailed(exception)
100
- flutterWidgetCallback.onDocumentLoadFailed(exception)
101
- methodChannel.invokeMethod(
102
- " onDocumentLoadFailed" ,
103
- mapOf (
104
- " error" to exception.message
135
+ Log .e(LOG_TAG , " Document load failed" , exception)
136
+
137
+ try {
138
+ flutterWidgetCallback.onDocumentLoadFailed(exception)
139
+ methodChannel.invokeMethod(
140
+ " onDocumentLoadFailed" ,
141
+ mapOf (
142
+ " error" to exception.message
143
+ )
105
144
)
106
- )
145
+ } catch (e: Exception ) {
146
+ Log .e(LOG_TAG , " Error sending onDocumentLoadFailed event" , e)
147
+ }
107
148
}
108
149
109
150
override fun onFragmentDetached (fm : FragmentManager , f : Fragment ) {
110
- if (f.tag?.contains(" PSPDFKit .Fragment" ) == true ) {
151
+ if (f.tag?.contains(" Nutrient .Fragment" ) == true ) {
111
152
if (f !is PdfFragment ) {
112
153
return
113
154
}
@@ -126,12 +167,20 @@ class FlutterPdfUiFragmentCallbacks(
126
167
pagePosition : PointF ? ,
127
168
clickedAnnotation : Annotation ?
128
169
): Boolean {
129
- flutterWidgetCallback.onPageClick(document, pageIndex, event, pagePosition, clickedAnnotation)
170
+ try {
171
+ flutterWidgetCallback.onPageClick(document, pageIndex, event, pagePosition, clickedAnnotation)
172
+ } catch (e: Exception ) {
173
+ Log .e(LOG_TAG , " Error sending onPageClick event" , e)
174
+ }
130
175
return true
131
176
}
132
177
133
178
override fun onDocumentSave (document : PdfDocument , saveOptions : DocumentSaveOptions ): Boolean {
134
- flutterWidgetCallback.onDocumentSave(document,saveOptions)
179
+ try {
180
+ flutterWidgetCallback.onDocumentSave(document, saveOptions)
181
+ } catch (e: Exception ) {
182
+ Log .e(LOG_TAG , " Error sending onDocumentSave event" , e)
183
+ }
135
184
return true
136
185
}
137
186
}
0 commit comments