misc : introduce List.firstIfSingle extension
This commit is contained in:
parent
98c0697a3f
commit
421bbba558
2 changed files with 18 additions and 11 deletions
|
|
@ -14,6 +14,7 @@ import io.element.android.features.knockrequests.impl.KnockRequest
|
||||||
import io.element.android.features.knockrequests.impl.R
|
import io.element.android.features.knockrequests.impl.R
|
||||||
import io.element.android.features.knockrequests.impl.getBestName
|
import io.element.android.features.knockrequests.impl.getBestName
|
||||||
import io.element.android.libraries.architecture.AsyncAction
|
import io.element.android.libraries.architecture.AsyncAction
|
||||||
|
import io.element.android.libraries.core.extensions.firstIfSingle
|
||||||
import kotlinx.collections.immutable.ImmutableList
|
import kotlinx.collections.immutable.ImmutableList
|
||||||
|
|
||||||
data class KnockRequestsBannerState(
|
data class KnockRequestsBannerState(
|
||||||
|
|
@ -23,17 +24,8 @@ data class KnockRequestsBannerState(
|
||||||
val canAccept: Boolean,
|
val canAccept: Boolean,
|
||||||
val eventSink: (KnockRequestsBannerEvents) -> Unit,
|
val eventSink: (KnockRequestsBannerEvents) -> Unit,
|
||||||
) {
|
) {
|
||||||
val subtitle = if (knockRequests.size == 1) {
|
val subtitle = knockRequests.firstIfSingle()?.userId?.value
|
||||||
knockRequests.first().userId.value
|
val reason = knockRequests.firstIfSingle()?.reason
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
val reason = if (knockRequests.size == 1) {
|
|
||||||
knockRequests.first().reason
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun formattedTitle(): String {
|
fun formattedTitle(): String {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2024 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
* Please see LICENSE in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.core.extensions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first element if the list contains exactly one element, otherwise returns null.
|
||||||
|
*/
|
||||||
|
inline fun <reified T> List<T>.firstIfSingle(): T? {
|
||||||
|
return if (size == 1) first() else null
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue