Skip to content

Commit

Permalink
Fixed problem with restoring view's state
Browse files Browse the repository at this point in the history
Small code optimization
  • Loading branch information
VladimirWrites committed Apr 4, 2018
1 parent 177e984 commit 409d30b
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 260 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
Change Log
==========
Version 1.4.3 *(2018-04-04)*
----------------------------

* Fixed problem with restoring view's state
* Small code optimization

Version 1.4.2 *(2018-01-16)*
----------------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ Add to your module's build.gradle:
and to your app build.gradle:

dependencies {
compile 'com.github.vlad1m1r990:Lemniscate:1.4.2'
compile 'com.github.vlad1m1r990:Lemniscate:1.4.3'
}

Usage
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
@@ -1,11 +1,11 @@
buildscript {
ext.kotlin_version = '1.2.21'
ext.kotlin_version = '1.2.31'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Wed Oct 25 23:03:04 CEST 2017
#Wed Apr 04 19:49:25 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
12 changes: 6 additions & 6 deletions lemniscate/build.gradle
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 142
versionName "1.4.2"
versionCode 143
versionName "1.4.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -65,11 +65,11 @@ task jacocoDebugReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'cre
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.11.0'
testImplementation 'org.mockito:mockito-core:2.17.0'
testImplementation 'com.nhaarman:mockito-kotlin:1.5.0'
testImplementation 'com.google.truth:truth:0.39'
testImplementation 'org.robolectric:robolectric:3.6.1'
implementation 'com.android.support:appcompat-v7:27.0.2'
testImplementation 'com.google.truth:truth:0.40'
testImplementation 'org.robolectric:robolectric:3.8'
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
Expand Down
Expand Up @@ -44,7 +44,6 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
Points())

private var valueAnimator: ValueAnimator? = null
private val interpolator = LinearInterpolator()

constructor(context: Context) : super(context)

Expand Down Expand Up @@ -126,41 +125,22 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
}

private fun animateLemniscate() {
if (valueAnimator != null) valueAnimator!!.end()
valueAnimator = ValueAnimator.ofInt(presenter.curveSettings.precision - 1, 0)
valueAnimator!!.duration = presenter.animationSettings.duration.toLong()
valueAnimator!!.repeatCount = -1
valueAnimator!!.repeatMode = ValueAnimator.RESTART
valueAnimator!!.interpolator = interpolator
valueAnimator!!.addUpdateListener { animation ->
presenter.updateStartingPointOnCurve(animation.animatedValue as Int)
valueAnimator?.end()
valueAnimator = ValueAnimator.ofInt(presenter.curveSettings.precision - 1, 0).apply {
duration = presenter.animationSettings.duration.toLong()
repeatCount = -1
repeatMode = ValueAnimator.RESTART
interpolator = LinearInterpolator()
addUpdateListener { animation ->
presenter.updateStartingPointOnCurve(animation.animatedValue as Int)
}
start()
}
valueAnimator!!.start()
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
valueAnimator!!.end()
}

public override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val ss = BaseCurveSavedState(superState)
ss.curveSettings = this.presenter.curveSettings
ss.animationSettings = this.presenter.animationSettings
return ss
}

public override fun onRestoreInstanceState(state: Parcelable) {
if (state !is BaseCurveSavedState) {
super.onRestoreInstanceState(state)
return
}

super.onRestoreInstanceState(state.superState)

this.presenter.curveSettings = state.curveSettings
this.presenter.animationSettings = state.animationSettings
valueAnimator?.end()
}

var strokeWidth
Expand Down Expand Up @@ -220,15 +200,33 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
presenter.curveSettings.hasHole = hasHole
}

protected class BaseCurveSavedState : View.BaseSavedState {
public override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val ss = BaseCurveSavedState(superState)
ss.curveSettings = this.presenter.curveSettings
ss.animationSettings = this.presenter.animationSettings
return ss
}

public override fun onRestoreInstanceState(state: Parcelable) {
if (state is BaseCurveSavedState) {
super.onRestoreInstanceState(state.superState)
this.presenter.curveSettings = state.curveSettings
this.presenter.animationSettings = state.animationSettings
} else {
super.onRestoreInstanceState(state)
}
}

protected open class BaseCurveSavedState : View.BaseSavedState {
internal lateinit var curveSettings: CurveSettings
internal lateinit var animationSettings: AnimationSettings

constructor(superState: Parcelable) : super(superState)

constructor(`in`: Parcel) : super(`in`) {
this.curveSettings = `in`.readParcelable(CurveSettings::class.java.classLoader)
this.animationSettings = `in`.readParcelable(AnimationSettings::class.java.classLoader)
constructor(state: Parcel) : super(state) {
this.curveSettings = state.readParcelable(CurveSettings::class.java.classLoader)
this.animationSettings = state.readParcelable(AnimationSettings::class.java.classLoader)
}

override fun writeToParcel(out: Parcel, flags: Int) {
Expand All @@ -237,13 +235,16 @@ abstract class BaseCurveProgressView : View, IBaseCurveView {
out.writeParcelable(this.animationSettings, flags)
}

val CREATOR: Parcelable.Creator<BaseCurveSavedState> = object : Parcelable.Creator<BaseCurveSavedState> {
override fun createFromParcel(`in`: Parcel): BaseCurveSavedState {
return BaseCurveSavedState(`in`)
}
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<BaseCurveSavedState> {
override fun createFromParcel(source: Parcel): BaseCurveSavedState {
return BaseCurveSavedState(source)
}

override fun newArray(size: Int): Array<BaseCurveSavedState?> {
return arrayOfNulls(size)
override fun newArray(size: Int): Array<BaseCurveSavedState?> {
return arrayOfNulls(size)
}
}
}
}
Expand Down
Expand Up @@ -36,9 +36,12 @@ class DrawState(val path:Path) {
path.lineTo(start.x, start.y)
} else if (end != null) {
path.moveTo(end.x, end.y)
}
}
}

internal fun isInRightDirectionToBeInHole(start: Point?, end: Point?)
= start != null && end != null && start.x > end.x

fun addPointsToPath(listOfPoints: List<Point>, curveSettings: CurveSettings, viewSize: ViewSize) {
resetPath()

Expand All @@ -53,7 +56,7 @@ class DrawState(val path:Path) {
end = listOfPoints[i + 1]

if (curveSettings.hasHole) {
if (start != null && end != null && start.x > end.x) {
if (isInRightDirectionToBeInHole(start, end)) {
start = CurveUtils.checkPointForHole(start, holeSize, viewSize.size)
end = CurveUtils.checkPointForHole(end, holeSize, viewSize.size)
}
Expand Down
Expand Up @@ -18,7 +18,7 @@ package com.vlad1m1r.lemniscate.base.models
import android.os.Parcel
import android.os.Parcelable

class LineLength : Parcelable {
class LineLength() : Parcelable {

var lineMinLength = 0.4f
set(value) {
Expand All @@ -38,9 +38,7 @@ class LineLength : Parcelable {
}
}

constructor()

internal constructor(`in`: Parcel) {
internal constructor(`in`: Parcel) : this() {
this.lineMinLength = `in`.readFloat()
this.lineMaxLength = `in`.readFloat()
}
Expand Down
Expand Up @@ -16,13 +16,8 @@
package com.vlad1m1r.lemniscate.base.models

class Point(x: Float, y: Float, strokeWidth: Float, viewSize: Float) {
val x: Float
val y: Float

init {
this.x = translateToPositiveCoordinates(x, strokeWidth, viewSize)
this.y = translateToPositiveCoordinates(y, strokeWidth, viewSize)
}
val x: Float = translateToPositiveCoordinates(x, strokeWidth, viewSize)
val y: Float = translateToPositiveCoordinates(y, strokeWidth, viewSize)

private fun compensateForStrokeWidth(coordinate: Float, strokeWidth: Float, viewSize: Float): Float {
val ratio = viewSize / (viewSize + 2 * strokeWidth)
Expand Down
Expand Up @@ -23,8 +23,8 @@ class Points {
val isEmpty: Boolean
get() = points.isEmpty()

fun getPoints(): ArrayList<Point> {
return points.clone() as ArrayList<Point>
fun getPoints(): List<Point> {
return points.toList()
}

fun addPoint(point: Point) {
Expand Down
Expand Up @@ -20,23 +20,22 @@ import android.os.Parcelable

class AnimationSettings(var startingPointOnCurve:Int = 0, var duration: Int = 1000) : Parcelable {

internal constructor(`in`: Parcel) : this() {
this.startingPointOnCurve = `in`.readInt()
this.duration = `in`.readInt()
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readInt())

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(startingPointOnCurve)
parcel.writeInt(duration)
}

override fun describeContents(): Int {
return 0
}

override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeInt(this.startingPointOnCurve)
dest.writeInt(this.duration)
}

companion object CREATOR : Parcelable.Creator<AnimationSettings> {
override fun createFromParcel(source: Parcel): AnimationSettings {
return AnimationSettings(source)
override fun createFromParcel(parcel: Parcel): AnimationSettings {
return AnimationSettings(parcel)
}

override fun newArray(size: Int): Array<AnimationSettings?> {
Expand Down
Expand Up @@ -20,8 +20,6 @@ import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.View

import com.vlad1m1r.lemniscate.base.BaseCurveProgressView
import com.vlad1m1r.lemniscate.roulette.settings.RouletteCurveSettings
import com.vlad1m1r.lemniscate.sample.lemniscate.R
Expand Down Expand Up @@ -91,44 +89,45 @@ abstract class BaseRouletteProgressView : BaseCurveProgressView {
}

override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val ss = RouletteCurveSavedState(superState)
val ss = RouletteCurveSavedState(super.onSaveInstanceState())
ss.rouletteCurveSettings = rouletteCurveSettings
return ss
}

override fun onRestoreInstanceState(state: Parcelable) {
if (state !is RouletteCurveSavedState) {
if (state is RouletteCurveSavedState) {
super.onRestoreInstanceState(state.superState)
this.rouletteCurveSettings = state.rouletteCurveSettings
} else {
super.onRestoreInstanceState(state)
return
}
super.onRestoreInstanceState(state.superState)

this.rouletteCurveSettings = state.rouletteCurveSettings
}

protected class RouletteCurveSavedState : View.BaseSavedState {
protected open class RouletteCurveSavedState : BaseCurveSavedState {

internal lateinit var rouletteCurveSettings: RouletteCurveSettings

constructor(superState: Parcelable) : super(superState)

constructor(`in`: Parcel) : super(`in`) {
this.rouletteCurveSettings = `in`.readParcelable(RouletteCurveSettings::class.java.classLoader)
constructor(source: Parcel) : super(source) {
this.rouletteCurveSettings = source.readParcelable(RouletteCurveSettings::class.java.classLoader)
}

override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeParcelable(this.rouletteCurveSettings, flags)
}

companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<RouletteCurveSavedState> {
override fun createFromParcel(source: Parcel): RouletteCurveSavedState {
return RouletteCurveSavedState(source)
}

val CREATOR: Parcelable.Creator<RouletteCurveSavedState> = object : Parcelable.Creator<RouletteCurveSavedState> {
override fun createFromParcel(`in`: Parcel): RouletteCurveSavedState {
return RouletteCurveSavedState(`in`)
}

override fun newArray(size: Int): Array<RouletteCurveSavedState?> {
return arrayOfNulls(size)
override fun newArray(size: Int): Array<RouletteCurveSavedState?> {
return arrayOfNulls(size)
}
}
}
}
Expand Down

0 comments on commit 409d30b

Please sign in to comment.