diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationListItem.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationListItem.kt index 2e347f4009c..d270df13b33 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationListItem.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationListItem.kt @@ -15,7 +15,6 @@ import android.widget.ImageView import androidx.annotation.DrawableRes import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background -import androidx.compose.foundation.border import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Box @@ -30,7 +29,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.InlineTextContent import androidx.compose.foundation.text.appendInlineContent import androidx.compose.material3.Icon @@ -102,8 +100,6 @@ private const val ICON_MSG_SIZE_DP = 14 private const val ICON_MSG_SPACING_DP = 2 private const val THREAD_ICON_ID = "threadIcon" private const val THREAD_ICON_SIZE_SP = 14 -private const val UNREAD_THRESHOLD = 1000 -private const val UNREAD_BUBBLE_STROKE_DP = 1.5f private const val MILLIS_PER_SECOND = 1_000L /** Groups the tap callbacks for [ConversationListItem] to keep the parameter count low. */ @@ -519,75 +515,12 @@ private fun ConversationLastMessageRow( private fun UnreadBubble(model: ConversationModel, currentUser: User) { if (model.unreadMessages <= 0) return - val text = if (model.unreadMessages >= UNREAD_THRESHOLD) { - stringResource(R.string.tooManyUnreadMessages) - } else { - model.unreadMessages.toString() - } - - val isOneToOne = model.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL val hasDmFlag = hasSpreedFeatureCapability( currentUser.capabilities?.spreedCapability, SpreedFeatures.DIRECT_MENTION_FLAG ) - val outlined = model.unreadMention && hasDmFlag && !model.unreadMentionDirect && !isOneToOne - - when { - outlined -> OutlinedUnreadChip(text = text) - isOneToOne || model.unreadMention -> FilledUnreadChip(text = text) - else -> GreyUnreadChip(text = text) - } -} - -@Composable -private fun FilledUnreadChip(text: String) { - Box( - modifier = Modifier - .background(MaterialTheme.colorScheme.primary, RoundedCornerShape(12.dp)) - .padding(horizontal = 6.dp, vertical = 2.dp), - contentAlignment = Alignment.Center - ) { - Text( - text = text, - color = MaterialTheme.colorScheme.onPrimary, - style = MaterialTheme.typography.labelSmall, - fontWeight = FontWeight.Bold - ) - } -} - -@Composable -private fun OutlinedUnreadChip(text: String) { - Box( - modifier = Modifier - .border(UNREAD_BUBBLE_STROKE_DP.dp, MaterialTheme.colorScheme.primary, RoundedCornerShape(12.dp)) - .padding(horizontal = 6.dp, vertical = 2.dp), - contentAlignment = Alignment.Center - ) { - Text( - text = text, - color = MaterialTheme.colorScheme.primary, - style = MaterialTheme.typography.labelSmall, - fontWeight = FontWeight.Bold - ) - } -} - -@Composable -private fun GreyUnreadChip(text: String) { - Box( - modifier = Modifier - .background(colorResource(R.color.conversation_unread_bubble), RoundedCornerShape(12.dp)) - .padding(horizontal = 6.dp, vertical = 2.dp), - contentAlignment = Alignment.Center - ) { - Text( - text = text, - color = colorResource(R.color.conversation_unread_bubble_text), - style = MaterialTheme.typography.labelSmall, - fontWeight = FontWeight.Bold - ) - } + val style = unreadBadgeStyle(model, hasDmFlag) ?: return + UnreadCountBadge(count = model.unreadMessages, style = style) } @Suppress("LongMethod", "CyclomaticComplexMethod", "ReturnCount") diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationTagsRow.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationTagsRow.kt index bb6d617c731..484ba5c882e 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationTagsRow.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationTagsRow.kt @@ -28,13 +28,17 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.semantics.clearAndSetSemantics +import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.platform.LocalViewConfiguration import androidx.compose.ui.res.dimensionResource +import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import com.nextcloud.talk.R +import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.models.json.tags.ConversationTagDto import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collectLatest @@ -42,7 +46,7 @@ import kotlinx.coroutines.flow.collectLatest @OptIn(ExperimentalMaterial3Api::class) @Composable fun ConversationTagsRow( - tags: List, + tags: List, selectedTagId: String?, onTagSelected: (String?) -> Unit, onManageTagsClick: () -> Unit, @@ -61,12 +65,13 @@ fun ConversationTagsRow( onLongClick = onManageTagsClick ) } - items(tags, key = { it.id }) { tag -> + items(tags, key = { it.tag.id }) { (tag, unreadInfo) -> val isSelected = tag.id == selectedTagId val isFavorites = tag.type == ConversationTagDto.TYPE_FAVORITES TagFilterChip( selected = isSelected, label = if (isFavorites) stringResource(R.string.nc_conversation_tags_favorites) else tag.name, + unreadInfo = unreadInfo, onClick = { onTagSelected(if (isSelected) null else tag.id) }, onLongClick = onManageTagsClick ) @@ -77,7 +82,13 @@ fun ConversationTagsRow( /** Detects long-press via the chip's own interactionSource so its native ripple stays untouched. */ @OptIn(ExperimentalMaterial3Api::class) @Composable -private fun TagFilterChip(selected: Boolean, label: String, onClick: () -> Unit, onLongClick: () -> Unit) { +private fun TagFilterChip( + selected: Boolean, + label: String, + onClick: () -> Unit, + onLongClick: () -> Unit, + unreadInfo: TagUnreadInfo? = null +) { val interactionSource = remember { MutableInteractionSource() } val hapticFeedback = LocalHapticFeedback.current val longPressTimeoutMillis = LocalViewConfiguration.current.longPressTimeoutMillis @@ -102,16 +113,67 @@ private fun TagFilterChip(selected: Boolean, label: String, onClick: () -> Unit, suppressClick = false }, label = { Text(label) }, + trailingIcon = unreadInfo?.let { info -> + { + val description = pluralStringResource( + R.plurals.nc_conversation_tags_unread_conversations, + info.conversationCount, + info.conversationCount + ) + UnreadCountBadge( + count = info.conversationCount, + style = info.style, + modifier = Modifier.clearAndSetSemantics { contentDescription = description } + ) + } + }, interactionSource = interactionSource ) } +/** + * Unread state of a tag filter chip: [conversationCount] is the number of conversations with this tag + * that have unread messages (not the number of unread messages), [style] is the most important + * badge style among those conversations (direct mention > group mention > plain unread). + */ +data class TagUnreadInfo(val conversationCount: Int, val style: UnreadBadgeStyle) + +/** A tag filter chip; [unreadInfo] is null when no conversation with this tag has unread messages. */ +data class ConversationTagChip(val tag: ConversationTagDto, val unreadInfo: TagUnreadInfo? = null) + +/** Builds the chips for all [tags] that have at least one of the given [rooms]. */ +fun buildConversationTagChips( + tags: List, + rooms: List, + hasDirectMentionFlag: Boolean +): List { + val distinctRooms = rooms.distinctBy { it.token } + return tags.mapNotNull { tag -> + val isFavorites = tag.type == ConversationTagDto.TYPE_FAVORITES + val taggedRooms = distinctRooms.filter { if (isFavorites) it.favorite else it.tagIds.contains(tag.id) } + if (taggedRooms.isEmpty()) return@mapNotNull null + + val styles = taggedRooms.mapNotNull { unreadBadgeStyle(it, hasDirectMentionFlag) } + val unreadInfo = styles.maxOrNull()?.let { TagUnreadInfo(conversationCount = styles.size, style = it) } + ConversationTagChip(tag, unreadInfo) + } +} + private fun previewTags() = listOf( - ConversationTagDto(id = "favorites", name = "", type = ConversationTagDto.TYPE_FAVORITES), - ConversationTagDto(id = "1", name = "Work", sortOrder = 0), - ConversationTagDto(id = "2", name = "Family", sortOrder = 1), - ConversationTagDto(id = "3", name = "Projects", sortOrder = 2) + ConversationTagChip( + ConversationTagDto(id = "favorites", name = "", type = ConversationTagDto.TYPE_FAVORITES), + TagUnreadInfo(conversationCount = 1, style = UnreadBadgeStyle.FILLED) + ), + ConversationTagChip( + ConversationTagDto(id = "1", name = "Work", sortOrder = 0), + TagUnreadInfo(conversationCount = 3, style = UnreadBadgeStyle.OUTLINED) + ), + ConversationTagChip( + ConversationTagDto(id = "2", name = "Family", sortOrder = 1), + TagUnreadInfo(conversationCount = 2, style = UnreadBadgeStyle.GREY) + ), + ConversationTagChip(ConversationTagDto(id = "3", name = "Projects", sortOrder = 2)) ) @Preview(showBackground = true, name = "Light") diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationsListScreen.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationsListScreen.kt index b8cfb4d7c11..7bb1133e070 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationsListScreen.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/ConversationsListScreen.kt @@ -186,17 +186,15 @@ fun ConversationsListScreen( val hasConversationTagsCapability = state.currentUser?.capabilities?.spreedCapability?.let { CapabilitiesUtil.hasSpreedFeatureCapability(it, SpreedFeatures.CONVERSATION_TAGS) } == true - val nonEmptyConversationTags = remember(conversationTags, visibleRooms) { - conversationTags.filter { tag -> - if (tag.type == ConversationTagDto.TYPE_FAVORITES) { - visibleRooms.any { it.favorite } - } else { - visibleRooms.any { it.tagIds.contains(tag.id) } - } - } + val hasDirectMentionFlag = CapabilitiesUtil.hasSpreedFeatureCapability( + state.currentUser?.capabilities?.spreedCapability, + SpreedFeatures.DIRECT_MENTION_FLAG + ) + val conversationTagChips = remember(conversationTags, visibleRooms, hasDirectMentionFlag) { + buildConversationTagChips(conversationTags, visibleRooms, hasDirectMentionFlag) } val showConversationTagsRow = hasConversationTagsCapability && - nonEmptyConversationTags.isNotEmpty() && + conversationTagChips.isNotEmpty() && !isSearchActive && !showShareTo && !isForward @@ -342,7 +340,7 @@ fun ConversationsListScreen( tagsRowContent = if (showConversationTagsRow) { { ConversationTagsRow( - tags = nonEmptyConversationTags, + tags = conversationTagChips, selectedTagId = selectedTagFilter, onTagSelected = { tagId -> val isFavorites = conversationTags.any { diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ui/UnreadCountBadge.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/UnreadCountBadge.kt new file mode 100644 index 00000000000..b0051b6c277 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ui/UnreadCountBadge.kt @@ -0,0 +1,106 @@ +/* + * Nextcloud Talk - Android Client + * + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package com.nextcloud.talk.conversationlist.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.layout +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import com.nextcloud.talk.R +import com.nextcloud.talk.models.domain.ConversationModel +import com.nextcloud.talk.models.json.conversations.ConversationEnums + +private const val UNREAD_THRESHOLD = 1000 +private const val UNREAD_BUBBLE_STROKE_DP = 1.5f + +/** Visual emphasis of an unread counter, ordered from lowest to highest priority. */ +enum class UnreadBadgeStyle { + /** Unread messages without a mention. */ + GREY, + + /** Unread group mention (e.g. @all) without a direct mention. */ + OUTLINED, + + /** Unread direct mention, or any unread message in a one-to-one conversation. */ + FILLED +} + +/** Returns the badge style for a conversation's unread counter, or null if it has no unread messages. */ +fun unreadBadgeStyle(model: ConversationModel, hasDirectMentionFlag: Boolean): UnreadBadgeStyle? { + if (model.unreadMessages <= 0) return null + + val isOneToOne = model.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL + val outlined = model.unreadMention && hasDirectMentionFlag && !model.unreadMentionDirect && !isOneToOne + + return when { + outlined -> UnreadBadgeStyle.OUTLINED + isOneToOne || model.unreadMention -> UnreadBadgeStyle.FILLED + else -> UnreadBadgeStyle.GREY + } +} + +@Composable +fun UnreadCountBadge(count: Int, style: UnreadBadgeStyle, modifier: Modifier = Modifier) { + val text = if (count >= UNREAD_THRESHOLD) { + stringResource(R.string.tooManyUnreadMessages) + } else { + count.toString() + } + + val shape = CircleShape + val (boxModifier, textColor) = when (style) { + UnreadBadgeStyle.FILLED -> + Modifier.background(MaterialTheme.colorScheme.primary, shape) to MaterialTheme.colorScheme.onPrimary + + UnreadBadgeStyle.OUTLINED -> + Modifier.border(UNREAD_BUBBLE_STROKE_DP.dp, MaterialTheme.colorScheme.primary, shape) to + MaterialTheme.colorScheme.primary + + UnreadBadgeStyle.GREY -> + Modifier.background(colorResource(R.color.conversation_unread_bubble), shape) to + colorResource(R.color.conversation_unread_bubble_text) + } + + UnreadBadgeBox(text = text, textColor = textColor, modifier = modifier.atLeastAsWideAsTall().then(boxModifier)) +} + +/** Makes single digit badges a circle, longer numbers stay a pill. */ +private fun Modifier.atLeastAsWideAsTall(): Modifier = + layout { measurable, constraints -> + val height = measurable.minIntrinsicHeight(constraints.maxWidth) + val minWidth = height.coerceIn(constraints.minWidth, constraints.maxWidth) + val placeable = measurable.measure(constraints.copy(minWidth = minWidth)) + layout(placeable.width, placeable.height) { placeable.placeRelative(0, 0) } + } + +@Composable +private fun UnreadBadgeBox(text: String, textColor: Color, modifier: Modifier) { + Box( + modifier = modifier.padding(horizontal = 6.dp, vertical = 2.dp), + contentAlignment = Alignment.Center + ) { + Text( + text = text, + color = textColor, + style = MaterialTheme.typography.labelSmall, + fontWeight = FontWeight.Bold + ) + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8aa0a54909..88040fe3c62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1044,6 +1044,10 @@ How to translate with transifex: Once a conversation is archived, it will be hidden by default. Select the filter \"Archived\" to view archived conversations. Direct mentions will still be received. Once a conversation is unarchived, it will be shown by default again. All + + %d conversation with unread messages + %d conversations with unread messages + Tags Manage tags New tag diff --git a/app/src/test/java/com/nextcloud/talk/conversationlist/ui/ConversationTagChipsTest.kt b/app/src/test/java/com/nextcloud/talk/conversationlist/ui/ConversationTagChipsTest.kt new file mode 100644 index 00000000000..25e2d4fce61 --- /dev/null +++ b/app/src/test/java/com/nextcloud/talk/conversationlist/ui/ConversationTagChipsTest.kt @@ -0,0 +1,142 @@ +/* + * Nextcloud Talk - Android Client + * + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package com.nextcloud.talk.conversationlist.ui + +import com.nextcloud.talk.data.user.model.User +import com.nextcloud.talk.models.domain.ConversationModel +import com.nextcloud.talk.models.json.conversations.ConversationDto +import com.nextcloud.talk.models.json.conversations.ConversationEnums +import com.nextcloud.talk.models.json.tags.ConversationTagDto +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class ConversationTagChipsTest { + + private val workTag = ConversationTagDto(id = "work", name = "Work") + private val favoritesTag = ConversationTagDto(id = "fav", name = "", type = ConversationTagDto.TYPE_FAVORITES) + + @Test + fun `counts conversations with unread messages, not the unread messages themselves`() { + val rooms = listOf( + conversation("a", tagIds = listOf("work"), unreadMessages = 5), + conversation("b", tagIds = listOf("work"), unreadMessages = 1), + conversation("c", tagIds = listOf("work"), unreadMessages = 0), + conversation("d", tagIds = listOf("other"), unreadMessages = 3) + ) + + val info = chips(listOf(workTag), rooms).single().unreadInfo!! + + assertEquals(2, info.conversationCount) + assertEquals(UnreadBadgeStyle.GREY, info.style) + } + + @Test + fun `no unread info when no tagged conversation is unread`() { + val rooms = listOf(conversation("a", tagIds = listOf("work"), unreadMessages = 0)) + + assertNull(chips(listOf(workTag), rooms).single().unreadInfo) + } + + @Test + fun `tags without conversations get no chip`() { + val rooms = listOf(conversation("a", tagIds = listOf("work"), unreadMessages = 1)) + + assertEquals(listOf(workTag), chips(listOf(workTag, favoritesTag), rooms).map { it.tag }) + } + + @Test + fun `duplicate conversations are counted once`() { + val room = conversation("a", tagIds = listOf("work"), unreadMessages = 1) + + assertEquals(1, chips(listOf(workTag), listOf(room, room.copy())).single().unreadInfo!!.conversationCount) + } + + @Test + fun `favorites tag counts unread favorite conversations`() { + val rooms = listOf( + conversation("a", favorite = true, unreadMessages = 2), + conversation("b", favorite = false, unreadMessages = 2) + ) + + assertEquals(1, chips(listOf(favoritesTag), rooms).single().unreadInfo!!.conversationCount) + } + + @Test + fun `direct mention wins over group mention and plain unread`() { + val rooms = listOf( + conversation("a", tagIds = listOf("work"), unreadMessages = 1), + conversation("b", tagIds = listOf("work"), unreadMessages = 1, unreadMention = true), + conversation( + "c", + tagIds = listOf("work"), + unreadMessages = 1, + unreadMention = true, + unreadMentionDirect = true + ) + ) + + assertEquals(UnreadBadgeStyle.FILLED, chips(listOf(workTag), rooms).single().unreadInfo!!.style) + } + + @Test + fun `group mention is outlined with the direct mention flag`() { + val rooms = listOf( + conversation("a", tagIds = listOf("work"), unreadMessages = 1), + conversation("b", tagIds = listOf("work"), unreadMessages = 1, unreadMention = true) + ) + + assertEquals(UnreadBadgeStyle.OUTLINED, chips(listOf(workTag), rooms).single().unreadInfo!!.style) + assertEquals( + "without the flag every mention is treated as direct", + UnreadBadgeStyle.FILLED, + chips(listOf(workTag), rooms, hasDirectMentionFlag = false).single().unreadInfo!!.style + ) + } + + @Test + fun `unread one-to-one conversation is highlighted like a direct mention`() { + val rooms = listOf( + conversation( + "a", + tagIds = listOf("work"), + unreadMessages = 1, + type = ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL + ) + ) + + assertEquals(UnreadBadgeStyle.FILLED, chips(listOf(workTag), rooms).single().unreadInfo!!.style) + } + + private fun chips( + tags: List, + rooms: List, + hasDirectMentionFlag: Boolean = true + ) = buildConversationTagChips(tags, rooms, hasDirectMentionFlag) + + @Suppress("LongParameterList") + private fun conversation( + token: String, + tagIds: List = emptyList(), + favorite: Boolean = false, + unreadMessages: Int = 0, + unreadMention: Boolean = false, + unreadMentionDirect: Boolean = false, + type: ConversationEnums.ConversationType = ConversationEnums.ConversationType.ROOM_GROUP_CALL + ): ConversationModel = + ConversationModel.mapToConversationModel( + ConversationDto(token = token, name = token, type = type), + User(id = 1L, userId = "me", username = "me", baseUrl = "https://server.example.com") + ).copy( + tagIds = tagIds, + favorite = favorite, + unreadMessages = unreadMessages, + unreadMention = unreadMention, + unreadMentionDirect = unreadMentionDirect + ) +}