Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = {})
}
}
}
74 changes: 74 additions & 0 deletions app/src/main/java/com/anindra/messages/ui/BackToTop.kt
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/anindra/messages/ui/ConversationsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Map<Long, Int>>(emptyMap()) }
var userScrolledAway by remember { mutableStateOf(false) }
var unreadSeeded by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -502,6 +510,7 @@ fun ConversationsScreen(
}
}

Box(Modifier.fillMaxSize()) {
Scaffold(
containerColor = MaterialTheme.colorScheme.background,
snackbarHost = { SnackbarHost(snackbarHostState) },
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">رسالة نصية</string>
<string name="conversations_archived">المؤرشفة</string>
<string name="conversations_spam_blocked">الرسائل غير المرغوبة والمحظورة</string>
<string name="conversations_back_to_top">العودة إلى الأعلى</string>
<string name="conversations_spam_blocked_empty">لا توجد محادثات غير مرغوبة أو محظورة</string>
<string name="spam_tab_conversations">المحادثات</string>
<string name="spam_tab_messages">الرسائل</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="text_placeholder">Textnachricht</string>
<string name="conversations_archived">Archiviert</string>
<string name="conversations_spam_blocked">Spam und blockiert</string>
<string name="conversations_back_to_top">Nach oben</string>
<string name="conversations_spam_blocked_empty">Keine Spam- oder blockierten Unterhaltungen</string>
<string name="spam_tab_conversations">Unterhaltungen</string>
<string name="spam_tab_messages">Nachrichten</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">Mensaje de texto</string>
<string name="conversations_archived">Archivadas</string>
<string name="conversations_spam_blocked">Spam y bloqueadas</string>
<string name="conversations_back_to_top">Volver arriba</string>
<string name="conversations_spam_blocked_empty">No hay conversaciones de spam ni bloqueadas</string>
<string name="spam_tab_conversations">Conversaciones</string>
<string name="spam_tab_messages">Mensajes</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fa/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">پیام</string>
<string name="conversations_archived">بایگانی‌شده</string>
<string name="conversations_spam_blocked">هرزنامه و مسدود</string>
<string name="conversations_back_to_top">بازگشت به بالا</string>
<string name="conversations_spam_blocked_empty">هیچ گفتگوی هرزنامه یا مسدودی نیست</string>
<string name="spam_tab_conversations">گفتگوها</string>
<string name="spam_tab_messages">پیام‌ها</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="text_placeholder">Message texte</string>
<string name="conversations_archived">Archivées</string>
<string name="conversations_spam_blocked">Spams et conversations bloquées</string>
<string name="conversations_back_to_top">Retour en haut</string>
<string name="conversations_spam_blocked_empty">Aucune conversation spam ou bloquée</string>
<string name="spam_tab_conversations">Conversations</string>
<string name="spam_tab_messages">Messages</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-hi/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">पाठ संदेश</string>
<string name="conversations_archived">संग्रहीत</string>
<string name="conversations_spam_blocked">स्पैम और ब्लॉक</string>
<string name="conversations_back_to_top">ऊपर जाएँ</string>
<string name="conversations_spam_blocked_empty">कोई स्पैम या ब्लॉक किया गया संवाद नहीं</string>
<string name="spam_tab_conversations">संवाद</string>
<string name="spam_tab_messages">संदेश</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">テキストメッセージ</string>
<string name="conversations_archived">アーカイブ</string>
<string name="conversations_spam_blocked">迷惑メールとブロック</string>
<string name="conversations_back_to_top">一番上へ</string>
<string name="conversations_spam_blocked_empty">迷惑メールまたはブロックされた会話はありません</string>
<string name="spam_tab_conversations">会話</string>
<string name="spam_tab_messages">メッセージ</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ko/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">문자 메시지</string>
<string name="conversations_archived">보관됨</string>
<string name="conversations_spam_blocked">스팸 및 차단됨</string>
<string name="conversations_back_to_top">맨 위로</string>
<string name="conversations_spam_blocked_empty">스팸 또는 차단된 대화가 없습니다</string>
<string name="spam_tab_conversations">대화</string>
<string name="spam_tab_messages">메시지</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-pl/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">Wiadomość tekstowa</string>
<string name="conversations_archived">Zarchiwizowane</string>
<string name="conversations_spam_blocked">Spam i zablokowane</string>
<string name="conversations_back_to_top">Wróć na górę</string>
<string name="conversations_spam_blocked_empty">Brak rozmów ze spamem lub zablokowanych</string>
<string name="spam_tab_conversations">Rozmowy</string>
<string name="spam_tab_messages">Wiadomości</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">Mensagem de texto</string>
<string name="conversations_archived">Arquivadas</string>
<string name="conversations_spam_blocked">Spam e bloqueadas</string>
<string name="conversations_back_to_top">Voltar ao topo</string>
<string name="conversations_spam_blocked_empty">Nenhuma conversa de spam ou bloqueada</string>
<string name="spam_tab_conversations">Conversas</string>
<string name="spam_tab_messages">Mensagens</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">Текстовое сообщение</string>
<string name="conversations_archived">Архив</string>
<string name="conversations_spam_blocked">Спам и блокировка</string>
<string name="conversations_back_to_top">Наверх</string>
<string name="conversations_spam_blocked_empty">Нет переписок со спамом или заблокированных</string>
<string name="spam_tab_conversations">Переписки</string>
<string name="spam_tab_messages">Сообщения</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="text_placeholder">输入消息</string>
<string name="conversations_archived">已归档</string>
<string name="conversations_spam_blocked">垃圾邮件和已阻止</string>
<string name="conversations_back_to_top">回到顶部</string>
<string name="conversations_spam_blocked_empty">没有垃圾邮件或已阻止的对话</string>
<string name="spam_tab_conversations">对话</string>
<string name="spam_tab_messages">消息</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="text_placeholder">輸入訊息</string>
<string name="conversations_archived">已封存</string>
<string name="conversations_spam_blocked">垃圾郵件與已封鎖</string>
<string name="conversations_back_to_top">回到頂端</string>
<string name="conversations_spam_blocked_empty">沒有垃圾郵件或已封鎖的對話</string>
<string name="spam_tab_conversations">對話</string>
<string name="spam_tab_messages">訊息</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="text_placeholder">Message</string>
<string name="conversations_archived">Archived</string>
<string name="conversations_spam_blocked">Spam &amp; Blocked</string>
<string name="conversations_back_to_top">Back to top</string>
<string name="conversations_spam_blocked_empty">No spam or blocked conversations</string>
<string name="spam_tab_conversations">Conversations</string>
<string name="spam_tab_messages">Messages</string>
Expand Down
37 changes: 37 additions & 0 deletions app/src/test/java/com/anindra/messages/ui/BackToTopTest.kt
Original file line number Diff line number Diff line change
@@ -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))
}
}
2 changes: 1 addition & 1 deletion scripts
Submodule scripts updated 1 files
+135 −0 test-back-to-top.sh