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

Commit b7bf17a8 authored by Chris Göllner's avatar Chris Göllner Committed by Chris Göllner
Browse files

Add PerDisplayRepository.getOrDefault

Helper that returns the instance of the default display, when the
specified display doesn't exist.

Test: PerDisplayInstanceRepositoryImplTest
Bug: 420886625
Flag: EXEMPT Trivial
Change-Id: I3bbcc7af58a4492a0c24b5051a5e5434eeea5fdc
parent 5cc433e3
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -89,6 +89,25 @@ interface PerDisplayRepository<T> {
    /** Gets the cached instance or create a new one for a given display. */
    operator fun get(displayId: Int): T?

    /**
     * Gets the cached instance or create a new one for a given display. If the given display
     * doesn't exist, returns an instance for the default display.
     */
    fun getOrDefault(displayId: Int): T {
        val instance = get(displayId)
        if (instance == null) {
            Log.e(
                "PerDisplayRepository",
                """<$debugName> getOrDefault: instance for display with id $displayId returned 
                    |null. The display likely doesn't exist anymore. Returning an instance for the 
                    |default display."""
                    .trimMargin(),
            )
            return get(DEFAULT_DISPLAY)!!
        }
        return instance
    }

    /** Debug name for this repository, mainly for tracing and logging. */
    val debugName: String