Skip to content
74 changes: 74 additions & 0 deletions app/src/main/java/com/android/polybazaar/chat/ui/ChatComponents.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.chat.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.android.polybazaar.R
import com.android.polybazaar.auth.ui.AuthColors
import com.android.polybazaar.listing.ui.decodePhotoPreview

/** Text of the "Paused" listing badges, from the Figma. */
internal val PausedBadgeText = Color(0xFF8C491F)

/** Fill of the "Deleted" listing badges, from the Figma. */
internal val DeletedBadgeBackground = Color(0xFFF2EAE5)

/** Text of the "Deleted" listing badges, from the Figma. */
internal val DeletedBadgeText = Color(0xFF67564D)

/** The listing's photo, or a placeholder with an image icon while it is missing. */
@Composable
internal fun ListingPhoto(photo: ImageBitmap?, size: Dp, modifier: Modifier = Modifier) {
Box(
modifier =
modifier
.size(size)
.clip(RoundedCornerShape(12.dp))
.background(AuthColors.AccentContainer),
contentAlignment = Alignment.Center,
) {
if (photo != null) {
// Decorative: the text next to it names the listing.
Image(
painter = BitmapPainter(photo),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.size(size),
)
} else {
Icon(
painter = painterResource(R.drawable.ic_image),
contentDescription = null,
tint = AuthColors.TextSecondary,
modifier = Modifier.size(20.dp),
)
}
}
}

/** Decodes the downloaded [photo] off the main thread. Null while missing or decoding. */
@Composable
internal fun rememberPhoto(photo: ByteArray?): ImageBitmap? {
val decoded by
produceState<ImageBitmap?>(initialValue = null, photo) {
value = photo?.let { decodePhotoPreview(it) }
}
return decoded
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.chat.ui

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import com.android.polybazaar.R
import com.android.polybazaar.chat.model.ChatMessage
import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter

/** One row of the message list: a message, or the day the messages under it were sent on. */
internal sealed interface ConversationItem {
data class DayDivider(val date: LocalDate) : ConversationItem

data class Message(val message: ChatMessage) : ConversationItem
}

/**
* [messages], oldest first, with a [ConversationItem.DayDivider] before the first message of each
* day in [zone]. The days only move forward: a message dated before the day above it, such as a
* pending one stamped by a device clock that is behind, stays under that day, so no day is listed
* twice.
*/
internal fun conversationItems(messages: List<ChatMessage>, zone: ZoneId): List<ConversationItem> {
val items = mutableListOf<ConversationItem>()
var day: LocalDate? = null
for (message in messages) {
val date = message.sentAt.atZone(zone).toLocalDate()
if (day == null || date.isAfter(day)) {
items += ConversationItem.DayDivider(date)
day = date
}
items += ConversationItem.Message(message)
}
return items
}

/** How a day divider names its day. */
internal sealed interface ConversationDay {
/** Today, or later when the device clock is behind the sender's. */
data object Today : ConversationDay

data object Yesterday : ConversationDay

/** An earlier day, with its year when it is not this year. */
data class Date(val date: LocalDate, val withYear: Boolean) : ConversationDay
}

/** How the divider of [date] names it, seen on [today]. */
internal fun conversationDay(date: LocalDate, today: LocalDate): ConversationDay =
when {
!date.isBefore(today) -> ConversationDay.Today
date == today.minusDays(1) -> ConversationDay.Yesterday
else -> ConversationDay.Date(date, withYear = date.year != today.year)
}

/** "Today", "Yesterday", "4 May" or "4 May 2025", in the app's locale. */
@Composable
internal fun ConversationDay.label(): String =
when (this) {
ConversationDay.Today -> stringResource(R.string.conversation_day_today)
ConversationDay.Yesterday -> stringResource(R.string.conversation_day_yesterday)
is ConversationDay.Date -> {
// From the configuration, so the date follows a change of locale.
val locale = LocalConfiguration.current.locales[0]
date.format(DateTimeFormatter.ofPattern(if (withYear) "d MMM yyyy" else "d MMM", locale))
}
}
Loading
Loading