Device Verification: Verify screen: bind transaction.confirmSASMatch()

This commit is contained in:
manuroe
2019-04-11 17:59:12 +02:00
parent dc9bcbeb82
commit 8e09880b95
6 changed files with 99 additions and 11 deletions
@@ -61,8 +61,8 @@ final class DeviceVerificationCoordinator: DeviceVerificationCoordinatorType {
/// - transaction: an existing device verification transaction
convenience init(session: MXSession, transaction: MXSASTransaction) {
self.init(session: session,
otherUserId: transaction.otherUser,
otherDeviceId: transaction.otherDevice)
otherUserId: transaction.otherUserId,
otherDeviceId: transaction.otherDeviceId)
self.transaction = transaction
}
@@ -84,7 +84,12 @@ final class DeviceVerificationCoordinator: DeviceVerificationCoordinatorType {
return // TODO
}
let rootCoordinator = sself.createDeviceVerificationStartCoordinator(otherUser: otherUser, otherDevice: otherDevice)
//let rootCoordinator = sself.createDeviceVerificationStartCoordinator(otherUser: otherUser, otherDevice: otherDevice)
// TODO: To remove. Only for dev
let rootCoordinator = DeviceVerificationVerifyCoordinator(session: sself.session)
rootCoordinator.delegate = self
rootCoordinator.start()
sself.add(childCoordinator: rootCoordinator)
@@ -68,7 +68,7 @@ final class DeviceVerificationStartViewModel: DeviceVerificationStartViewModelTy
private func beginVerifying() {
self.update(viewState: .loading)
self.verificationManager.beginKeyVerification(withUserId: self.otherUser.userId, andDeviceId: self.otherDevice.deviceId, method: kMXKeyVerificationMethodSAS, complete: { [weak self] (transaction) in
self.verificationManager.beginKeyVerification(withUserId: self.otherUser.userId, andDeviceId: self.otherDevice.deviceId, method: kMXKeyVerificationMethodSAS, success: { [weak self] (transaction) in
guard let sself = self else {
return
@@ -81,6 +81,8 @@ final class DeviceVerificationStartViewModel: DeviceVerificationStartViewModelTy
sself.registerTransactionDidStateChangeNotification(transaction: sasTransaction)
sself.update(viewState: .loaded)
}, failure: {[weak self] error in
self?.update(viewState: .error(error))
})
}
@@ -109,9 +111,9 @@ final class DeviceVerificationStartViewModel: DeviceVerificationStartViewModelTy
}
switch transaction.state {
case MXOutgoingSASTransactionStateShowSAS:
case MXSASTransactionStateShowSAS:
self.coordinatorDelegate?.deviceVerificationStartViewModel(self, didCompleteWithOutgoingTransaction: transaction)
case MXOutgoingSASTransactionStateCancelled:
case MXSASTransactionStateCancelled:
self.coordinatorDelegate?.deviceVerificationStartViewModel(self, didTransactionCancelled: transaction)
default:
break
@@ -37,6 +37,16 @@ final class DeviceVerificationVerifyCoordinator: DeviceVerificationVerifyCoordin
weak var delegate: DeviceVerificationVerifyCoordinatorDelegate?
// MARK: - Setup
// TODO: To remove. Only for dev
init(session: MXSession) {
self.session = session
let deviceVerificationVerifyViewModel = DeviceVerificationVerifyViewModel(session: self.session)
let deviceVerificationVerifyViewController = DeviceVerificationVerifyViewController.instantiate(with: deviceVerificationVerifyViewModel)
self.deviceVerificationVerifyViewModel = deviceVerificationVerifyViewModel
self.deviceVerificationVerifyViewController = deviceVerificationVerifyViewController
}
init(session: MXSession, transaction: MXSASTransaction) {
self.session = session
@@ -152,7 +152,11 @@ final class DeviceVerificationVerifyViewController: UIViewController {
case .loading:
self.renderLoading()
case .loaded:
self.renderLoaded()
self.renderVerified()
case .cancelled(let reason):
self.renderCancelled(reason: reason)
case .cancelledByMe(let reason):
self.renderCancelledByMe(reason: reason)
case .error(let error):
self.render(error: error)
}
@@ -162,13 +166,22 @@ final class DeviceVerificationVerifyViewController: UIViewController {
self.activityPresenter.presentActivityIndicator(on: self.view, animated: true)
}
private func renderLoaded() {
private func renderVerified() {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
// TODO
self.continueButtonBackgroundView.isHidden = true
self.waitingPartnerLabel.isHidden = false
}
private func renderCancelled(reason: MXTransactionCancelCode) {
// TODO
}
private func renderCancelledByMe(reason: MXTransactionCancelCode) {
// TODO
}
private func render(error: Error) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: nil)
@@ -39,6 +39,23 @@ final class DeviceVerificationVerifyViewModel: DeviceVerificationVerifyViewModel
self.session = session
self.transaction = transaction
self.emojis = self.transaction.sasEmoji
self.registerTransactionDidStateChangeNotification(transaction: transaction)
}
// TODO: To remove. Only for dev
init(session: MXSession) {
self.session = session
self.transaction = MXSASTransaction()
self.emojis = [
MXEmojiRepresentation(emoji: "🙂", andName: "BIGsMileYYELLOW"),
MXEmojiRepresentation(emoji: "🤖", andName: "Headphones"),
MXEmojiRepresentation(emoji: "🎩", andName: "Butterfly"),
MXEmojiRepresentation(emoji: "👓", andName: "Strawberry"),
MXEmojiRepresentation(emoji: "🔧", andName: "Light bulb"),
MXEmojiRepresentation(emoji: "🎅", andName: "Headphones"),
MXEmojiRepresentation(emoji: "👍", andName: "Thumbs up")
]
}
deinit {
@@ -62,10 +79,49 @@ final class DeviceVerificationVerifyViewModel: DeviceVerificationVerifyViewModel
private func confirm() {
self.update(viewState: .loading)
// TODO
self.transaction.confirmSASMatch()
}
private func update(viewState: DeviceVerificationVerifyViewState) {
self.viewDelegate?.deviceVerificationVerifyViewModel(self, didUpdateViewState: viewState)
}
// MARK: - MXDeviceVerificationTransactionDidChange
private func registerTransactionDidStateChangeNotification(transaction: MXSASTransaction) {
NotificationCenter.default.addObserver(self, selector: #selector(transactionDidStateChange(notification:)), name: NSNotification.Name.MXDeviceVerificationTransactionDidChange, object: transaction)
}
@objc private func transactionDidStateChange(notification: Notification) {
guard let transaction = notification.object as? MXOutgoingSASTransaction else {
return
}
switch transaction.state {
case MXSASTransactionStateVerified:
self.update(viewState: .loaded)
case MXSASTransactionStateCancelled:
guard let reason = transaction.reasonCancelCode else {
return
}
self.update(viewState: .cancelled(reason))
case MXSASTransactionStateError:
guard let error = transaction.error else {
return
}
self.update(viewState: .error(error))
case MXSASTransactionStateCancelled:
guard let reason = transaction.reasonCancelCode else {
return
}
self.update(viewState: .cancelled(reason))
case MXSASTransactionStateCancelledByMe:
guard let reason = transaction.reasonCancelCode else {
return
}
self.update(viewState: .cancelledByMe(reason))
default:
break
}
}
}
@@ -21,6 +21,8 @@ import Foundation
/// DeviceVerificationVerifyViewController view state
enum DeviceVerificationVerifyViewState {
case loading
case loaded
case loaded // verified
case cancelled(MXTransactionCancelCode)
case cancelledByMe(MXTransactionCancelCode)
case error(Error)
}