Skip to content

Commit 44b9b7d

Browse files
committed
Add markThreadAsUnreadAction when message is the root of a thread and inside a thread view
1 parent 6364a23 commit 44b9b7d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Reactions/MessageActions/DefaultMessageActions.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ extension MessageAction {
110110
messageActions.append(copyAction)
111111
}
112112

113+
if message.isRootOfThread {
114+
let messageController = InjectedValues[\.utils]
115+
.channelControllerFactory
116+
.makeMessageController(for: message.id, channelId: channel.cid)
117+
// At the moment, this is the only way to know if we are inside a thread.
118+
// This should be optimised in the future and provide the view context.
119+
if messageController.replies.count > 0 {
120+
let markThreadUnreadAction = markThreadAsUnreadAction(
121+
messageController: messageController,
122+
message: message,
123+
onFinish: onFinish,
124+
onError: onError
125+
)
126+
messageActions.append(markThreadUnreadAction)
127+
}
128+
}
129+
113130
if message.isSentByCurrentUser {
114131
if message.poll == nil {
115132
let editAction = editMessageAction(
@@ -512,6 +529,38 @@ extension MessageAction {
512529
return unreadAction
513530
}
514531

532+
private static func markThreadAsUnreadAction(
533+
messageController: ChatMessageController,
534+
message: ChatMessage,
535+
onFinish: @escaping (MessageActionInfo) -> Void,
536+
onError: @escaping (Error) -> Void
537+
) -> MessageAction {
538+
let action = {
539+
messageController.markThreadUnread() { error in
540+
if let error {
541+
onError(error)
542+
} else {
543+
onFinish(
544+
MessageActionInfo(
545+
message: message,
546+
identifier: MessageActionId.markUnread
547+
)
548+
)
549+
}
550+
}
551+
}
552+
let unreadAction = MessageAction(
553+
id: MessageActionId.markUnread,
554+
title: L10n.Message.Actions.markUnread,
555+
iconName: "message.badge",
556+
action: action,
557+
confirmationPopup: nil,
558+
isDestructive: false
559+
)
560+
561+
return unreadAction
562+
}
563+
515564
private static func muteAction(
516565
for message: ChatMessage,
517566
channel: ChatChannel,

0 commit comments

Comments
 (0)