Skip to content

Commit

Permalink
use edit message API
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Feb 20, 2025
1 parent 9e94407 commit c615a09
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
35 changes: 31 additions & 4 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1661,8 +1661,26 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
navigationController?.popToRootViewController(animated: true)
}

private func reply(at indexPath: IndexPath) {
replyToMessage(at: indexPath)
private func editSentMessage(at indexPath: IndexPath) {
let message = dcContext.getMessage(id: messageIds[indexPath.row])

draft.clear()
draft.sendEditRequestFor = message.id
configureDraftArea(draft: draft)
messageInputBar.inputTextView.text = message.text

/*
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.addTextField { textfield in
textfield.text = message.text
}
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default) { [weak self] _ in
guard let self, let textfield = alert.textFields?.first else { return }
dcContext.sendEditRequest(msgId: message.id, newText: textfield.text ?? "")
})
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
present(alert, animated: true)
*/
}

private func toggleSave(at indexPath: IndexPath) {
Expand Down Expand Up @@ -1895,7 +1913,7 @@ extension ChatViewController {
children.append(UIMenu(title: String.localized("react"), image: UIImage(systemName: "face.smiling"), children: items))
}
children.append(
UIAction.menuAction(localizationKey: "notify_reply_button", systemImageName: "arrowshape.turn.up.left", indexPath: indexPath, action: { self.reply(at: $0 ) })
UIAction.menuAction(localizationKey: "notify_reply_button", systemImageName: "arrowshape.turn.up.left", indexPath: indexPath, action: replyToMessage)
)
}

Expand All @@ -1909,6 +1927,12 @@ extension ChatViewController {
UIAction.menuAction(localizationKey: "forward", systemImageName: "arrowshape.turn.up.forward", indexPath: indexPath, action: forward)
)

if message.isFromCurrentSender && message.hasText && !message.isMarkerOrInfo && dcChat.canSend {
children.append(
UIAction.menuAction(localizationKey: "global_menu_edit_desktop", systemImageName: "pencil", indexPath: indexPath, action: editSentMessage)
)
}

if !dcChat.isSelfTalk && message.canSave {
if message.savedMessageId != 0 {
children.append(
Expand Down Expand Up @@ -2364,7 +2388,10 @@ extension ChatViewController: InputBarAccessoryViewDelegate {
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
let trimmedText = text.replacingOccurrences(of: "\u{FFFC}", with: "", options: .literal, range: nil)
.trimmingCharacters(in: .whitespacesAndNewlines)
if let filePath = draft.attachment, let viewType = draft.viewType {

if let sendEditRequestFor = draft.sendEditRequestFor {
dcContext.sendEditRequest(msgId: sendEditRequestFor, newText: text)
} else if let filePath = draft.attachment, let viewType = draft.viewType {
switch viewType {
case DC_MSG_GIF, DC_MSG_IMAGE, DC_MSG_FILE, DC_MSG_VIDEO, DC_MSG_WEBXDC, DC_MSG_VCARD:
self.sendAttachmentMessage(viewType: viewType, filePath: filePath, message: trimmedText, quoteMessage: draft.quoteMessage)
Expand Down
10 changes: 9 additions & 1 deletion deltachat-ios/Chat/DraftModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class DraftModel {
var dcContext: DcContext
var text: String?
let chatId: Int
var isEditing: Bool = false
var isEditing: Bool = false // multi edit
var sendEditRequestFor: Int?
var quoteMessage: DcMsg? {
return draftMsg?.quoteMessage
}
Expand Down Expand Up @@ -39,6 +40,7 @@ public class DraftModel {
draftMsg = dcContext.newMessage(viewType: DC_MSG_TEXT)
}
draftMsg?.quoteMessage = quotedMsg
sendEditRequestFor = nil
}

public func setAttachment(viewType: Int32?, path: String?, mimetype: String? = nil) {
Expand All @@ -62,6 +64,11 @@ public class DraftModel {
}

public func save(context: DcContext) {
if sendEditRequestFor != nil {
clear()
return
}

if (text?.isEmpty ?? true) &&
(draftMsg == nil || quoteMessage == nil && attachment == nil) {
self.clear()
Expand All @@ -82,6 +89,7 @@ public class DraftModel {
public func clear() {
text = nil
draftMsg = nil
sendEditRequestFor = nil
dcContext.setDraft(chatId: chatId, message: nil)
}
}
9 changes: 8 additions & 1 deletion deltachat-ios/Chat/Views/QuotePreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ public class QuotePreview: DraftPreview {
}

override public func configure(draft: DraftModel) {
if !draft.isEditing,
if !draft.isEditing, let sendEditRequestFor = draft.sendEditRequestFor {
quoteView.senderTitle.text = String.localized("edit_message")
quoteView.senderTitle.textColor = DcColors.unknownSender
quoteView.quote.text = draft.dcContext.getMessage(id: sendEditRequestFor).text
quoteView.setImagePreview(nil)
quoteView.citeBar.backgroundColor = DcColors.unknownSender
isHidden = false
} else if !draft.isEditing,
let quoteText = draft.quoteText {
quoteView.quote.text = quoteText
compactView = draft.attachment != nil
Expand Down
6 changes: 5 additions & 1 deletion deltachat-ios/Chat/Views/StatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public class StatusView: UIView {
}

public func update(message: DcMsg, tintColor: UIColor) {
dateLabel.text = message.formattedSentDate()
dateLabel.text = if message.isEdited {
message.formattedSentDate() + " " + String.localized("edited")
} else {
message.formattedSentDate()
}
dateLabel.textColor = tintColor

if message.showPadlock() {
Expand Down
4 changes: 4 additions & 0 deletions deltachat-ios/DC/DcContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public class DcContext {
dc_send_msg(contextPointer, UInt32(chatId), message.messagePointer)
}

public func sendEditRequest(msgId: Int, newText: String) {
dc_send_edit_request(contextPointer, UInt32(msgId), newText)

Check failure on line 66 in deltachat-ios/DC/DcContext.swift

View workflow job for this annotation

GitHub Actions / build

cannot find 'dc_send_edit_request' in scope
}

public func downloadFullMessage(id: Int) {
dc_download_full_msg(contextPointer, Int32(id))
}
Expand Down
9 changes: 8 additions & 1 deletion deltachat-ios/DC/DcMsg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class DcMsg {
return dc_msg_is_forwarded(messagePointer) != 0
}

public var isEdited: Bool {
return dc_msg_is_edited(messagePointer) != 0

Check failure on line 35 in deltachat-ios/DC/DcMsg.swift

View workflow job for this annotation

GitHub Actions / build

cannot find 'dc_msg_is_edited' in scope
}

public var isValid: Bool {
return messagePointer != nil
}
Expand All @@ -43,7 +47,6 @@ public class DcMsg {
return Int(dc_msg_get_id(messagePointer))
}


public var fromContactId: Int {
return Int(dc_msg_get_from_id(messagePointer))
}
Expand Down Expand Up @@ -103,6 +106,10 @@ public class DcMsg {
}
}

public var hasText: Bool {
return !(text ?? "").isEmpty
}

public var subject: String {
guard let cString = dc_msg_get_subject(messagePointer) else { return "" }
let swiftString = String(cString: cString)
Expand Down

0 comments on commit c615a09

Please sign in to comment.