consume

Function

Android
@ExperimentalFoundationApi
fun TransferableContent.consume(predicate: (ClipData.Item) -> Boolean): TransferableContent?

Helper function to consume parts of TransferableContent in Android by splitting it to ClipData.Item parts. Use this function in contentReceiver modifier's onReceive callback to easily separate remaining parts from incoming TransferableContent.

Parameters

predicateDecides whether to consume or leave the given item out. Return true to indicate that this particular item was processed here, it shouldn't be passed further down the content receiver chain. Return false to keep it in the returned TransferableContent.

Returns

Remaining parts of this TransferableContent.

Code Examples

ReceiveContentBasicSample

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ReceiveContentBasicSample() {
    val state = rememberTextFieldState()
    var images by remember { mutableStateOf<List<ImageBitmap>>(emptyList()) }
    Column {
        Row { images.forEach { Image(bitmap = it, contentDescription = null) } }
        BasicTextField(
            state = state,
            modifier =
                Modifier.contentReceiver { transferableContent ->
                    if (!transferableContent.hasMediaType(MediaType.Image)) {
                        return@contentReceiver transferableContent
                    }
                    val newImages = mutableListOf<ImageBitmap>()
                    transferableContent
                        .consume { item ->
                            // only consume this item if we can read an imageBitmap
                            item.readImageBitmap()?.let {
                                newImages += it
                                true
                            } ?: false
                        }
                        .also { images = newImages }
                },
        )
    }
}