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

Commit 70e5bd79 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Provide lifecycleScope for PreferenceLifecycleContext

Bug: 332201912
Flag: EXEMPT library
Test: manual
Change-Id: Ica995acd55aabd54a22b43133590dfa37d349b75
parent 72b68a82
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.CoroutineScope

/**
 * Interface to provide dynamic preference title.
@@ -138,6 +140,13 @@ interface PreferenceLifecycleProvider {
 */
abstract class PreferenceLifecycleContext(context: Context) : ContextWrapper(context) {

    /**
     * [CoroutineScope] tied to the lifecycle, which is cancelled when the lifecycle is destroyed.
     *
     * @see [androidx.lifecycle.lifecycleScope]
     */
    abstract val lifecycleScope: LifecycleCoroutineScope

    /** Returns the preference widget object associated with given key. */
    abstract fun <T> findPreference(key: String): T?

+5 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.settingslib.preference
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceDataStore
import androidx.preference.PreferenceGroup
@@ -57,6 +59,9 @@ class PreferenceScreenBindingHelper(

    private val preferenceLifecycleContext =
        object : PreferenceLifecycleContext(context) {
            override val lifecycleScope: LifecycleCoroutineScope
                get() = fragment.lifecycleScope

            override fun <T> findPreference(key: String) =
                preferenceScreen.findPreference(key) as T?