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

Commit 88d3864b authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add default AppListModel filter and getSummary

To simply some simple usage case and unit test.

Also add Flow<List<T>>.mapItem(), we could use mapItem() instead of
asyncMapItem() when async is not need.

Bug: 260660819
Test: Make Settings
Change-Id: I958aeaeb8c0e98ee0873b39a958a5ef5158dd76a
parent 493d0f3d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -26,6 +26,13 @@ import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map

/**
 * Returns a [Flow] whose values are a list which containing the results of applying the given
 * [transform] function to each element in the original flow's list.
 */
inline fun <T, R> Flow<List<T>>.mapItem(crossinline transform: (T) -> R): Flow<List<R>> =
    map { list -> list.map(transform) }

/**
 * Returns a [Flow] whose values are a list which containing the results of asynchronously applying
 * the given [transform] function to each element in the original flow's list.
+3 −2
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@ interface AppListModel<T : AppRecord> {
     *
     * @return the [AppRecord] list which will be displayed.
     */
    fun filter(userIdFlow: Flow<Int>, option: Int, recordListFlow: Flow<List<T>>): Flow<List<T>>
    fun filter(userIdFlow: Flow<Int>, option: Int, recordListFlow: Flow<List<T>>): Flow<List<T>> =
        recordListFlow

    /**
     * This function is called when the App List's loading is finished and displayed to the user.
@@ -67,5 +68,5 @@ interface AppListModel<T : AppRecord> {
     * @return null if no summary should be displayed.
     */
    @Composable
    fun getSummary(option: Int, record: T): State<String>?
    fun getSummary(option: Int, record: T): State<String>? = null
}
+2 −11
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import android.content.pm.ApplicationInfo
import androidx.compose.runtime.Composable
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spa.framework.util.asyncMapItem
import com.android.settingslib.spa.framework.util.mapItem
import com.android.settingslib.spa.testutils.waitUntil
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -116,16 +116,7 @@ private class TestAppListModel : AppListModel<TestAppRecord> {
    var onFirstLoadedCalled = false

    override fun transform(userIdFlow: Flow<Int>, appListFlow: Flow<List<ApplicationInfo>>) =
        appListFlow.asyncMapItem { TestAppRecord(it) }

    @Composable
    override fun getSummary(option: Int, record: TestAppRecord) = null

    override fun filter(
        userIdFlow: Flow<Int>,
        option: Int,
        recordListFlow: Flow<List<TestAppRecord>>,
    ) = recordListFlow
        appListFlow.mapItem(::TestAppRecord)

    override suspend fun onFirstLoaded(recordList: List<TestAppRecord>) {
        onFirstLoadedCalled = true
+2 −12
Original line number Diff line number Diff line
@@ -17,8 +17,7 @@
package com.android.settingslib.spaprivileged.tests.testutils

import android.content.pm.ApplicationInfo
import androidx.compose.runtime.Composable
import com.android.settingslib.spa.framework.util.asyncMapItem
import com.android.settingslib.spa.framework.util.mapItem
import com.android.settingslib.spaprivileged.model.app.AppListModel
import com.android.settingslib.spaprivileged.model.app.AppRecord
import kotlinx.coroutines.flow.Flow
@@ -35,16 +34,7 @@ class TestAppListModel(
    override fun getSpinnerOptions() = options

    override fun transform(userIdFlow: Flow<Int>, appListFlow: Flow<List<ApplicationInfo>>) =
        appListFlow.asyncMapItem { TestAppRecord(it) }

    @Composable
    override fun getSummary(option: Int, record: TestAppRecord) = null

    override fun filter(
        userIdFlow: Flow<Int>,
        option: Int,
        recordListFlow: Flow<List<TestAppRecord>>,
    ) = recordListFlow
        appListFlow.mapItem(::TestAppRecord)

    override fun getGroupTitle(option: Int, record: TestAppRecord) =
        if (enableGrouping) record.group else null