@@ -17,6 +17,7 @@ package com.pspdfkit.flutter.pspdfkit
1717// /
1818import android.content.Context
1919import android.graphics.PointF
20+ import android.util.Log
2021import android.view.MotionEvent
2122import androidx.fragment.app.Fragment
2223import androidx.fragment.app.FragmentManager
@@ -31,6 +32,8 @@ import com.pspdfkit.ui.PdfFragment
3132import io.flutter.plugin.common.BinaryMessenger
3233import io.flutter.plugin.common.MethodChannel
3334
35+ private const val LOG_TAG = " FlutterPdfUiCallbacks"
36+
3437/* *
3538 * Callbacks for the FlutterPdfUiFragment.
3639 * This class is responsible for notifying the Flutter side about document loading events.
@@ -41,8 +44,8 @@ import io.flutter.plugin.common.MethodChannel
4144 * @param flutterWidgetCallback The callback to notify the Flutter side about document loading events.
4245 */
4346class 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 >>? ,
4649 private val binaryMessenger : BinaryMessenger ,
4750 private val flutterWidgetCallback : FlutterWidgetCallback
4851) : FragmentManager.FragmentLifecycleCallbacks(), DocumentListener {
@@ -55,9 +58,10 @@ class FlutterPdfUiFragmentCallbacks(
5558 f : Fragment ,
5659 context : Context
5760 ) {
58- if (f.tag?.contains(" PSPDFKit .Fragment" ) == true ) {
61+ if (f.tag?.contains(" Nutrient .Fragment" ) == true ) {
5962 EventDispatcher .getInstance().notifyPdfFragmentAdded()
6063 if (f !is PdfFragment ) {
64+ Log .w(LOG_TAG , " Fragment is not a PdfFragment: ${f::class .java.simpleName} " )
6165 return
6266 }
6367 if (pdfFragment != null ) {
@@ -69,45 +73,82 @@ class FlutterPdfUiFragmentCallbacks(
6973 }
7074
7175 override fun onDocumentLoaded (document : PdfDocument ) {
76+ // Apply measurement configurations if available
7277 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)
74113 }
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)
84114 }
85115
86116 override fun onPageChanged (document : PdfDocument , pageIndex : Int ) {
87117 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+ )
94127 )
95- )
128+ } catch (e: Exception ) {
129+ Log .e(LOG_TAG , " Error sending onPageChanged event" , e)
130+ }
96131 }
97132
98133 override fun onDocumentLoadFailed (exception : Throwable ) {
99134 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+ )
105144 )
106- )
145+ } catch (e: Exception ) {
146+ Log .e(LOG_TAG , " Error sending onDocumentLoadFailed event" , e)
147+ }
107148 }
108149
109150 override fun onFragmentDetached (fm : FragmentManager , f : Fragment ) {
110- if (f.tag?.contains(" PSPDFKit .Fragment" ) == true ) {
151+ if (f.tag?.contains(" Nutrient .Fragment" ) == true ) {
111152 if (f !is PdfFragment ) {
112153 return
113154 }
@@ -126,12 +167,20 @@ class FlutterPdfUiFragmentCallbacks(
126167 pagePosition : PointF ? ,
127168 clickedAnnotation : Annotation ?
128169 ): 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+ }
130175 return true
131176 }
132177
133178 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+ }
135184 return true
136185 }
137186}
0 commit comments