Pantrix UI

Sending a text to another app

Why, in Compose Multiplatform, sending a text or a link to another app, a message, a mail, a note, is other code on each platform, what the library gives for it, and how it is tested. The problem is not Pantrix's own: it is seen the same way in every Compose Multiplatform app that says "Send with another app".

What is said of the platforms rests on the documentation of Android's Intent.createChooser, UIKit's UIActivityViewController and the browsers' Web Share API (navigator.share), or was seen in a test that runs in this repository; what was seen where is written in section 6.

1. The problem

Symptom

Cause

2. The solution

PieceWhat it gives
1One common interface, and its implementation for each platformThe button is written once in common code
2None where there is noneThe desktop and a browser without navigator.share offer to copy in place of sending
3One type of resultShared, closed, shown or not shown: the screen reads it the same way on every platform
4A sheet of the test's ownNo system sheet opens, and what was sent is read

3. With Pantrix

3.1. The sheet

rememberShareSheet() is in com.pantrixui:foundation (com.pantrixui.foundation.platform). It gives a ShareSheet, or null on a platform without one:

val sheet = rememberShareSheet()
val clipboard = rememberPantrixClipboard()
val scope = rememberCoroutineScope()
if (sheet != null) {
    PantrixButton(
        text = appWords.sendWithAnotherApp,
        onClick = { sheet.share(link, title = documentName) { result -> onShared(result) } },
        tagKey = "send"
    )
} else {
    // On the desktop and in a browser without navigator.share: copying the link.
    PantrixButton(text = appWords.copyLink, onClick = { scope.launch { clipboard.copyText(link) } }, tagKey = "copy")
}
PlatformSheetShareResult
AndroidThe system's chooser, Intent.ACTION_SEND with text/plain; the title is the subject (EXTRA_SUBJECT) and the chooser's headingStarted: the chooser does not say what its reader chose
iOSUIActivityViewController; on an iPad a popover with no arrow in the middle of the window; the title is a mail's subjectShared or Cancelled
Webnavigator.share, the title the page's titleShared; Cancelled where it was closed; Failed where it was refused
DesktopNone: rememberShareSheet() gives null—

3.2. Testing

PantrixTestTheme gives the screen a TestShareSheet of its own: no system sheet opens. A test that reads what was sent gives its own inside the theme, and a test of a platform without a sheet gives none:

val sheet = TestShareSheet()
setContent {
    PantrixTestTheme { CompositionLocalProvider(LocalShareSheet provides sheet) { ShareScreen(link) } }
}
onPantrixNode("Button", "send").performClick()
assertEquals(listOf(SharedText(link, title = "Orders")), sheet.shared)

// The desktop's screen, which copies the link:
setContent { PantrixTestTheme { CompositionLocalProvider(LocalShareSheet provides null) { ShareScreen(link) } } }
onPantrixNode("Button", "copy").assertExists()

TestShareSheet(result = ShareResult.Cancelled) answers as a reader who closes the sheet.

4. Without Pantrix

In a project without the library each platform makes its own call, and common code reaches it through an expect fun:

PlatformCallMind
Androidcontext.startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, text), title))Outside an activity it needs FLAG_ACTIVITY_NEW_TASK
iOStop.presentViewController(UIActivityViewController(listOf(text), null), true, null)top is the controller presented on top; on an iPad popoverPresentationController is given a sourceView and a sourceRect
Webnavigator.share({ text: text, title: title })From inside a press; in a browser without it typeof navigator.share is "undefined"
DesktopNoneCopying to the clipboard

5. Pitfalls

6. Verification

Tests

WhatWhere
The sheet given is the screen's, and none where none is givenui/foundation/src/commonTest/.../platform/ShareSheetTest.kt (JVM, Android, iOS, JS, Wasm)
Android: the chooser's ACTION_SEND intent, text/plain, the text, the subject and the chooser's heading; no subject for a text without a title; Startedui/foundation/src/androidHostTest/.../platform/ShareSheetOnAndroidTest.kt (Robolectric)
iOS: Cancelled in a test binary that has no applicationui/foundation/src/iosTest/.../platform/ShareSheetOnIosTest.kt
Web: the text and title handed to navigator.share, a text without a title; Shared, Cancelled, Failed; no sheet without navigator.share (with the test's own navigator.share)ui/foundation/src/webTest/.../platform/ShareSheetOnTheWebTest.kt (JS, Wasm)
Desktop: no sheetui/foundation/src/jvmTest/.../platform/ShareSheetOnTheDesktopTest.kt
TestShareSheet and the test theme's own sheetui/testing/src/commonTest/.../TestDoublesTest.kt, PantrixTestThemeTest.kt

Looked at by hand

Not verified

7. Where it is in this repository