feat(desktop): add encrypted DMs with split-pane layout (NIP-04/NIP-17)
Full desktop DM implementation with shared components extracted to commons: - Relay layer: DM relay state, filter construction, subscription coordinator - Shared: ChatroomFeedFilter, ChatroomFeedViewModel, ChatNewMessageState - Shared UI: ChatBubbleLayout, ChatMessageCompose, ChatroomHeader, ChatTheme - Desktop: split-pane (conversation list + chat), keyboard shortcuts, hover reactions - IAccount extended with signer, sendNip04/17, chatroomList, isAcceptable Refs: vitorpamplona/amethyst#1704 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,11 @@
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.model
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.privateChats.ChatroomList
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
|
||||
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
|
||||
import com.vitorpamplona.quartz.nip04Dm.messages.PrivateDmEvent
|
||||
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.LnZapPaymentRequestEvent
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.LnZapPaymentResponseEvent
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.Request
|
||||
@@ -73,6 +78,9 @@ interface IAccount {
|
||||
/** Whether account has write permissions */
|
||||
fun isWriteable(): Boolean
|
||||
|
||||
/** Nostr signer for signing/encrypting events */
|
||||
val signer: NostrSigner
|
||||
|
||||
/** Current user's public key */
|
||||
val pubKey: String
|
||||
|
||||
@@ -86,4 +94,16 @@ interface IAccount {
|
||||
fun followingKeySet(): Set<String>
|
||||
|
||||
fun isHidden(user: User): Boolean
|
||||
|
||||
/** Chatroom list for private DM conversations */
|
||||
val chatroomList: ChatroomList
|
||||
|
||||
/** Whether a note is acceptable (not hidden, not blocked, etc.) */
|
||||
fun isAcceptable(note: Note): Boolean
|
||||
|
||||
/** Send a NIP-04 encrypted direct message */
|
||||
suspend fun sendNip04PrivateMessage(eventTemplate: EventTemplate<PrivateDmEvent>)
|
||||
|
||||
/** Send a NIP-17 gift-wrapped direct message */
|
||||
suspend fun sendNip17PrivateMessage(template: EventTemplate<ChatMessageEvent>)
|
||||
}
|
||||
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.chat
|
||||
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.compositeOver
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatBubbleMaxSizeModifier
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatBubbleShapeMe
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatBubbleShapeThem
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatHalfHalfVertPadding
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatPaddingInnerQuoteModifier
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatPaddingModifier
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatRowColSpacing5dp
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.MessageBubbleLimits
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.chatBubbleBackground
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.chatBubbleDraftBackground
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.chatBubbleMeBackground
|
||||
|
||||
/**
|
||||
* Shared chat bubble layout used by both Android and Desktop.
|
||||
*
|
||||
* Renders a chat message bubble with appropriate shape, color, and alignment
|
||||
* depending on whether the message is from the logged-in user or another user.
|
||||
*
|
||||
* Content is provided via composable lambdas (slots) so platform-specific
|
||||
* implementations can plug in their own action menus, author lines, detail
|
||||
* rows, and message content.
|
||||
*
|
||||
* @param isLoggedInUser Whether this message was sent by the current user
|
||||
* @param isDraft Whether this message is a draft
|
||||
* @param innerQuote Whether this bubble is rendered as an inner quote (reply preview)
|
||||
* @param isComplete Whether the UI is in "complete" mode (always show details)
|
||||
* @param hasDetailsToShow Whether there are details (reactions, zaps) to display
|
||||
* @param drawAuthorInfo Whether to show the author avatar and name
|
||||
* @param parentBackgroundColor Background color of the parent (for compositing)
|
||||
* @param onClick Called on tap; return true if the click was consumed
|
||||
* @param onAuthorClick Called when the author line is tapped
|
||||
* @param actionMenu Composable for the long-press action menu popup
|
||||
* @param detailRow Composable for the detail row (time, reactions, relays)
|
||||
* @param drawAuthorLine Composable for the author avatar + name row
|
||||
* @param inner Composable for the message body content; receives the resolved background color
|
||||
*/
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun ChatBubbleLayout(
|
||||
isLoggedInUser: Boolean,
|
||||
isDraft: Boolean,
|
||||
innerQuote: Boolean,
|
||||
isComplete: Boolean,
|
||||
hasDetailsToShow: Boolean,
|
||||
drawAuthorInfo: Boolean,
|
||||
parentBackgroundColor: MutableState<Color>? = null,
|
||||
onClick: () -> Boolean,
|
||||
onAuthorClick: () -> Unit,
|
||||
actionMenu: @Composable (onDismiss: () -> Unit) -> Unit,
|
||||
detailRow: @Composable () -> Unit,
|
||||
drawAuthorLine: @Composable () -> Unit,
|
||||
inner: @Composable (MutableState<Color>) -> Unit,
|
||||
) {
|
||||
val loggedInColors = MaterialTheme.colorScheme.chatBubbleMeBackground
|
||||
val otherColors = MaterialTheme.colorScheme.chatBubbleBackground
|
||||
val defaultBackground = MaterialTheme.colorScheme.background
|
||||
val draftColor = MaterialTheme.colorScheme.chatBubbleDraftBackground
|
||||
|
||||
val bgColor =
|
||||
remember {
|
||||
if (isLoggedInUser) {
|
||||
if (isDraft) {
|
||||
mutableStateOf(
|
||||
draftColor.compositeOver(parentBackgroundColor?.value ?: defaultBackground),
|
||||
)
|
||||
} else {
|
||||
mutableStateOf(
|
||||
loggedInColors.compositeOver(parentBackgroundColor?.value ?: defaultBackground),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
mutableStateOf(otherColors.compositeOver(parentBackgroundColor?.value ?: defaultBackground))
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = if (innerQuote) ChatPaddingInnerQuoteModifier else ChatPaddingModifier,
|
||||
horizontalArrangement = if (isLoggedInUser) Arrangement.End else Arrangement.Start,
|
||||
) {
|
||||
val popupExpanded = remember { mutableStateOf(false) }
|
||||
|
||||
val showDetails =
|
||||
remember {
|
||||
mutableStateOf(
|
||||
if (isComplete) {
|
||||
true
|
||||
} else {
|
||||
hasDetailsToShow
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
val clickableModifier =
|
||||
remember {
|
||||
Modifier.combinedClickable(
|
||||
onClick = {
|
||||
if (!onClick()) {
|
||||
if (!isComplete) {
|
||||
showDetails.value = !showDetails.value
|
||||
}
|
||||
}
|
||||
},
|
||||
onLongClick = { popupExpanded.value = true },
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
horizontalArrangement = if (isLoggedInUser) Arrangement.End else Arrangement.Start,
|
||||
modifier = if (innerQuote) Modifier else ChatBubbleMaxSizeModifier,
|
||||
) {
|
||||
Surface(
|
||||
color = bgColor.value,
|
||||
shape = if (isLoggedInUser) ChatBubbleShapeMe else ChatBubbleShapeThem,
|
||||
modifier = clickableModifier,
|
||||
) {
|
||||
Column(modifier = MessageBubbleLimits, verticalArrangement = ChatRowColSpacing5dp) {
|
||||
if (drawAuthorInfo) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = if (isLoggedInUser) Arrangement.End else Arrangement.Start,
|
||||
modifier = ChatHalfHalfVertPadding.clickable(onClick = onAuthorClick),
|
||||
) {
|
||||
drawAuthorLine()
|
||||
}
|
||||
}
|
||||
|
||||
inner(bgColor)
|
||||
|
||||
if (showDetails.value) {
|
||||
detailRow()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (popupExpanded.value) {
|
||||
actionMenu {
|
||||
popupExpanded.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.chat
|
||||
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
|
||||
/**
|
||||
* Shared chat message composable that provides structure without platform-specific dependencies.
|
||||
*
|
||||
* This is a simplified, slot-based version of the Android ChatroomMessageCompose.
|
||||
* Platform implementations provide the actual content via lambda slots, allowing
|
||||
* both Android and Desktop to use the same bubble layout with different content renderers.
|
||||
*
|
||||
* The Android app can wrap this or continue using its own deeper implementation.
|
||||
* The Desktop app uses this directly with simpler content slots.
|
||||
*
|
||||
* @param note The chat message note
|
||||
* @param isLoggedInUser Whether this note was authored by the current user
|
||||
* @param isDraft Whether this is a draft message
|
||||
* @param innerQuote Whether this is rendered as a quoted reply
|
||||
* @param isComplete Whether to always show detail row
|
||||
* @param hasDetailsToShow Whether there are reactions/zaps to display
|
||||
* @param drawAuthorInfo Whether to show author avatar and name
|
||||
* @param parentBackgroundColor Parent background for color compositing
|
||||
* @param onClick Called on tap; return true if consumed
|
||||
* @param onAuthorClick Called when author info is tapped
|
||||
* @param actionMenu Long-press action menu popup slot
|
||||
* @param authorLine Author avatar and name slot
|
||||
* @param detailRow Detail row slot (time, reactions)
|
||||
* @param messageContent Message body slot; receives the resolved background color
|
||||
*/
|
||||
@Composable
|
||||
fun ChatMessageCompose(
|
||||
note: Note,
|
||||
isLoggedInUser: Boolean,
|
||||
isDraft: Boolean,
|
||||
innerQuote: Boolean = false,
|
||||
isComplete: Boolean = true,
|
||||
hasDetailsToShow: Boolean = false,
|
||||
drawAuthorInfo: Boolean = false,
|
||||
parentBackgroundColor: MutableState<Color>? = null,
|
||||
onClick: () -> Boolean = { false },
|
||||
onAuthorClick: () -> Unit = {},
|
||||
actionMenu: @Composable (onDismiss: () -> Unit) -> Unit = {},
|
||||
authorLine: @Composable () -> Unit = {},
|
||||
detailRow: @Composable () -> Unit = {},
|
||||
messageContent: @Composable (MutableState<Color>) -> Unit,
|
||||
) {
|
||||
ChatBubbleLayout(
|
||||
isLoggedInUser = isLoggedInUser,
|
||||
isDraft = isDraft,
|
||||
innerQuote = innerQuote,
|
||||
isComplete = isComplete,
|
||||
hasDetailsToShow = hasDetailsToShow,
|
||||
drawAuthorInfo = drawAuthorInfo,
|
||||
parentBackgroundColor = parentBackgroundColor,
|
||||
onClick = onClick,
|
||||
onAuthorClick = onAuthorClick,
|
||||
actionMenu = actionMenu,
|
||||
drawAuthorLine = authorLine,
|
||||
detailRow = detailRow,
|
||||
) { bgColor ->
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
messageContent(bgColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.chat
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
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.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.vitorpamplona.amethyst.commons.model.User
|
||||
import com.vitorpamplona.amethyst.commons.ui.components.UserAvatar
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatSize34dp
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatStdPadding
|
||||
|
||||
/**
|
||||
* Shared chatroom header for a single-user conversation.
|
||||
* Displays the user's avatar and display name.
|
||||
*
|
||||
* Uses the shared UserAvatar component from commons for image loading,
|
||||
* avoiding Android-specific dependencies.
|
||||
*
|
||||
* @param user The chat partner
|
||||
* @param modifier Layout modifier (defaults to standard padding)
|
||||
* @param onClick Called when the header is tapped (e.g., navigate to profile)
|
||||
*/
|
||||
@Composable
|
||||
fun ChatroomHeader(
|
||||
user: User,
|
||||
modifier: Modifier = ChatStdPadding,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
Column(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable(onClick = onClick),
|
||||
) {
|
||||
Column(modifier, Arrangement.Center) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
UserAvatar(
|
||||
userHex = user.pubkeyHex,
|
||||
pictureUrl = user.profilePicture(),
|
||||
size = ChatSize34dp,
|
||||
)
|
||||
|
||||
Column(modifier = Modifier.padding(start = 10.dp)) {
|
||||
Text(
|
||||
text = user.toBestDisplayName(),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared chatroom header for a group conversation.
|
||||
* Displays multiple user avatars and a combined room name.
|
||||
*
|
||||
* @param users List of users in the group conversation
|
||||
* @param modifier Layout modifier (defaults to standard padding)
|
||||
* @param onClick Called when the header is tapped
|
||||
*/
|
||||
@Composable
|
||||
fun GroupChatroomHeader(
|
||||
users: List<User>,
|
||||
modifier: Modifier = ChatStdPadding,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
Column(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable(onClick = onClick),
|
||||
) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center,
|
||||
modifier = modifier,
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
// Show first user's avatar as the group icon
|
||||
users.firstOrNull()?.let { firstUser ->
|
||||
UserAvatar(
|
||||
userHex = firstUser.pubkeyHex,
|
||||
pictureUrl = firstUser.profilePicture(),
|
||||
size = ChatSize34dp,
|
||||
)
|
||||
}
|
||||
|
||||
Column(modifier = Modifier.padding(start = 10.dp)) {
|
||||
Text(
|
||||
text = users.joinToString(", ") { it.toBestDisplayName() },
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.chat
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxScope
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatAuthorBox
|
||||
import com.vitorpamplona.amethyst.commons.ui.theme.ChatStdHorzSpacer
|
||||
|
||||
/**
|
||||
* Layout for displaying a user's picture and name in a chat message author line.
|
||||
* Shared between Android and Desktop.
|
||||
*
|
||||
* The picture slot uses a BoxScope so callers can overlay badges/icons
|
||||
* (e.g., following indicator, user cards) on top of the avatar.
|
||||
*/
|
||||
@Composable
|
||||
fun UserDisplayNameLayout(
|
||||
picture: @Composable BoxScope.() -> Unit,
|
||||
name: @Composable () -> Unit,
|
||||
) {
|
||||
Box(ChatAuthorBox, contentAlignment = Alignment.TopEnd) {
|
||||
picture()
|
||||
}
|
||||
|
||||
Spacer(modifier = ChatStdHorzSpacer)
|
||||
|
||||
name()
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.feeds
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.ListChange
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
|
||||
interface ChangesFlowFilter<T> : IAdditiveFeedFilter<T> {
|
||||
fun changesFlow(): MutableSharedFlow<ListChange<T>>
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.feeds
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.IAccount
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
import com.vitorpamplona.quartz.nip17Dm.base.ChatroomKey
|
||||
|
||||
class ChatroomFeedFilter(
|
||||
val withUser: ChatroomKey,
|
||||
val account: IAccount,
|
||||
) : AdditiveFeedFilter<Note>(),
|
||||
ChangesFlowFilter<Note> {
|
||||
fun chatroom() = account.chatroomList.getOrCreatePrivateChatroom(withUser)
|
||||
|
||||
override fun changesFlow() = chatroom().changesFlow()
|
||||
|
||||
// returns the last Note of each user.
|
||||
override fun feedKey(): String = withUser.hashCode().toString()
|
||||
|
||||
override fun feed(): List<Note> = chatroom().messages.filter { account.isAcceptable(it) }.sortedWith(DefaultFeedOrder)
|
||||
|
||||
override fun applyFilter(newItems: Set<Note>): Set<Note> {
|
||||
val chatroom = chatroom()
|
||||
return newItems.filter { it in chatroom.messages && account.isAcceptable(it) }.toSet()
|
||||
}
|
||||
|
||||
override fun sort(items: Set<Note>): List<Note> = items.sortedWith(DefaultFeedOrder)
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.feeds
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
|
||||
val DefaultFeedOrder: Comparator<Note> =
|
||||
compareByDescending<Note> { it.createdAt() }.thenBy { it.idHex }
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.ui.theme
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.ColorScheme
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
// Chat bubble shapes
|
||||
val ChatBubbleShapeMe = RoundedCornerShape(15.dp, 15.dp, 3.dp, 15.dp)
|
||||
val ChatBubbleShapeThem = RoundedCornerShape(3.dp, 15.dp, 15.dp, 15.dp)
|
||||
|
||||
// Chat bubble modifiers
|
||||
val ChatBubbleMaxSizeModifier = Modifier.fillMaxWidth(0.85f)
|
||||
val ChatPaddingInnerQuoteModifier = Modifier
|
||||
val ChatPaddingModifier =
|
||||
Modifier
|
||||
.fillMaxWidth(1f)
|
||||
.padding(
|
||||
start = 12.dp,
|
||||
end = 12.dp,
|
||||
top = 3.dp,
|
||||
bottom = 3.dp,
|
||||
)
|
||||
|
||||
// Message bubble internal padding
|
||||
val MessageBubbleLimits = Modifier.padding(start = 10.dp, end = 10.dp, top = 7.dp, bottom = 6.dp)
|
||||
|
||||
// Chat author area
|
||||
val ChatAuthorBox = Modifier.size(20.dp)
|
||||
val ChatAuthorImage = Modifier.size(20.dp).clip(shape = CircleShape)
|
||||
|
||||
// Spacing values reused across chat composables
|
||||
val ChatRowColSpacing5dp = Arrangement.spacedBy(5.dp)
|
||||
val ChatHalfHalfVertPadding = Modifier.padding(vertical = 3.dp)
|
||||
val ChatStdHorzSpacer = Modifier.width(5.dp)
|
||||
val ChatStdPadding = Modifier.padding(10.dp)
|
||||
val ChatReactionRowHeight = Modifier.height(20.dp)
|
||||
|
||||
// Common size values
|
||||
val ChatSize20dp = 20.dp
|
||||
val ChatSize34dp = 34.dp
|
||||
|
||||
// Chat color extensions for ColorScheme
|
||||
val ColorScheme.chatBubbleBackground: Color
|
||||
get() = if (isLight) onSurface.copy(alpha = 0.08f) else onSurface.copy(alpha = 0.12f)
|
||||
|
||||
val ColorScheme.chatBubbleDraftBackground: Color
|
||||
get() = onSurface.copy(alpha = 0.15f)
|
||||
|
||||
val ColorScheme.chatBubbleMeBackground: Color
|
||||
get() = primary.copy(alpha = 0.32f)
|
||||
|
||||
val ColorScheme.chatPlaceholderText: Color
|
||||
get() = onSurface.copy(alpha = 0.42f)
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.viewmodels
|
||||
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import com.vitorpamplona.amethyst.commons.model.IAccount
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
import com.vitorpamplona.amethyst.commons.model.User
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hashtags
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.references.references
|
||||
import com.vitorpamplona.quartz.nip04Dm.messages.PrivateDmEvent
|
||||
import com.vitorpamplona.quartz.nip10Notes.content.findHashtags
|
||||
import com.vitorpamplona.quartz.nip10Notes.content.findNostrEventUris
|
||||
import com.vitorpamplona.quartz.nip10Notes.content.findURLs
|
||||
import com.vitorpamplona.quartz.nip17Dm.base.BaseDMGroupEvent
|
||||
import com.vitorpamplona.quartz.nip17Dm.base.ChatroomKey
|
||||
import com.vitorpamplona.quartz.nip17Dm.base.NIP17Group
|
||||
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
|
||||
import com.vitorpamplona.quartz.nip18Reposts.quotes.quotes
|
||||
import com.vitorpamplona.quartz.nip19Bech32.toNpub
|
||||
import com.vitorpamplona.quartz.utils.Hex
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
/**
|
||||
* Slim shared state for DM message composition.
|
||||
* Holds only core fields needed for typing and sending messages.
|
||||
*
|
||||
* Platform-specific concerns (uploads, emoji suggestions, drafts, zapraiser,
|
||||
* location, user suggestions) remain in the platform ViewModel layer.
|
||||
*
|
||||
* Used by both Android ChatNewMessageViewModel and Desktop DM screen.
|
||||
*/
|
||||
@Stable
|
||||
class ChatNewMessageState(
|
||||
val account: IAccount,
|
||||
val cache: ICacheProvider,
|
||||
val scope: CoroutineScope,
|
||||
) {
|
||||
private val _message = MutableStateFlow(TextFieldValue(""))
|
||||
val message: StateFlow<TextFieldValue> = _message.asStateFlow()
|
||||
|
||||
private val _nip17 = MutableStateFlow(false)
|
||||
val nip17: StateFlow<Boolean> = _nip17.asStateFlow()
|
||||
|
||||
private val _replyTo = MutableStateFlow<Note?>(null)
|
||||
val replyTo: StateFlow<Note?> = _replyTo.asStateFlow()
|
||||
|
||||
private val _subject = MutableStateFlow(TextFieldValue(""))
|
||||
val subject: StateFlow<TextFieldValue> = _subject.asStateFlow()
|
||||
|
||||
private val _room = MutableStateFlow<ChatroomKey?>(null)
|
||||
val room: StateFlow<ChatroomKey?> = _room.asStateFlow()
|
||||
|
||||
/** Whether NIP-17 is required (group chat with >1 recipient) */
|
||||
private val _requiresNip17 = MutableStateFlow(false)
|
||||
val requiresNip17: StateFlow<Boolean> = _requiresNip17.asStateFlow()
|
||||
|
||||
/** Whether a message can be sent (non-blank text + room set) */
|
||||
val canSend: Boolean
|
||||
get() = _message.value.text.isNotBlank() && _room.value != null
|
||||
|
||||
/**
|
||||
* Load a chatroom. Sets the room key, formats toUsers display,
|
||||
* and auto-detects NIP-17 requirement (group chats require NIP-17).
|
||||
*/
|
||||
fun load(roomKey: ChatroomKey) {
|
||||
_room.value = roomKey
|
||||
updateNip17FromRoom()
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-detect NIP-17 based on room:
|
||||
* - Group chats (>1 recipient) always require NIP-17
|
||||
* - Single recipient: NIP-17 off by default (can be toggled)
|
||||
*/
|
||||
fun updateNip17FromRoom() {
|
||||
val currentRoom = _room.value
|
||||
if (currentRoom != null) {
|
||||
_requiresNip17.value = currentRoom.users.size > 1
|
||||
if (_requiresNip17.value) {
|
||||
_nip17.value = true
|
||||
}
|
||||
} else {
|
||||
_requiresNip17.value = false
|
||||
_nip17.value = false
|
||||
}
|
||||
}
|
||||
|
||||
fun updateMessage(newMessage: TextFieldValue) {
|
||||
_message.value = newMessage
|
||||
}
|
||||
|
||||
fun updateSubject(newSubject: TextFieldValue) {
|
||||
_subject.value = newSubject
|
||||
}
|
||||
|
||||
fun setReply(note: Note) {
|
||||
_replyTo.value = note
|
||||
}
|
||||
|
||||
fun clearReply() {
|
||||
_replyTo.value = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle NIP-04/NIP-17 mode.
|
||||
* If NIP-17 is required (group chat), stays on NIP-17.
|
||||
*/
|
||||
fun toggleNip17() {
|
||||
if (_requiresNip17.value) {
|
||||
_nip17.value = true
|
||||
} else {
|
||||
_nip17.value = !_nip17.value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable NIP-17 (e.g., when recipient has DM relay list).
|
||||
*/
|
||||
fun enableNip17() {
|
||||
_nip17.value = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the current message. Builds the appropriate event template
|
||||
* (NIP-04 or NIP-17) and delegates to IAccount for signing/broadcasting.
|
||||
*
|
||||
* @return true if send was initiated, false if preconditions not met
|
||||
*/
|
||||
suspend fun send(): Boolean {
|
||||
val currentRoom = _room.value ?: return false
|
||||
val messageText = _message.value.text
|
||||
if (messageText.isBlank()) return false
|
||||
|
||||
if (_nip17.value || currentRoom.users.size > 1 || _replyTo.value?.event is NIP17Group) {
|
||||
sendNip17(currentRoom, messageText)
|
||||
} else {
|
||||
sendNip04(currentRoom, messageText)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private suspend fun sendNip17(
|
||||
room: ChatroomKey,
|
||||
messageText: String,
|
||||
) {
|
||||
val pTags =
|
||||
room.users.mapNotNull { hexKey ->
|
||||
(cache.getOrCreateUser(hexKey) as? User)?.toPTag()
|
||||
}
|
||||
|
||||
val replyHint = _replyTo.value?.toEventHint<BaseDMGroupEvent>()
|
||||
|
||||
val template =
|
||||
if (replyHint == null) {
|
||||
ChatMessageEvent.build(messageText, pTags) {
|
||||
hashtags(findHashtags(messageText))
|
||||
references(findURLs(messageText))
|
||||
quotes(findNostrEventUris(messageText))
|
||||
}
|
||||
} else {
|
||||
ChatMessageEvent.reply(messageText, replyHint) {
|
||||
hashtags(findHashtags(messageText))
|
||||
references(findURLs(messageText))
|
||||
quotes(findNostrEventUris(messageText))
|
||||
}
|
||||
}
|
||||
|
||||
account.sendNip17PrivateMessage(template)
|
||||
}
|
||||
|
||||
private suspend fun sendNip04(
|
||||
room: ChatroomKey,
|
||||
messageText: String,
|
||||
) {
|
||||
val toUser = (cache.getOrCreateUser(room.users.first()) as? User)?.toPTag() ?: return
|
||||
|
||||
val template =
|
||||
PrivateDmEvent.build(
|
||||
toUser = toUser,
|
||||
message = messageText,
|
||||
replyingTo = _replyTo.value?.toEventHint<PrivateDmEvent>(),
|
||||
signer = account.signer,
|
||||
)
|
||||
|
||||
account.sendNip04PrivateMessage(template)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all composition state after sending or cancelling.
|
||||
*/
|
||||
fun clear() {
|
||||
_message.value = TextFieldValue("")
|
||||
_subject.value = TextFieldValue("")
|
||||
_replyTo.value = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Format room users as npub display string.
|
||||
* Useful for showing recipients in the UI.
|
||||
*/
|
||||
fun toUsersDisplay(): String {
|
||||
val currentRoom = _room.value ?: return ""
|
||||
return currentRoom.users
|
||||
.mapNotNull { hexKey ->
|
||||
runCatching { Hex.decode(hexKey).toNpub() }.getOrNull()
|
||||
}.joinToString(", ") { "@$it" }
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.viewmodels
|
||||
|
||||
import com.vitorpamplona.amethyst.commons.model.IAccount
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.amethyst.commons.ui.feeds.ChatroomFeedFilter
|
||||
import com.vitorpamplona.quartz.nip17Dm.base.ChatroomKey
|
||||
|
||||
class ChatroomFeedViewModel(
|
||||
val user: ChatroomKey,
|
||||
val account: IAccount,
|
||||
cacheProvider: ICacheProvider,
|
||||
) : ListChangeFeedViewModel(ChatroomFeedFilter(user, account), cacheProvider)
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.viewmodels
|
||||
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.vitorpamplona.amethyst.commons.model.ListChange
|
||||
import com.vitorpamplona.amethyst.commons.model.Note
|
||||
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
|
||||
import com.vitorpamplona.amethyst.commons.ui.feeds.ChangesFlowFilter
|
||||
import com.vitorpamplona.amethyst.commons.ui.feeds.FeedContentState
|
||||
import com.vitorpamplona.amethyst.commons.ui.feeds.InvalidatableContent
|
||||
import com.vitorpamplona.quartz.utils.Log
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Stable
|
||||
abstract class ListChangeFeedViewModel(
|
||||
localFilter: ChangesFlowFilter<Note>,
|
||||
cacheProvider: ICacheProvider,
|
||||
) : ViewModel(),
|
||||
InvalidatableContent {
|
||||
val feedState = FeedContentState(localFilter, viewModelScope, cacheProvider)
|
||||
|
||||
override val isRefreshing = feedState.isRefreshing
|
||||
|
||||
override fun invalidateData(ignoreIfDoing: Boolean) = feedState.invalidateData(ignoreIfDoing)
|
||||
|
||||
init {
|
||||
Log.d("Init", "Starting new Model: ${this::class.simpleName}")
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
localFilter.changesFlow().collect {
|
||||
Log.d("Init", "Collecting changes to: ${this@ListChangeFeedViewModel::class.simpleName}")
|
||||
when (it) {
|
||||
is ListChange.Addition -> feedState.updateFeedWith(setOf(it.item))
|
||||
is ListChange.Deletion -> feedState.deleteFromFeed(setOf(it.item))
|
||||
is ListChange.SetAddition -> feedState.updateFeedWith(it.item)
|
||||
is ListChange.SetDeletion -> feedState.deleteFromFeed(it.item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user