Skip to content

Commit

Permalink
add internal state saving system to save view state in rotation,...
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrabadiAM committed Aug 11, 2018
1 parent 531a451 commit d070872
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
@@ -1,9 +1,7 @@
package nl.dionsegijn.steppertouchdemo

import android.os.Bundle
import android.support.v4.view.ViewCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import nl.dionsegijn.steppertouch.OnStepCallback
import nl.dionsegijn.steppertouch.StepperTouch
Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/nl/dionsegijn/steppertouch/StepperTouch.kt
Expand Up @@ -6,6 +6,8 @@ import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.os.Parcelable
import android.support.animation.SpringAnimation
import android.support.annotation.ColorRes
import android.support.v4.content.ContextCompat
Expand All @@ -23,6 +25,12 @@ import android.widget.TextView
* Created by dionsegijn on 3/19/17.
*/
class StepperTouch : FrameLayout, OnStepCallback {

object VIEW_RESTORE {
val COUNT_KEY = "count"
val SUPER_STATE_KEY = "superState"
}

constructor(context: Context) : super(context) {
prepareElements()
}
Expand Down Expand Up @@ -106,6 +114,23 @@ class StepperTouch : FrameLayout, OnStepCallback {
addView(viewStepper)
}

public override fun onSaveInstanceState(): Parcelable? {
val superState = super.onSaveInstanceState()
val bundle = Bundle()
bundle.putParcelable(VIEW_RESTORE.SUPER_STATE_KEY, superState)
bundle.putInt(VIEW_RESTORE.COUNT_KEY, stepper.getValue())
return bundle
}

public override fun onRestoreInstanceState(state: Parcelable) {
val bundle: Bundle = state as Bundle
val superState = bundle.getParcelable<Parcelable>(VIEW_RESTORE.SUPER_STATE_KEY)
super.onRestoreInstanceState(superState)

val count = bundle.getInt(VIEW_RESTORE.COUNT_KEY)
stepper.setValue(count)
}

fun enableSideTapForView(textView: View) {
textView.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
Expand Down

0 comments on commit d070872

Please sign in to comment.