2626import androidx .recyclerview .widget .RecyclerView ;
2727
2828import com .compdfkit .core .annotation .CPDFAnnotation ;
29- import com .compdfkit .core .undo .CPDFUndoManager ;
3029import com .compdfkit .tools .R ;
3130import com .compdfkit .tools .annotation .pdfannotationbar .adapter .CPDFAnnotationToolListAdapter ;
3231import com .compdfkit .tools .annotation .pdfannotationbar .bean .CAnnotToolBean ;
3332import com .compdfkit .tools .annotation .pdfannotationbar .data .CAnnotationToolDatas ;
3433import com .compdfkit .tools .common .interfaces .COnAnnotationChangeListener ;
3534import com .compdfkit .tools .common .interfaces .COnAnnotationCreatePreparedListener ;
3635import com .compdfkit .tools .common .pdf .config .AnnotationsConfig ;
36+ import com .compdfkit .tools .common .pdf .undo .InkUndoRedoCoordinator ;
3737import com .compdfkit .tools .common .utils .CListUtil ;
3838import com .compdfkit .tools .common .utils .CLog ;
3939import com .compdfkit .tools .common .utils .annotation .CPDFAnnotationManager ;
@@ -105,13 +105,13 @@ public class CAnnotationToolbar extends FrameLayout {
105105
106106 private boolean listeningSelectAnnot = false ;
107107
108- public enum UndoManagerType {
109- AnnotUndo ,
108+ private boolean isInkEditing ;
110109
111- InkUndo
112- }
110+ @ Nullable
111+ private InkUndoRedoCoordinator undoRedoCoordinator ;
113112
114- private UndoManagerType undoManagerType = UndoManagerType .AnnotUndo ;
113+ private final InkUndoRedoCoordinator .OnStateChangedListener undoRedoStateListener =
114+ this ::updateUndoRedoButtons ;
115115
116116 public CAnnotationToolbar (@ NonNull Context context ) {
117117 this (context , null );
@@ -149,6 +149,8 @@ private void initListener() {
149149
150150 public void initWithPDFView (CPDFViewCtrl pdfView ) {
151151 this .pdfView = pdfView ;
152+ undoRedoCoordinator = InkUndoRedoCoordinator .get (pdfView .getCPdfReaderView ());
153+ undoRedoCoordinator .addOnStateChangedListener (undoRedoStateListener );
152154 toolListAdapter .setList (CAnnotationToolDatas .getAnnotationList (pdfView ));
153155 setupSelectedAnnotationCallbacks ();
154156 this .pdfView .addOnPDFFocusedTypeChangeListener (type -> {
@@ -161,14 +163,14 @@ public void initWithPDFView(CPDFViewCtrl pdfView) {
161163 updateSettingButtonState ();
162164 }
163165 if (toolListAdapter .getCurrentAnnotType () != CAnnotationType .INK_ERASER ) {
164- setUndoManagerType ( UndoManagerType . AnnotUndo );
166+ setInkEditing ( false );
165167 }
166168 }else if (type == CPDFAnnotation .Type .INK ){
167169 if (toolListAdapter .getCurrentAnnotType () == CAnnotationType .INK ) {
168- setUndoManagerType ( UndoManagerType . InkUndo );
170+ setInkEditing ( true );
169171 }
170172 }else {
171- setUndoManagerType ( UndoManagerType . AnnotUndo );
173+ setInkEditing ( false );
172174 }
173175 });
174176 syncSelectAnnotListenerWithViewMode (pdfView .getCPdfReaderView ().getViewMode ());
@@ -274,26 +276,33 @@ private void updateSettingButtonState() {
274276 || toolListAdapter .annotEnableSetting ());
275277 }
276278 }
277- private void setUndoManagerType (UndoManagerType undoManagerType ) {
278- this .undoManagerType = undoManagerType ;
279- if (undoManagerType == UndoManagerType .InkUndo ) {
280- CPDFReaderView .TInkDrawHelper helper = pdfView .getCPdfReaderView ().getInkDrawHelper ();
281- if (ivUndo != null ) {
282- ivUndo .setEnabled (helper .canUndo ());
283- }
284- if (ivRedo != null ) {
285- ivRedo .setEnabled (helper .canRedo ());
286- }
287- } else {
288- CPDFUndoManager undoManager = pdfView .getCPdfReaderView ().getUndoManager ();
289- if (ivRedo != null ) {
290- ivRedo .setEnabled (undoManager .canRedo ());
291- }
292- if (ivUndo != null ) {
293- ivUndo .setEnabled (undoManager .canUndo ());
294- }
279+ private void setInkEditing (boolean isInkEditing ) {
280+ this .isInkEditing = isInkEditing ;
281+ updateUndoRedoButtons ();
282+ }
283+
284+ private void updateUndoRedoButtons () {
285+ if (pdfView == null ) {
286+ return ;
295287 }
288+ if (undoRedoCoordinator == null ) {
289+ return ;
290+ }
291+ if (ivUndo != null ) {
292+ ivUndo .setEnabled (undoRedoCoordinator .canUndo (getInkUndoRedoMode (), isInkEditing ));
293+ }
294+ if (ivRedo != null ) {
295+ ivRedo .setEnabled (undoRedoCoordinator .canRedo (getInkUndoRedoMode (), isInkEditing ));
296+ }
297+ }
296298
299+ private AnnotationsConfig .InkUndoRedoMode getInkUndoRedoMode () {
300+ if (pdfView == null || pdfView .getCPDFConfiguration () == null
301+ || pdfView .getCPDFConfiguration ().annotationsConfig == null
302+ || pdfView .getCPDFConfiguration ().annotationsConfig .inkUndoRedoMode == null ) {
303+ return AnnotationsConfig .InkUndoRedoMode .HYBRID ;
304+ }
305+ return pdfView .getCPDFConfiguration ().annotationsConfig .inkUndoRedoMode ;
297306 }
298307
299308 private void showAnnotStyleDialog () {
@@ -364,13 +373,14 @@ private boolean hasAnnotationStyleSelection(CAnnotStyle style) {
364373
365374 public void switchAnnotationUnknown (){
366375 if (toolListAdapter .getCurrentAnnotType () == CAnnotationType .INK_ERASER ) {
367- setUndoManagerType ( UndoManagerType . AnnotUndo );
376+ setInkEditing ( false );
368377 }
369378 toolListAdapter .selectByType (CAnnotationType .UNKNOWN );
370379 clearSelectedAnnotationStyleTarget ();
371380 updateSettingButtonState ();
372381 pdfView .resetAnnotationType ();
373382 pdfView .getCPdfReaderView ().getInkDrawHelper ().onSave ();
383+ invalidateRedoHistory ();
374384 pdfView .getCPdfReaderView ().getInkDrawHelper ().setMode (Mode .DRAW );
375385 annotationChangeListenersChanged (CAnnotationType .UNKNOWN );
376386 }
@@ -385,6 +395,7 @@ public void switchAnnotationType(CAnnotationType type) {
385395 updateSettingButtonState ();
386396 AnnotationsConfig annotationsConfig = pdfView .getCPDFConfiguration ().annotationsConfig ;
387397 pdfView .getCPdfReaderView ().getInkDrawHelper ().onSave ();
398+ invalidateRedoHistory ();
388399 pdfView .getCPdfReaderView ().removeAllAnnotFocus ();
389400 switch (type ) {
390401 case TEXT :
@@ -396,7 +407,7 @@ public void switchAnnotationType(CAnnotationType type) {
396407 pdfView .getCPdfReaderView ().getInkDrawHelper ().setEffect (IInkDrawCallback .Effect .NORMAL );
397408 break ;
398409 case INK_ERASER :
399- setUndoManagerType ( UndoManagerType . AnnotUndo );
410+ setInkEditing ( false );
400411 pdfView .resetAnnotationType ();
401412 pdfView .getCPdfReaderView ().setTouchMode (CPDFReaderView .TouchMode .ERASE_INK );
402413 break ;
@@ -457,87 +468,31 @@ public void annotationCreatePreparedListenersChanged(CAnnotationType type,CPDFAn
457468 }
458469
459470 private void redoUndoManager () {
460- pdfView .getCPdfReaderView ().getUndoManager ().enable (true );
461- CPDFUndoManager undoManager = pdfView .getCPdfReaderView ().getUndoManager ();
462- if (ivRedo != null ) {
463- ivRedo .setEnabled (undoManager .canRedo ());
471+ if (undoRedoCoordinator == null ) {
472+ undoRedoCoordinator = InkUndoRedoCoordinator .get (pdfView .getCPdfReaderView ());
473+ undoRedoCoordinator .addOnStateChangedListener (undoRedoStateListener );
464474 }
465- if (ivUndo != null ) {
466- ivUndo .setEnabled (undoManager .canUndo ());
467- }
468- pdfView .getCPdfReaderView ().getUndoManager ().addOnUndoHistoryChangeListener ((cpdfUndoManager , operation , type ) -> {
469- if (undoManagerType == UndoManagerType .AnnotUndo ){
470- boolean canUndo = cpdfUndoManager .canUndo ();
471- boolean canRedo = cpdfUndoManager .canRedo ();
472- if (ivUndo != null ) {
473- ivUndo .setEnabled (canUndo );
474- }
475- if (ivRedo != null ) {
476- ivRedo .setEnabled (canRedo );
477- }
478- }
479- });
480-
481- CPDFReaderView .TInkDrawHelper helper = pdfView .getCPdfReaderView ().getInkDrawHelper ();
482- helper .setInkUndoRedoCallback ((undo , redo ) -> {
483- if (undoManagerType == UndoManagerType .InkUndo ) {
484- if (ivUndo != null ) {
485- ivUndo .setEnabled (undo );
486- }
487- if (ivRedo != null ) {
488- ivRedo .setEnabled (redo );
489- }
490- }
491- });
475+ updateUndoRedoButtons ();
492476 }
493477
494478 public void undo () {
495- if (undoManagerType == UndoManagerType .InkUndo ) {
496- inkUndo ();
497- }else {
498- annotUndo ();
499- }
500- }
501-
502- public void redo () {
503- if (undoManagerType == UndoManagerType .InkUndo ) {
504- inkRedo ();
505- }else {
506- annotRedo ();
507- }
508- }
509-
510- public void annotUndo (){
511- try {
512- CPDFUndoManager undoManager = pdfView .getCPdfReaderView ().getUndoManager ();
513- if (undoManager .canUndo ()) {
514- undoManager .undo ();
515- }
516- } catch (Exception e ) {
479+ if (pdfView == null ) {
480+ return ;
517481 }
518- }
519-
520- public void annotRedo () {
521- try {
522- CPDFUndoManager undoManager = pdfView .getCPdfReaderView ().getUndoManager ();
523- if (undoManager .canRedo ()) {
524- undoManager .redo ();
525- }
526- } catch (Exception e ) {
527-
482+ if (undoRedoCoordinator != null ) {
483+ undoRedoCoordinator .undo (getInkUndoRedoMode (), isInkEditing );
528484 }
485+ updateUndoRedoButtons ();
529486 }
530487
531- private void inkUndo () {
532- if (pdfView ! = null ) {
533- pdfView . getCPdfReaderView (). getInkDrawHelper (). onUndo () ;
488+ public void redo () {
489+ if (pdfView = = null ) {
490+ return ;
534491 }
535- }
536-
537- private void inkRedo () {
538- if (pdfView != null ) {
539- pdfView .getCPdfReaderView ().getInkDrawHelper ().onRedo ();
492+ if (undoRedoCoordinator != null ) {
493+ undoRedoCoordinator .redo (getInkUndoRedoMode (), isInkEditing );
540494 }
495+ updateUndoRedoButtons ();
541496 }
542497
543498 public void updateItemColor () {
@@ -617,10 +572,17 @@ private void saveInk() {
617572 if (readerView .getViewMode () == ViewMode .ANNOT ){
618573 if (readerView .getCurrentFocusedType () == CPDFAnnotation .Type .INK ){
619574 readerView .getInkDrawHelper ().onSave ();
575+ invalidateRedoHistory ();
620576 }
621577 }
622578 }
623579
580+ private void invalidateRedoHistory () {
581+ if (undoRedoCoordinator != null ) {
582+ undoRedoCoordinator .invalidateRedoHistory ();
583+ }
584+ }
585+
624586 public AppCompatImageView getSettingButton () {
625587 return ivSetting ;
626588 }
@@ -716,6 +678,10 @@ public void release() {
716678 pdfView .removeOnPDFViewModeChangeListener (viewModeChangedListener );
717679 }
718680 viewModeChangedListener = null ;
681+ if (undoRedoCoordinator != null ) {
682+ undoRedoCoordinator .removeOnStateChangedListener (undoRedoStateListener );
683+ undoRedoCoordinator = null ;
684+ }
719685 clearSelectedAnnotationStyleTarget ();
720686 updateSettingButtonState ();
721687 }
0 commit comments