mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
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:
@ -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>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,7 +18,6 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
|||||||
import com.simplemobiletools.draw.pro.R
|
import com.simplemobiletools.draw.pro.R
|
||||||
import com.simplemobiletools.draw.pro.extensions.*
|
import com.simplemobiletools.draw.pro.extensions.*
|
||||||
import com.simplemobiletools.draw.pro.interfaces.CanvasListener
|
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.MyPath
|
||||||
import com.simplemobiletools.draw.pro.models.PaintOptions
|
import com.simplemobiletools.draw.pro.models.PaintOptions
|
||||||
import java.util.concurrent.ExecutionException
|
import java.util.concurrent.ExecutionException
|
||||||
@ -86,21 +85,17 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
updateUndoVisibility()
|
updateUndoVisibility()
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun onSaveInstanceState(): Parcelable {
|
public override fun onSaveInstanceState(): Parcelable? {
|
||||||
val superState = super.onSaveInstanceState()
|
DrawingStateHolder.operations = mOperations
|
||||||
val savedState = MyParcelable(superState!!)
|
return super.onSaveInstanceState()
|
||||||
savedState.operations = mOperations
|
|
||||||
return savedState
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun onRestoreInstanceState(state: Parcelable) {
|
public override fun onRestoreInstanceState(state: Parcelable) {
|
||||||
if (state !is MyParcelable) {
|
val savedOperations = DrawingStateHolder.operations
|
||||||
super.onRestoreInstanceState(state)
|
if (savedOperations != null) {
|
||||||
return
|
mOperations = savedOperations
|
||||||
}
|
}
|
||||||
|
super.onRestoreInstanceState(state)
|
||||||
super.onRestoreInstanceState(state.superState)
|
|
||||||
mOperations = state.operations
|
|
||||||
updateUndoVisibility()
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user