IOS-1697 move completion block of keysync handShake alert from VC to VM IOS-1697
authorAlejandro Gelos <agp@pep.security>
Thu, 08 Aug 2019 14:33:34 +0200
branchIOS-1697
changeset 96038b6427c4c87d
parent 9602 51d9c7d8b90a
child 9604 f49b03fbc556
IOS-1697 move completion block of keysync handShake alert from VC to VM
pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift
pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewModel.swift
pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift
pEpForiOSTests/UI/KeySyncHandshake/KeySyncHandshakeViewModelTest.swift
     1.1 --- a/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift	Wed Aug 07 16:22:18 2019 +0200
     1.2 +++ b/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewController.swift	Thu Aug 08 14:33:34 2019 +0200
     1.3 @@ -39,7 +39,7 @@
     1.4      }
     1.5      @IBOutlet weak var message: UILabel! {
     1.6          didSet {
     1.7 -            message.text = NSLocalizedString("A second device is detected. Please confirm the Trustwords on both devices to sync all your privacy. Shall we synchronize?", comment: "keySync handshake alert message")
     1.8 +            message.text = NSLocalizedString("A second device is detected. \nPlease confirm the Trustwords on both devices to sync all your privacy. Shall we synchronize?", comment: "keySync handshake alert message")
     1.9          }
    1.10      }
    1.11  
    1.12 @@ -70,8 +70,6 @@
    1.13          }
    1.14      }
    1.15  
    1.16 -    var completion: ((Action) -> Void)?
    1.17 -
    1.18      private let viewModel = KeySyncHandshakeViewModel()
    1.19      private var pickerLanguages = [String]()
    1.20      private var meFPR: String?
    1.21 @@ -101,15 +99,16 @@
    1.22          }
    1.23          viewModel.didLongPressWords()
    1.24      }
    1.25 +
    1.26 +    func completionHandler(_ block: @escaping (Action) -> Void) {
    1.27 +        viewModel.completionHandler = block
    1.28 +    }
    1.29  }
    1.30  
    1.31  // MARK: - KeySyncHandshakeViewModelDelegate
    1.32 +
    1.33  extension KeySyncHandshakeViewController: KeySyncHandshakeViewModelDelegate {
    1.34 -    func didPress(action: KeySyncHandshakeViewModel.Action) {
    1.35 -        guard let action = viewControllerAction(viewModelAction: action) else {
    1.36 -            return
    1.37 -        }
    1.38 -        completion?(action)
    1.39 +    func dissmissView() {
    1.40          DispatchQueue.main.async { [weak self] in
    1.41              self?.dismiss(animated: true, completion: nil)
    1.42          }
    1.43 @@ -173,17 +172,4 @@
    1.44              return nil
    1.45          }
    1.46      }
    1.47 -
    1.48 -    private func viewControllerAction(viewModelAction: KeySyncHandshakeViewModel.Action) -> Action? {
    1.49 -        switch viewModelAction {
    1.50 -        case .accept:
    1.51 -            return .accept
    1.52 -        case .cancel:
    1.53 -            return .cancel
    1.54 -        case .decline:
    1.55 -            return .decline
    1.56 -        case .changeLanguage:
    1.57 -            return nil
    1.58 -        }
    1.59 -    }
    1.60  }
     2.1 --- a/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewModel.swift	Wed Aug 07 16:22:18 2019 +0200
     2.2 +++ b/pEpForiOS/UI/KeySyncHandshake/KeySyncHandshakeViewModel.swift	Thu Aug 08 14:33:34 2019 +0200
     2.3 @@ -10,7 +10,7 @@
     2.4  import PEPObjCAdapterFramework
     2.5  
     2.6  protocol KeySyncHandshakeViewModelDelegate: class {
     2.7 -    func didPress(action: KeySyncHandshakeViewModel.Action)
     2.8 +    func dissmissView()
     2.9      func showPicker(withLanguages languages: [String])
    2.10      func closePicker()
    2.11      func change(handshakeWordsTo: String)
    2.12 @@ -21,6 +21,8 @@
    2.13          case cancel, decline, accept, changeLanguage
    2.14      }
    2.15  
    2.16 +    var completionHandler: ((KeySyncHandshakeViewController.Action) -> Void)?
    2.17 +
    2.18      weak var delegate: KeySyncHandshakeViewModelDelegate?
    2.19      var fullTrustWords = false //Internal since testing
    2.20      private var languageCode = Locale.current.languageCode
    2.21 @@ -54,7 +56,11 @@
    2.22      func handle(action: Action) {
    2.23          switch action {
    2.24          case .accept, .cancel, .decline:
    2.25 -            delegate?.didPress(action: action)
    2.26 +            guard let action = viewControllerAction(viewModelAction: action) else {
    2.27 +                return
    2.28 +            }
    2.29 +            completionHandler?(action)
    2.30 +            delegate?.dissmissView()
    2.31          case .changeLanguage:
    2.32              handleChangeLanguageButton()
    2.33          }
    2.34 @@ -97,4 +103,18 @@
    2.35          let languagesNames = languages.map { $0.name }
    2.36          delegate?.showPicker(withLanguages: languagesNames)
    2.37      }
    2.38 +
    2.39 +    private func viewControllerAction(viewModelAction: KeySyncHandshakeViewModel.Action)
    2.40 +        -> KeySyncHandshakeViewController.Action? {
    2.41 +            switch viewModelAction {
    2.42 +            case .accept:
    2.43 +                return .accept
    2.44 +            case .cancel:
    2.45 +                return .cancel
    2.46 +            case .decline:
    2.47 +                return .decline
    2.48 +            case .changeLanguage:
    2.49 +                return nil
    2.50 +            }
    2.51 +    }
    2.52  }
     3.1 --- a/pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift	Wed Aug 07 16:22:18 2019 +0200
     3.2 +++ b/pEpForiOS/UI/Util/Extensions/UIViewController+Extension.swift	Thu Aug 08 14:33:34 2019 +0200
     3.3 @@ -50,12 +50,12 @@
     3.4                  Log.shared.errorAndCrash("Fail to instantiateViewController KeySyncHandshakeViewController")
     3.5                  return
     3.6          }
     3.7 -        handShakeViewController.completion = { action in
     3.8 +        handShakeViewController.completionHandler { action in
     3.9              completion(action)
    3.10          }
    3.11          handShakeViewController.finderPrints(meFPR: meFPR, partnerFPR: partnerFPR)
    3.12  
    3.13 -        handShakeViewController.modalPresentationStyle = .overCurrentContext
    3.14 +        handShakeViewController.modalPresentationStyle = .fullScreen
    3.15          present(handShakeViewController, animated: true, completion: nil)
    3.16      }
    3.17  }
     4.1 --- a/pEpForiOSTests/UI/KeySyncHandshake/KeySyncHandshakeViewModelTest.swift	Wed Aug 07 16:22:18 2019 +0200
     4.2 +++ b/pEpForiOSTests/UI/KeySyncHandshake/KeySyncHandshakeViewModelTest.swift	Thu Aug 08 14:33:34 2019 +0200
     4.3 @@ -23,6 +23,9 @@
     4.4          keySyncHandshakeVM?.delegate = self
     4.5  
     4.6          setDefaultActualState()
     4.7 +        keySyncHandshakeVM?.completionHandler = { [weak self] action in
     4.8 +            self?.actual?.pressedAction = action
     4.9 +        }
    4.10          expected = nil
    4.11      }
    4.12  
    4.13 @@ -56,7 +59,7 @@
    4.14              XCTFail()
    4.15              return
    4.16          }
    4.17 -        expected = State(didCallDidPressAction: true, pressedAction: .accept)
    4.18 +        expected = State(didCallDissmissView: true, pressedAction: .accept)
    4.19  
    4.20          // WHEN
    4.21          keySyncHandshakeVM.handle(action: .accept)
    4.22 @@ -86,7 +89,7 @@
    4.23              XCTFail()
    4.24              return
    4.25          }
    4.26 -        expected = State(didCallDidPressAction: true, pressedAction: .decline)
    4.27 +        expected = State(didCallDissmissView: true, pressedAction: .decline)
    4.28  
    4.29          // WHEN
    4.30          keySyncHandshakeVM.handle(action: .decline)
    4.31 @@ -101,7 +104,7 @@
    4.32              XCTFail()
    4.33              return
    4.34          }
    4.35 -        expected = State(didCallDidPressAction: true, pressedAction: .cancel)
    4.36 +        expected = State(didCallDissmissView: true, pressedAction: .cancel)
    4.37  
    4.38          // WHEN
    4.39          keySyncHandshakeVM.handle(action: .cancel)
    4.40 @@ -129,15 +132,14 @@
    4.41  // MARK: - KeySyncHandshakeViewModelDelegate
    4.42  
    4.43  extension KeySyncHandshakeViewModelTest: KeySyncHandshakeViewModelDelegate {
    4.44 +    func dissmissView() {
    4.45 +        actual?.didCallDissmissView = true
    4.46 +    }
    4.47 +
    4.48      func closePicker() {
    4.49          actual?.didCallClosePicker = true
    4.50      }
    4.51  
    4.52 -    func didPress(action: KeySyncHandshakeViewModel.Action) {
    4.53 -        actual?.didCallDidPressAction = true
    4.54 -        actual?.pressedAction = action
    4.55 -    }
    4.56 -
    4.57      func showPicker(withLanguages languages: [String]) {
    4.58          actual?.didCallShowPicker = true
    4.59          actual?.languagesToShow = languages
    4.60 @@ -166,7 +168,7 @@
    4.61          //bools
    4.62          XCTAssertEqual(expected.didCallShowPicker, actual.didCallShowPicker)
    4.63          XCTAssertEqual(expected.didCallClosePicker, actual.didCallClosePicker)
    4.64 -        XCTAssertEqual(expected.didCallDidPressAction, actual.didCallDidPressAction)
    4.65 +        XCTAssertEqual(expected.didCallDissmissView, actual.didCallDissmissView)
    4.66          XCTAssertEqual(expected.didCallToUpdateTrustedWords, actual.didCallToUpdateTrustedWords)
    4.67  
    4.68          //values
    4.69 @@ -186,27 +188,27 @@
    4.70      struct State: Equatable {
    4.71          var didCallShowPicker: Bool
    4.72          var didCallClosePicker: Bool
    4.73 -        var didCallDidPressAction: Bool
    4.74 +        var didCallDissmissView: Bool
    4.75          var didCallToUpdateTrustedWords: Bool
    4.76  
    4.77          var fullWordsVersion: Bool?
    4.78          var languagesToShow: [String]?
    4.79          var handShakeWords: String?
    4.80 -        var pressedAction: KeySyncHandshakeViewModel.Action?
    4.81 +        var pressedAction: KeySyncHandshakeViewController.Action?
    4.82  
    4.83          // Default value are default initial state
    4.84          init(didCallShowPicker: Bool = false,
    4.85               didCallClosePicker: Bool = false,
    4.86 -             didCallDidPressAction: Bool = false,
    4.87 +             didCallDissmissView: Bool = false,
    4.88               didCallToUpdateTrustedWords: Bool = false,
    4.89               fullWordsVersion: Bool = false,
    4.90               languagesToShow: [String] = [],
    4.91               handShakeWords: String = "",
    4.92 -             pressedAction: KeySyncHandshakeViewModel.Action? = nil) {
    4.93 +             pressedAction: KeySyncHandshakeViewController.Action? = nil) {
    4.94  
    4.95              self.didCallShowPicker = didCallShowPicker
    4.96              self.didCallClosePicker = didCallClosePicker
    4.97 -            self.didCallDidPressAction = didCallDidPressAction
    4.98 +            self.didCallDissmissView = didCallDissmissView
    4.99              self.didCallToUpdateTrustedWords = didCallToUpdateTrustedWords
   4.100  
   4.101              self.fullWordsVersion = fullWordsVersion