|
@@ -14,12 +14,16 @@ import android.app.Activity;
|
|
|
|
|
|
import android.net.Uri;
|
|
|
import android.util.Log;
|
|
|
+import android.util.SparseArray;
|
|
|
+import android.view.View;
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
import androidx.fragment.app.FragmentActivity;
|
|
|
|
|
|
import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
|
|
|
import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
|
|
|
+import com.compdfkit.tools.common.views.pdfview.CPDFViewCtrl.COnSaveCallback;
|
|
|
+import com.compdfkit.tools.common.views.pdfview.CPDFViewCtrl.COnSaveError;
|
|
|
import com.compdfkitpdf.reactnative.view.CPDFView;
|
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
import com.facebook.react.bridge.ReadableArray;
|
|
@@ -31,8 +35,12 @@ import java.util.Map;
|
|
|
|
|
|
public class CPDFViewManager extends ViewGroupManager<CPDFView> {
|
|
|
|
|
|
+ private static final String TAG = "ComPDFKitRN";
|
|
|
+
|
|
|
private ReactApplicationContext reactContext;
|
|
|
|
|
|
+ private SparseArray<CPDFView> mDocumentViews = new SparseArray<>();
|
|
|
+
|
|
|
public CPDFViewManager(ReactApplicationContext context) {
|
|
|
this.reactContext = context;
|
|
|
}
|
|
@@ -43,6 +51,21 @@ public class CPDFViewManager extends ViewGroupManager<CPDFView> {
|
|
|
return "RCTCPDFReaderView";
|
|
|
}
|
|
|
|
|
|
+ private View.OnAttachStateChangeListener mOnAttachStateChangeListener = new View.OnAttachStateChangeListener() {
|
|
|
+ @Override
|
|
|
+ public void onViewAttachedToWindow(View v) {
|
|
|
+ CPDFView documentView = (CPDFView) v;
|
|
|
+ Log.d(TAG, "add to map: " + v.getId());
|
|
|
+ mDocumentViews.put(v.getId(), documentView);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onViewDetachedFromWindow(View v) {
|
|
|
+ Log.d(TAG, "remove from map: " + v.getId());
|
|
|
+ mDocumentViews.remove(v.getId());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
@NonNull
|
|
|
@Override
|
|
|
protected CPDFView createViewInstance(@NonNull ThemedReactContext themedReactContext) {
|
|
@@ -50,48 +73,45 @@ public class CPDFViewManager extends ViewGroupManager<CPDFView> {
|
|
|
if (currentActivity instanceof FragmentActivity fragmentActivity) {
|
|
|
CPDFView pdfView = new CPDFView(fragmentActivity);
|
|
|
pdfView.setup(themedReactContext, fragmentActivity.getSupportFragmentManager());
|
|
|
-
|
|
|
+ pdfView.addOnAttachStateChangeListener(mOnAttachStateChangeListener);
|
|
|
return pdfView;
|
|
|
} else {
|
|
|
throw new IllegalStateException("CPDFView can only be used in FragmentActivity subclasses");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@ReactProp(name = "document")
|
|
|
- public void setDocument(CPDFView pdfView, ReadableMap documentInfo) {
|
|
|
- Log.e("ComPDFKitRN", "CPDFViewManager-setDocument()");
|
|
|
- String document = documentInfo.getString("path");
|
|
|
- String password = documentInfo.getString("password");
|
|
|
- Log.e("ComPDFKitRN", "document:" + document);
|
|
|
- Log.e("ComPDFKitRN", "password:" + password);
|
|
|
- pdfView.setDocument(document, password);
|
|
|
+ public void setDocument(CPDFView pdfView, String document) {
|
|
|
+ Log.d(TAG, "CPDFViewManager-setDocument()");
|
|
|
+ Log.d(TAG, "document:" + document);
|
|
|
+ pdfView.setDocument(document);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactProp(name = "password")
|
|
|
+ public void setPassword(CPDFView pdfView, String password) {
|
|
|
+ Log.d(TAG, "CPDFViewManager-setPassword(password: " + password + ")");
|
|
|
+ pdfView.setPassword(password);
|
|
|
}
|
|
|
|
|
|
@ReactProp(name = "configuration")
|
|
|
public void setConfiguration(CPDFView pdfView, String configurationJson) {
|
|
|
- Log.e("ComPDFKitRN", "CPDFViewManager-setConfiguration()");
|
|
|
+ Log.d(TAG, "CPDFViewManager-setConfiguration()");
|
|
|
CPDFConfiguration configuration = CPDFConfigurationUtils.fromJson(configurationJson);
|
|
|
pdfView.setConfiguration(configuration);
|
|
|
}
|
|
|
|
|
|
- @Nullable
|
|
|
- @Override
|
|
|
- public Map<String, Integer> getCommandsMap() {
|
|
|
- Log.e("ComPDFKitRN", "CPDFViewManager-getCommandsMap()");
|
|
|
- return super.getCommandsMap();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void receiveCommand(@NonNull CPDFView root, String commandId,
|
|
|
- @Nullable ReadableArray args) {
|
|
|
- super.receiveCommand(root, commandId, args);
|
|
|
- Log.e("ComPDFKitRN", "CPDFViewManager-receiveCommand()");
|
|
|
-
|
|
|
+ public void save(int tag, COnSaveCallback saveCallback, COnSaveError error) {
|
|
|
+ CPDFView pdfView = mDocumentViews.get(tag);
|
|
|
+ if (pdfView != null) {
|
|
|
+ pdfView.documentFragment.pdfView.savePDF(saveCallback, error);
|
|
|
+ } else {
|
|
|
+ error.error(new Exception("save() Unable to find DocumentView"));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean needsCustomLayoutForChildren() {
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
}
|