|
| 1 | +package com.anytypeio.anytype.ui.home |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.Spacer |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.height |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.layout.size |
| 13 | +import androidx.compose.foundation.layout.width |
| 14 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 15 | +import androidx.compose.material.Text |
| 16 | +import androidx.compose.runtime.Composable |
| 17 | +import androidx.compose.ui.Alignment |
| 18 | +import androidx.compose.ui.Modifier |
| 19 | +import androidx.compose.ui.res.colorResource |
| 20 | +import androidx.compose.ui.res.stringResource |
| 21 | +import androidx.compose.ui.text.style.TextOverflow |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import androidx.compose.ui.unit.sp |
| 24 | +import com.anytypeio.anytype.R |
| 25 | +import com.anytypeio.anytype.core_models.ObjectWrapper |
| 26 | +import com.anytypeio.anytype.core_models.Relations |
| 27 | +import com.anytypeio.anytype.core_models.chats.Chat |
| 28 | +import com.anytypeio.anytype.core_models.chats.NotificationState |
| 29 | +import com.anytypeio.anytype.core_ui.views.BodySemiBold |
| 30 | +import com.anytypeio.anytype.core_ui.views.PreviewTitle2Medium |
| 31 | +import com.anytypeio.anytype.core_ui.views.Relations2 |
| 32 | +import com.anytypeio.anytype.core_ui.views.Relations3 |
| 33 | +import com.anytypeio.anytype.core_ui.views.Title3 |
| 34 | +import com.anytypeio.anytype.core_ui.widgets.ListWidgetObjectIcon |
| 35 | +import com.anytypeio.anytype.data.auth.repo.config.GatewayProvider |
| 36 | +import com.anytypeio.anytype.domain.config.ConfigStorage |
| 37 | +import com.anytypeio.anytype.domain.misc.UrlBuilder |
| 38 | +import com.anytypeio.anytype.domain.objects.StoreOfObjectTypes |
| 39 | +import com.anytypeio.anytype.domain.objects.getTypeOfObject |
| 40 | +import com.anytypeio.anytype.domain.primitives.FieldParser |
| 41 | +import com.anytypeio.anytype.other.BasicLogger |
| 42 | +import com.anytypeio.anytype.presentation.mapper.objectIcon |
| 43 | +import com.anytypeio.anytype.presentation.objects.ObjectIcon |
| 44 | +import com.anytypeio.anytype.presentation.vault.VaultSpaceView |
| 45 | +import com.anytypeio.anytype.ui.vault.UnreadIndicatorsRow |
| 46 | +import com.anytypeio.anytype.ui.vault.buildChatContentWithInlineIcons |
| 47 | +import com.anytypeio.anytype.ui.vault.getChatTextColor |
| 48 | + |
| 49 | +/** |
| 50 | + * Compact chat widget card for home screen and widgets. |
| 51 | + * |
| 52 | + * Features: |
| 53 | + * - 72dp height (compact vs 96dp vault cards) |
| 54 | + * - 40dp icon size (compact vs 64dp vault cards) |
| 55 | + * - Chat object icon (not space icon) |
| 56 | + * - Unread counts from chat preview state (not space-aggregated) |
| 57 | + * - Single-line message preview with creator name |
| 58 | + * - Unread badges (message count + mention icon) |
| 59 | + * |
| 60 | + */ |
| 61 | +@Composable |
| 62 | +fun ChatWidgetCard( |
| 63 | + modifier: Modifier = Modifier, |
| 64 | + chatIcon: ObjectIcon, |
| 65 | + chatName: String, |
| 66 | + creatorName: String? = null, |
| 67 | + messageText: String? = null, |
| 68 | + messageTime: String? = null, |
| 69 | + attachmentPreviews: List<VaultSpaceView.AttachmentPreview> = emptyList(), |
| 70 | + unreadMessageCount: Int, |
| 71 | + unreadMentionCount: Int, |
| 72 | + chatNotificationState: NotificationState, |
| 73 | + onClick: () -> Unit |
| 74 | +) { |
| 75 | + |
| 76 | + Row( |
| 77 | + modifier = modifier |
| 78 | + .fillMaxWidth() |
| 79 | + .height(72.dp) |
| 80 | + .background( |
| 81 | + color = colorResource(id = R.color.background_secondary), |
| 82 | + shape = RoundedCornerShape(16.dp) |
| 83 | + ) |
| 84 | + .clickable(onClick = onClick) |
| 85 | + .padding(vertical = 12.dp), |
| 86 | + verticalAlignment = Alignment.CenterVertically |
| 87 | + ) { |
| 88 | + // Chat icon (40dp) |
| 89 | + ListWidgetObjectIcon( |
| 90 | + icon = chatIcon, |
| 91 | + modifier = Modifier |
| 92 | + .padding(start = 16.dp) |
| 93 | + .size(48.dp), |
| 94 | + iconSize = 48.dp |
| 95 | + ) |
| 96 | + |
| 97 | + Spacer(modifier = Modifier.width(12.dp)) |
| 98 | + |
| 99 | + // Chat content |
| 100 | + Column( |
| 101 | + modifier = Modifier |
| 102 | + .weight(1f) |
| 103 | + .padding(end = 16.dp), |
| 104 | + verticalArrangement = Arrangement.spacedBy(2.dp) |
| 105 | + ) { |
| 106 | + // Line 1: Chat Name + Time |
| 107 | + Row( |
| 108 | + modifier = Modifier.fillMaxWidth(), |
| 109 | + verticalAlignment = Alignment.CenterVertically |
| 110 | + ) { |
| 111 | + // Determine text color for chat name |
| 112 | + val chatNameColor = getChatTextColor( |
| 113 | + notificationMode = chatNotificationState, |
| 114 | + unreadMessageCount = unreadMessageCount, |
| 115 | + unreadMentionCount = unreadMentionCount |
| 116 | + ) |
| 117 | + |
| 118 | + Text( |
| 119 | + text = chatName, |
| 120 | + style = PreviewTitle2Medium, |
| 121 | + color = chatNameColor, |
| 122 | + maxLines = 1, |
| 123 | + overflow = TextOverflow.Ellipsis, |
| 124 | + modifier = Modifier.weight(1f) |
| 125 | + ) |
| 126 | + |
| 127 | + if (messageTime != null) { |
| 128 | + Spacer(modifier = Modifier.width(6.dp)) |
| 129 | + Text( |
| 130 | + text = messageTime, |
| 131 | + style = Relations3, |
| 132 | + color = colorResource(id = R.color.text_secondary) |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Line 2: Message Preview + Indicators |
| 138 | + Row( |
| 139 | + modifier = Modifier.fillMaxWidth(), |
| 140 | + verticalAlignment = Alignment.CenterVertically |
| 141 | + ) { |
| 142 | + // Determine text color for message preview |
| 143 | + val textColor = getChatTextColor( |
| 144 | + notificationMode = chatNotificationState, |
| 145 | + unreadMessageCount = unreadMessageCount, |
| 146 | + unreadMentionCount = unreadMentionCount |
| 147 | + ) |
| 148 | + |
| 149 | + val (chatText, inlineContent) = buildChatContentWithInlineIcons( |
| 150 | + creatorName = creatorName, |
| 151 | + messageText = messageText, |
| 152 | + attachmentPreviews = attachmentPreviews, |
| 153 | + fallbackSubtitle = "", // Empty fallback for now |
| 154 | + singleLineFormat = true, |
| 155 | + textColor = textColor |
| 156 | + ) |
| 157 | + |
| 158 | + Text( |
| 159 | + text = chatText, |
| 160 | + inlineContent = inlineContent, |
| 161 | + modifier = Modifier.weight(1f), |
| 162 | + maxLines = 1, |
| 163 | + lineHeight = 18.sp, |
| 164 | + overflow = TextOverflow.Ellipsis, |
| 165 | + color = textColor |
| 166 | + ) |
| 167 | + |
| 168 | + // Unread indicators (mention icon + message count badge) |
| 169 | + if (unreadMessageCount > 0 || unreadMentionCount > 0) { |
| 170 | + Spacer(modifier = Modifier.width(6.dp)) |
| 171 | + UnreadIndicatorsRow( |
| 172 | + unreadMessageCount = unreadMessageCount, |
| 173 | + unreadMentionCount = unreadMentionCount, |
| 174 | + notificationMode = chatNotificationState, |
| 175 | + isPinned = false |
| 176 | + ) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
0 commit comments