package com.example.aiapp import android.content.Intent import android.net.Uri import androidx.core.content.IntentCompat /** * What another app handed this one through the share sheet, waiting to be attached to a session. * * Held as the URIs rather than uploaded on arrival, because an upload belongs to a session and the * share arrives before anyone has said which. [serial] makes two shares of the same thing two * requests, for the reason [SessionOpenRequest] carries one. */ data class ShareRequest(val uris: List, val text: String?, val serial: Int) /** The share in [intent], or null when it is some other intent. */ fun sharedContent(intent: Intent, serial: Int): ShareRequest? { val uris = when (intent.action) { Intent.ACTION_SEND -> listOfNotNull( IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java) ) Intent.ACTION_SEND_MULTIPLE -> IntentCompat.getParcelableArrayListExtra( intent, Intent.EXTRA_STREAM, Uri::class.java, ) .orEmpty() else -> return null } val text = intent.getStringExtra(Intent.EXTRA_TEXT)?.takeIf { it.isNotBlank() } if (uris.isEmpty() && text == null) return null return ShareRequest(uris, text, serial) } /** What is waiting, for the banner that says so. */ fun ShareRequest.summary(): String = when { uris.size == 1 -> "1 file to attach" uris.isNotEmpty() -> "${uris.size} files to attach" else -> "Text to attach" }