fix animations leaks in android app

This commit is contained in:
kulikov0 committed 2026-05-30 12:24:28 +02:00
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 var sweepRotationDegrees = 0f
private var spinAnimator: ValueAnimator? = null
private var spinDurationMs = 0L
private var spinDesired = false
private var currentState: State = State.IDLE
private val accent = ContextCompat.getColor(context, R.color.accent_emerald)
@@ -47,28 +49,37 @@ class HeroRingOuterView @JvmOverloads constructor(
currentState = state
when (state) {
State.IDLE -> {
stopSpin()
visibility = GONE
setBlur(0f)
}
State.CONNECTING -> {
visibility = VISIBLE
setBlur(2f)
startSpin(durationMs = 6_000L)
spinDurationMs = 6_000L
}
State.CONNECTED -> {
visibility = VISIBLE
setBlur(0.5f)
startSpin(durationMs = 18_000L)
spinDurationMs = 18_000L
}
}
syncSpin()
invalidate()
}
private fun startSpin(durationMs: Long) {
spinAnimator?.cancel()
spinAnimator = ValueAnimator.ofFloat(0f, 360f).apply {
duration = durationMs
private fun syncSpin() {
val shouldRun = spinDesired && currentState != State.IDLE && isAttachedToWindow
if (shouldRun) startSpin() else stopSpin()
}
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
interpolator = LinearInterpolator()
addUpdateListener {
@@ -82,7 +93,6 @@ class HeroRingOuterView @JvmOverloads constructor(
private fun stopSpin() {
spinAnimator?.cancel()
spinAnimator = null
sweepRotationDegrees = 0f
}
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() {
spinDesired = false
stopSpin()
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
syncSpin()
}
override fun onDetachedFromWindow() {
stopSpin()
super.onDetachedFromWindow()
@@ -39,6 +39,7 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
container.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
container.bindHero(connected = isHostConnected(), status = hostStatus())
if (!isResumed) container.pauseAnimations()
container.onAddCallClicked = {
AddDestinationSheet.show(parentFragmentManager)
@@ -73,6 +74,17 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
super.onResume()
content?.bindCalls(Prefs.savedDestinations, Prefs.activeDestinationId)
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() {
@@ -97,14 +109,18 @@ class MainFragment : Fragment(R.layout.fragment_main_screen) {
}
fun onConnectedChanged(connected: Boolean) {
content?.bindHero(connected = connected, status = hostStatus())
if (connected) {
if (connectedSinceMs == 0L) connectedSinceMs = System.currentTimeMillis()
} else {
connectedSinceMs = 0L
}
if (!isResumed) return
content?.bindHero(connected = connected, status = hostStatus())
if (connected) {
refreshStats()
tickHandler.removeCallbacks(tickRunnable)
tickHandler.postDelayed(tickRunnable, 1000L)
} else {
connectedSinceMs = 0L
tickHandler.removeCallbacks(tickRunnable)
}
}
@@ -49,6 +49,7 @@ class MainFragmentView(private val root: View) {
var onCallLongPressed: ParamCallback<CallConfig>? = null
private var pulseAnimator: ValueAnimator? = null
private var pulseRunning: Boolean = false
private var collapsedToActive: Boolean = false
private var currentCalls: List<CallConfig> = emptyList()
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() {
heroRingOuter.release()
stopPulse()
@@ -269,6 +282,7 @@ class MainFragmentView(private val root: View) {
}
private fun startPulse() {
pulseRunning = true
if (pulseAnimator?.isStarted == true) return
heroPulse.visibility = View.VISIBLE
heroPulse.scaleX = 1f
@@ -290,6 +304,7 @@ class MainFragmentView(private val root: View) {
}
private fun stopPulse() {
pulseRunning = false
pulseAnimator?.cancel()
pulseAnimator = null
heroPulse.visibility = View.GONE