Key Backup: Add a screen for recovering with the local private key

This commit is contained in:
manuroe
2020-04-08 14:09:55 +02:00
parent 0f9f2d2945
commit f7112a5698
15 changed files with 645 additions and 5 deletions
@@ -0,0 +1,69 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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 KeyBackupRecoverFromPrivateKeyCoordinator: KeyBackupRecoverFromPrivateKeyCoordinatorType {
// MARK: - Properties
// MARK: Private
private var keyBackupRecoverFromPrivateKeyViewModel: KeyBackupRecoverFromPrivateKeyViewModelType
private let keyBackupRecoverFromPrivateKeyViewController: KeyBackupRecoverFromPrivateKeyViewController
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
weak var delegate: KeyBackupRecoverFromPrivateKeyCoordinatorDelegate?
// MARK: - Setup
init(keyBackup: MXKeyBackup, keyBackupVersion: MXKeyBackupVersion) {
let keyBackupRecoverFromPrivateKeyViewModel = KeyBackupRecoverFromPrivateKeyViewModel(keyBackup: keyBackup, keyBackupVersion: keyBackupVersion)
let keyBackupRecoverFromPrivateKeyViewController = KeyBackupRecoverFromPrivateKeyViewController.instantiate(with: keyBackupRecoverFromPrivateKeyViewModel)
self.keyBackupRecoverFromPrivateKeyViewModel = keyBackupRecoverFromPrivateKeyViewModel
self.keyBackupRecoverFromPrivateKeyViewController = keyBackupRecoverFromPrivateKeyViewController
}
// MARK: - Public methods
func start() {
self.keyBackupRecoverFromPrivateKeyViewModel.coordinatorDelegate = self
}
func toPresentable() -> UIViewController {
return self.keyBackupRecoverFromPrivateKeyViewController
}
}
// MARK: - KeyBackupRecoverFromPrivateKeyViewModelCoordinatorDelegate
extension KeyBackupRecoverFromPrivateKeyCoordinator: KeyBackupRecoverFromPrivateKeyViewModelCoordinatorDelegate {
func keyBackupRecoverFromPrivateKeyViewModelDidRecover(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType) {
self.delegate?.keyBackupRecoverFromPrivateKeyCoordinatorDidRecover(self)
}
func keyBackupRecoverFromPrivateKeyViewModelDidCancel(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType) {
self.delegate?.keyBackupRecoverFromPrivateKeyCoordinatorDidCancel(self)
}
}
@@ -0,0 +1,29 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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 KeyBackupRecoverFromPrivateKeyCoordinatorDelegate: class {
func keyBackupRecoverFromPrivateKeyCoordinatorDidRecover(_ coordinator: KeyBackupRecoverFromPrivateKeyCoordinatorType)
func keyBackupRecoverFromPrivateKeyCoordinatorDidCancel(_ coordinator: KeyBackupRecoverFromPrivateKeyCoordinatorType)
}
/// `KeyBackupRecoverFromPrivateKeyCoordinatorType` is a protocol describing a Coordinator that handle key backup setup passphrase navigation flow.
protocol KeyBackupRecoverFromPrivateKeyCoordinatorType: Coordinator, Presentable {
var delegate: KeyBackupRecoverFromPrivateKeyCoordinatorDelegate? { get }
}
@@ -0,0 +1,25 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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
/// KeyBackupRecoverFromPrivateKeyViewController view actions exposed to view model
enum KeyBackupRecoverFromPrivateKeyViewAction {
case recover
case cancel
}
@@ -0,0 +1,97 @@
<?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="cb6-oF-e0m">
<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>
<!--Key Backup Recover Data Loading View Controller-->
<scene sceneID="JIv-4y-eqa">
<objects>
<viewController extendedLayoutIncludesOpaqueBars="YES" automaticallyAdjustsScrollViewInsets="NO" id="cb6-oF-e0m" customClass="KeyBackupRecoverFromPrivateKeyViewController" customModule="Riot" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="JOd-8G-rga">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="cUL-rS-rfi">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="a4s-VR-9rG">
<rect key="frame" x="0.0" y="0.0" width="375" height="239"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="AA6-5y-aKB">
<rect key="frame" x="0.0" y="0.0" width="375" height="239"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="key_backup_logo" translatesAutoresizingMaskIntoConstraints="NO" id="c3s-XT-wGy">
<rect key="frame" x="163.5" y="35" width="48" height="46"/>
<constraints>
<constraint firstAttribute="height" constant="46" id="G7f-5x-wqu"/>
<constraint firstAttribute="width" constant="48" id="raJ-A8-OeL"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Restoring backup…" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1dN-Ld-mvf">
<rect key="frame" x="20" y="111" width="335" height="18"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="c3s-XT-wGy" firstAttribute="top" secondItem="AA6-5y-aKB" secondAttribute="top" constant="35" id="2hk-29-LeR"/>
<constraint firstItem="1dN-Ld-mvf" firstAttribute="top" secondItem="c3s-XT-wGy" secondAttribute="bottom" constant="30" id="7oJ-n1-Vec"/>
<constraint firstAttribute="trailing" secondItem="1dN-Ld-mvf" secondAttribute="trailing" constant="20" id="RRJ-XS-DKi"/>
<constraint firstItem="1dN-Ld-mvf" firstAttribute="leading" secondItem="AA6-5y-aKB" secondAttribute="leading" constant="20" id="bgC-6o-Qd3"/>
<constraint firstItem="c3s-XT-wGy" firstAttribute="centerX" secondItem="AA6-5y-aKB" secondAttribute="centerX" id="hhx-MR-Ssb"/>
<constraint firstAttribute="width" priority="750" constant="500" id="qn9-3x-Vus"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="AA6-5y-aKB" secondAttribute="trailing" id="9zl-EA-onb"/>
<constraint firstAttribute="bottom" secondItem="AA6-5y-aKB" secondAttribute="bottom" id="QEe-pI-nde"/>
<constraint firstItem="AA6-5y-aKB" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="a4s-VR-9rG" secondAttribute="leading" id="dUg-r0-GLK"/>
<constraint firstAttribute="height" constant="239" id="f7G-GL-m2p"/>
<constraint firstItem="AA6-5y-aKB" firstAttribute="centerX" secondItem="a4s-VR-9rG" secondAttribute="centerX" id="jfM-Ga-xmC"/>
<constraint firstItem="AA6-5y-aKB" firstAttribute="top" secondItem="a4s-VR-9rG" secondAttribute="top" id="sUF-Z1-GNm"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="a4s-VR-9rG" secondAttribute="bottom" id="1y5-kr-9rM"/>
<constraint firstItem="a4s-VR-9rG" firstAttribute="leading" secondItem="cUL-rS-rfi" secondAttribute="leading" id="C8p-p4-H7y"/>
<constraint firstItem="a4s-VR-9rG" firstAttribute="top" secondItem="cUL-rS-rfi" secondAttribute="top" id="Ex0-c1-ak8"/>
<constraint firstAttribute="trailing" secondItem="a4s-VR-9rG" secondAttribute="trailing" id="d6Y-su-yMj"/>
<constraint firstItem="a4s-VR-9rG" firstAttribute="width" secondItem="cUL-rS-rfi" secondAttribute="width" id="uTy-iK-Qq9"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" red="0.94509803920000002" green="0.96078431369999995" blue="0.97254901959999995" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="cUL-rS-rfi" firstAttribute="leading" secondItem="Y3k-2C-Pek" secondAttribute="leading" id="9ZI-Gm-3DT"/>
<constraint firstItem="Y3k-2C-Pek" firstAttribute="trailing" secondItem="cUL-rS-rfi" secondAttribute="trailing" id="QwC-RO-L6M"/>
<constraint firstItem="Y3k-2C-Pek" firstAttribute="top" secondItem="cUL-rS-rfi" secondAttribute="top" id="ffm-HV-RhA"/>
<constraint firstAttribute="bottom" secondItem="cUL-rS-rfi" secondAttribute="bottom" id="rib-a1-j68"/>
</constraints>
<viewLayoutGuide key="safeArea" id="Y3k-2C-Pek"/>
</view>
<connections>
<outlet property="informationLabel" destination="1dN-Ld-mvf" id="RAQ-9H-hXQ"/>
<outlet property="shieldImageView" destination="c3s-XT-wGy" id="jVg-AC-PGB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dR3-YY-guh" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3772" y="-774"/>
</scene>
</scenes>
<resources>
<image name="key_backup_logo" width="48" height="46"/>
</resources>
</document>
@@ -0,0 +1,158 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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 KeyBackupRecoverFromPrivateKeyViewController: UIViewController {
// MARK: - Constants
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var shieldImageView: UIImageView!
@IBOutlet private weak var informationLabel: UILabel!
// MARK: Private
private var viewModel: KeyBackupRecoverFromPrivateKeyViewModelType!
private var theme: Theme!
private var errorPresenter: MXKErrorPresentation!
private var activityPresenter: ActivityIndicatorPresenter!
// MARK: - Setup
class func instantiate(with viewModel: KeyBackupRecoverFromPrivateKeyViewModelType) -> KeyBackupRecoverFromPrivateKeyViewController {
let viewController = StoryboardScene.KeyBackupRecoverFromPrivateKeyViewController.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 = VectorL10n.keyBackupRecoverTitle
self.setupViews()
self.activityPresenter = ActivityIndicatorPresenter()
self.errorPresenter = MXKErrorAlertPresentation()
self.registerThemeServiceDidChangeThemeNotification()
self.update(theme: self.theme)
self.viewModel.viewDelegate = self
self.viewModel.process(viewAction: .recover)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.theme.statusBarStyle
}
// MARK: - Private
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
let shieldImage = Asset.Images.keyBackupLogo.image.withRenderingMode(.alwaysTemplate)
self.shieldImageView.image = shieldImage
self.informationLabel.text = VectorL10n.keyBackupRecoverFromPrivateKeyInfo
}
private func update(theme: Theme) {
self.theme = theme
self.view.backgroundColor = theme.headerBackgroundColor
if let navigationBar = self.navigationController?.navigationBar {
theme.applyStyle(onNavigationBar: navigationBar)
}
self.shieldImageView.tintColor = theme.textPrimaryColor
self.informationLabel.textColor = theme.textPrimaryColor
}
private func render(viewState: KeyBackupRecoverFromPrivateKeyViewState) {
switch viewState {
case .loading:
self.renderLoading()
case .loaded:
self.renderLoaded()
case .error(let error):
self.render(error: error)
}
}
private func renderLoading() {
self.activityPresenter.presentActivityIndicator(on: self.view, animated: true)
}
private func renderLoaded() {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
}
private func render(error: Error) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: nil)
}
// MARK: - Actions
private func cancelButtonAction() {
self.viewModel.process(viewAction: .cancel)
}
}
// MARK: - KeyBackupRecoverFromPrivateKeyViewModelViewDelegate
extension KeyBackupRecoverFromPrivateKeyViewController: KeyBackupRecoverFromPrivateKeyViewModelViewDelegate {
func keyBackupRecoverFromPrivateKeyViewModel(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType, didUpdateViewState viewSate: KeyBackupRecoverFromPrivateKeyViewState) {
self.render(viewState: viewSate)
}
}
@@ -0,0 +1,89 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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
final class KeyBackupRecoverFromPrivateKeyViewModel: KeyBackupRecoverFromPrivateKeyViewModelType {
// MARK: - Properties
// MARK: Private
private let keyBackup: MXKeyBackup
private var currentHTTPOperation: MXHTTPOperation?
private let keyBackupVersion: MXKeyBackupVersion
// MARK: Public
weak var viewDelegate: KeyBackupRecoverFromPrivateKeyViewModelViewDelegate?
weak var coordinatorDelegate: KeyBackupRecoverFromPrivateKeyViewModelCoordinatorDelegate?
// MARK: - Setup
init(keyBackup: MXKeyBackup, keyBackupVersion: MXKeyBackupVersion) {
self.keyBackup = keyBackup
self.keyBackupVersion = keyBackupVersion
}
deinit {
}
// MARK: - Public
func process(viewAction: KeyBackupRecoverFromPrivateKeyViewAction) {
switch viewAction {
case .recover:
self.recoverWithPrivateKey()
case .cancel:
self.coordinatorDelegate?.keyBackupRecoverFromPrivateKeyViewModelDidCancel(self)
}
}
// MARK: - Private
private func recoverWithPrivateKey() {
self.update(viewState: .loading)
self.currentHTTPOperation = keyBackup.restoreUsingPrivateKey(inCryptoStore: keyBackupVersion, room: nil, session: nil, success: { [weak self] (_, _) in
guard let sself = self else {
return
}
// Trust on decrypt
sself.currentHTTPOperation = sself.keyBackup.trust(sself.keyBackupVersion, trust: true, success: { [weak sself] () in
guard let ssself = sself else {
return
}
ssself.update(viewState: .loaded)
ssself.coordinatorDelegate?.keyBackupRecoverFromPrivateKeyViewModelDidRecover(ssself)
}, failure: { [weak sself] error in
sself?.update(viewState: .error(error))
})
}, failure: { [weak self] error in
self?.update(viewState: .error(error))
})
}
private func update(viewState: KeyBackupRecoverFromPrivateKeyViewState) {
self.viewDelegate?.keyBackupRecoverFromPrivateKeyViewModel(self, didUpdateViewState: viewState)
}
}
@@ -0,0 +1,37 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
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 KeyBackupRecoverFromPrivateKeyViewModelViewDelegate: class {
func keyBackupRecoverFromPrivateKeyViewModel(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType, didUpdateViewState viewSate: KeyBackupRecoverFromPrivateKeyViewState)
}
protocol KeyBackupRecoverFromPrivateKeyViewModelCoordinatorDelegate: class {
func keyBackupRecoverFromPrivateKeyViewModelDidRecover(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType)
func keyBackupRecoverFromPrivateKeyViewModelDidCancel(_ viewModel: KeyBackupRecoverFromPrivateKeyViewModelType)
}
/// Protocol describing the view model used by `KeyBackupRecoverFromPrivateKeyViewController`
protocol KeyBackupRecoverFromPrivateKeyViewModelType {
var viewDelegate: KeyBackupRecoverFromPrivateKeyViewModelViewDelegate? { get set }
var coordinatorDelegate: KeyBackupRecoverFromPrivateKeyViewModelCoordinatorDelegate? { get set }
func process(viewAction: KeyBackupRecoverFromPrivateKeyViewAction)
}
@@ -0,0 +1,26 @@
// File created from ScreenTemplate
// $ createScreen.sh .KeyBackup/Recover/PrivateKey KeyBackupRecoverFromPrivateKey
/*
Copyright 2019 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
/// KeyBackupRecoverFromPrivateKeyViewController view state
enum KeyBackupRecoverFromPrivateKeyViewState {
case loading
case loaded
case error(Error)
}