From 807205b93a7c8a7ff58aa28f3bb9d6a1d2e9978a Mon Sep 17 00:00:00 2001 From: Michal Wolski Date: Wed, 17 Jul 2024 15:10:15 +0200 Subject: [PATCH] Add undoStackCount, isUndoAvailable, isRedoAvailable in PhotoEditor --- .../photoediting/EditImageActivity.kt | 12 +++++------ .../burhanrashid52/photoeditor/PhotoEditor.kt | 21 +++++++++++++++++++ .../photoeditor/PhotoEditorImpl.kt | 7 ++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/burhanrashid52/photoediting/EditImageActivity.kt b/app/src/main/java/com/burhanrashid52/photoediting/EditImageActivity.kt index 06a43a2f..48869ced 100644 --- a/app/src/main/java/com/burhanrashid52/photoediting/EditImageActivity.kt +++ b/app/src/main/java/com/burhanrashid52/photoediting/EditImageActivity.kt @@ -206,8 +206,8 @@ class EditImageActivity : BaseActivity(), OnPhotoEditorListener, View.OnClickLis "onAddViewListener() called with: viewType = [$viewType], numberOfAddedViews = [$numberOfAddedViews]" ) - mImgUndo.isEnabled = !mPhotoEditor.isCacheEmpty - mImgRedo.isEnabled = mPhotoEditor.redoStackCount > 0 + mImgUndo.isEnabled = mPhotoEditor.isUndoAvailable + mImgRedo.isEnabled = mPhotoEditor.isRedoAvailable } override fun onRemoveViewListener(viewType: ViewType, numberOfAddedViews: Int) { @@ -216,8 +216,8 @@ class EditImageActivity : BaseActivity(), OnPhotoEditorListener, View.OnClickLis "onRemoveViewListener() called with: viewType = [$viewType], numberOfAddedViews = [$numberOfAddedViews]" ) - mImgUndo.isEnabled = !mPhotoEditor.isCacheEmpty - mImgRedo.isEnabled = mPhotoEditor.redoStackCount > 0 + mImgUndo.isEnabled = mPhotoEditor.isUndoAvailable + mImgRedo.isEnabled = mPhotoEditor.isRedoAvailable } override fun onStartViewChangeListener(viewType: ViewType) { @@ -237,11 +237,11 @@ class EditImageActivity : BaseActivity(), OnPhotoEditorListener, View.OnClickLis when (view.id) { R.id.imgUndo -> { mImgUndo.isEnabled = mPhotoEditor.undo() - mImgRedo.isEnabled = mPhotoEditor.redoStackCount > 0 + mImgRedo.isEnabled = mPhotoEditor.isRedoAvailable } R.id.imgRedo -> { - mImgUndo.isEnabled = !mPhotoEditor.isCacheEmpty + mImgUndo.isEnabled = mPhotoEditor.isUndoAvailable mImgRedo.isEnabled = mPhotoEditor.redo() } diff --git a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.kt b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.kt index a12e7c2f..587a0762 100644 --- a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.kt +++ b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.kt @@ -186,6 +186,20 @@ interface PhotoEditor { */ fun undo(): Boolean + /** + * How many undo operations are available. + * + * @return size of undo stack + */ + val undoStackCount: Int + + /** + * Returns whether any undo operation is available. + * + * @return `true` if no undo operations available, `false` otherwise + */ + val isUndoAvailable get() = undoStackCount > 0 + /** * Redo the last operation perform on the [PhotoEditor] * @@ -200,6 +214,13 @@ interface PhotoEditor { */ val redoStackCount: Int + /** + * Returns whether any redo operation is available. + * + * @return `true` if no redo operations available, `false` otherwise + */ + val isRedoAvailable get() = redoStackCount > 0 + /** * Removes all the edited operations performed [PhotoEditorView] * This will also clear the undo and redo stack diff --git a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorImpl.kt b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorImpl.kt index b567d302..37604ee5 100644 --- a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorImpl.kt +++ b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorImpl.kt @@ -188,12 +188,13 @@ internal class PhotoEditorImpl @SuppressLint("ClickableViewAccessibility") const return mGraphicManager.undoView() } + override val undoStackCount: Int get() = viewState.addedViewsCount + override fun redo(): Boolean { return mGraphicManager.redoView() } - override val redoStackCount: Int - get() = mGraphicManager.redoStackCount + override val redoStackCount: Int get() = mGraphicManager.redoStackCount override fun clearAllViews() { mBoxHelper.clearAllViews(drawingView) @@ -266,7 +267,7 @@ internal class PhotoEditorImpl @SuppressLint("ClickableViewAccessibility") const } override val isCacheEmpty: Boolean - get() = viewState.addedViewsCount == 0 && viewState.redoViewsCount == 0 + get() = !isUndoAvailable && !isRedoAvailable // region Shape override fun setShape(shapeBuilder: ShapeBuilder) {