114 lines
4.0 KiB
Kotlin
114 lines
4.0 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.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 coil.compose.AsyncImage
|
|
import androidx.compose.ui.layout.ContentScale
|
|
|
|
@Composable
|
|
fun ChatItem(
|
|
chat: Chat,
|
|
onClick: (String) -> Unit
|
|
) {
|
|
Row(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.clickable { onClick(chat.id) }
|
|
.padding(12.dp),
|
|
verticalAlignment = Alignment.CenterVertically
|
|
) {
|
|
// Аватар (мягкий квадрат)
|
|
Box(
|
|
modifier = Modifier
|
|
.size(50.dp)
|
|
.clip(RoundedCornerShape(12.dp))
|
|
.background(MaterialTheme.colorScheme.primaryContainer),
|
|
contentAlignment = Alignment.Center
|
|
) {
|
|
if (chat.avatar != null) {
|
|
AsyncImage(
|
|
model = chat.avatar,
|
|
contentDescription = null,
|
|
modifier = Modifier.fillMaxSize(),
|
|
contentScale = ContentScale.Crop
|
|
)
|
|
} else {
|
|
Text(
|
|
text = chat.name.take(1).uppercase(),
|
|
style = MaterialTheme.typography.titleMedium
|
|
)
|
|
}
|
|
}
|
|
|
|
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 {
|
|
Text(
|
|
text = it.createdAt.takeLast(5), // Упрощенный формат времени
|
|
style = MaterialTheme.typography.labelSmall,
|
|
color = Color.Gray
|
|
)
|
|
}
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.SpaceBetween
|
|
) {
|
|
Text(
|
|
text = chat.lastMessage?.content ?: "No messages yet",
|
|
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
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|