Skip to content

Commit 051ac33

Browse files
author
irgendeinich
committed
Merge branch 'master' into reinhard/update-rn-0.60.4
# Conflicts: # package.json # samples/Catalog/package.json
2 parents a37ab39 + 77dd26d commit 051ac33

File tree

12 files changed

+334
-179
lines changed

12 files changed

+334
-179
lines changed

android/src/main/java/com/pspdfkit/react/ReactPdfViewManager.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ public void accept(List<Annotation> annotations) {
186186
}
187187
break;
188188
case COMMAND_ADD_ANNOTATION:
189-
if (args != null) {
190-
annotationDisposables.add(root.addAnnotation(args.getMap(0)));
189+
if (args != null && args.size() == 2) {
190+
final int requestId = args.getInt(0);
191+
annotationDisposables.add(root.addAnnotation(requestId, args.getMap(1)));
191192
}
192193
break;
193194
case COMMAND_REMOVE_ANNOTATION:
194-
if (args != null) {
195-
annotationDisposables.add(root.removeAnnotation(args.getMap(0)));
195+
if (args != null && args.size() == 2) {
196+
final int requestId = args.getInt(0);
197+
annotationDisposables.add(root.removeAnnotation(requestId, args.getMap(1)));
196198
}
197199
break;
198200
case COMMAND_GET_ALL_UNSAVED_ANNOTATIONS:
@@ -211,8 +213,9 @@ public void accept(JSONObject jsonObject) {
211213
}
212214
break;
213215
case COMMAND_ADD_ANNOTATIONS:
214-
if (args != null && args.size() == 1) {
215-
annotationDisposables.add(root.addAnnotations(args.getMap(0)));
216+
if (args != null && args.size() == 2) {
217+
final int requestId = args.getInt(0);
218+
annotationDisposables.add(root.addAnnotations(requestId, args.getMap(1)));
216219
}
217220
break;
218221
case COMMAND_GET_FORM_FIELD_VALUE:

android/src/main/java/com/pspdfkit/react/events/PdfViewDataReturnedEvent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull List<
4747
payload = Arguments.makeNativeMap(map);
4848
}
4949

50+
public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, boolean result) {
51+
super(viewId);
52+
this.requestId = requestId;
53+
Map<String, Object> map = new HashMap<>();
54+
map.put("requestId", requestId);
55+
map.put("result", result);
56+
57+
payload = Arguments.makeNativeMap(map);
58+
}
59+
5060
public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull JSONObject jsonObject) {
5161
super(viewId);
5262
this.requestId = requestId;

android/src/main/java/com/pspdfkit/views/PdfView.java

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@
5555
import java.util.ArrayList;
5656
import java.util.EnumSet;
5757
import java.util.List;
58+
import java.util.NoSuchElementException;
5859
import java.util.concurrent.Callable;
5960

61+
import io.reactivex.Completable;
6062
import io.reactivex.Observable;
6163
import io.reactivex.ObservableSource;
6264
import io.reactivex.Single;
@@ -172,47 +174,46 @@ public void setFragmentTag(String fragmentTag) {
172174
}
173175

174176
public void setConfiguration(PdfActivityConfiguration configuration) {
177+
if (configuration != null && !configuration.equals(this.configuration)) {
178+
// The configuration changed, recreate the fragment.
179+
// We set the current page index so the fragment is created at this location.
180+
this.pageIndex = fragment != null ? fragment.getPageIndex() : this.pageIndex;
181+
removeFragment(false);
182+
}
175183
this.configuration = configuration;
176184
setupFragment();
177185
}
178186

179-
public void setDocument(@Nullable String document) {
180-
if (document == null) {
187+
public void setDocument(@Nullable String documentPath) {
188+
if (documentPath == null) {
189+
this.document = null;
181190
removeFragment(false);
182191
return;
183192
}
184193

185-
if (Uri.parse(document).getScheme() == null) {
194+
if (Uri.parse(documentPath).getScheme() == null) {
186195
// If there is no scheme it might be a raw path.
187196
try {
188-
File file = new File(document);
189-
document = Uri.fromFile(file).toString();
197+
File file = new File(documentPath);
198+
documentPath = Uri.fromFile(file).toString();
190199
} catch (Exception e) {
191-
document = FILE_SCHEME + document;
200+
documentPath = FILE_SCHEME + document;
192201
}
193202
}
194203
if (documentOpeningDisposable != null) {
195204
documentOpeningDisposable.dispose();
196205
}
197206
updateState();
198-
documentOpeningDisposable = PdfDocumentLoader.openDocumentAsync(getContext(), Uri.parse(document))
207+
documentOpeningDisposable = PdfDocumentLoader.openDocumentAsync(getContext(), Uri.parse(documentPath))
199208
.subscribeOn(Schedulers.io())
200209
.observeOn(AndroidSchedulers.mainThread())
201-
.subscribe(new Consumer<PdfDocument>() {
202-
203-
@Override
204-
public void accept(PdfDocument pdfDocument) throws Exception {
205-
PdfView.this.document = pdfDocument;
206-
setupFragment();
207-
}
208-
209-
}, new Consumer<Throwable>() {
210-
@Override
211-
public void accept(Throwable throwable) throws Exception {
212-
PdfView.this.document = null;
213-
setupFragment();
214-
eventDispatcher.dispatchEvent(new PdfViewDocumentLoadFailedEvent(getId(), throwable.getMessage()));
215-
}
210+
.subscribe(pdfDocument -> {
211+
PdfView.this.document = pdfDocument;
212+
setupFragment();
213+
}, throwable -> {
214+
PdfView.this.document = null;
215+
setupFragment();
216+
eventDispatcher.dispatchEvent(new PdfViewDocumentLoadFailedEvent(getId(), throwable.getMessage()));
216217
});
217218
}
218219

@@ -330,15 +331,17 @@ public void removeFragment(boolean makeInactive) {
330331
PdfFragment pdfFragment = (PdfFragment) fragmentManager.findFragmentByTag(fragmentTag);
331332
if (pdfFragment != null) {
332333
fragmentManager.beginTransaction()
333-
.remove(pdfFragment)
334-
.commitAllowingStateLoss();
334+
.remove(pdfFragment)
335+
.commitNowAllowingStateLoss();
335336
}
336337
if (makeInactive) {
338+
// Clear everything.
337339
isActive = false;
340+
document = null;
338341
}
339342

340343
fragment = null;
341-
document = null;
344+
342345
fragmentGetter.onComplete();
343346
fragmentGetter = BehaviorSubject.create();
344347
pendingFragmentActions.dispose();
@@ -493,17 +496,19 @@ private EnumSet<AnnotationType> getTypeFromString(@Nullable String type) {
493496
return EnumSet.noneOf(AnnotationType.class);
494497
}
495498

496-
public Disposable addAnnotation(ReadableMap annotation) {
499+
public Disposable addAnnotation(final int requestId, ReadableMap annotation) {
497500
return fragmentGetter.take(1).map(PdfFragment::getDocument).subscribeOn(Schedulers.io())
498-
.observeOn(AndroidSchedulers.mainThread())
499-
.subscribe(pdfDocument -> {
500-
JSONObject json = new JSONObject(annotation.toHashMap());
501-
pdfDocument.getAnnotationProvider().createAnnotationFromInstantJson(json.toString());
502-
});
503-
501+
.map(pdfDocument -> {
502+
JSONObject json = new JSONObject(annotation.toHashMap());
503+
return pdfDocument.getAnnotationProvider().createAnnotationFromInstantJson(json.toString());
504+
})
505+
.map(Annotation::toInstantJson)
506+
.observeOn(AndroidSchedulers.mainThread())
507+
.subscribe((instantJson) -> eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, true)),
508+
(throwable) -> eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, throwable)));
504509
}
505510

506-
public Disposable removeAnnotation(ReadableMap annotation) {
511+
public Disposable removeAnnotation(final int requestId, ReadableMap annotation) {
507512
return fragmentGetter.take(1).map(PdfFragment::getDocument).subscribeOn(Schedulers.io())
508513
.observeOn(AndroidSchedulers.mainThread())
509514
.flatMap(pdfDocument -> {
@@ -516,10 +521,23 @@ public Disposable removeAnnotation(ReadableMap annotation) {
516521
if (pageIndex == -1 || type == null || name == null) {
517522
return Observable.empty();
518523
}
524+
519525
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfType(getTypeFromString(type), pageIndex, 1)
520526
.filter(annotationToFilter -> name.equals(annotationToFilter.getName()))
521527
.map(filteredAnnotation -> new Pair<>(filteredAnnotation, pdfDocument));
522-
}).subscribe(pair -> pair.second.getAnnotationProvider().removeAnnotationFromPage(pair.first));
528+
})
529+
.firstOrError()
530+
.flatMapCompletable(pair -> Completable.fromAction(() -> {
531+
pair.second.getAnnotationProvider().removeAnnotationFromPage(pair.first);
532+
}))
533+
.subscribe(() -> eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, true)), (throwable -> {
534+
if (throwable instanceof NoSuchElementException) {
535+
// We didn't find an annotation so return false.
536+
eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, false));
537+
} else {
538+
eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, throwable));
539+
}
540+
}));
523541
}
524542

525543
public Single<JSONObject> getAllUnsavedAnnotations() {
@@ -536,14 +554,16 @@ public JSONObject call() throws Exception {
536554
});
537555
}
538556

539-
public Disposable addAnnotations(ReadableMap annotation) {
557+
public Disposable addAnnotations(final int requestId, ReadableMap annotation) {
540558
return fragmentGetter.take(1).map(PdfFragment::getDocument).subscribeOn(Schedulers.io())
541-
.observeOn(AndroidSchedulers.mainThread())
542-
.subscribe(pdfDocument -> {
543-
JSONObject json = new JSONObject(annotation.toHashMap());
544-
final DataProvider dataProvider = new DocumentJsonDataProvider(json);
545-
DocumentJsonFormatter.importDocumentJson(pdfDocument, dataProvider);
546-
});
559+
.flatMapCompletable(currentDocument -> Completable.fromAction(() -> {
560+
JSONObject json = new JSONObject(annotation.toHashMap());
561+
final DataProvider dataProvider = new DocumentJsonDataProvider(json);
562+
DocumentJsonFormatter.importDocumentJson(currentDocument, dataProvider);
563+
}))
564+
.observeOn(AndroidSchedulers.mainThread())
565+
.subscribe(() -> eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, true)),
566+
(throwable) -> eventDispatcher.dispatchEvent(new PdfViewDataReturnedEvent(getId(), requestId, throwable)));
547567
}
548568

549569
public Disposable getFormFieldValue(final int requestId, @NonNull String formElementName) {

index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,26 @@ class PSPDFKitView extends React.Component {
186186
* Adds a new annotation to the current document.
187187
*
188188
* @param annotation InstantJson of the annotation to add.
189+
*
190+
* Returns a promise resolving to true if the annotation was added. Otherwise, returns false if an error has occurred.
189191
*/
190192
addAnnotation = function(annotation) {
191193
if (Platform.OS === "android") {
194+
let requestId = this._nextRequestId++;
195+
let requestMap = this._requestMap;
196+
197+
// We create a promise here that will be resolved once onDataReturned is called.
198+
let promise = new Promise(function(resolve, reject) {
199+
requestMap[requestId] = { resolve: resolve, reject: reject };
200+
});
201+
192202
UIManager.dispatchViewManagerCommand(
193203
findNodeHandle(this.refs.pdfView),
194204
this._getViewManagerConfig("RCTPSPDFKitView").Commands.addAnnotation,
195-
[annotation]
205+
[requestId, annotation]
196206
);
207+
208+
return promise;
197209
} else if (Platform.OS === "ios") {
198210
return NativeModules.PSPDFKitViewManager.addAnnotation(
199211
annotation,
@@ -206,14 +218,26 @@ class PSPDFKitView extends React.Component {
206218
* Removes an existing annotation from the current document.
207219
*
208220
* @param annotation InstantJson of the annotation to remove.
221+
*
222+
* Returns a promise resolving to true if the annotation was removed. Otherwise, returns false if the annotation couldn't be found.
209223
*/
210224
removeAnnotation = function(annotation) {
211225
if (Platform.OS === "android") {
226+
let requestId = this._nextRequestId++;
227+
let requestMap = this._requestMap;
228+
229+
// We create a promise here that will be resolved once onDataReturned is called.
230+
let promise = new Promise(function(resolve, reject) {
231+
requestMap[requestId] = { resolve: resolve, reject: reject };
232+
});
233+
212234
UIManager.dispatchViewManagerCommand(
213235
findNodeHandle(this.refs.pdfView),
214236
this._getViewManagerConfig("RCTPSPDFKitView").Commands.removeAnnotation,
215-
[annotation]
237+
[requestId, annotation]
216238
);
239+
240+
return promise;
217241
} else if (Platform.OS === "ios") {
218242
return NativeModules.PSPDFKitViewManager.removeAnnotation(
219243
annotation,
@@ -256,14 +280,26 @@ class PSPDFKitView extends React.Component {
256280
* Applies the passed in document instant json.
257281
*
258282
* @param annotations The document instant json to apply.
283+
*
284+
* Returns a promise resolving to true if the annotation was added.
259285
*/
260286
addAnnotations = function(annotations) {
261287
if (Platform.OS === "android") {
288+
let requestId = this._nextRequestId++;
289+
let requestMap = this._requestMap;
290+
291+
// We create a promise here that will be resolved once onDataReturned is called.
292+
let promise = new Promise(function(resolve, reject) {
293+
requestMap[requestId] = { resolve: resolve, reject: reject };
294+
});
295+
262296
UIManager.dispatchViewManagerCommand(
263297
findNodeHandle(this.refs.pdfView),
264298
this._getViewManagerConfig("RCTPSPDFKitView").Commands.addAnnotations,
265-
[annotations]
299+
[requestId, annotations]
266300
);
301+
302+
return promise;
267303
} else if (Platform.OS === "ios") {
268304
return NativeModules.PSPDFKitViewManager.addAnnotations(
269305
annotations,

ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@interface RCTConvert (PSPDFAnnotation)
1515

16-
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations;
16+
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations error:(NSError **)error;
1717
+ (PSPDFAnnotationType)annotationTypeFromInstantJSONType:(NSString *)type;
1818

1919
@end

ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
@implementation RCTConvert (PSPDFAnnotation)
1313

14-
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations {
14+
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations error:(NSError **)error {
1515
NSMutableArray <NSDictionary *> *annotationsJSON = [NSMutableArray new];
1616
for (PSPDFAnnotation *annotation in annotations) {
1717
NSDictionary <NSString *, NSString *> *uuidDict = @{@"uuid" : annotation.uuid};
18-
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
18+
NSData *annotationData = [annotation generateInstantJSONWithError:error];
1919
if (annotationData) {
20-
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL] mutableCopy];
20+
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:error] mutableCopy];
2121
[annotationDictionary addEntriesFromDictionary:uuidDict];
2222
if (annotationDictionary) {
2323
[annotationsJSON addObject:annotationDictionary];
@@ -27,7 +27,7 @@ @implementation RCTConvert (PSPDFAnnotation)
2727
[annotationsJSON addObject:uuidDict];
2828
}
2929
}
30-
30+
3131
return [annotationsJSON copy];
3232
}
3333

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ NS_ASSUME_NONNULL_BEGIN
3636
- (BOOL)exitCurrentlyActiveMode;
3737

3838
/// Document
39-
- (BOOL)saveCurrentDocument;
39+
- (BOOL)saveCurrentDocumentWithError:(NSError *_Nullable *)error;
4040

4141
/// Anotations
42-
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
43-
- (BOOL)addAnnotation:(id)jsonAnnotation;
42+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error;
43+
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error;
4444
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
45-
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
46-
- (BOOL)addAnnotations:(NSString *)jsonAnnotations;
45+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error;
46+
- (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error;
4747

4848
/// Forms
4949
- (NSDictionary<NSString *, NSString *> *)getFormFieldValue:(NSString *)fullyQualifiedName;

0 commit comments

Comments
 (0)