update chat room
This commit is contained in:
@@ -239,16 +239,69 @@ fun HomeScreen(onOpenChat: (Long) -> Unit) {
|
|||||||
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
|
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun ChatRoom(room: Room, onClick: () -> Unit) {
|
fun ChatRoom(room: Room, onClick: () -> Unit) {
|
||||||
val title = room.subject ?: "Room"
|
val viewModel = LocalNostrViewModel.current
|
||||||
|
|
||||||
|
val memberMetadataList = room.members.map { pubkey ->
|
||||||
|
viewModel.getMetadata(pubkey).collectAsState()
|
||||||
|
}
|
||||||
|
|
||||||
|
val displayName = if (!room.subject.isNullOrBlank()) {
|
||||||
|
room.subject
|
||||||
|
} else if (room.isGroup()) {
|
||||||
|
val profiles = memberMetadataList.map { it.value?.asRecord() }
|
||||||
|
val names = profiles.take(2).mapNotNull { it?.name ?: it?.displayName }
|
||||||
|
|
||||||
|
var combined = names.joinToString(", ")
|
||||||
|
if (profiles.size > 2) {
|
||||||
|
combined += ", +${profiles.size - 2}"
|
||||||
|
}
|
||||||
|
combined.ifBlank { "Unknown group" }
|
||||||
|
} else {
|
||||||
|
val firstMember = room.members.firstOrNull()
|
||||||
|
val profile = memberMetadataList.firstOrNull()?.value?.asRecord()
|
||||||
|
profile?.name ?: profile?.displayName ?: firstMember?.short() ?: "Unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
val firstMemberMetadata by if (room.members.isNotEmpty()) {
|
||||||
|
viewModel.getMetadata(room.members.first()).collectAsState()
|
||||||
|
} else {
|
||||||
|
remember { mutableStateOf(null) }
|
||||||
|
}
|
||||||
|
val picture = firstMemberMetadata?.asRecord()?.picture
|
||||||
|
|
||||||
ListItem(
|
ListItem(
|
||||||
modifier = Modifier.clickable { onClick },
|
modifier = Modifier.clickable { onClick },
|
||||||
|
leadingContent = {
|
||||||
|
if (!picture.isNullOrBlank()) {
|
||||||
|
AsyncImage(
|
||||||
|
model = picture,
|
||||||
|
contentDescription = "Room Avatar",
|
||||||
|
modifier = Modifier
|
||||||
|
.size(48.dp)
|
||||||
|
.clip(CircleShape),
|
||||||
|
contentScale = ContentScale.Crop,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Icon(
|
||||||
|
painter = painterResource(Res.drawable.ic_avatar),
|
||||||
|
contentDescription = "User"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
headlineContent = {
|
headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
text = title,
|
text = displayName ?: "Unknown",
|
||||||
style = MaterialTheme.typography.titleMediumEmphasized
|
style = MaterialTheme.typography.titleMediumEmphasized
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
supportingContent = {
|
||||||
|
if (!room.lastMessage.isNullOrBlank()) {
|
||||||
|
Text(
|
||||||
|
text = room.lastMessage!!,
|
||||||
|
style = MaterialTheme.typography.bodyMedium
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
colors = ListItemDefaults.colors(
|
colors = ListItemDefaults.colors(
|
||||||
containerColor = MaterialTheme.colorScheme.surface
|
containerColor = MaterialTheme.colorScheme.surface
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ data class Room(
|
|||||||
val createdAt: Timestamp,
|
val createdAt: Timestamp,
|
||||||
val subject: String?,
|
val subject: String?,
|
||||||
val members: Set<PublicKey>,
|
val members: Set<PublicKey>,
|
||||||
val kind: RoomKind = RoomKind.default()
|
val kind: RoomKind = RoomKind.default(),
|
||||||
|
val lastMessage: String? = null
|
||||||
) : Comparable<Room> {
|
) : Comparable<Room> {
|
||||||
override fun hashCode(): Int = id.hashCode()
|
override fun hashCode(): Int = id.hashCode()
|
||||||
|
|
||||||
@@ -55,7 +56,8 @@ data class Room(
|
|||||||
id = id,
|
id = id,
|
||||||
createdAt = createdAt,
|
createdAt = createdAt,
|
||||||
subject = subject,
|
subject = subject,
|
||||||
members = pubkeys as Set<PublicKey>
|
members = pubkeys as Set<PublicKey>,
|
||||||
|
lastMessage = rumor.content()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user