Browse Source

removed duplicated code

nayooti 5 years ago
parent
commit
42d92d3304

+ 0 - 17
deltachat-ios/Controller/QrPageController.swift

@@ -205,23 +205,6 @@ extension QrPageController: QrCodeReaderDelegate {
         present(alert, animated: true, completion: nil)
     }
 
-    /*
-    private func showProgressAlert() {
-        self.present(self.progressAlert, animated: true, completion: {
-            let rect = CGRect(x: 10, y: 10, width: 20, height: 20)
-            let progressView = UIActivityIndicatorView(frame: rect)
-            progressView.tintColor = .blue
-            progressView.startAnimating()
-            progressView.translatesAutoresizingMaskIntoConstraints = false
-            self.progressAlert.view.addSubview(progressView)
-            self.progressAlert.view.addConstraints([
-                progressView.constraintCenterXTo(self.progressAlert.view),
-                progressView.constraintAlignTopTo(self.progressAlert.view, paddingTop: 45)
-            ])
-        })
-    }
-    */
-
     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

+ 0 - 181
deltachat-ios/Controller/QrViewController.swift

@@ -5,9 +5,6 @@ import DcCore
 class QrViewController: UITableViewController {
     private let rowQRCode = 0
 
-    weak var coordinator: QrViewCoordinator?
-    let qrCodeReaderController = QrCodeReaderController()
-    var secureJoinObserver: Any?
     var dcContext: DcContext
     var contact: DcContact? {
         // This is nil if we do not have an account setup yet
@@ -29,7 +26,6 @@ class QrViewController: UITableViewController {
     override func viewDidLoad() {
         super.viewDidLoad()
         title = String.localized("qr_code")
-        qrCodeReaderController.delegate = self
         tableView.separatorStyle = .none
     }
     
@@ -60,100 +56,6 @@ class QrViewController: UITableViewController {
         }
     }
 
-    private lazy var progressAlert: UIAlertController = {
-        var title = String.localized("one_moment")+"\n\n"
-        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
-
-        let rect = CGRect(x: 0, y: 0, width: 25, height: 25)
-        let activityIndicator = UIActivityIndicatorView(frame: rect)
-        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
-        activityIndicator.style = .gray
-
-        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: { _ in
-            self.dcContext.stopOngoingProcess()
-            self.dismiss(animated: true, completion: nil)
-        }))
-        return alert
-    }()
-
-    private func showProgressAlert() {
-        self.present(self.progressAlert, animated: true, completion: {
-            let rect = CGRect(x: 10, y: 10, width: 20, height: 20)
-            let progressView = UIActivityIndicatorView(frame: rect)
-            progressView.tintColor = .blue
-            progressView.startAnimating()
-            progressView.translatesAutoresizingMaskIntoConstraints = false
-            self.progressAlert.view.addSubview(progressView)
-            self.progressAlert.view.addConstraints([
-                progressView.constraintCenterXTo(self.progressAlert.view),
-                progressView.constraintAlignTopTo(self.progressAlert.view, paddingTop: 45)
-            ])
-        })
-    }
-
-    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)
-        }))
-    }
-
-    private func addSecureJoinProgressListener() {
-        let nc = NotificationCenter.default
-        secureJoinObserver = nc.addObserver(
-            forName: dcNotificationSecureJoinerProgress,
-            object: nil,
-            queue: nil
-        ) { notification in
-            print("secure join: ", notification)
-            if let ui = notification.userInfo {
-                if ui["progress"] as? Int == 400 {
-                    if let contactId = ui["contact_id"] as? Int {
-                        self.progressAlert.message = String.localizedStringWithFormat(
-                            String.localized("qrscan_x_verified_introduce_myself"),
-                            DcContact(id: contactId).nameNAddr)
-                    }
-                }
-            }
-        }
-    }
-
-    private func removeSecureJoinProgressListener() {
-        let nc = NotificationCenter.default
-        if let secureJoinObserver = self.secureJoinObserver {
-            nc.removeObserver(secureJoinObserver)
-        }
-    }
-
-      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
-            alert.dismiss(animated: true, completion: nil)
-            self.showProgressAlert()
-            // execute blocking secure join in background
-            DispatchQueue.global(qos: .background).async {
-                self.addSecureJoinProgressListener()
-                self.dcContext.lastErrorString = nil
-                let chatId = self.dcContext.joinSecurejoin(qrCode: code)
-                let errorString = self.dcContext.lastErrorString
-                self.removeSecureJoinProgressListener()
-
-                DispatchQueue.main.async {
-                    self.progressAlert.dismiss(animated: true, completion: nil)
-                    if chatId != 0 {
-                        self.coordinator?.showChat(chatId: chatId)
-                    } else if errorString != nil {
-                        self.showErrorAlert(error: errorString!)
-                    }
-                }
-            }
-        }))
-        present(alert, animated: true, completion: nil)
-    }
-
     private func createQRCodeCell() -> UITableViewCell {
         let cell = UITableViewCell(style: .default, reuseIdentifier: "qrCodeCell")
         let qrCode = createQRCodeView()
@@ -211,86 +113,3 @@ class QrViewController: UITableViewController {
         navigationController?.pushViewController(chatVC, animated: true)
     }
 }
-
-// MARK: - QRCodeDelegate
-extension QrViewController: QrCodeReaderDelegate {
-
-    func handleQrCode(_ code: String) {
-        qrCodeReaderController.dismiss(animated: true) {
-            self.processQrCode(code)
-        }
-    }
-
-    private func processQrCode(_ code: String) {
-        let qrParsed: DcLot = self.dcContext.checkQR(qrCode: code)
-        let state = Int32(qrParsed.state)
-        switch state {
-        case DC_QR_ASK_VERIFYCONTACT:
-            let nameAndAddress = DcContact(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 = DcContact(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 = DcContact(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("start_chat"), style: .default, handler: { _ in
-                let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
-                self.coordinator?.showChat(chatId: chatId)
-            }))
-            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
-            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("open"), style: .default, handler: { _ in
-                if let url = URL(string: url) {
-                    UIApplication.shared.open(url)
-                }
-            }))
-            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
-            present(alert, animated: true, completion: nil)
-
-        case DC_QR_ACCOUNT:
-            let alert = UIAlertController(title: String.localized("qraccount_use_on_new_install"), message: nil, preferredStyle: .alert)
-            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default))
-            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)
-        }
-    }
-}

+ 3 - 6
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -54,18 +54,15 @@ class AppCoordinator: NSObject, Coordinator {
         return nav
     }()
 
-
     private lazy var qrController: UIViewController = {
         let controller = QrViewController(dcContext: dcContext)
         let nav = UINavigationController(rootViewController: controller)
         let settingsImage = UIImage(named: "qr_code")
         nav.tabBarItem = UITabBarItem(title: String.localized("qr_code"), image: settingsImage, tag: qrTab)
-        let coordinator = QrViewCoordinator(navigationController: nav)
-        self.childCoordinators.append(coordinator)
-        controller.coordinator = coordinator
         return nav
     }()
 
+
     private lazy var chatListController: UIViewController = {
         let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: false)
         let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
@@ -142,8 +139,8 @@ class AppCoordinator: NSObject, Coordinator {
         showTab(index: qrTab)
         if let navController = qrController as? UINavigationController,
             let topViewController = navController.topViewController,
-            let qrViewController = topViewController as? QrViewController {
-            qrViewController.handleQrCode(code)
+            let qrPageController = topViewController as? QrPageController {
+            qrPageController.handleQrCode(code)
         }
     }