From 8f9d47a6430903464fbe6beedf95ff5ceb761e47 Mon Sep 17 00:00:00 2001 From: Anindra Date: Thu, 19 Nov 2026 18:06:25 +0530 Subject: [PATCH] feat: the inbox gets a back-to-top shortcut MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scrolling a long inbox back to the top meant flicking through every row again. Past eight rows out of sight, a small round button appears in the middle of the bottom edge with an up chevron; tapping it animates the list back to the top, where the button disappears again. - It sits in the middle of the bottom edge, at 70% opacity: it floats over the rows, and at full strength the disc read as a hole punched through the list. The start-chat button keeps its corner. - Drawn as a Surface rather than a floating-action-button: the FAB's own container added a corner shape and a minimum touch target around the content, which drew a square edge around a round button. - It is hidden on the archived view, and while the list is at the top. JUnit: BackToTopTest (the eight-row threshold either side, and the 70% alpha the design asks for) and a preview so PreviewCoverageTest stays green. scripts/test-back-to-top.sh: 6 checks on emulator-5556, 0 failed — no button at the top, one after three screens, centred on the bottom edge, and the list back at its first row after a tap. The same run fails on the previous build, which has no button at all. The new label is in all 13 locales. --- .../messages/ui/previews/BackToTopPreviews.kt | 28 +++++++ .../java/com/anindra/messages/ui/BackToTop.kt | 74 +++++++++++++++++++ .../messages/ui/ConversationsScreen.kt | 22 ++++++ .../res/values-ar/strings_conversations.xml | 1 + .../res/values-de/strings_conversations.xml | 1 + .../res/values-es/strings_conversations.xml | 1 + .../res/values-fa/strings_conversations.xml | 1 + .../res/values-fr/strings_conversations.xml | 1 + .../res/values-hi/strings_conversations.xml | 1 + .../res/values-ja/strings_conversations.xml | 1 + .../res/values-ko/strings_conversations.xml | 1 + .../res/values-pl/strings_conversations.xml | 1 + .../values-pt-rBR/strings_conversations.xml | 1 + .../res/values-ru/strings_conversations.xml | 1 + .../values-zh-rCN/strings_conversations.xml | 1 + .../values-zh-rTW/strings_conversations.xml | 1 + .../main/res/values/strings_conversations.xml | 1 + .../com/anindra/messages/ui/BackToTopTest.kt | 37 ++++++++++ scripts | 2 +- 19 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 app/src/debug/java/com/anindra/messages/ui/previews/BackToTopPreviews.kt create mode 100644 app/src/main/java/com/anindra/messages/ui/BackToTop.kt create mode 100644 app/src/test/java/com/anindra/messages/ui/BackToTopTest.kt diff --git a/app/src/debug/java/com/anindra/messages/ui/previews/BackToTopPreviews.kt b/app/src/debug/java/com/anindra/messages/ui/previews/BackToTopPreviews.kt new file mode 100644 index 00000000..e476c2ba --- /dev/null +++ b/app/src/debug/java/com/anindra/messages/ui/previews/BackToTopPreviews.kt @@ -0,0 +1,28 @@ +package com.anindra.messages.ui.previews + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.PreviewLightDark +import androidx.compose.ui.unit.dp +import com.anindra.messages.ui.BackToTopFab +import com.anindra.messages.ui.theme.MessagesTheme + +@PreviewLightDark +@Composable +private fun BackToTopFabPreview() { + MessagesTheme(mode = "system") { + Column( + modifier = Modifier.padding(16.dp), + horizontalAlignment = Alignment.End, + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + Text("Scrolled far enough for the shortcut") + BackToTopFab(onClick = {}) + } + } +} diff --git a/app/src/main/java/com/anindra/messages/ui/BackToTop.kt b/app/src/main/java/com/anindra/messages/ui/BackToTop.kt new file mode 100644 index 00000000..932377e4 --- /dev/null +++ b/app/src/main/java/com/anindra/messages/ui/BackToTop.kt @@ -0,0 +1,74 @@ +package com.anindra.messages.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.rounded.KeyboardArrowUp +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.unit.dp +import com.anindra.messages.R + +/** + * When the inbox offers to take you back to the top. + * + * A list you have scrolled a screen or two into is one flick from the top, and + * the button would sit on top of the conversations for nothing. Past this many + * rows the flick is a chore and the button earns its place. + */ +internal object BackToTop { + + /** Rows that have to be out of sight before the button appears. */ + const val THRESHOLD = 8 + + /** How solid the button is drawn. It floats over the rows in the middle of + * the screen, so a fully opaque disc would read as a hole punched through + * the list; at 70% the conversation behind it stays legible. */ + const val ALPHA = 0.7f + + /** The disc the button is drawn on. */ + val SIZE = 40.dp + + fun visible(firstVisibleIndex: Int): Boolean = firstVisibleIndex >= THRESHOLD +} + +/** + * "Back to top", floating in the middle of the bottom edge. + * + * A Surface rather than a floating-action-button: the FAB's container adds its + * own corner shape and minimum touch target around the content, which drew a + * square edge around a round button. This one is the circle it looks like. + */ +@Composable +internal fun BackToTopFab(onClick: () -> Unit) { + val context = LocalContext.current + Surface( + onClick = onClick, + shape = CircleShape, + color = MaterialTheme.colorScheme.surfaceContainerHigh, + contentColor = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier + .size(BackToTop.SIZE) + .alpha(BackToTop.ALPHA) + .semantics { contentDescription = context.getString(R.string.conversations_back_to_top) } + ) { + Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { + Icon( + Icons.Rounded.KeyboardArrowUp, + contentDescription = null, + modifier = Modifier.size(24.dp) + ) + } + } +} diff --git a/app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt b/app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt index eb3b0fd0..66a53435 100644 --- a/app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt @@ -27,6 +27,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.navigationBarsPadding import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding @@ -94,6 +95,7 @@ import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow @@ -147,6 +149,7 @@ import com.anindra.messages.data.StarredMessage import com.anindra.messages.data.ConversationList import com.anindra.messages.data.SwipeAction import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.collect @@ -187,6 +190,11 @@ fun ConversationsScreen( listState.firstVisibleItemIndex == 0 && listState.firstVisibleItemScrollOffset == 0 } }.value + // Past a screen or two of rows, a shortcut back to the top earns its place. + val backToTop = remember(listState) { + derivedStateOf { BackToTop.visible(listState.firstVisibleItemIndex) } + }.value + val scope = rememberCoroutineScope() var surfacedUnread by remember { mutableStateOf>(emptyMap()) } var userScrolledAway by remember { mutableStateOf(false) } var unreadSeeded by remember { mutableStateOf(false) } @@ -502,6 +510,7 @@ fun ConversationsScreen( } } + Box(Modifier.fillMaxSize()) { Scaffold( containerColor = MaterialTheme.colorScheme.background, snackbarHost = { SnackbarHost(snackbarHostState) }, @@ -793,6 +802,19 @@ fun ConversationsScreen( } } + // Middle of the bottom edge, over the list: the FAB slot would place it at + // the end and pad it, which is not the middle of the screen. + AnimatedVisibility( + visible = backToTop && !showArchived, + modifier = Modifier + .align(Alignment.BottomCenter) + .navigationBarsPadding() + .padding(bottom = 24.dp) + ) { + BackToTopFab(onClick = { scope.launch { listState.animateScrollToItem(0) } }) + } + } + if (sheetConvo != null) { val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) ModalBottomSheet( diff --git a/app/src/main/res/values-ar/strings_conversations.xml b/app/src/main/res/values-ar/strings_conversations.xml index e34459b9..d6f3d1af 100644 --- a/app/src/main/res/values-ar/strings_conversations.xml +++ b/app/src/main/res/values-ar/strings_conversations.xml @@ -14,6 +14,7 @@ رسالة نصية المؤرشفة الرسائل غير المرغوبة والمحظورة + العودة إلى الأعلى لا توجد محادثات غير مرغوبة أو محظورة المحادثات الرسائل diff --git a/app/src/main/res/values-de/strings_conversations.xml b/app/src/main/res/values-de/strings_conversations.xml index 69236ac4..08b1ed08 100644 --- a/app/src/main/res/values-de/strings_conversations.xml +++ b/app/src/main/res/values-de/strings_conversations.xml @@ -13,6 +13,7 @@ Textnachricht Archiviert Spam und blockiert + Nach oben Keine Spam- oder blockierten Unterhaltungen Unterhaltungen Nachrichten diff --git a/app/src/main/res/values-es/strings_conversations.xml b/app/src/main/res/values-es/strings_conversations.xml index 835b3bd7..60ba87c4 100644 --- a/app/src/main/res/values-es/strings_conversations.xml +++ b/app/src/main/res/values-es/strings_conversations.xml @@ -14,6 +14,7 @@ Mensaje de texto Archivadas Spam y bloqueadas + Volver arriba No hay conversaciones de spam ni bloqueadas Conversaciones Mensajes diff --git a/app/src/main/res/values-fa/strings_conversations.xml b/app/src/main/res/values-fa/strings_conversations.xml index 69a9c348..d3b997c9 100644 --- a/app/src/main/res/values-fa/strings_conversations.xml +++ b/app/src/main/res/values-fa/strings_conversations.xml @@ -14,6 +14,7 @@ پیام بایگانی‌شده هرزنامه و مسدود + بازگشت به بالا هیچ گفتگوی هرزنامه یا مسدودی نیست گفتگوها پیام‌ها diff --git a/app/src/main/res/values-fr/strings_conversations.xml b/app/src/main/res/values-fr/strings_conversations.xml index 5da45690..a65732d1 100644 --- a/app/src/main/res/values-fr/strings_conversations.xml +++ b/app/src/main/res/values-fr/strings_conversations.xml @@ -13,6 +13,7 @@ Message texte Archivées Spams et conversations bloquées + Retour en haut Aucune conversation spam ou bloquée Conversations Messages diff --git a/app/src/main/res/values-hi/strings_conversations.xml b/app/src/main/res/values-hi/strings_conversations.xml index 4c24f7e7..1abb6244 100644 --- a/app/src/main/res/values-hi/strings_conversations.xml +++ b/app/src/main/res/values-hi/strings_conversations.xml @@ -14,6 +14,7 @@ पाठ संदेश संग्रहीत स्पैम और ब्लॉक + ऊपर जाएँ कोई स्पैम या ब्लॉक किया गया संवाद नहीं संवाद संदेश diff --git a/app/src/main/res/values-ja/strings_conversations.xml b/app/src/main/res/values-ja/strings_conversations.xml index c292528a..ab5e8f6a 100644 --- a/app/src/main/res/values-ja/strings_conversations.xml +++ b/app/src/main/res/values-ja/strings_conversations.xml @@ -14,6 +14,7 @@ テキストメッセージ アーカイブ 迷惑メールとブロック + 一番上へ 迷惑メールまたはブロックされた会話はありません 会話 メッセージ diff --git a/app/src/main/res/values-ko/strings_conversations.xml b/app/src/main/res/values-ko/strings_conversations.xml index a8f9fa3e..2f294e4e 100644 --- a/app/src/main/res/values-ko/strings_conversations.xml +++ b/app/src/main/res/values-ko/strings_conversations.xml @@ -14,6 +14,7 @@ 문자 메시지 보관됨 스팸 및 차단됨 + 맨 위로 스팸 또는 차단된 대화가 없습니다 대화 메시지 diff --git a/app/src/main/res/values-pl/strings_conversations.xml b/app/src/main/res/values-pl/strings_conversations.xml index 6f2672bd..34f433c7 100644 --- a/app/src/main/res/values-pl/strings_conversations.xml +++ b/app/src/main/res/values-pl/strings_conversations.xml @@ -14,6 +14,7 @@ Wiadomość tekstowa Zarchiwizowane Spam i zablokowane + Wróć na górę Brak rozmów ze spamem lub zablokowanych Rozmowy Wiadomości diff --git a/app/src/main/res/values-pt-rBR/strings_conversations.xml b/app/src/main/res/values-pt-rBR/strings_conversations.xml index 36c0e450..7b9af076 100644 --- a/app/src/main/res/values-pt-rBR/strings_conversations.xml +++ b/app/src/main/res/values-pt-rBR/strings_conversations.xml @@ -14,6 +14,7 @@ Mensagem de texto Arquivadas Spam e bloqueadas + Voltar ao topo Nenhuma conversa de spam ou bloqueada Conversas Mensagens diff --git a/app/src/main/res/values-ru/strings_conversations.xml b/app/src/main/res/values-ru/strings_conversations.xml index 294c0c0b..2d7f5c19 100644 --- a/app/src/main/res/values-ru/strings_conversations.xml +++ b/app/src/main/res/values-ru/strings_conversations.xml @@ -14,6 +14,7 @@ Текстовое сообщение Архив Спам и блокировка + Наверх Нет переписок со спамом или заблокированных Переписки Сообщения diff --git a/app/src/main/res/values-zh-rCN/strings_conversations.xml b/app/src/main/res/values-zh-rCN/strings_conversations.xml index 71c62023..d57ad7ab 100644 --- a/app/src/main/res/values-zh-rCN/strings_conversations.xml +++ b/app/src/main/res/values-zh-rCN/strings_conversations.xml @@ -13,6 +13,7 @@ 输入消息 已归档 垃圾邮件和已阻止 + 回到顶部 没有垃圾邮件或已阻止的对话 对话 消息 diff --git a/app/src/main/res/values-zh-rTW/strings_conversations.xml b/app/src/main/res/values-zh-rTW/strings_conversations.xml index ca891641..1b2be7a3 100644 --- a/app/src/main/res/values-zh-rTW/strings_conversations.xml +++ b/app/src/main/res/values-zh-rTW/strings_conversations.xml @@ -13,6 +13,7 @@ 輸入訊息 已封存 垃圾郵件與已封鎖 + 回到頂端 沒有垃圾郵件或已封鎖的對話 對話 訊息 diff --git a/app/src/main/res/values/strings_conversations.xml b/app/src/main/res/values/strings_conversations.xml index 60954dc3..59ba088d 100644 --- a/app/src/main/res/values/strings_conversations.xml +++ b/app/src/main/res/values/strings_conversations.xml @@ -14,6 +14,7 @@ Message Archived Spam & Blocked + Back to top No spam or blocked conversations Conversations Messages diff --git a/app/src/test/java/com/anindra/messages/ui/BackToTopTest.kt b/app/src/test/java/com/anindra/messages/ui/BackToTopTest.kt new file mode 100644 index 00000000..6ba34238 --- /dev/null +++ b/app/src/test/java/com/anindra/messages/ui/BackToTopTest.kt @@ -0,0 +1,37 @@ +package com.anindra.messages.ui + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The "back to top" shortcut is only worth a button once the top is far enough + * away that scrolling back is a chore: a list one flick from the top gets a + * shortcut it does not need, sitting on the rows. + */ +class BackToTopTest { + + @Test + fun aListAtTheTopOffersNoShortcut() { + assertFalse(BackToTop.visible(firstVisibleIndex = 0)) + } + + @Test + fun aListAFewRowsDownOffersNoShortcut() { + assertFalse(BackToTop.visible(firstVisibleIndex = BackToTop.THRESHOLD - 1)) + } + + @Test + fun theButtonIsDrawnAtSeventyPercent() { + // It floats over the rows in the middle of the screen: fully opaque it + // reads as a hole punched through the list. + assertEquals(0.7f, BackToTop.ALPHA, 0.001f) + } + + @Test + fun aListScrolledPastTheThresholdOffersOne() { + assertTrue(BackToTop.visible(firstVisibleIndex = BackToTop.THRESHOLD)) + assertTrue(BackToTop.visible(firstVisibleIndex = 40)) + } +} diff --git a/scripts b/scripts index 5deba1ed..1819f411 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 5deba1ed0b2642bd162ff699826af5200e3d4399 +Subproject commit 1819f41150b27ae48363f2907d4d6c6613f2e61b