|
| 1 | +package net.thunderbird.feature.mail.message.list.ui.component.config |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.remember |
| 5 | +import kotlinx.collections.immutable.PersistentList |
| 6 | +import kotlinx.collections.immutable.persistentListOf |
| 7 | +import kotlinx.collections.immutable.toPersistentList |
| 8 | +import net.thunderbird.feature.mail.message.list.preferences.MessageListPreferences |
| 9 | +import net.thunderbird.feature.mail.message.list.ui.component.molecule.MessageConversationCounterBadgeColor |
| 10 | +import net.thunderbird.feature.mail.message.list.ui.state.MessageItemUi |
| 11 | + |
| 12 | +/** |
| 13 | + * Configuration class that defines the visual presentation and layout settings for |
| 14 | + * a message item. |
| 15 | + * |
| 16 | + * ``` |
| 17 | + * Message Item structure: |
| 18 | + * ┌───────────┬──────────────────────┬──────────┐ |
| 19 | + * │ Leading │ Primary Line │ Trailing │ |
| 20 | + * │ Area ├──────────────────────┤ Area │ |
| 21 | + * │ │ Secondary Line │ │ |
| 22 | + * │ │ Excerpt Line │ │ |
| 23 | + * └───────────┴──────────────────────┴──────────┘ |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * @property maxExcerptLines The maximum number of lines to display for the message |
| 27 | + * excerpt before truncation. |
| 28 | + * @property swapPrimaryLineWithSecondaryLine When true, the primary and secondary |
| 29 | + * lines of the message display are swapped in their positions. |
| 30 | + * @property leadingConfiguration Configuration for elements displayed on the leading |
| 31 | + * edge of the message item, such as badges and avatars. |
| 32 | + * @property accountIndicator Optional account indicator that can be displayed to |
| 33 | + * distinguish messages from different accounts. |
| 34 | + * @property secondaryLineConfiguration Configuration for the secondary line of the |
| 35 | + * message item, including any leading items to display. |
| 36 | + * @property excerptLineConfiguration Configuration for the excerpt/preview line of |
| 37 | + * the message, including any leading items to display. |
| 38 | + * @property trailingConfiguration Configuration for elements displayed on the trailing |
| 39 | + * edge of the message item, such as badges and action buttons. |
| 40 | + */ |
| 41 | +data class MessageItemConfiguration( |
| 42 | + val maxExcerptLines: Int = 2, |
| 43 | + val swapPrimaryLineWithSecondaryLine: Boolean = false, |
| 44 | + val leadingConfiguration: MessageItemLeadingConfiguration = MessageItemLeadingConfiguration(), |
| 45 | + val accountIndicator: MessageItemAccountIndicator? = null, |
| 46 | + val secondaryLineConfiguration: MessageSublineConfiguration = MessageSublineConfiguration(), |
| 47 | + val excerptLineConfiguration: MessageSublineConfiguration = MessageSublineConfiguration(), |
| 48 | + val trailingConfiguration: MessageItemTrailingConfiguration = MessageItemTrailingConfiguration(), |
| 49 | +) |
| 50 | + |
| 51 | +/** |
| 52 | + * Remembers and creates a configuration for a message item based on current parameters. |
| 53 | + * |
| 54 | + * @param messageItemUi The UI state of the message item containing information about state, |
| 55 | + * senders, content, and metadata flags needed to build the configuration. |
| 56 | + * @param preferences The user's message list preferences that control display options such as |
| 57 | + * number of excerpt lines and other visual settings. |
| 58 | + * @param color The color scheme for the conversation counter badge, defining container, |
| 59 | + * content, and border colors. |
| 60 | + * @param accountIndicator Optional account indicator containing the color to display for |
| 61 | + * account identification in unified inbox view. Can be null if no indicator is needed. |
| 62 | + * @return A MessageItemConfiguration instance containing all layout and display settings |
| 63 | + * for rendering the message item. |
| 64 | + */ |
| 65 | +@Composable |
| 66 | +internal fun rememberMessageItemConfiguration( |
| 67 | + messageItemUi: MessageItemUi, |
| 68 | + preferences: MessageListPreferences, |
| 69 | + color: MessageConversationCounterBadgeColor, |
| 70 | + accountIndicator: MessageItemAccountIndicator?, |
| 71 | +): MessageItemConfiguration = remember(messageItemUi, preferences) { |
| 72 | + val leadingItems = buildLeadingItems(messageItemUi, color) |
| 73 | + MessageItemConfiguration( |
| 74 | + maxExcerptLines = preferences.excerptLines, |
| 75 | + leadingConfiguration = MessageItemLeadingConfiguration( |
| 76 | + badgeStyle = messageItemUi.state.toBadgeStyle(), |
| 77 | + avatar = messageItemUi.senders.avatar, |
| 78 | + avatarColor = messageItemUi.senders.color, |
| 79 | + ), |
| 80 | + accountIndicator = accountIndicator, |
| 81 | + secondaryLineConfiguration = MessageSublineConfiguration( |
| 82 | + leadingItems = if (preferences.excerptLines == 0) leadingItems else persistentListOf(), |
| 83 | + ), |
| 84 | + excerptLineConfiguration = MessageSublineConfiguration( |
| 85 | + leadingItems = if (preferences.excerptLines > 0) leadingItems else persistentListOf(), |
| 86 | + ), |
| 87 | + trailingConfiguration = MessageItemTrailingConfiguration( |
| 88 | + elements = buildTrailingElementsList(preferences, messageItemUi), |
| 89 | + ), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +private fun buildTrailingElementsList( |
| 94 | + preferences: MessageListPreferences, |
| 95 | + messageItemUi: MessageItemUi, |
| 96 | +): PersistentList<MessageItemTrailingElement> = buildList { |
| 97 | + if (messageItemUi.encrypted) { |
| 98 | + add(MessageItemTrailingElement.EncryptedBadge) |
| 99 | + } |
| 100 | + if (preferences.showFavouriteButton) { |
| 101 | + add(MessageItemTrailingElement.FavouriteIconButton(favourite = messageItemUi.starred)) |
| 102 | + } |
| 103 | +}.toPersistentList() |
| 104 | + |
| 105 | +private fun buildLeadingItems( |
| 106 | + messageItemUi: MessageItemUi, |
| 107 | + color: MessageConversationCounterBadgeColor, |
| 108 | +): PersistentList<MessageSublineLeadingIndicator> = buildList { |
| 109 | + if (messageItemUi.threadCount > 1) { |
| 110 | + add( |
| 111 | + MessageSublineLeadingIndicator.ConversationCounterBadge( |
| 112 | + count = messageItemUi.threadCount, |
| 113 | + color = color, |
| 114 | + ), |
| 115 | + ) |
| 116 | + } |
| 117 | + if (messageItemUi.hasAttachments) { |
| 118 | + add(MessageSublineLeadingIndicator.AttachmentIcon) |
| 119 | + } |
| 120 | +}.toPersistentList() |
| 121 | + |
| 122 | +private fun MessageItemUi.State.toBadgeStyle(): MessageBadgeStyle? = when (this) { |
| 123 | + MessageItemUi.State.New -> MessageBadgeStyle.New |
| 124 | + MessageItemUi.State.Unread -> MessageBadgeStyle.Unread |
| 125 | + MessageItemUi.State.Read -> null |
| 126 | +} |
0 commit comments