Skip to content

Commit

Permalink
use modern UIMenu for chat's action bar
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Jan 23, 2025
1 parent e320c98 commit fcf832d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
41 changes: 21 additions & 20 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1246,30 +1246,15 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
present(alert, animated: true, completion: nil)
}

private func showMoreMenu() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
if canResend() {
alert.addAction(UIAlertAction(title: String.localized("resend"), style: .default, handler: onResendActionPressed(_:)))
}
if canShare() {
alert.addAction(UIAlertAction(title: String.localized("menu_share"), style: .default, handler: onShareActionPressed(_:)))
}
if canInfo() {
alert.addAction(UIAlertAction(title: String.localized("info"), style: .default, handler: onInfoActionPressed(_:)))
}
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}

private func onResendActionPressed(_ action: UIAlertAction) {
private func onResendActionPressed() {
if let rows = tableView.indexPathsForSelectedRows {
let selectedMsgIds = rows.compactMap { messageIds[$0.row] }
dcContext.resendMessages(msgIds: selectedMsgIds)
setEditing(isEditing: false)
}
}

private func onShareActionPressed(_ action: UIAlertAction) {
private func onShareActionPressed() {
if let rows = tableView.indexPathsForSelectedRows {
let selectedMsgIds = rows.compactMap { messageIds[$0.row] }
if let msgId = selectedMsgIds.first {
Expand All @@ -1279,7 +1264,7 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
}
}

private func onInfoActionPressed(_ action: UIAlertAction) {
private func onInfoActionPressed() {
if let rows = tableView.indexPathsForSelectedRows, let firstRow = rows.first {
info(at: firstRow)
}
Expand Down Expand Up @@ -2430,8 +2415,24 @@ extension ChatViewController: ChatEditingDelegate {
}
}

func onMorePressed() {
showMoreMenu()
func onMorePressed() -> UIMenu {
var actions = [UIMenuElement]()
if canResend() {
actions.append(UIAction(title: String.localized("resend"), image: UIImage(systemName: "paperplane")) { [weak self] _ in
self?.onResendActionPressed()
})
}
if canShare() {
actions.append(UIAction(title: String.localized("menu_share"), image: UIImage(systemName: "square.and.arrow.up")) { [weak self] _ in
self?.onShareActionPressed()
})
}
if canInfo() {
actions.append(UIAction(title: String.localized("info"), image: UIImage(systemName: "info.circle")) { [weak self] _ in
self?.onInfoActionPressed()
})
}
return UIMenu(children: actions)
}

func onForwardPressed() {
Expand Down
14 changes: 8 additions & 6 deletions deltachat-ios/Chat/Views/ChatEditingBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public protocol ChatEditingDelegate: AnyObject {
func onForwardPressed()
func onCancelPressed()
func onCopyPressed()
func onMorePressed()
func onMorePressed() -> UIMenu
}

public class ChatEditingBar: UIView {
Expand Down Expand Up @@ -110,19 +110,21 @@ public class ChatEditingBar: UIView {
])

copyButton.addTarget(self, action: #selector(ChatEditingBar.onCopyPressed), for: .touchUpInside)
moreButton.addTarget(self, action: #selector(ChatEditingBar.onMorePressed), for: .touchUpInside)
forwardButton.addTarget(self, action: #selector(ChatEditingBar.onForwardPressed), for: .touchUpInside)
deleteButton.addTarget(self, action: #selector(ChatEditingBar.onDeletePressed), for: .touchUpInside)

moreButton.showsMenuAsPrimaryAction = true
moreButton.menu = UIMenu() // otherwise .menuActionTriggered is not triggered
moreButton.addAction(UIAction { [weak self] _ in
guard let self else { return }
moreButton.menu = delegate?.onMorePressed()
}, for: .menuActionTriggered)
}

@objc func onCopyPressed() {
delegate?.onCopyPressed()
}

@objc func onMorePressed() {
delegate?.onMorePressed()
}

@objc func onForwardPressed() {
delegate?.onForwardPressed()
}
Expand Down
1 change: 0 additions & 1 deletion deltachat-ios/View/ChatListEditingBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class ChatListEditingBar: UIView {
guard let self else { return }
moreButton.menu = delegate?.onMorePressed()
}, for: .menuActionTriggered)

}

@objc func deleteButtonPressed() {
Expand Down

0 comments on commit fcf832d

Please sign in to comment.