Skip to content

Commit

Permalink
Allow deleting ephemeral message
Browse files Browse the repository at this point in the history
  • Loading branch information
nuno-vieira committed Oct 29, 2024
1 parent e482d54 commit f862fbb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
skipPush: Bool = false,
skipEnrichUrl: Bool = false,
extraData: [String: RawJSON] = [:],
completion: ((Result<MessageId, Error>) -> Void)? = nil
completion: ((Result<ChatMessage, Error>) -> Void)? = nil
) {
/// Perform action only if channel is already created on backend side and have a valid `cid`.
guard let cid = cid, isChannelAlreadyCreated else {
Expand All @@ -1343,7 +1343,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
extraData: extraData
) { result in
self.callback {
completion?(result.map(\.id))
completion?(result)
}
}
}
Expand All @@ -1367,29 +1367,38 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP

public class EphemeralMessageEditor {
private unowned let channelController: ChatChannelController
private var messageId: MessageId?
private var message: ChatMessage?

init(channelController: ChatChannelController) {
self.channelController = channelController
}

public func updateMessage(text: String, extraData: [String: RawJSON] = [:]) {
if let messageId {
if let messageId = message?.id {
channelController.updateEphemeralMessage(id: messageId, text: text, extraData: extraData)
return
}

let group = DispatchGroup()
group.enter()

channelController.createEphemeralMessage(text: text, extraData: extraData) {
self.messageId = try? $0.get()
group.leave()
self.message = try? $0.get()
}
}

public func publish() {
messageId.map { channelController.publishEphemeralMessage(id: $0) }
messageId = nil
message.map { channelController.publishEphemeralMessage(id: $0.id) }
message = nil
}

public func delete() {
guard let cid = channelController.cid, let messageId = message?.id else {
return
}

message = nil
channelController
.client
.messageController(cid: cid, messageId: messageId)
.deleteMessage()
}
}

Expand Down
14 changes: 10 additions & 4 deletions Sources/StreamChatUI/Composer/ComposerVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1438,11 +1438,17 @@ open class ComposerVC: _ViewController,
// it will update `UITextView.text` and vice-versa.
guard textView.text != content.text else { return }

content.text = textView.text
if textView.text.isEmpty && !content.text.isEmpty {
channelController?
.ephemeralMessageEditor
.delete()
} else {
channelController?
.ephemeralMessageEditor
.updateMessage(text: textView.text, extraData: ["is_live": true])
}

channelController?
.ephemeralMessageEditor
.updateMessage(text: content.text, extraData: ["is_live": true])
content.text = textView.text
}

open func textView(
Expand Down
4 changes: 2 additions & 2 deletions Sources/StreamChatUI/Utils/ChatMessage+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import StreamChat
public extension ChatMessage {
/// A boolean value that checks if actions are available on the message (e.g. `edit`, `delete`, `resend`, etc.).
var isInteractionEnabled: Bool {
if extraData["is_live"]?.boolValue == true {
if type == .ephemeral && localState == .sending {
return true
}

Expand Down Expand Up @@ -49,7 +49,7 @@ public extension ChatMessage {

/// The text which should be shown in a text view inside the message bubble.
var textContent: String? {
if type == .ephemeral && extraData["is_live"]?.boolValue != true {
if type == .ephemeral && localState != .sending {
return nil
}

Expand Down

0 comments on commit f862fbb

Please sign in to comment.