Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#52 Do not start indeterminate update loop if the view is detached #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(

// Properties
private var progressAnimator: ValueAnimator? = null
private var indeterminateModeHandler: Handler? = null
private var indeterminateModeHandlerOrNull: Handler? = null
private val indeterminateModeHandler: Handler
get() = indeterminateModeHandlerOrNull
?: Handler().also(::indeterminateModeHandlerOrNull::set)

// View
private var rectF = RectF()
Expand All @@ -36,6 +39,7 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(
isAntiAlias = true
style = Paint.Style.STROKE
}
private var isAttached: Boolean = false

//region Attributes
var progress: Float = 0f
Expand Down Expand Up @@ -139,12 +143,12 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(
progressDirectionIndeterminateMode = ProgressDirection.TO_RIGHT
startAngleIndeterminateMode = DEFAULT_START_ANGLE

indeterminateModeHandler?.removeCallbacks(indeterminateModeRunnable)
progressAnimator?.cancel()
indeterminateModeHandler = Handler()

if (field) {
indeterminateModeHandler?.post(indeterminateModeRunnable)
indeterminateModeHandlerOrNull?.removeCallbacks(indeterminateModeRunnable)
if (value) {
if (isAttached)
indeterminateModeHandler.post(indeterminateModeRunnable)
}
}
var onProgressChangeListener: ((Float) -> Unit)? = null
Expand All @@ -170,7 +174,8 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(

private val indeterminateModeRunnable = Runnable {
if (indeterminateMode) {
postIndeterminateModeHandler()
if (isAttached)
postIndeterminateModeHandler()
// whatever you want to do below
this@CircularProgressBar.progressDirectionIndeterminateMode = this@CircularProgressBar.progressDirectionIndeterminateMode.reverse()
if (this@CircularProgressBar.progressDirectionIndeterminateMode.isToRight()) {
Expand All @@ -182,7 +187,7 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(
}

private fun postIndeterminateModeHandler() {
indeterminateModeHandler?.postDelayed(indeterminateModeRunnable, DEFAULT_ANIMATION_DURATION)
indeterminateModeHandler.postDelayed(indeterminateModeRunnable, DEFAULT_ANIMATION_DURATION)
}
//endregion

Expand Down Expand Up @@ -231,10 +236,17 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View(
attributes.recycle()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
isAttached = true
indeterminateModeRunnable.run()
}

override fun onDetachedFromWindow() {
isAttached = false
super.onDetachedFromWindow()
progressAnimator?.cancel()
indeterminateModeHandler?.removeCallbacks(indeterminateModeRunnable)
indeterminateModeHandlerOrNull?.removeCallbacks(indeterminateModeRunnable)
}

//region Draw Method
Expand Down