Skip to content

Commit

Permalink
Add undoStackCount, isUndoAvailable, isRedoAvailable in PhotoEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
wasky committed Jul 17, 2024
1 parent 0a6c45c commit 807205b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 807205b

Please sign in to comment.