Skip to content

Commit

Permalink
Move QR-code-handling to AppCoordinator (#2134)
Browse files Browse the repository at this point in the history
All just so that `cancel` in that alert works.
  • Loading branch information
zeitschlag committed Oct 9, 2024
1 parent 4c2b5bb commit cda393f
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 178 deletions.
2 changes: 1 addition & 1 deletion deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@ extension ChatViewController: BaseMessageCellDelegate {
} else if url.isDeltaChatInvitation,
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let appCoordinator = appDelegate.appCoordinator {
appCoordinator.handleDeltaChatInvitation(url: url)
appCoordinator.handleDeltaChatInvitation(url: url, from: self)
} else {
UIApplication.shared.open(url)
}
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ios/Controller/ContactDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ extension ContactDetailViewController: MultilineLabelCellDelegate {
} else if url.isDeltaChatInvitation,
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let appCoordinator = appDelegate.appCoordinator {
appCoordinator.handleDeltaChatInvitation(url: url)
appCoordinator.handleDeltaChatInvitation(url: url, from: self)
} else {
UIApplication.shared.open(url)
}
Expand Down
174 changes: 2 additions & 172 deletions deltachat-ios/Controller/QrPageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,6 @@ class QrPageController: UIPageViewController {
}
}

// MARK: - coordinator
private func showChats() {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.appCoordinator.showTab(index: appDelegate.appCoordinator.chatsTab)
}
}

private func showChat(chatId: Int) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.appCoordinator.showChat(chatId: chatId, animated: false, clearViewControllerStack: true)
}
}
}

// MARK: - UIPageViewControllerDataSource, UIPageViewControllerDelegate
Expand Down Expand Up @@ -222,165 +210,7 @@ extension QrPageController: UIPageViewControllerDataSource, UIPageViewController
extension QrPageController: QrCodeReaderDelegate {

func handleQrCode(_ code: String) {
self.showChats()
let qrParsed: DcLot = self.dcContext.checkQR(qrCode: code)
let state = Int32(qrParsed.state)
switch state {
case DC_QR_ASK_VERIFYCONTACT:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), nameAndAddress), code: code)

case DC_QR_ASK_VERIFYGROUP:
let groupName = qrParsed.text1 ?? "ErrGroupName"
joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("qrscan_ask_join_group"), groupName), code: code)

case DC_QR_FPR_WITHOUT_ADDR:
let msg = String.localized("qrscan_no_addr_found") + "\n\n" +
String.localized("qrscan_fingerprint_label") + ":\n" + (qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_FPR_MISMATCH:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
let msg = String.localizedStringWithFormat(String.localized("qrscan_fingerprint_mismatch"), nameAndAddress)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_ADDR, DC_QR_FPR_OK:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
self.showChat(chatId: chatId)
}))
present(alert, animated: true, completion: nil)

case DC_QR_TEXT:
let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_URL:
let url = qrParsed.text1 ?? ""
let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_url"), url)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("open"), style: .default, handler: { _ in
if let url = URL(string: url) {
UIApplication.shared.open(url)
}
}))
present(alert, animated: true, completion: nil)

case DC_QR_ACCOUNT, DC_QR_LOGIN:
let msg = String.localizedStringWithFormat(String.localized(state == DC_QR_ACCOUNT ? "qraccount_ask_create_and_login_another" : "qrlogin_ask_login_another"), qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
_ = self.dcAccounts.add()
appDelegate.reloadDcContext(accountCode: code)
}))
present(alert, animated: true, completion: nil)

case DC_QR_BACKUP, DC_QR_BACKUP2:
// alert is shown in WelcomeViewController
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
_ = dcAccounts.add()
appDelegate.reloadDcContext(accountCode: code)

case DC_QR_WEBRTC_INSTANCE:
guard let domain = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("videochat_instance_from_qr"), domain),
message: nil,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { [weak self] _ in
guard let self else { return }
let success = self.dcContext.setConfigFromQR(qrCode: code)
if !success {
logger.warning("Could not set webrtc instance from QR code.")
// TODO: alert?!
}
}))
present(alert, animated: true)

case DC_QR_WITHDRAW_VERIFYCONTACT:
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
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_REVIVE_VERIFYCONTACT:
let alert = UIAlertController(title: String.localized("revive_verifycontact_explain"),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("revive_qr_code"), style: .default, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_WITHDRAW_VERIFYGROUP:
guard let groupName = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("withdraw_verifygroup_explain"), groupName),
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
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_REVIVE_VERIFYGROUP:
guard let groupName = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("revive_verifygroup_explain"), groupName),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("revive_qr_code"), style: .default, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

default:
var msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), code)
if state == DC_QR_ERROR {
if let errorMsg = qrParsed.text1 {
msg = errorMsg + "\n\n" + msg
}
}
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}

private func joinSecureJoin(alertMessage: String, code: String) {
let alert = UIAlertController(title: alertMessage,
message: nil,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
let chatId = self.dcContext.joinSecurejoin(qrCode: code)
if chatId != 0 {
self.showChat(chatId: chatId)
} else {
self.showErrorAlert(error: self.dcContext.lastErrorString)
}
}))
present(alert, animated: true, completion: nil)
}

private func showErrorAlert(error: String) {
let alert = UIAlertController(title: String.localized("error"), message: error, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
alert.dismiss(animated: true, completion: nil)
}))
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
appDelegate.appCoordinator.reallyHandleQRCode(code, from: self)
}
}
Loading

0 comments on commit cda393f

Please sign in to comment.