4949import java .util .List ;
5050import java .util .concurrent .Callable ;
5151
52+ import io .reactivex .ObservableSource ;
5253import io .reactivex .Single ;
5354import io .reactivex .android .schedulers .AndroidSchedulers ;
55+ import io .reactivex .disposables .CompositeDisposable ;
5456import io .reactivex .disposables .Disposable ;
5557import io .reactivex .functions .Action ;
5658import io .reactivex .functions .Consumer ;
59+ import io .reactivex .functions .Function ;
5760import io .reactivex .schedulers .Schedulers ;
61+ import io .reactivex .subjects .BehaviorSubject ;
5862
5963/**
6064 * This view displays a {@link com.pspdfkit.ui.PdfFragment} and all associated toolbars.
@@ -79,7 +83,12 @@ public class PdfView extends FrameLayout {
7983
8084 private PdfThumbnailBar pdfThumbnailBar ;
8185
86+ @ NonNull
87+ private CompositeDisposable pendingFragmentActions = new CompositeDisposable ();
88+
89+ @ Nullable
8290 private PdfFragment fragment ;
91+ private BehaviorSubject <PdfFragment > fragmentGetter = BehaviorSubject .create ();
8392
8493 public PdfView (@ NonNull Context context ) {
8594 super (context );
@@ -218,6 +227,7 @@ private void setupFragment() {
218227 }
219228
220229 fragment = pdfFragment ;
230+ fragmentGetter .onNext (fragment );
221231 }
222232 }
223233
@@ -280,6 +290,10 @@ public void removeFragment() {
280290 isActive = false ;
281291
282292 fragment = null ;
293+ fragmentGetter .onComplete ();
294+ fragmentGetter = BehaviorSubject .create ();
295+ pendingFragmentActions .dispose ();
296+ pendingFragmentActions = new CompositeDisposable ();
283297 }
284298
285299 void manuallyLayoutChildren () {
@@ -330,15 +344,25 @@ public EventDispatcher getEventDispatcher() {
330344 }
331345
332346 public void enterAnnotationCreationMode () {
333- if (fragment != null ) {
334- fragment .enterAnnotationCreationMode ();
335- }
347+ pendingFragmentActions .add (fragmentGetter .take (1 )
348+ .observeOn (Schedulers .io ())
349+ .subscribe (new Consumer <PdfFragment >() {
350+ @ Override
351+ public void accept (PdfFragment pdfFragment ) {
352+ pdfFragment .enterAnnotationCreationMode ();
353+ }
354+ }));
336355 }
337356
338357 public void exitCurrentlyActiveMode () {
339- if (fragment != null ) {
340- fragment .exitCurrentlyActiveMode ();
341- }
358+ pendingFragmentActions .add (fragmentGetter .take (1 )
359+ .observeOn (Schedulers .io ())
360+ .subscribe (new Consumer <PdfFragment >() {
361+ @ Override
362+ public void accept (PdfFragment pdfFragment ) {
363+ pdfFragment .exitCurrentlyActiveMode ();
364+ }
365+ }));
342366 }
343367
344368 public void saveCurrentDocument () {
@@ -354,9 +378,19 @@ public void saveCurrentDocument() {
354378 }
355379 }
356380
357- public Single <List <Annotation >> getAnnotations (int pageIndex , @ Nullable String type ) {
358- return fragment .getDocument ().getAnnotationProvider ().getAllAnnotationsOfType (getTypeFromString (type ), pageIndex , 1 )
359- .toList ();
381+ public Single <List <Annotation >> getAnnotations (final int pageIndex , @ Nullable final String type ) {
382+ return fragmentGetter .take (1 ).map (new Function <PdfFragment , PdfDocument >() {
383+
384+ @ Override
385+ public PdfDocument apply (PdfFragment pdfFragment ) {
386+ return pdfFragment .getDocument ();
387+ }
388+ }).flatMap (new Function <PdfDocument , ObservableSource <Annotation >>() {
389+ @ Override
390+ public ObservableSource <Annotation > apply (PdfDocument pdfDocument ) {
391+ return pdfDocument .getAnnotationProvider ().getAllAnnotationsOfType (getTypeFromString (type ), pageIndex , 1 );
392+ }
393+ }).toList ();
360394 }
361395
362396 private EnumSet <AnnotationType > getTypeFromString (@ Nullable String type ) {
@@ -526,7 +560,7 @@ public void accept(FormElement formElement) {
526560 selectedIndices .add (indices .getInt (i ));
527561 }
528562 choiceFormElement .setSelectedIndexes (selectedIndices );
529- }catch (JSONException ex ) {
563+ } catch (JSONException ex ) {
530564 // This isn't an index maybe we can set a custom value on a combobox.
531565 if (formElement instanceof ComboBoxFormElement ) {
532566 ((ComboBoxFormElement ) formElement ).setCustomText (value );
0 commit comments