fix animations leaks in android app
This commit is contained in:
1 parent
98525f333c
commit
a6ab65fb73
3 files changed
+67
-10
No files matched your search
@@ -27,6 +27,8 @@ class HeroRingOuterView @JvmOverloads constructor(
|
|||||||
private val matrix = Matrix()
|
private val matrix = Matrix()
|
||||||
private var sweepRotationDegrees = 0f
|
private var sweepRotationDegrees = 0f
|
||||||
private var spinAnimator: ValueAnimator? = null
|
private var spinAnimator: ValueAnimator? = null
|
||||||
|
private var spinDurationMs = 0L
|
||||||
|
private var spinDesired = false
|
||||||
private var currentState: State = State.IDLE
|
private var currentState: State = State.IDLE
|
||||||
|
|
||||||
private val accent = ContextCompat.getColor(context, R.color.accent_emerald)
|
private val accent = ContextCompat.getColor(context, R.color.accent_emerald)
|
||||||
@@ -47,28 +49,37 @@ class HeroRingOuterView @JvmOverloads constructor(
|
|||||||
currentState = state
|
currentState = state
|
||||||
when (state) {
|
when (state) {
|
||||||
State.IDLE -> {
|
State.IDLE -> {
|
||||||
stopSpin()
|
|
||||||
visibility = GONE
|
visibility = GONE
|
||||||
setBlur(0f)
|
setBlur(0f)
|
||||||
}
|
}
|
||||||
State.CONNECTING -> {
|
State.CONNECTING -> {
|
||||||
visibility = VISIBLE
|
visibility = VISIBLE
|
||||||
setBlur(2f)
|
setBlur(2f)
|
||||||
startSpin(durationMs = 6_000L)
|
spinDurationMs = 6_000L
|
||||||
}
|
}
|
||||||
State.CONNECTED -> {
|
State.CONNECTED -> {
|
||||||
visibility = VISIBLE
|
visibility = VISIBLE
|
||||||
setBlur(0.5f)
|
setBlur(0.5f)
|
||||||
startSpin(durationMs = 18_000L)
|
spinDurationMs = 18_000L
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
syncSpin()
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startSpin(durationMs: Long) {
|
private fun syncSpin() {
|
||||||
spinAnimator?.cancel()
|
val shouldRun = spinDesired && currentState != State.IDLE && isAttachedToWindow
|
||||||
spinAnimator = ValueAnimator.ofFloat(0f, 360f).apply {
|
if (shouldRun) startSpin() else stopSpin()
|
||||||
duration = durationMs
|
}
|
||||||
|
|
||||||
|
private fun startSpin() {
|
||||||
|
if (!isAttachedToWindow) return
|
||||||
|
val existing = spinAnimator
|
||||||
|
if (existing != null && existing.duration == spinDurationMs) return
|
||||||
|
existing?.cancel()
|
||||||
|
val startDegrees = sweepRotationDegrees % 360f
|
||||||
|
spinAnimator = ValueAnimator.ofFloat(startDegrees, startDegrees + 360f).apply {
|
||||||
|
duration = spinDurationMs
|
||||||
repeatCount = ValueAnimator.INFINITE
|
repeatCount = ValueAnimator.INFINITE
|
||||||
interpolator = LinearInterpolator()
|
interpolator = LinearInterpolator()
|
||||||
addUpdateListener {
|
addUpdateListener {
|
||||||
@@ -82,7 +93,6 @@ class HeroRingOuterView @JvmOverloads constructor(
|
|||||||
private fun stopSpin() {
|
private fun stopSpin() {
|
||||||
spinAnimator?.cancel()
|
spinAnimator?.cancel()
|
||||||
spinAnimator = null
|
spinAnimator = null
|
||||||
sweepRotationDegrees = 0f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setBlur(radiusPx: Float) {
|
private fun setBlur(radiusPx: Float) {
|
||||||
@@ -94,10 +104,26 @@ class HeroRingOuterView @JvmOverloads constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun pauseAnimation() {
|
||||||
|
spinDesired = false
|
||||||
|
syncSpin()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resumeAnimation() {
|
||||||
|
spinDesired = true
|
||||||
|
syncSpin()
|
||||||
|
}
|
||||||
|
|
||||||
fun release() {
|
fun release() {
|
||||||
|
spinDesired = false
|
||||||
stopSpin()
|
stopSpin()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onAttachedToWindow() {
|
||||||
|
super.onAttachedToWindow()
|
||||||
|
syncSpin()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDetachedFromWindow() {
|
override fun onDetachedFromWindow() {
|
||||||
stopSpin()
|
stopSpin()
|
||||||
super.onDetachedFromWindow()
|
super.onDetachedFromWindow()
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
|
|||||||
|
|
||||||
container.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
|
container.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
|
||||||
container.bindHero(connected = isHostConnected(), status = hostStatus())
|
container.bindHero(connected = isHostConnected(), status = hostStatus())
|
||||||
|
if (!isResumed) container.pauseAnimations()
|
||||||
|
|
||||||
container.onAddCallClicked = {
|
container.onAddCallClicked = {
|
||||||
AddDestinationSheet.show(parentFragmentManager)
|
AddDestinationSheet.show(parentFragmentManager)
|
||||||
@@ -73,6 +74,17 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
|
|||||||
super.onResume()
|
super.onResume()
|
||||||
content?.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
|
content?.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
|
||||||
content?.bindHero(connected = isHostConnected(), status = hostStatus())
|
content?.bindHero(connected = isHostConnected(), status = hostStatus())
|
||||||
|
content?.resumeAnimations()
|
||||||
|
if (isHostConnected()) {
|
||||||
|
tickHandler.removeCallbacks(tickRunnable)
|
||||||
|
tickHandler.postDelayed(tickRunnable, 1000L)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
content?.pauseAnimations()
|
||||||
|
tickHandler.removeCallbacks(tickRunnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
@@ -97,14 +109,18 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun onConnectedChanged(connected: Boolean) {
|
fun onConnectedChanged(connected: Boolean) {
|
||||||
content?.bindHero(connected = connected, status = hostStatus())
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
if (connectedSinceMs == 0L) connectedSinceMs = System.currentTimeMillis()
|
if (connectedSinceMs == 0L) connectedSinceMs = System.currentTimeMillis()
|
||||||
|
} else {
|
||||||
|
connectedSinceMs = 0L
|
||||||
|
}
|
||||||
|
if (!isResumed) return
|
||||||
|
content?.bindHero(connected = connected, status = hostStatus())
|
||||||
|
if (connected) {
|
||||||
refreshStats()
|
refreshStats()
|
||||||
tickHandler.removeCallbacks(tickRunnable)
|
tickHandler.removeCallbacks(tickRunnable)
|
||||||
tickHandler.postDelayed(tickRunnable, 1000L)
|
tickHandler.postDelayed(tickRunnable, 1000L)
|
||||||
} else {
|
} else {
|
||||||
connectedSinceMs = 0L
|
|
||||||
tickHandler.removeCallbacks(tickRunnable)
|
tickHandler.removeCallbacks(tickRunnable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ class MainFragmentView(private val root: View) {
|
|||||||
var onCallLongPressed: ParamCallback<CallConfig>? = null
|
var onCallLongPressed: ParamCallback<CallConfig>? = null
|
||||||
|
|
||||||
private var pulseAnimator: ValueAnimator? = null
|
private var pulseAnimator: ValueAnimator? = null
|
||||||
|
private var pulseRunning: Boolean = false
|
||||||
private var collapsedToActive: Boolean = false
|
private var collapsedToActive: Boolean = false
|
||||||
private var currentCalls: List<CallConfig> = emptyList()
|
private var currentCalls: List<CallConfig> = emptyList()
|
||||||
private var activeCallId: String = ""
|
private var activeCallId: String = ""
|
||||||
@@ -199,6 +200,18 @@ class MainFragmentView(private val root: View) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun pauseAnimations() {
|
||||||
|
heroRingOuter.pauseAnimation()
|
||||||
|
pulseAnimator?.cancel()
|
||||||
|
pulseAnimator = null
|
||||||
|
pingButton.clearAnimation()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resumeAnimations() {
|
||||||
|
heroRingOuter.resumeAnimation()
|
||||||
|
if (pulseRunning) startPulse()
|
||||||
|
}
|
||||||
|
|
||||||
fun detach() {
|
fun detach() {
|
||||||
heroRingOuter.release()
|
heroRingOuter.release()
|
||||||
stopPulse()
|
stopPulse()
|
||||||
@@ -269,6 +282,7 @@ class MainFragmentView(private val root: View) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startPulse() {
|
private fun startPulse() {
|
||||||
|
pulseRunning = true
|
||||||
if (pulseAnimator?.isStarted == true) return
|
if (pulseAnimator?.isStarted == true) return
|
||||||
heroPulse.visibility = View.VISIBLE
|
heroPulse.visibility = View.VISIBLE
|
||||||
heroPulse.scaleX = 1f
|
heroPulse.scaleX = 1f
|
||||||
@@ -290,6 +304,7 @@ class MainFragmentView(private val root: View) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopPulse() {
|
private fun stopPulse() {
|
||||||
|
pulseRunning = false
|
||||||
pulseAnimator?.cancel()
|
pulseAnimator?.cancel()
|
||||||
pulseAnimator = null
|
pulseAnimator = null
|
||||||
heroPulse.visibility = View.GONE
|
heroPulse.visibility = View.GONE
|
||||||
|
|||||||
Reference in new issue
Block a user