Merge "Add default AppListModel filter and getSummary"

This commit is contained in:
Chaohui Wang
2022-12-01 10:21:21 +00:00
committed by Android (Google) Code Review
4 changed files with 14 additions and 25 deletions

View File

@@ -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.

View File

@@ -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
}

View File

@@ -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

View File

@@ -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