move MyParcelable in a new class

This commit is contained in:
tibbi 2017-11-07 20:31:24 +01:00
parent e9bc0a14f2
commit 97c076f34c
3 changed files with 42 additions and 36 deletions

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.draw
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.graphics.* import android.graphics.*
import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
@ -13,6 +12,7 @@ import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.models.MyParcelable
import com.simplemobiletools.draw.models.MyPath import com.simplemobiletools.draw.models.MyPath
import com.simplemobiletools.draw.models.PaintOptions import com.simplemobiletools.draw.models.PaintOptions
import java.util.* import java.util.*
@ -260,38 +260,4 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
mPaths = state.paths mPaths = state.paths
pathsUpdated() pathsUpdated()
} }
internal class MyParcelable : View.BaseSavedState {
var paths = 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)
paths.put(key, paintOptions)
}
}
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeInt(paths.size)
for ((path, paintOptions) in paths) {
out.writeSerializable(path)
out.writeInt(paintOptions.color)
out.writeFloat(paintOptions.strokeWidth)
out.writeInt(if (paintOptions.isEraser) 1 else 0)
}
}
companion object {
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

@ -21,12 +21,12 @@ import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.draw.BuildConfig import com.simplemobiletools.draw.BuildConfig
import com.simplemobiletools.draw.MyCanvas import com.simplemobiletools.draw.MyCanvas
import com.simplemobiletools.draw.R import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.models.Svg
import com.simplemobiletools.draw.dialogs.SaveImageDialog import com.simplemobiletools.draw.dialogs.SaveImageDialog
import com.simplemobiletools.draw.extensions.config import com.simplemobiletools.draw.extensions.config
import com.simplemobiletools.draw.helpers.JPG import com.simplemobiletools.draw.helpers.JPG
import com.simplemobiletools.draw.helpers.PNG import com.simplemobiletools.draw.helpers.PNG
import com.simplemobiletools.draw.helpers.SVG import com.simplemobiletools.draw.helpers.SVG
import com.simplemobiletools.draw.models.Svg
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File

View File

@ -0,0 +1,40 @@
package com.simplemobiletools.draw.models
import android.os.Parcel
import android.os.Parcelable
import android.view.View
import java.util.*
internal class MyParcelable : View.BaseSavedState {
var paths = 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)
paths.put(key, paintOptions)
}
}
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeInt(paths.size)
for ((path, paintOptions) in paths) {
out.writeSerializable(path)
out.writeInt(paintOptions.color)
out.writeFloat(paintOptions.strokeWidth)
out.writeInt(if (paintOptions.isEraser) 1 else 0)
}
}
companion object {
val CREATOR: Parcelable.Creator<MyParcelable> = object : Parcelable.Creator<MyParcelable> {
override fun createFromParcel(source: Parcel) = MyParcelable(source)
override fun newArray(size: Int) = arrayOf<MyParcelable>()
}
}
}