IOS-1697 implement UIViewController extension to present KeySyncHandShakeViewController alert and call it from KeySyncHandShakeService IOS-1697
authorAlejandro Gelos <agp@pep.security>
Wed, 10 Jul 2019 13:03:48 +0200
branchIOS-1697
changeset 936566865ea9b66a
parent 9364 bb5a31ba761e
child 9368 9a9f7f0f3a83
IOS-1697 implement UIViewController extension to present KeySyncHandShakeViewController alert and call it from KeySyncHandShakeService
pEpForiOS/Base.lproj/Reusable.storyboard
pEpForiOS/KeySync/KeySyncHandshakeService.swift
pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift
pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift
     1.1 --- a/pEpForiOS/Base.lproj/Reusable.storyboard	Wed Jul 10 10:47:07 2019 +0200
     1.2 +++ b/pEpForiOS/Base.lproj/Reusable.storyboard	Wed Jul 10 13:03:48 2019 +0200
     1.3 @@ -106,7 +106,7 @@
     1.4          <!--Key Sync Handshake View Controller-->
     1.5          <scene sceneID="5ce-Hn-mnm">
     1.6              <objects>
     1.7 -                <viewController id="GZS-2u-rXb" customClass="KeySyncHandshakeViewController" customModule="pEpForiOS" customModuleProvider="target" sceneMemberID="viewController">
     1.8 +                <viewController storyboardIdentifier="KeySyncHandshakeViewController" id="GZS-2u-rXb" customClass="KeySyncHandshakeViewController" customModule="pEpForiOS" customModuleProvider="target" sceneMemberID="viewController">
     1.9                      <view key="view" contentMode="scaleToFill" id="IdW-oi-ZvZ">
    1.10                          <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
    1.11                          <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
     2.1 --- a/pEpForiOS/KeySync/KeySyncHandshakeService.swift	Wed Jul 10 10:47:07 2019 +0200
     2.2 +++ b/pEpForiOS/KeySync/KeySyncHandshakeService.swift	Wed Jul 10 13:03:48 2019 +0200
     2.3 @@ -33,37 +33,49 @@
     2.4                  return
     2.5              }
     2.6  
     2.7 -            let lang = Locale.current.languageCode
     2.8 -            let trustwords = try! PEPSession().getTrustwordsFpr1(meFPR,
     2.9 -                                                                 fpr2: partnerFPR,
    2.10 -                                                                 language: lang,
    2.11 -                                                                 full: true)
    2.12 +//            let lang = Locale.current.languageCode
    2.13 +//            let trustwords = try! PEPSession().getTrustwordsFpr1(meFPR,
    2.14 +//                                                                 fpr2: partnerFPR,
    2.15 +//                                                                 language: lang,
    2.16 +//                                                                 full: true)
    2.17 +//
    2.18 +//            // Show Handshake
    2.19 +//            let newAlertView = UIAlertController.pEpAlertController(title: "Handshake",
    2.20 +//                                                                    message: trustwords,
    2.21 +//                                                                    preferredStyle: .alert)
    2.22 +//            // Accept Action
    2.23 +//            newAlertView.addAction(UIAlertAction(title: "Confirm Trustwords", style: .default) { action in
    2.24 +//                completion?(PEPSyncHandshakeResult.accepted)
    2.25 +//            })
    2.26 +//
    2.27 +//            // Reject Action
    2.28 +//            newAlertView.addAction(UIAlertAction(title: "Wrong Trustwords", style: .destructive) { action in
    2.29 +//                completion?(PEPSyncHandshakeResult.rejected)
    2.30 +//            })
    2.31 +//
    2.32 +//            // Cancel Action
    2.33 +//            newAlertView.addAction(UIAlertAction(title: "Cancel", style: .cancel) { action in
    2.34 +//                completion?(PEPSyncHandshakeResult.cancel)
    2.35 +//            })
    2.36 +//            safeSelf.alertView = newAlertView
    2.37  
    2.38 -            // Show Handshake
    2.39 -            let newAlertView = UIAlertController.pEpAlertController(title: "Handshake",
    2.40 -                                                                    message: trustwords,
    2.41 -                                                                    preferredStyle: .alert)
    2.42 -            // Accept Action
    2.43 -            newAlertView.addAction(UIAlertAction(title: "Confirm Trustwords", style: .default) { action in
    2.44 -                completion?(PEPSyncHandshakeResult.accepted)
    2.45 -            })
    2.46 -
    2.47 -            // Reject Action
    2.48 -            newAlertView.addAction(UIAlertAction(title: "Wrong Trustwords", style: .destructive) { action in
    2.49 -                completion?(PEPSyncHandshakeResult.rejected)
    2.50 -            })
    2.51 -
    2.52 -            // Cancel Action
    2.53 -            newAlertView.addAction(UIAlertAction(title: "Cancel", style: .cancel) { action in
    2.54 -                completion?(PEPSyncHandshakeResult.cancel)
    2.55 -            })
    2.56 -            safeSelf.alertView = newAlertView
    2.57 -
    2.58 -            guard let vc = safeSelf.presenter else {
    2.59 +            guard let viewController = safeSelf.presenter else {
    2.60                  Log.shared.errorAndCrash("No Presenter")
    2.61                  return
    2.62              }
    2.63 -            vc.present(newAlertView, animated: true, completion: nil)
    2.64 +
    2.65 +            viewController.presentKeySyncHandShakeAlert(meFPR: meFPR, partnerFPR: partnerFPR)
    2.66 +            { action in
    2.67 +                switch action {
    2.68 +                case .accept:
    2.69 +                    completion?(PEPSyncHandshakeResult.accepted)
    2.70 +                case .cancel:
    2.71 +                    completion?(PEPSyncHandshakeResult.cancel)
    2.72 +                case .decline:
    2.73 +                    completion?(PEPSyncHandshakeResult.rejected)
    2.74 +                }
    2.75 +            }
    2.76 +//            vc.present(newAlertView, animated: true, completion: nil)
    2.77          }
    2.78  
    2.79          //        DispatchQueue.main.async { [weak self] in
     3.1 --- a/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift	Wed Jul 10 10:47:07 2019 +0200
     3.2 +++ b/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift	Wed Jul 10 13:03:48 2019 +0200
     3.3 @@ -9,6 +9,7 @@
     3.4  import UIKit
     3.5  
     3.6  final class KeySyncHandshakeViewController: UIViewController {
     3.7 +    static let storyboardId = "KeySyncHandshakeViewController"
     3.8      
     3.9      @IBOutlet weak var titile: UILabel!
    3.10      @IBOutlet weak var message: UILabel!
    3.11 @@ -21,6 +22,12 @@
    3.12          }
    3.13      }
    3.14  
    3.15 +    enum Action {
    3.16 +        case cancel, decline, accept
    3.17 +    }
    3.18 +
    3.19 +    var completion: ((Action) -> Void)?
    3.20 +
    3.21      private let viewModel = KeySyncHandshakeViewModel()
    3.22      private var pickerLanguages = [String]()
    3.23  
    3.24 @@ -39,7 +46,10 @@
    3.25  // MARK: - KeySyncHandshakeViewModelDelegate
    3.26  extension KeySyncHandshakeViewController: KeySyncHandshakeViewModelDelegate {
    3.27      func didPress(action: KeySyncHandshakeViewModel.Action) {
    3.28 -
    3.29 +        guard let action = viewControllerAction(viewModelAction: action) else {
    3.30 +            return
    3.31 +        }
    3.32 +        completion?(action)
    3.33      }
    3.34  
    3.35      func showPicker(withLanguages languages: [String]) {
    3.36 @@ -100,4 +110,17 @@
    3.37              return nil
    3.38          }
    3.39      }
    3.40 +
    3.41 +    private func viewControllerAction(viewModelAction: KeySyncHandshakeViewModel.Action) -> Action? {
    3.42 +        switch viewModelAction {
    3.43 +        case .accept:
    3.44 +            return .accept
    3.45 +        case .cancel:
    3.46 +            return .cancel
    3.47 +        case .decline:
    3.48 +            return .decline
    3.49 +        case .changeLanguage:
    3.50 +            return nil
    3.51 +        }
    3.52 +    }
    3.53  }
     4.1 --- a/pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift	Wed Jul 10 10:47:07 2019 +0200
     4.2 +++ b/pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift	Wed Jul 10 13:03:48 2019 +0200
     4.3 @@ -40,4 +40,21 @@
     4.4              return nil
     4.5          }
     4.6      }
     4.7 +
     4.8 +    func presentKeySyncHandShakeAlert(meFPR: String, partnerFPR: String,
     4.9 +                                      completion: @escaping (KeySyncHandshakeViewController.Action) -> Void ) {
    4.10 +        let storyboard = UIStoryboard(name: "Reusable", bundle: .main)
    4.11 +        guard let handShakeViewController = storyboard.instantiateViewController(
    4.12 +            withIdentifier: KeySyncHandshakeViewController.storyboardId) as? KeySyncHandshakeViewController else {
    4.13 +                Log.shared.errorAndCrash("Fail to instantiateViewController KeySyncHandshakeViewController")
    4.14 +                return
    4.15 +        }
    4.16 +        handShakeViewController.completion = { action in
    4.17 +            completion(action)
    4.18 +        }
    4.19 +        handShakeViewController.finderPrints(meFPR: meFPR, partnerFPR: partnerFPR)
    4.20 +
    4.21 +        handShakeViewController.modalPresentationStyle = .overCurrentContext
    4.22 +        present(handShakeViewController, animated: true, completion: nil)
    4.23 +    }
    4.24  }