@@ -13,6 +13,7 @@ import com.bumptech.glide.request.RequestOptions
1313import com.simplemobiletools.commons.extensions.getContrastColor
1414import com.simplemobiletools.commons.extensions.toast
1515import com.simplemobiletools.draw.R
16+ import com.simplemobiletools.draw.interfaces.CanvasListener
1617import com.simplemobiletools.draw.models.MyParcelable
1718import com.simplemobiletools.draw.models.MyPath
1819import com.simplemobiletools.draw.models.PaintOptions
@@ -24,7 +25,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
2425
2526 var mPaths = LinkedHashMap <MyPath , PaintOptions >()
2627 var mBackgroundBitmap: Bitmap ? = null
27- var mListener: PathsChangedListener ? = null
28+ var mListener: CanvasListener ? = null
29+
30+ // allow undoing Clear
31+ var mLastPaths = LinkedHashMap <MyPath , PaintOptions >()
32+ var mLastBackgroundBitmap: Bitmap ? = null
2833
2934 private var mPaint = Paint ()
3035 private var mPath = MyPath ()
@@ -53,6 +58,15 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
5358 }
5459
5560 fun undo () {
61+ if (mPaths.isEmpty() && mLastPaths.isNotEmpty()) {
62+ mPaths = mLastPaths.clone() as LinkedHashMap <MyPath , PaintOptions >
63+ mBackgroundBitmap = mLastBackgroundBitmap
64+ mLastPaths.clear()
65+ pathsUpdated()
66+ invalidate()
67+ return
68+ }
69+
5670 if (mPaths.isEmpty())
5771 return
5872
@@ -182,6 +196,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
182196 }
183197
184198 fun clearCanvas () {
199+ mLastPaths = mPaths.clone() as LinkedHashMap <MyPath , PaintOptions >
200+ mLastBackgroundBitmap = mBackgroundBitmap
185201 mBackgroundBitmap = null
186202 mPath.reset()
187203 mPaths.clear()
@@ -219,7 +235,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
219235 }
220236
221237 private fun pathsUpdated () {
222- mListener?.pathsChanged (mPaths.size )
238+ mListener?.toggleUndoVisibility (mPaths.isNotEmpty() || mLastPaths.isNotEmpty() )
223239 }
224240
225241 override fun onTouchEvent (event : MotionEvent ): Boolean {
@@ -240,10 +256,6 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
240256 return true
241257 }
242258
243- interface PathsChangedListener {
244- fun pathsChanged (cnt : Int )
245- }
246-
247259 public override fun onSaveInstanceState (): Parcelable {
248260 val superState = super .onSaveInstanceState()
249261 val savedState = MyParcelable (superState)
0 commit comments