5454import java .util .ArrayList ;
5555import java .util .EnumSet ;
5656import java .util .List ;
57+ import java .util .NoSuchElementException ;
5758import java .util .concurrent .Callable ;
5859
60+ import io .reactivex .Completable ;
5961import io .reactivex .Observable ;
6062import io .reactivex .ObservableSource ;
6163import io .reactivex .Single ;
@@ -171,47 +173,46 @@ public void setFragmentTag(String fragmentTag) {
171173 }
172174
173175 public void setConfiguration (PdfActivityConfiguration configuration ) {
176+ if (configuration != null && !configuration .equals (this .configuration )) {
177+ // The configuration changed, recreate the fragment.
178+ // We set the current page index so the fragment is created at this location.
179+ this .pageIndex = fragment != null ? fragment .getPageIndex () : this .pageIndex ;
180+ removeFragment (false );
181+ }
174182 this .configuration = configuration ;
175183 setupFragment ();
176184 }
177185
178- public void setDocument (@ Nullable String document ) {
179- if (document == null ) {
186+ public void setDocument (@ Nullable String documentPath ) {
187+ if (documentPath == null ) {
188+ this .document = null ;
180189 removeFragment (false );
181190 return ;
182191 }
183192
184- if (Uri .parse (document ).getScheme () == null ) {
193+ if (Uri .parse (documentPath ).getScheme () == null ) {
185194 // If there is no scheme it might be a raw path.
186195 try {
187- File file = new File (document );
188- document = Uri .fromFile (file ).toString ();
196+ File file = new File (documentPath );
197+ documentPath = Uri .fromFile (file ).toString ();
189198 } catch (Exception e ) {
190- document = FILE_SCHEME + document ;
199+ documentPath = FILE_SCHEME + document ;
191200 }
192201 }
193202 if (documentOpeningDisposable != null ) {
194203 documentOpeningDisposable .dispose ();
195204 }
196205 updateState ();
197- documentOpeningDisposable = PdfDocumentLoader .openDocumentAsync (getContext (), Uri .parse (document ))
206+ documentOpeningDisposable = PdfDocumentLoader .openDocumentAsync (getContext (), Uri .parse (documentPath ))
198207 .subscribeOn (Schedulers .io ())
199208 .observeOn (AndroidSchedulers .mainThread ())
200- .subscribe (new Consumer <PdfDocument >() {
201-
202- @ Override
203- public void accept (PdfDocument pdfDocument ) throws Exception {
204- PdfView .this .document = pdfDocument ;
205- setupFragment ();
206- }
207-
208- }, new Consumer <Throwable >() {
209- @ Override
210- public void accept (Throwable throwable ) throws Exception {
211- PdfView .this .document = null ;
212- setupFragment ();
213- eventDispatcher .dispatchEvent (new PdfViewDocumentLoadFailedEvent (getId (), throwable .getMessage ()));
214- }
209+ .subscribe (pdfDocument -> {
210+ PdfView .this .document = pdfDocument ;
211+ setupFragment ();
212+ }, throwable -> {
213+ PdfView .this .document = null ;
214+ setupFragment ();
215+ eventDispatcher .dispatchEvent (new PdfViewDocumentLoadFailedEvent (getId (), throwable .getMessage ()));
215216 });
216217 }
217218
@@ -329,15 +330,17 @@ public void removeFragment(boolean makeInactive) {
329330 PdfFragment pdfFragment = (PdfFragment ) fragmentManager .findFragmentByTag (fragmentTag );
330331 if (pdfFragment != null ) {
331332 fragmentManager .beginTransaction ()
332- .remove (pdfFragment )
333- . commitAllowingStateLoss ();
333+ .remove (pdfFragment )
334+ . commitNowAllowingStateLoss ();
334335 }
335336 if (makeInactive ) {
337+ // Clear everything.
336338 isActive = false ;
339+ document = null ;
337340 }
338341
339342 fragment = null ;
340- document = null ;
343+
341344 fragmentGetter .onComplete ();
342345 fragmentGetter = BehaviorSubject .create ();
343346 pendingFragmentActions .dispose ();
@@ -492,17 +495,19 @@ private EnumSet<AnnotationType> getTypeFromString(@Nullable String type) {
492495 return EnumSet .noneOf (AnnotationType .class );
493496 }
494497
495- public Disposable addAnnotation (ReadableMap annotation ) {
498+ public Disposable addAnnotation (final int requestId , ReadableMap annotation ) {
496499 return fragmentGetter .take (1 ).map (PdfFragment ::getDocument ).subscribeOn (Schedulers .io ())
497- .observeOn (AndroidSchedulers .mainThread ())
498- .subscribe (pdfDocument -> {
499- JSONObject json = new JSONObject (annotation .toHashMap ());
500- pdfDocument .getAnnotationProvider ().createAnnotationFromInstantJson (json .toString ());
501- });
502-
500+ .map (pdfDocument -> {
501+ JSONObject json = new JSONObject (annotation .toHashMap ());
502+ return pdfDocument .getAnnotationProvider ().createAnnotationFromInstantJson (json .toString ());
503+ })
504+ .map (Annotation ::toInstantJson )
505+ .observeOn (AndroidSchedulers .mainThread ())
506+ .subscribe ((instantJson ) -> eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , true )),
507+ (throwable ) -> eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , throwable )));
503508 }
504509
505- public Disposable removeAnnotation (ReadableMap annotation ) {
510+ public Disposable removeAnnotation (final int requestId , ReadableMap annotation ) {
506511 return fragmentGetter .take (1 ).map (PdfFragment ::getDocument ).subscribeOn (Schedulers .io ())
507512 .observeOn (AndroidSchedulers .mainThread ())
508513 .flatMap (pdfDocument -> {
@@ -515,10 +520,23 @@ public Disposable removeAnnotation(ReadableMap annotation) {
515520 if (pageIndex == -1 || type == null || name == null ) {
516521 return Observable .empty ();
517522 }
523+
518524 return pdfDocument .getAnnotationProvider ().getAllAnnotationsOfType (getTypeFromString (type ), pageIndex , 1 )
519525 .filter (annotationToFilter -> name .equals (annotationToFilter .getName ()))
520526 .map (filteredAnnotation -> new Pair <>(filteredAnnotation , pdfDocument ));
521- }).subscribe (pair -> pair .second .getAnnotationProvider ().removeAnnotationFromPage (pair .first ));
527+ })
528+ .firstOrError ()
529+ .flatMapCompletable (pair -> Completable .fromAction (() -> {
530+ pair .second .getAnnotationProvider ().removeAnnotationFromPage (pair .first );
531+ }))
532+ .subscribe (() -> eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , true )), (throwable -> {
533+ if (throwable instanceof NoSuchElementException ) {
534+ // We didn't find an annotation so return false.
535+ eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , false ));
536+ } else {
537+ eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , throwable ));
538+ }
539+ }));
522540 }
523541
524542 public Single <JSONObject > getAllUnsavedAnnotations () {
@@ -535,14 +553,16 @@ public JSONObject call() throws Exception {
535553 });
536554 }
537555
538- public Disposable addAnnotations (ReadableMap annotation ) {
556+ public Disposable addAnnotations (final int requestId , ReadableMap annotation ) {
539557 return fragmentGetter .take (1 ).map (PdfFragment ::getDocument ).subscribeOn (Schedulers .io ())
540- .observeOn (AndroidSchedulers .mainThread ())
541- .subscribe (pdfDocument -> {
542- JSONObject json = new JSONObject (annotation .toHashMap ());
543- final DataProvider dataProvider = new DocumentJsonDataProvider (json );
544- DocumentJsonFormatter .importDocumentJson (pdfDocument , dataProvider );
545- });
558+ .flatMapCompletable (currentDocument -> Completable .fromAction (() -> {
559+ JSONObject json = new JSONObject (annotation .toHashMap ());
560+ final DataProvider dataProvider = new DocumentJsonDataProvider (json );
561+ DocumentJsonFormatter .importDocumentJson (currentDocument , dataProvider );
562+ }))
563+ .observeOn (AndroidSchedulers .mainThread ())
564+ .subscribe (() -> eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , true )),
565+ (throwable ) -> eventDispatcher .dispatchEvent (new PdfViewDataReturnedEvent (getId (), requestId , throwable )));
546566 }
547567
548568 public Disposable getFormFieldValue (final int requestId , @ NonNull String formElementName ) {
0 commit comments