Save drawing operations to a state holder object

This is to avoid throwing `TransactionTooLargeException` when there are a large number of operations
This commit is contained in:
Naveen 2023-03-11 05:57:47 +05:30
parent 0d24e8bfe3
commit befed87c96
2 changed files with 12 additions and 52 deletions

View file

@ -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<MyPath, PaintOptions>()
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<MyParcelable> = object : Parcelable.Creator<MyParcelable> {
override fun createFromParcel(source: Parcel) = MyParcelable(source)
override fun newArray(size: Int) = arrayOf<MyParcelable>()
}
}
}

View file

@ -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<MyPath, PaintOptions>? = null
}