5555import java .util .ArrayList ;
5656import java .util .EnumSet ;
5757import java .util .List ;
58+ import java .util .NoSuchElementException ;
5859import java .util .concurrent .Callable ;
5960
61+ import io .reactivex .Completable ;
6062import io .reactivex .Observable ;
6163import io .reactivex .ObservableSource ;
6264import 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 ) {
0 commit comments