diff --git a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt index fe97e951..d008c8d2 100644 --- a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt +++ b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt @@ -31,7 +31,6 @@ import com.anindra.messages.data.SimCard import com.anindra.messages.data.SwipeAction import com.anindra.messages.data.DatabaseCompatibility import com.anindra.messages.ui.AttachAction -import com.anindra.messages.ui.AttachSheet import com.anindra.messages.ui.AttachTile import com.anindra.messages.ui.AttachTray import com.anindra.messages.ui.BubblePosition @@ -723,21 +722,6 @@ private fun SuggestionChipsPreview() { } } -@PreviewLightDark -@Preview(name = "Attach sheet", showBackground = true, widthDp = 380, heightDp = 640) -@Composable -private fun AttachSheetPreview() { - MessagesTheme(mode = "system") { - Box( - Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.background) - ) { - AttachSheet(onGallery = {}, onCamera = {}, onAudio = {}, onLocation = {}, onContact = {}, onDismiss = {}) - } - } -} - @PreviewLightDark @Preview(name = "Skeleton rows", showBackground = true, widthDp = 380) @Composable diff --git a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt index 03d23f5d..1c708b8e 100644 --- a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.LinearEasing +import androidx.compose.animation.core.MutableTransitionState import androidx.compose.animation.core.animateFloat import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.RepeatMode @@ -68,7 +69,10 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.navigationBarsPadding +import androidx.compose.foundation.layout.asPaddingValues +import androidx.compose.foundation.layout.ime import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -120,7 +124,6 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.LocalMinimumInteractiveComponentSize import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.RadioButton import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarDuration @@ -140,7 +143,6 @@ import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.TimePicker import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults -import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.material3.rememberTimePickerState import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider @@ -164,8 +166,10 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip +import androidx.compose.ui.draw.rotate import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.TransformOrigin import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.Color @@ -182,7 +186,9 @@ import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalLayoutDirection +import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.AnnotatedString @@ -995,7 +1001,40 @@ fun ChatScreen( var draftLoaded by remember { mutableStateOf(false) } var showEmoji by remember { mutableStateOf(false) } var menuOpen by remember { mutableStateOf(false) } - var attachSheet by remember { mutableStateOf(false) } + // The attachment tray takes the keyboard's place rather than covering the + // chat, so opening it dismisses the IME and closing it gives the field back. + var attachOpen by remember { mutableStateOf(false) } + var showLocationDialog by remember { mutableStateOf(false) } + val fieldFocus = remember { FocusRequester() } + val keyboard = LocalSoftwareKeyboardController.current + val focusManager = LocalFocusManager.current + // The keyboard's height while it is up. The tray takes this slot over, so + // the composer keeps its place across the swap instead of being pushed by + // both for a frame. The height comes from ImeGuard, not the raw report: an + // OEM that overstates the inset would otherwise hand the tray a slot the + // size of the window. + val imeBottom = imeBottomDp() + val navBottom = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding() + // The keyboard's inset already covers the navigation bar's strip, and the + // bar's own column pads that separately: without taking one out, the + // composer floats a navigation bar above the keyboard. + val keyboardInset = (imeBottom - navBottom).coerceAtLeast(0.dp) + // Remember how tall this device's keyboard is. The tray replaces that slot, + // so it is built to the same height and follows a resized keyboard (a taller + // one in landscape, a different IME, the user's own height setting). + var keyboardSlot by rememberSaveable { mutableFloatStateOf(0f) } + // The IME's inset arrives in animation frames — the first ones describe a + // half-open keyboard — so the height is read again once it has settled. + // Every open re-reads it, which is what makes the tray follow a resized + // keyboard instead of remembering the first one it ever saw. + var imeSeen by remember { mutableFloatStateOf(0f) } + LaunchedEffect(imeBottom) { imeSeen = imeBottom.value } + LaunchedEffect(imeBottom > 0.dp) { + if (imeBottom <= 0.dp) return@LaunchedEffect + delay(450) + val settled = imeSeen - navBottom.value + if (settled > 120f) keyboardSlot = settled + } // Picked media waits here until the user sends it, so a picture can be // explained or dropped before it leaves — it is no longer sent on pick. val pendingMedia = remember { mutableStateListOf() } @@ -1204,6 +1243,18 @@ fun ChatScreen( } } + // A device without a camera app, a photo picker, a document picker or a + // contact picker throws instead of opening anything: an unhandled + // ActivityNotFoundException crashed the chat the moment that tile was + // tapped, which is how an AVD with the camera package removed behaved. + fun launchOrExplain(messageRes: Int, launch: () -> Unit) { + try { + launch() + } catch (_: android.content.ActivityNotFoundException) { + Toast.makeText(context, context.getString(messageRes), Toast.LENGTH_SHORT).show() + } + } + fun leaveChat() { vm.saveDraftAndMaybeTrash(conversationId, draft.trim()) onBack() @@ -1441,6 +1492,9 @@ fun ChatScreen( // back closes it instead of leaving the chat. BackHandler(enabled = previewImage != null) { previewImage = null } BackHandler(enabled = backHandled, onBack = ::leaveChat) + // Registered after leave-chat so it wins: the last enabled handler is the + // one Compose calls, and the tray is a keyboard replacement, not a screen. + BackHandler(enabled = attachOpen) { attachOpen = false } BackHandler(enabled = selectionActive) { clearSelection() } BackHandler(enabled = searchOpen) { searchOpen = false @@ -1601,6 +1655,41 @@ fun ChatScreen( } } + // The tray is drawn (or still fading out) and therefore owns the keyboard's + // slot right now. Read off the animation itself, so the composer keeps + // clearing nothing until the tray has actually left. + val tray = remember { MutableTransitionState(false) } + tray.targetState = attachOpen && !selectionActive + val trayOnScreen = tray.currentState || tray.targetState + // The tray just closed to give the field back, and the keyboard is on its + // way up. Until it arrives the composer holds the slot the tray vacated — + // without this it drops to the bottom of the window for a frame and climbs + // back with the keyboard, which is the flicker this is here to stop. + var keyboardSwap by remember { mutableStateOf(false) } + LaunchedEffect(imeBottom) { + if (imeBottom > 0.dp) keyboardSwap = false + } + LaunchedEffect(keyboardSwap) { + if (!keyboardSwap) return@LaunchedEffect + // Focus was asked for but no keyboard ever came: stop holding the slot. + delay(1200) + keyboardSwap = false + } + // What the composer clears: nothing while the tray holds the slot, the + // keyboard's own height while it returns, the guarded inset otherwise. + val composerInset = when { + trayOnScreen -> 0.dp + keyboardSwap -> maxOf(keyboardInset, keyboardSlot.dp) + else -> keyboardInset + } + LaunchedEffect(imeBottom, navBottom, keyboardSlot, attachOpen, keyboardSwap) { + android.util.Log.d( + "AttachTrayHeight", + "ime=${imeBottom.value} nav=${navBottom.value} inset=${keyboardInset.value} " + + "slot=$keyboardSlot composer=${composerInset.value} " + + "open=$attachOpen swap=$keyboardSwap" + ) + } Scaffold( containerColor = MaterialTheme.colorScheme.background, snackbarHost = { SnackbarHost(snackbarHostState) }, @@ -1855,8 +1944,43 @@ fun ChatScreen( showSchedulePicker = true } }, - onEmojiToggle = { showEmoji = !showEmoji }, - onAttach = { attachSheet = true }, + onEmojiToggle = { + attachOpen = false + showEmoji = !showEmoji + }, + onAttach = { + if (attachOpen) { + attachOpen = false + // The keyboard is coming back, so hold its slot + // from the moment the tray starts leaving. + keyboardSwap = true + fieldFocus.requestFocus() + } else { + attachOpen = true + keyboardSwap = false + showEmoji = false + // Clearing the whole focus owner, not just this + // requester: that is what drops the IME, and the + // tray then grows into the space it left. + focusManager.clearFocus() + keyboard?.hide() + } + }, + attachOpen = attachOpen, + fieldFocusRequester = fieldFocus, + onFieldFocus = { + // Tapping the field while the tray is open is the + // other way back to typing: close the tray into the + // keyboard rather than past it. + if (attachOpen) { + attachOpen = false + keyboardSwap = true + } + }, + // While the tray holds the keyboard's slot the composer + // must not reserve an inset as well, or the tray lands on + // top of it. + composerBottomInset = composerInset, showEmojiButton = vm.settings.emojiButtonEnabled, voiceEnabled = hasMicrophone, voiceMaxSeconds = voiceMaxSeconds, @@ -1905,6 +2029,73 @@ fun ChatScreen( } else { AlphanumericNotice(address = chatAddress) } + AnimatedVisibility( + visibleState = tray, + // No growth animation: the tray takes the keyboard's height + // in the same frame the keyboard's inset is released, so the + // composer does not ride up and back down while they swap. + // It leaves quickly, before the returning keyboard reaches + // it, instead of hanging over the keys for a few frames. + enter = if (reduceMotion) fadeIn(tween(0)) + else fadeIn(motionTween(false, Motion.DURATION_SHORT4)), + exit = if (reduceMotion) fadeOut(tween(0)) + else fadeOut(tween(90)) + ) { + AttachTray( + cameraLabel = stringResource(R.string.chat_media_camera), + cameraIcon = Icons.Rounded.CameraAlt, + onCamera = { + attachOpen = false + val dir = File(context.cacheDir, "camera").apply { mkdirs() } + val file = File(dir, "photo_${System.currentTimeMillis()}.jpg") + cameraFileUri = FileProvider.getUriForFile( + context, "${context.packageName}.fileprovider", file + ) + launchOrExplain(R.string.chat_camera_unavailable) { + cameraLauncher.launch(cameraFileUri!!) + } + }, + keyboardHeight = keyboardSlot.takeIf { it > 0f }?.dp, + actions = listOf( + AttachAction( + label = stringResource(R.string.chat_media_gallery), + icon = Icons.Rounded.Image + ) { + attachOpen = false + launchOrExplain(R.string.chat_gallery_unavailable) { + galleryLauncher.launch( + PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly) + ) + } + }, + AttachAction( + label = stringResource(R.string.chat_media_audio), + icon = Icons.Rounded.Audiotrack + ) { + attachOpen = false + launchOrExplain(R.string.chat_gallery_unavailable) { + audioLauncher.launch(arrayOf("audio/*")) + } + }, + AttachAction( + label = stringResource(R.string.chat_media_location), + icon = Icons.Rounded.LocationOn + ) { + attachOpen = false + showLocationDialog = true + }, + AttachAction( + label = stringResource(R.string.chat_media_contact), + icon = Icons.Rounded.Person + ) { + attachOpen = false + launchOrExplain(R.string.chat_contact_unavailable) { + contactLauncher.launch(null) + } + } + ) + ) + } } } ) { padding -> @@ -2127,8 +2318,6 @@ fun ChatScreen( } } - var showLocationDialog by remember { mutableStateOf(false) } - if (showLocationDialog) { LocationDialog( onDismiss = { showLocationDialog = false }, @@ -2140,52 +2329,6 @@ fun ChatScreen( // contact picker throws instead of opening anything: an unhandled // ActivityNotFoundException crashed the chat the moment that tile was // tapped, which is how an AVD with the camera package removed behaved. - fun launchOrExplain(messageRes: Int, launch: () -> Unit) { - try { - launch() - } catch (_: android.content.ActivityNotFoundException) { - Toast.makeText(context, context.getString(messageRes), Toast.LENGTH_SHORT).show() - } - } - - if (attachSheet) { - AttachSheet( - onGallery = { - attachSheet = false - launchOrExplain(R.string.chat_gallery_unavailable) { - galleryLauncher.launch( - PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly) - ) - } - }, - onAudio = { - attachSheet = false - launchOrExplain(R.string.chat_gallery_unavailable) { - audioLauncher.launch(arrayOf("audio/*")) - } - }, - onLocation = { - attachSheet = false - showLocationDialog = true - }, - onContact = { - attachSheet = false - launchOrExplain(R.string.chat_contact_unavailable) { - contactLauncher.launch(null) - } - }, - onCamera = { - attachSheet = false - val dir = File(context.cacheDir, "camera").apply { mkdirs() } - val file = File(dir, "photo_${System.currentTimeMillis()}.jpg") - cameraFileUri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file) - launchOrExplain(R.string.chat_camera_unavailable) { - cameraLauncher.launch(cameraFileUri!!) - } - }, - onDismiss = { attachSheet = false } - ) - } if (showRetrySimPicker && retryingMessageId > 0) { SimPickerDialog( @@ -3675,6 +3818,14 @@ internal fun InputBar( schedulingEnabled: Boolean = true, onEmojiToggle: () -> Unit, onAttach: () -> Unit, + /** The tray is open under the bar, so the attach button offers to close it. */ + attachOpen: Boolean = false, + /** Lets the caller hand focus back when the tray closes. */ + fieldFocusRequester: FocusRequester? = null, + /** Tapped into the field while the tray was open. */ + onFieldFocus: () -> Unit = {}, + /** Height the bar sits above. Null means the IME's guarded inset decides. */ + composerBottomInset: Dp? = null, showEmojiButton: Boolean = false, hasAttachments: Boolean = false, sims: List = emptyList(), @@ -3752,7 +3903,17 @@ internal fun InputBar( stopRecording(send = true) } } - Column(modifier = Modifier.fillMaxWidth().imeGuardedPadding()) { + Column( + modifier = Modifier + .fillMaxWidth() + // The tray replaces the keyboard's slot, so while it is open the bar + // clears nothing and the tray sits directly under it. Closed, the + // guarded IME inset is the only thing that decides the padding. + .then( + if (composerBottomInset != null) Modifier.padding(bottom = composerBottomInset) + else Modifier.imeGuardedPadding() + ) + ) { if (suggestions.isNotEmpty()) { SuggestionChips( suggestions = suggestions, @@ -3798,14 +3959,26 @@ internal fun InputBar( ) { Box(Modifier.size(48.dp), contentAlignment = Alignment.Center) { IconButton(onClick = onAttach) { + // One ring, one animation: the plus turns 45° into the + // cross that closes the tray, so closing it is the same + // control it was opened with. + val turn by animateFloatAsState( + targetValue = if (attachOpen) 45f else 0f, + animationSpec = motionSpring(LocalReduceMotion.current), + label = "attachTurn" + ) // GM badges the attach button with a dot that overlaps the // ring's top-right; the ring shows through as broken // because the dot carries a ring of field colour around it. Box { Icon( Icons.Rounded.AddCircleOutline, - stringResource(R.string.icon_attach), - tint = MaterialTheme.colorScheme.onSurfaceVariant + stringResource( + if (attachOpen) R.string.common_close + else R.string.icon_attach + ), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.rotate(turn) ) val dot = MaterialTheme.colorScheme.composerAttachDot // A dot colour has no text equivalent, so it names @@ -3825,6 +3998,9 @@ internal fun InputBar( // GM's dot measures ~5dp across; the 8dp box // is the cut-out ring plus that dot. .size(8.dp) + // The badge belongs to "something to + // attach", so it fades with the turn. + .alpha(if (attachOpen) 0f else 1f) .background(MaterialTheme.colorScheme.inputPill, CircleShape) .padding(1.5.dp) .background(dot, CircleShape) @@ -3852,7 +4028,18 @@ internal fun InputBar( capitalization = androidx.compose.ui.text.input.KeyboardCapitalization.Sentences ), maxLines = 4, - modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp) + modifier = Modifier + .fillMaxWidth() + // Handed back when the tray closes, so the + // crossed attach button returns typing to the + // field it came from. + .then( + if (fieldFocusRequester != null) + Modifier.focusRequester(fieldFocusRequester) + else Modifier + ) + .onFocusChanged { if (it.isFocused) onFieldFocus() } + .padding(vertical = 16.dp) ) } if (SimSwitcher.shouldShowSwitch(sims.size)) { @@ -4320,71 +4507,6 @@ private const val HALO_ALPHA = 0.35f private const val HALO_BREATHE_GROWTH = 0.22f private const val HALO_BREATHE_ALPHA = 0.20f -@OptIn(ExperimentalMaterial3Api::class) -@Composable -internal fun AttachSheet( - onGallery: () -> Unit, - onCamera: () -> Unit, - onAudio: () -> Unit, - onLocation: () -> Unit, - onContact: () -> Unit, - onDismiss: () -> Unit -) { - ModalBottomSheet( - onDismissRequest = onDismiss, - sheetState = rememberModalBottomSheetState() - ) { - Column(Modifier.padding(horizontal = 16.dp, vertical = 12.dp)) { - Text( - stringResource(R.string.chat_share), - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold, - modifier = Modifier.padding(bottom = 12.dp) - ) - Row( - Modifier.fillMaxWidth().clickable { onGallery() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Image, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_gallery), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onContact() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Person, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_contact), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onLocation() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.LocationOn, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_location), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onAudio() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Audiotrack, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_audio), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onCamera() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.CameraAlt, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_camera), style = MaterialTheme.typography.bodyLarge) - } - } - } -} - @Composable internal fun SimPickerDialog( sims: List, diff --git a/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt b/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt index 731b6f42..f06e6b12 100644 --- a/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt +++ b/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.unit.Dp /** What the platform says the keyboard covers, with the lying weeded out. * @@ -69,17 +70,21 @@ object ImeGuard { } } -/** The composer's bottom padding: the reported IME inset, clamped to what the - * window can actually show above the keyboard. */ +/** The keyboard's height right now, with an OEM's dishonest report clamped by + * [ImeGuard]. The composer's padding and the tray's slot are both sized from + * this one number, so the two cannot disagree about how tall the keyboard is. */ @Composable -fun Modifier.imeGuardedPadding(): Modifier { +fun imeBottomDp(): Dp { val density = LocalDensity.current val root = LocalView.current.rootView val reported = ImeGuard.reportedBottomPx(WindowInsets.ime.getBottom(density)) val applied = androidx.compose.runtime.remember(reported) { ImeGuard.appliedBottomPx(root, reported) } - return this.then( - Modifier.padding(bottom = with(density) { applied.toDp() }) - ) + return with(density) { applied.toDp() } } + +/** The composer's bottom padding: the reported IME inset, clamped to what the + * window can actually show above the keyboard. */ +@Composable +fun Modifier.imeGuardedPadding(): Modifier = this.then(Modifier.padding(bottom = imeBottomDp())) diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt new file mode 100644 index 00000000..6256b74d --- /dev/null +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt @@ -0,0 +1,144 @@ +package com.anindra.messages.ui + +import java.io.File +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The tray the attach button opens is the tile grid that sits in the keyboard's + * slot, not the modal sheet it used to open. + * + * Merging the branch that built the tray kept the older `AttachSheet` wired into + * the chat, so `AttachTray` compiled and shipped unused: the screen still dimmed + * the conversation behind a sheet and left the keyboard up underneath it. These + * assertions hold the chat to the tray, in the chat's own bottom bar. + */ +class AttachTrayWiringTest { + + private val chat: String by lazy { + val app = generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "app") } + .firstOrNull { File(it, "src").isDirectory } + ?: error("app/src not found") + File(app, "src/main/java/com/anindra/messages/ui/ChatScreen.kt").readText() + } + + /** The chat's bottom bar: the composer and, under it, the tray. */ + private val bottomBar: String + get() = chat.substringAfter("bottomBar = {").substringBefore(") { padding ->") + + @Test + fun theAttachButtonOpensTheTray() { + assertTrue( + "the chat must draw AttachTray", + bottomBar.contains("AttachTray(") + ) + assertTrue( + "the tray belongs to the bottom bar, under the composer, not to the " + + "screen's root as an overlay", + bottomBar.indexOf("AttachTray(") > bottomBar.indexOf("InputBar(") + ) + } + + @Test + fun theTrayTakesItsHeightFromTheKeyboard() { + assertTrue( + "the tray is built to the keyboard's slot, so the composer holds still", + bottomBar.contains("keyboardHeight = keyboardSlot") + ) + } + + @Test + fun theTrayOffersEveryAction() { + val tray = bottomBar.substringAfter("AttachTray(") + for (resource in listOf( + "chat_media_camera", "chat_media_gallery", "chat_media_audio", + "chat_media_location", "chat_media_contact" + )) { + assertTrue("$resource must have a tile", tray.contains("R.string.$resource")) + } + } + + @Test + fun theOldSheetIsGone() { + assertFalse( + "AttachSheet is dead once the tray is wired in; leaving it behind ships " + + "a second, unused attach UI", + chat.contains("fun AttachSheet(") + ) + } + + @Test + fun openingTheTrayTakesTheKeyboardAway() { + val attach = chat.substringAfter("onAttach = {").substringBefore("showEmojiButton") + assertTrue("opening the tray must clear focus", attach.contains("focusManager.clearFocus()")) + assertTrue("and hide the keyboard", attach.contains("keyboard?.hide()")) + assertTrue( + "closing it must hand the field back", + attach.contains("fieldFocus.requestFocus()") + ) + } + + @Test + fun backClosesTheTrayBeforeTheChat() { + assertTrue( + "Back must close the tray instead of leaving the conversation", + chat.contains("BackHandler(enabled = attachOpen) { attachOpen = false }") + ) + val leaveChat = chat.indexOf("BackHandler(enabled = backHandled, onBack = ::leaveChat)") + val trayBack = chat.indexOf("BackHandler(enabled = attachOpen)") + assertTrue( + "the tray's handler is registered after leave-chat, so it wins", + trayBack > leaveChat + ) + } + + @Test + fun theComposerDoesNotReserveTheKeyboardUnderTheTray() { + assertTrue( + "with the tray open the composer must clear nothing, or the tray " + + "lands on top of it", + chat.contains("trayOnScreen -> 0.dp") + ) + assertTrue( + "the bar's height comes from the guarded IME inset, minus the strip " + + "the bottom bar already pads", + chat.contains("val keyboardInset = (imeBottom - navBottom).coerceAtLeast(0.dp)") + ) + } + + @Test + fun theComposerHoldsItsPlaceWhenTheKeyboardComesBack() { + assertTrue( + "the composer's inset must be the swap-aware one, not the raw keyboard inset", + chat.contains("composerBottomInset = composerInset") + ) + assertTrue( + "closing the tray into the keyboard must hold the slot the tray vacated", + chat.contains("keyboardSwap -> maxOf(keyboardInset, keyboardSlot.dp)") + ) + assertTrue( + "closing the tray by its own button asks for the keyboard back", + chat.contains("keyboardSwap = true\n fieldFocus.requestFocus()") + ) + assertTrue( + "tapping the field while the tray is open is the same swap", + chat.contains("if (attachOpen) {\n attachOpen = false\n keyboardSwap = true\n }") + ) + } + + @Test + fun theTrayLeavesBeforeTheKeyboardArrives() { + assertTrue( + "the tray's visibility is read back by the composer, so it must be " + + "driven through a transition state", + chat.contains("visibleState = tray") + ) + assertTrue( + "the tray fades out faster than the keyboard rises, so it never sits " + + "over the keys", + chat.contains("fadeOut(tween(90))") + ) + } +} diff --git a/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt b/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt index 1f5ec2a6..150d1c90 100644 --- a/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt @@ -8,9 +8,9 @@ import java.io.File * Issue #481: an audio file can be attached, not only a voice note. * * The transport already handled `audio` — a voice note is stored that way — so - * what was missing was the way in: a row in the attach sheet and a picker that - * can reach music and recordings. The photo picker cannot, so the entry has to - * go through the document picker. + * what was missing was the way in: an Audio tile in the attach tray and a picker + * that can reach music and recordings. The photo picker cannot, so the entry has + * to go through the document picker. */ class AudioAttachmentTest { @@ -25,10 +25,16 @@ class AudioAttachmentTest { } @Test - fun theAttachSheetOffersAudio() { - val sheet = chat.substringAfter("internal fun AttachSheet(").substringBefore("@Composable") - assertTrue("the sheet must take an onAudio callback", sheet.contains("onAudio: () -> Unit")) - assertTrue("and draw a row for it", sheet.contains("R.string.chat_media_audio")) + fun theAttachTrayOffersAudio() { + val tray = chat.substringAfter("AttachTray(") + assertTrue( + "the tray must offer an Audio tile", + tray.contains("R.string.chat_media_audio") + ) + assertTrue( + "and the tile must be its own action with its own icon", + tray.contains("Icons.Rounded.Audiotrack") + ) } @Test diff --git a/scripts b/scripts index 96984298..0649647a 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 969842981861d4ac5e76554249b96df682d93ba8 +Subproject commit 0649647ae59623f2f23b4d2fdc107506e6551424