125 lines
4.4 KiB
Kotlin
125 lines
4.4 KiB
Kotlin
package chats.presentation.components
|
|
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.foundation.clickable
|
|
import androidx.compose.foundation.layout.*
|
|
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.draw.clip
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.compose.ui.layout.ContentScale
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
import androidx.compose.ui.text.style.TextOverflow
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.compose.ui.unit.sp
|
|
import chats.domain.model.Chat
|
|
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
import core.presentation.components.AppAvatar
|
|
import chats.domain.model.MediaType
|
|
import androidx.compose.runtime.remember
|
|
|
|
@Composable
|
|
fun ChatItem(
|
|
chat: Chat,
|
|
onClick: (String) -> Unit
|
|
) {
|
|
Row(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.clickable { onClick(chat.id) }
|
|
.padding(12.dp),
|
|
verticalAlignment = Alignment.CenterVertically
|
|
) {
|
|
AppAvatar(
|
|
url = chat.avatar,
|
|
name = chat.name,
|
|
size = 50.dp
|
|
)
|
|
|
|
Spacer(modifier = Modifier.width(12.dp))
|
|
|
|
Column(modifier = Modifier.weight(1f)) {
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.SpaceBetween
|
|
) {
|
|
Text(
|
|
text = chat.name,
|
|
style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Bold),
|
|
maxLines = 1,
|
|
overflow = TextOverflow.Ellipsis
|
|
)
|
|
chat.lastMessage?.let {
|
|
val formattedTime = remember(it.createdAt) {
|
|
try {
|
|
it.createdAt.substringAfter('T').take(5)
|
|
} catch (e: Exception) {
|
|
""
|
|
}
|
|
}
|
|
Text(
|
|
text = formattedTime,
|
|
style = MaterialTheme.typography.labelSmall,
|
|
color = Color.Gray
|
|
)
|
|
}
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.SpaceBetween
|
|
) {
|
|
val previewText = remember(chat.lastMessage) {
|
|
val msg = chat.lastMessage
|
|
if (msg == null) return@remember "No messages yet"
|
|
|
|
if (!msg.content.isNullOrBlank()) {
|
|
msg.content
|
|
} else if (msg.mediaType == MediaType.AUDIO) {
|
|
"Голосовое сообщение"
|
|
} else if (msg.media.isNotEmpty()) {
|
|
when (msg.mediaType) {
|
|
MediaType.IMAGE -> "Фото"
|
|
MediaType.VIDEO -> "Видео"
|
|
MediaType.FILE -> "Файл"
|
|
else -> "Медиа"
|
|
}
|
|
} else {
|
|
"Сообщение"
|
|
}
|
|
}
|
|
Text(
|
|
text = previewText,
|
|
style = MaterialTheme.typography.bodyMedium,
|
|
color = Color.Gray,
|
|
maxLines = 1,
|
|
overflow = TextOverflow.Ellipsis,
|
|
modifier = Modifier.weight(1f)
|
|
)
|
|
|
|
if (chat.unreadCount > 0) {
|
|
Box(
|
|
modifier = Modifier
|
|
.padding(start = 8.dp)
|
|
.background(MaterialTheme.colorScheme.primary, CircleShape)
|
|
.padding(horizontal = 6.dp, vertical = 2.dp),
|
|
contentAlignment = Alignment.Center
|
|
) {
|
|
Text(
|
|
text = chat.unreadCount.toString(),
|
|
color = Color.White,
|
|
fontSize = 10.sp,
|
|
fontWeight = FontWeight.Bold
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|