Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff7425aa authored by Andreas Miko's avatar Andreas Miko
Browse files

Provide onDeactivated method

During refactor I've noticed that many places use try/finally in
onActivated to do cleanup work. This was required before as we had to
return Nothing but this construct is unintuitive.

A onDeactivated method is easy to read and write even without any
knowledge about the Activatable API.

Bug: b/420591935
Test: Refactor only
Flag: com.android.systemui.scene_container
Change-Id: I81feff6a4e992b5f26714dd67f741e1c8842d033
parent 50ba323d
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ abstract class ExclusiveActivatable : Activatable {
            awaitCancellation()
        } finally {
            isActive = false
            onDeactivated()
        }
    }

@@ -50,13 +51,7 @@ abstract class ExclusiveActivatable : Activatable {
     * Notifies that the [Activatable] has been activated.
     *
     * Serves as an entrypoint to kick off coroutine work that the object requires in order to keep
     * its state fresh and/or perform side-effects.
     *
     * The method suspends and doesn't return until all work required by the object is finished. In
     * most cases, it's expected for the work to remain ongoing forever so this method will forever
     * suspend its caller until the coroutine that called it is canceled.
     *
     * Implementations could follow this pattern:
     * its state fresh and/or perform side-effects. Implementations could follow this pattern:
     * ```kotlin
     * override suspend fun onActivated() {
     *     coroutineScope {
@@ -69,5 +64,8 @@ abstract class ExclusiveActivatable : Activatable {
     *
     * @see activate
     */
    protected abstract suspend fun onActivated()
    protected open suspend fun onActivated() {}

    /** Notifies that the [Activatable] has been deactivated. */
    protected open suspend fun onDeactivated() {}
}
+4 −4
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ abstract class HydratedActivatable(
            } finally {
                requestChannel?.cancel()
                requestChannel = null
                onDeactivated()
            }
        }
    }
@@ -69,10 +70,6 @@ abstract class HydratedActivatable(
     * Serves as an entrypoint to kick off coroutine work that the object requires in order to keep
     * its state fresh and/or perform side-effects.
     *
     * The method suspends and doesn't return until all work required by the object is finished. In
     * most cases, it's expected for the work to remain ongoing forever so this method will forever
     * suspend its caller until the coroutine that called it is canceled.
     *
     * Implementations could follow this pattern:
     * ```kotlin
     * override suspend fun onActivated() {
@@ -88,6 +85,9 @@ abstract class HydratedActivatable(
     */
    protected open suspend fun onActivated() {}

    /** Notifies that the [Activatable] has been deactivated. */
    protected open suspend fun onDeactivated() {}

    /**
     * Queues [block] for execution on the activated scope. Requests are executed sequentially.
     *
+6 −9
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.systemui.lifecycle

import kotlinx.coroutines.awaitCancellation

class FakeActivatable(
    private val onActivation: () -> Unit = {},
    private val onDeactivation: () -> Unit = {},
@@ -25,14 +23,13 @@ class FakeActivatable(
    var activationCount = 0
    var cancellationCount = 0

    override suspend fun onActivated(): Nothing {
    override suspend fun onActivated() {
        activationCount++
        onActivation()
        try {
            awaitCancellation()
        } finally {
    }

    override suspend fun onDeactivated() {
        cancellationCount++
        onDeactivation()
    }
}
}
+6 −8
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.systemui.ui.viewmodel

import com.android.systemui.lifecycle.HydratedActivatable
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -40,14 +39,13 @@ class FakeHydratedViewModel(
    fun publicEnqueueOnActivatedScope(runnable: suspend () -> Unit) =
        enqueueOnActivatedScope(runnable)

    override suspend fun onActivated(): Nothing {
    override suspend fun onActivated() {
        activationCount++
        onActivation()
        try {
            awaitCancellation()
        } finally {
    }

    override suspend fun onDeactivated() {
        cancellationCount++
        onDeactivation()
    }
}
}