User verification: Implement start user verification screen.

This commit is contained in:
SBiOSoftWhare
2020-01-30 17:38:33 +01:00
parent 5593598d3f
commit 50d146c6cd
8 changed files with 735 additions and 0 deletions
@@ -0,0 +1,77 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
import UIKit
final class UserVerificationStartCoordinator: UserVerificationStartCoordinatorType {
// MARK: - Properties
// MARK: Private
private let session: MXSession
private let roomMember: MXRoomMember
private var userVerificationStartViewModel: UserVerificationStartViewModelType
private let userVerificationStartViewController: UserVerificationStartViewController
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
weak var delegate: UserVerificationStartCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession, roomMember: MXRoomMember) {
self.session = session
self.roomMember = roomMember
let userVerificationStartViewModel = UserVerificationStartViewModel(session: self.session, roomMember: self.roomMember)
let userVerificationStartViewController = UserVerificationStartViewController.instantiate(with: userVerificationStartViewModel)
self.userVerificationStartViewModel = userVerificationStartViewModel
self.userVerificationStartViewController = userVerificationStartViewController
}
// MARK: - Public methods
func start() {
self.userVerificationStartViewModel.coordinatorDelegate = self
}
func toPresentable() -> UIViewController {
return self.userVerificationStartViewController
}
}
// MARK: - UserVerificationStartViewModelCoordinatorDelegate
extension UserVerificationStartCoordinator: UserVerificationStartViewModelCoordinatorDelegate {
func userVerificationStartViewModelDidCancel(_ viewModel: UserVerificationStartViewModelType) {
self.delegate?.userVerificationStartCoordinatorDidCancel(self)
}
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction) {
self.delegate?.userVerificationStartCoordinator(self, didCompleteWithOutgoingTransaction: transaction)
}
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didTransactionCancelled transaction: MXSASTransaction) {
self.delegate?.userVerificationStartCoordinator(self, didTransactionCancelled: transaction)
}
}
@@ -0,0 +1,32 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
protocol UserVerificationStartCoordinatorDelegate: class {
func userVerificationStartCoordinator(_ coordinator: UserVerificationStartCoordinatorType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction)
func userVerificationStartCoordinator(_ coordinator: UserVerificationStartCoordinatorType, didTransactionCancelled transaction: MXSASTransaction)
func userVerificationStartCoordinatorDidCancel(_ coordinator: UserVerificationStartCoordinatorType)
}
/// `UserVerificationStartCoordinatorType` is a protocol describing a Coordinator that handle key backup setup passphrase navigation flow.
protocol UserVerificationStartCoordinatorType: Coordinator, Presentable {
var delegate: UserVerificationStartCoordinatorDelegate? { get }
}
@@ -0,0 +1,26 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
/// UserVerificationStartViewController view actions exposed to view model
enum UserVerificationStartViewAction {
case loadData
case startVerification
case cancel
}
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="V8j-Lb-PgC">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--User Verification Start View Controller-->
<scene sceneID="mt5-wz-YKA">
<objects>
<viewController extendedLayoutIncludesOpaqueBars="YES" automaticallyAdjustsScrollViewInsets="NO" id="V8j-Lb-PgC" customClass="UserVerificationStartViewController" customModule="Riot" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="EL9-GA-lwo">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="dQD-Vs-7h6">
<rect key="frame" x="0.0" y="254" width="375" height="179"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="For extra security, verify @user:matrix.org by checking a one-time code on both your devices." textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="b2K-LP-jqv">
<rect key="frame" x="20" y="10" width="335" height="36"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Waiting for User…" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0QC-yi-0hN">
<rect key="frame" x="20" y="78" width="335" height="20.5"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3sZ-h5-05D">
<rect key="frame" x="20" y="66" width="335" height="44"/>
<accessibility key="accessibilityConfiguration" identifier="AuthenticationVCLoginButton"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="2qO-rj-st6"/>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="100" id="8kK-yb-srh"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="17"/>
<inset key="contentEdgeInsets" minX="30" minY="0.0" maxX="30" maxY="0.0"/>
<state key="normal" title="Start Verification">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="startVerificationButtonAction:" destination="V8j-Lb-PgC" eventType="touchUpInside" id="3Nd-Km-ucJ"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="To be secure, do this in person or use another way to communicate." textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Vli-IZ-UjP">
<rect key="frame" x="20" y="130" width="335" height="29"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="3sZ-h5-05D" firstAttribute="centerX" secondItem="dQD-Vs-7h6" secondAttribute="centerX" id="2OM-Zh-gnO"/>
<constraint firstAttribute="trailing" secondItem="0QC-yi-0hN" secondAttribute="trailing" constant="20" id="70Z-qZ-AG4"/>
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="3sZ-h5-05D" secondAttribute="trailing" constant="20" id="ABg-Iy-IdN"/>
<constraint firstItem="Vli-IZ-UjP" firstAttribute="top" secondItem="3sZ-h5-05D" secondAttribute="bottom" constant="20" id="BIE-UM-I0Z"/>
<constraint firstItem="3sZ-h5-05D" firstAttribute="top" secondItem="b2K-LP-jqv" secondAttribute="bottom" constant="20" id="HCf-2F-jBr"/>
<constraint firstItem="Vli-IZ-UjP" firstAttribute="leading" secondItem="dQD-Vs-7h6" secondAttribute="leading" constant="20" id="Nz0-mv-fF5"/>
<constraint firstItem="3sZ-h5-05D" firstAttribute="width" secondItem="dQD-Vs-7h6" secondAttribute="width" priority="250" id="RuV-mq-5JZ"/>
<constraint firstAttribute="trailing" secondItem="Vli-IZ-UjP" secondAttribute="trailing" constant="20" id="XXe-5j-SxE"/>
<constraint firstAttribute="bottom" secondItem="Vli-IZ-UjP" secondAttribute="bottom" constant="20" id="Yfh-kG-UKJ"/>
<constraint firstItem="b2K-LP-jqv" firstAttribute="leading" secondItem="dQD-Vs-7h6" secondAttribute="leading" constant="20" id="dgE-c5-D4E"/>
<constraint firstItem="0QC-yi-0hN" firstAttribute="leading" secondItem="dQD-Vs-7h6" secondAttribute="leading" constant="20" id="g4e-WC-LQ7"/>
<constraint firstItem="0QC-yi-0hN" firstAttribute="centerY" secondItem="3sZ-h5-05D" secondAttribute="centerY" id="hqE-9r-yhx"/>
<constraint firstAttribute="height" priority="250" id="j1W-Y7-dYc"/>
<constraint firstItem="3sZ-h5-05D" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="dQD-Vs-7h6" secondAttribute="leading" constant="20" id="oP3-BI-kYI"/>
<constraint firstAttribute="trailing" secondItem="b2K-LP-jqv" secondAttribute="trailing" constant="20" id="uJ5-fk-7Xw"/>
<constraint firstItem="b2K-LP-jqv" firstAttribute="top" secondItem="dQD-Vs-7h6" secondAttribute="top" constant="10" id="vlb-Yg-HWD"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="0.94509803921568625" green="0.96078431372549022" blue="0.97254901960784312" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="dQD-Vs-7h6" firstAttribute="centerY" secondItem="bFg-jh-JZB" secondAttribute="centerY" id="GmI-7r-ejI"/>
<constraint firstItem="dQD-Vs-7h6" firstAttribute="trailing" secondItem="bFg-jh-JZB" secondAttribute="trailing" id="XX2-fS-8cb"/>
<constraint firstItem="dQD-Vs-7h6" firstAttribute="leading" secondItem="bFg-jh-JZB" secondAttribute="leading" id="yKt-js-MnW"/>
</constraints>
<viewLayoutGuide key="safeArea" id="bFg-jh-JZB"/>
</view>
<connections>
<outlet property="additionalInformationLabel" destination="Vli-IZ-UjP" id="4UB-LL-Om3"/>
<outlet property="informationLabel" destination="b2K-LP-jqv" id="O4J-7t-Dn6"/>
<outlet property="startVerificationButton" destination="3sZ-h5-05D" id="vNi-m9-x7g"/>
<outlet property="verificationWaitingLabel" destination="0QC-yi-0hN" id="Qoz-cV-Su7"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="zK0-v6-7Wt" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3198" y="-647"/>
</scene>
</scenes>
</document>
@@ -0,0 +1,229 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
final class UserVerificationStartViewController: UIViewController {
// MARK: - Constants
private enum Constants {
static let verifyButtonCornerRadius: CGFloat = 8.0
static let informationTextDefaultFont = UIFont.systemFont(ofSize: 15.0)
static let informationTextBoldFont = UIFont.systemFont(ofSize: 15.0, weight: .medium)
}
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var informationLabel: UILabel!
@IBOutlet private weak var startVerificationButton: UIButton!
@IBOutlet private weak var verificationWaitingLabel: UILabel!
@IBOutlet private weak var additionalInformationLabel: UILabel!
// MARK: Private
private var viewModel: UserVerificationStartViewModelType!
private var theme: Theme!
private var errorPresenter: MXKErrorPresentation!
private var activityPresenter: ActivityIndicatorPresenter!
// MARK: - Setup
class func instantiate(with viewModel: UserVerificationStartViewModelType) -> UserVerificationStartViewController {
let viewController = StoryboardScene.UserVerificationStartViewController.initialScene.instantiate()
viewController.viewModel = viewModel
viewController.theme = ThemeService.shared().theme
return viewController
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "Verify user"
self.setupViews()
self.activityPresenter = ActivityIndicatorPresenter()
self.errorPresenter = MXKErrorAlertPresentation()
self.registerThemeServiceDidChangeThemeNotification()
self.update(theme: self.theme)
self.viewModel.viewDelegate = self
self.viewModel.process(viewAction: .loadData)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.theme.statusBarStyle
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.startVerificationButton.layer.cornerRadius = Constants.verifyButtonCornerRadius
}
// MARK: - Private
private func update(theme: Theme) {
self.theme = theme
self.view.backgroundColor = theme.headerBackgroundColor
if let navigationBar = self.navigationController?.navigationBar {
theme.applyStyle(onNavigationBar: navigationBar)
}
self.informationLabel.textColor = theme.textPrimaryColor
self.startVerificationButton.vc_setBackgroundColor(theme.tintColor, for: .normal)
self.verificationWaitingLabel.textColor = theme.textSecondaryColor
self.additionalInformationLabel.textColor = theme.textSecondaryColor
}
private func registerThemeServiceDidChangeThemeNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .themeServiceDidChangeTheme, object: nil)
}
@objc private func themeDidChange() {
self.update(theme: ThemeService.shared().theme)
}
private func setupViews() {
let cancelBarButtonItem = MXKBarButtonItem(title: VectorL10n.cancel, style: .plain) { [weak self] in
self?.cancelButtonAction()
}
self.navigationItem.rightBarButtonItem = cancelBarButtonItem
self.startVerificationButton.layer.masksToBounds = true
self.startVerificationButton.setTitle("Start verification", for: .normal)
}
private func render(viewState: UserVerificationStartViewState) {
switch viewState {
case .loading:
self.renderLoading()
case .loaded(let viewData):
self.renderLoaded(viewData: viewData)
case .error(let error):
self.render(error: error)
case .verificationPending:
self.renderVerificationPending()
case .cancelled(let reason):
self.renderCancelled(reason: reason)
case .cancelledByMe(let reason):
self.renderCancelledByMe(reason: reason)
}
}
private func renderLoading() {
self.activityPresenter.presentActivityIndicator(on: self.view, animated: true)
}
private func renderLoaded(viewData: UserVerificationStartViewData) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.informationLabel.attributedText = self.buildInformationAttributedText(with: viewData.userId)
self.verificationWaitingLabel.text = self.buildVerificationWaitingText(with: viewData)
}
private func render(error: Error) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: nil)
}
private func renderVerificationPending() {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.startVerificationButton.isHidden = true
self.verificationWaitingLabel.isHidden = false
}
private func renderCancelled(reason: MXTransactionCancelCode) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, title: "", message: VectorL10n.deviceVerificationCancelled, animated: true) {
self.viewModel.process(viewAction: .cancel)
}
}
private func renderCancelledByMe(reason: MXTransactionCancelCode) {
if reason.value != MXTransactionCancelCode.user().value {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, title: "", message: VectorL10n.deviceVerificationCancelledByMe(reason.humanReadable), animated: true) {
self.viewModel.process(viewAction: .cancel)
}
} else {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
}
}
private func buildInformationAttributedText(with userId: String) -> NSAttributedString {
let informationAttributedText: NSMutableAttributedString = NSMutableAttributedString()
let informationTextDefaultAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: self.theme.textPrimaryColor,
.font: Constants.informationTextDefaultFont]
let informationTextBoldAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: self.theme.textPrimaryColor,
.font: Constants.informationTextBoldFont]
let informationAttributedStringPart1 = NSAttributedString(string: "For extra security, verify ", attributes: informationTextDefaultAttributes)
let informationAttributedStringPart2 = NSAttributedString(string: userId, attributes: informationTextBoldAttributes)
let informationAttributedStringPart3 = NSAttributedString(string: " by checking a one-time code on both your devices.", attributes: informationTextDefaultAttributes)
informationAttributedText.append(informationAttributedStringPart1)
informationAttributedText.append(informationAttributedStringPart2)
informationAttributedText.append(informationAttributedStringPart3)
return informationAttributedText
}
private func buildVerificationWaitingText(with viewData: UserVerificationStartViewData) -> String {
let userName = viewData.userDisplayName ?? viewData.userId
return "Waiting for \(userName)"
}
// MARK: - Actions
@IBAction private func startVerificationButtonAction(_ sender: Any) {
self.viewModel.process(viewAction: .startVerification)
}
private func cancelButtonAction() {
self.viewModel.process(viewAction: .cancel)
}
}
// MARK: - UserVerificationStartViewModelViewDelegate
extension UserVerificationStartViewController: UserVerificationStartViewModelViewDelegate {
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didUpdateViewState viewSate: UserVerificationStartViewState) {
self.render(viewState: viewSate)
}
}
@@ -0,0 +1,200 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
enum UserVerificationStartViewModelError: Error {
case keyVerificationRequestExpired
}
struct UserVerificationStartViewData {
let userId: String
let userDisplayName: String?
let userAvatarURL: String?
}
final class UserVerificationStartViewModel: UserVerificationStartViewModelType {
// MARK: - Properties
// MARK: Private
private let session: MXSession
private let roomMember: MXRoomMember
private let verificationManager: MXDeviceVerificationManager
private var keyVerificationRequest: MXKeyVerificationRequest?
private var viewData: UserVerificationStartViewData {
return UserVerificationStartViewData(userId: self.roomMember.userId, userDisplayName: self.roomMember.displayname, userAvatarURL: self.roomMember.avatarUrl)
}
// MARK: Public
weak var viewDelegate: UserVerificationStartViewModelViewDelegate?
weak var coordinatorDelegate: UserVerificationStartViewModelCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession, roomMember: MXRoomMember) {
self.session = session
self.verificationManager = session.crypto.deviceVerificationManager
self.roomMember = roomMember
}
deinit {
}
// MARK: - Public
func process(viewAction: UserVerificationStartViewAction) {
switch viewAction {
case .loadData:
self.loadData()
case .startVerification:
self.startVerification()
case .cancel:
self.cancelKeyVerificationRequest()
self.coordinatorDelegate?.userVerificationStartViewModelDidCancel(self)
}
}
// MARK: - Private
private func loadData() {
self.update(viewState: .loaded(self.viewData))
}
private func startVerification() {
self.update(viewState: .verificationPending)
self.verificationManager.requestVerificationByDM(withUserId: self.roomMember.userId,
roomId: nil,
fallbackText: "",
methods: [MXKeyVerificationMethodSAS],
success: { [weak self] (keyVerificationRequest) in
guard let self = self else {
return
}
self.keyVerificationRequest = keyVerificationRequest
self.update(viewState: .loaded(self.viewData))
self.registerKeyVerificationDidChangeNotification(keyVerificationRequest: keyVerificationRequest)
self.registerTransactionDidStateChangeNotification()
}, failure: { [weak self] error in
self?.update(viewState: .error(error))
})
}
private func update(viewState: UserVerificationStartViewState) {
self.viewDelegate?.userVerificationStartViewModel(self, didUpdateViewState: viewState)
}
private func cancelKeyVerificationRequest() {
guard let keyVerificationRequest = self.keyVerificationRequest else {
return
}
keyVerificationRequest.cancel(with: MXTransactionCancelCode.user(), success: nil, failure: nil)
}
// MARK: - MXDeviceVerificationTransactionDidChange
private func registerTransactionDidStateChangeNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(transactionDidStateChange(notification:)), name: .MXDeviceVerificationTransactionDidChange, object: nil)
}
private func unregisterTransactionDidStateChangeNotification() {
NotificationCenter.default.removeObserver(self, name: .MXDeviceVerificationTransactionDidChange, object: nil)
}
@objc private func transactionDidStateChange(notification: Notification) {
guard let transaction = notification.object as? MXIncomingSASTransaction else {
return
}
guard let keyVerificationRequest = self.keyVerificationRequest,
let transactionDMEventId = transaction.dmEventId,
keyVerificationRequest.requestId == transactionDMEventId else {
return
}
switch transaction.state {
case MXSASTransactionStateShowSAS:
self.unregisterTransactionDidStateChangeNotification()
self.coordinatorDelegate?.userVerificationStartViewModel(self, didCompleteWithOutgoingTransaction: transaction)
case MXSASTransactionStateCancelled:
guard let reason = transaction.reasonCancelCode else {
return
}
self.unregisterTransactionDidStateChangeNotification()
self.update(viewState: .cancelled(reason))
case MXSASTransactionStateCancelledByMe:
guard let reason = transaction.reasonCancelCode else {
return
}
self.unregisterTransactionDidStateChangeNotification()
self.update(viewState: .cancelledByMe(reason))
default:
break
}
}
// MARK: - MXDeviceVerificationTransactionDidChange
private func registerKeyVerificationDidChangeNotification(keyVerificationRequest: MXKeyVerificationRequest) {
NotificationCenter.default.addObserver(self, selector: #selector(keyVerificationRequestDidChange(notification:)), name: .MXKeyVerificationRequestDidChange, object: keyVerificationRequest)
}
private func unregisterKeyVerificationDidChangeNotification() {
NotificationCenter.default.removeObserver(self, name: .MXKeyVerificationRequestDidChange, object: nil)
}
@objc private func keyVerificationRequestDidChange(notification: Notification) {
guard let keyVerificationRequest = notification.object as? MXKeyVerificationByDMRequest else {
return
}
guard let currentKeyVerificationRequest = self.keyVerificationRequest, keyVerificationRequest.requestId == currentKeyVerificationRequest.requestId else {
return
}
switch keyVerificationRequest.state {
case MXKeyVerificationRequestStateAccepted:
self.unregisterKeyVerificationDidChangeNotification()
case MXKeyVerificationRequestStateCancelled:
guard let reason = keyVerificationRequest.reasonCancelCode else {
return
}
self.unregisterKeyVerificationDidChangeNotification()
self.update(viewState: .cancelled(reason))
case MXKeyVerificationRequestStateCancelledByMe:
guard let reason = keyVerificationRequest.reasonCancelCode else {
return
}
self.unregisterKeyVerificationDidChangeNotification()
self.update(viewState: .cancelledByMe(reason))
case MXKeyVerificationRequestStateExpired:
self.unregisterKeyVerificationDidChangeNotification()
self.update(viewState: .error(UserVerificationStartViewModelError.keyVerificationRequestExpired))
default:
break
}
}
}
@@ -0,0 +1,41 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
protocol UserVerificationStartViewModelViewDelegate: class {
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didUpdateViewState viewSate: UserVerificationStartViewState)
}
protocol UserVerificationStartViewModelCoordinatorDelegate: class {
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didCompleteWithOutgoingTransaction transaction: MXSASTransaction)
func userVerificationStartViewModel(_ viewModel: UserVerificationStartViewModelType, didTransactionCancelled transaction: MXSASTransaction)
func userVerificationStartViewModelDidCancel(_ viewModel: UserVerificationStartViewModelType)
}
/// Protocol describing the view model used by `UserVerificationStartViewController`
protocol UserVerificationStartViewModelType {
var viewDelegate: UserVerificationStartViewModelViewDelegate? { get set }
var coordinatorDelegate: UserVerificationStartViewModelCoordinatorDelegate? { get set }
func process(viewAction: UserVerificationStartViewAction)
}
@@ -0,0 +1,29 @@
// File created from ScreenTemplate
// $ createScreen.sh Start UserVerificationStart
/*
Copyright 2020 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
/// UserVerificationStartViewController view state
enum UserVerificationStartViewState {
case loading
case loaded(UserVerificationStartViewData)
case verificationPending
case cancelled(MXTransactionCancelCode)
case cancelledByMe(MXTransactionCancelCode)
case error(Error)
}