Skip to content

Commit

Permalink
use modern UIMenu global QR code viewer/scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Jan 24, 2025
1 parent 076deae commit 48945d9
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions deltachat-ios/Controller/QrPageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class QrPageController: UIPageViewController {
return control
}()

private lazy var moreButton: UIBarButtonItem = {
let image = UIImage(systemName: "ellipsis.circle")
return UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(showMoreOptions))
}()

init(dcAccounts: DcAccounts) {
self.dcAccounts = dcAccounts
self.dcContext = dcAccounts.getSelected()
Expand All @@ -67,7 +62,7 @@ class QrPageController: UIPageViewController {
dataSource = self
delegate = self
navigationItem.titleView = qrSegmentControl
navigationItem.rightBarButtonItem = moreButton
updateMenuItems()

setViewControllers(
[qrViewController],
Expand Down Expand Up @@ -98,43 +93,59 @@ class QrPageController: UIPageViewController {
} else {
setViewControllers([qrCodeReaderController], direction: .forward, animated: true, completion: nil)
}
updateMenuItems()
}

private func updateMenuItems() {
let menu = moreButtonMenu()
let button = UIBarButtonItem(image: UIImage(systemName: "ellipsis.circle"), menu: menu)
navigationItem.rightBarButtonItem = button
}

@objc private func showMoreOptions() {
let alert = UIAlertController(title: String.localized("qr_code"), message: nil, preferredStyle: .safeActionSheet)
private func moreButtonMenu() -> UIMenu {
var actions = [UIMenuElement]()
if qrSegmentControl.selectedSegmentIndex == 0 {
alert.addAction(UIAlertAction(title: String.localized("menu_share"), style: .default, handler: share(_:)))
alert.addAction(UIAlertAction(title: String.localized("menu_copy_to_clipboard"), style: .default, handler: copyToClipboard(_:)))
alert.addAction(UIAlertAction(title: String.localized("withdraw_qr_code"), style: .default, handler: withdrawQrCode(_:)))
actions.append(UIAction(title: String.localized("menu_share"), image: UIImage(systemName: "square.and.arrow.up")) { [weak self] _ in
self?.share()
})
actions.append(UIAction(title: String.localized("menu_copy_to_clipboard"), image: UIImage(systemName: "document.on.document")) { [weak self] _ in
self?.copyToClipboard()
})
}
alert.addAction(UIAlertAction(title: String.localized("paste_from_clipboard"), style: .default, handler: pasteFromClipboard(_:)))

actions.append(UIAction(title: String.localized("paste_from_clipboard"), image: UIImage(systemName: "doc.on.clipboard")) { [weak self] _ in
self?.pasteFromClipboard()
})
if dcContext.isChatmail == false {
let addContactManuallyAction = UIAlertAction(title: String.localized("menu_new_classic_contact"), style: .default, handler: { [weak self] _ in
actions.append(UIAction(title: String.localized("menu_new_classic_contact"), image: UIImage(systemName: "pencil")) { [weak self] _ in
guard let self else { return }

let newContactController = NewContactController(dcContext: self.dcContext)
self.navigationController?.pushViewController(newContactController, animated: true)
self.navigationController?.pushViewController(NewContactController(dcContext: self.dcContext), animated: true)
})

alert.addAction(addContactManuallyAction)
}

alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
self.present(alert, animated: true, completion: nil)
if qrSegmentControl.selectedSegmentIndex == 0 {
actions.append(UIMenu(options: [.displayInline],
children: [
UIAction(title: String.localized("withdraw_qr_code"), image: UIImage(systemName: "trash"), attributes: [.destructive]) { [weak self] _ in
self?.withdrawQrCode()
},
]
))
}
return UIMenu(children: actions)
}

@objc func share(_ action: UIAlertAction) {
func share() {
if let inviteLink = Utils.getInviteLink(context: dcContext, chatId: 0) {
Utils.share(url: inviteLink, parentViewController: self, sourceItem: moreButton)
if let sourceItem = navigationItem.rightBarButtonItem {
Utils.share(url: inviteLink, parentViewController: self, sourceItem: sourceItem)
}
}
}

@objc func copyToClipboard(_ action: UIAlertAction) {
func copyToClipboard() {
UIPasteboard.general.string = Utils.getInviteLink(context: dcContext, chatId: 0)
}

@objc func withdrawQrCode(_ action: UIAlertAction) {
func withdrawQrCode() {
let alert = UIAlertController(title: String.localized("withdraw_verifycontact_explain"), message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("withdraw_qr_code"), style: .destructive, handler: { [weak self] _ in
Expand All @@ -148,7 +159,7 @@ class QrPageController: UIPageViewController {
present(alert, animated: true)
}

@objc func pasteFromClipboard(_ action: UIAlertAction) {
func pasteFromClipboard() {
handleQrCode(UIPasteboard.general.string ?? "")
}

Expand Down Expand Up @@ -186,6 +197,7 @@ extension QrPageController: UIPageViewControllerDataSource, UIPageViewController
} else {
qrSegmentControl.selectedSegmentIndex = 1
}
updateMenuItems()
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
Expand All @@ -195,6 +207,7 @@ extension QrPageController: UIPageViewControllerDataSource, UIPageViewController
} else {
qrSegmentControl.selectedSegmentIndex = 1
}
updateMenuItems()
}
}
}
Expand Down

0 comments on commit 48945d9

Please sign in to comment.