From befed87c9669a8a266a246e292a27bb7b77f0224 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 11 Mar 2023 05:57:47 +0530 Subject: [PATCH] Save drawing operations to a state holder object This is to avoid throwing `TransactionTooLargeException` when there are a large number of operations --- .../draw/pro/models/MyParcelable.kt | 40 ------------------- .../draw/pro/views/MyCanvas.kt | 24 +++++------ 2 files changed, 12 insertions(+), 52 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/draw/pro/models/MyParcelable.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/pro/models/MyParcelable.kt b/app/src/main/kotlin/com/simplemobiletools/draw/pro/models/MyParcelable.kt deleted file mode 100644 index 6c94d98..0000000 --- a/app/src/main/kotlin/com/simplemobiletools/draw/pro/models/MyParcelable.kt +++ /dev/null @@ -1,40 +0,0 @@ -package com.simplemobiletools.draw.pro.models - -import android.os.Parcel -import android.os.Parcelable -import android.view.View - -internal class MyParcelable : View.BaseSavedState { - var operations = LinkedHashMap() - - constructor(superState: Parcelable) : super(superState) - - constructor(parcel: Parcel) : super(parcel) { - val size = parcel.readInt() - for (i in 0 until size) { - val key = parcel.readSerializable() as MyPath - val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat(), parcel.readInt() == 1) - operations[key] = paintOptions - } - } - - override fun writeToParcel(out: Parcel, flags: Int) { - super.writeToParcel(out, flags) - out.writeInt(operations.size) - for ((path, paintOptions) in operations) { - out.writeSerializable(path) - out.writeInt(paintOptions.color) - out.writeFloat(paintOptions.strokeWidth) - out.writeInt(if (paintOptions.isEraser) 1 else 0) - } - } - - companion object { - @JvmField - val CREATOR: Parcelable.Creator = object : Parcelable.Creator { - override fun createFromParcel(source: Parcel) = MyParcelable(source) - - override fun newArray(size: Int) = arrayOf() - } - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt b/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt index 0c26a04..6eb835a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt @@ -18,7 +18,6 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.draw.pro.R import com.simplemobiletools.draw.pro.extensions.* import com.simplemobiletools.draw.pro.interfaces.CanvasListener -import com.simplemobiletools.draw.pro.models.MyParcelable import com.simplemobiletools.draw.pro.models.MyPath import com.simplemobiletools.draw.pro.models.PaintOptions import java.util.concurrent.ExecutionException @@ -86,21 +85,17 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { updateUndoVisibility() } - public override fun onSaveInstanceState(): Parcelable { - val superState = super.onSaveInstanceState() - val savedState = MyParcelable(superState!!) - savedState.operations = mOperations - return savedState + public override fun onSaveInstanceState(): Parcelable? { + DrawingStateHolder.operations = mOperations + return super.onSaveInstanceState() } public override fun onRestoreInstanceState(state: Parcelable) { - if (state !is MyParcelable) { - super.onRestoreInstanceState(state) - return + val savedOperations = DrawingStateHolder.operations + if (savedOperations != null) { + mOperations = savedOperations } - - super.onRestoreInstanceState(state.superState) - mOperations = state.operations + super.onRestoreInstanceState(state) updateUndoVisibility() } @@ -450,3 +445,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { } } } + +// since we don't use view models, this serves as a simple state holder to save drawing operations +object DrawingStateHolder { + var operations: LinkedHashMap? = null +}