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

Commit d22225ef authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Add Iterable<T>.asyncForEach for SpaLib"

parents 042d08aa 313b9a3e
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -19,7 +19,23 @@ package com.android.settingslib.spa.framework.util
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

/**
 * Performs the given [action] on each element asynchronously.
 */
suspend inline fun <T> Iterable<T>.asyncForEach(crossinline action: (T) -> Unit) {
    coroutineScope {
        forEach {
            launch { action(it) }
        }
    }
}

/**
 * Returns a list containing the results of asynchronously applying the given [transform] function
 * to each element in the original collection.
 */
suspend inline fun <R, T> Iterable<T>.asyncMap(crossinline transform: (T) -> R): List<R> =
    coroutineScope {
        map { item ->
@@ -27,6 +43,11 @@ suspend inline fun <R, T> Iterable<T>.asyncMap(crossinline transform: (T) -> R):
        }.awaitAll()
    }

/**
 * Returns a list containing only elements matching the given [predicate].
 *
 * The filter operation is done asynchronously.
 */
suspend inline fun <T> Iterable<T>.asyncFilter(crossinline predicate: (T) -> Boolean): List<T> =
    asyncMap { item -> item to predicate(item) }
        .filter { it.second }
+1 −1
Original line number Diff line number Diff line
@@ -67,6 +67,6 @@ private fun PreferenceDivider() {
        Modifier
            .padding(horizontal = SettingsDimension.itemPaddingEnd)
            .size(width = 1.dp, height = SettingsDimension.itemDividerHeight)
            .background(color = MaterialTheme.colorScheme.onSurface.copy(SettingsOpacity.Disabled))
            .background(color = MaterialTheme.colorScheme.onSurface.copy(SettingsOpacity.Divider))
    )
}