Implement biometrics module

This commit is contained in:
ismailgulek
2020-07-24 17:53:23 +03:00
parent c9c434b3aa
commit c37d4291ec
16 changed files with 830 additions and 23 deletions
@@ -38,10 +38,10 @@ final class EnterPinCodeCoordinator: EnterPinCodeCoordinatorType {
// MARK: - Setup
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode) {
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode, pinCodePreferences: PinCodePreferences = .shared) {
self.session = session
let enterPinCodeViewModel = EnterPinCodeViewModel(session: self.session, viewMode: viewMode, pinCodePreferences: .shared)
let enterPinCodeViewModel = EnterPinCodeViewModel(session: self.session, viewMode: viewMode, pinCodePreferences: pinCodePreferences)
let enterPinCodeViewController = EnterPinCodeViewController.instantiate(with: enterPinCodeViewModel)
self.enterPinCodeViewModel = enterPinCodeViewModel
self.enterPinCodeViewController = enterPinCodeViewController
@@ -163,7 +163,7 @@ final class EnterPinCodeViewController: UIViewController {
self.renderConfirmPin()
case .pinsDontMatch:
self.renderPinsDontMatch()
case .unlockByPin:
case .unlock:
self.renderUnlockByPin()
case .wrongPin:
self.renderWrongPin()
@@ -109,7 +109,7 @@ final class EnterPinCodeViewModel: EnterPinCodeViewModelType {
update(viewState: .pinsDontMatch)
}
}
case .unlockByPin, .confirmPinToDeactivate:
case .unlock, .confirmPinToDeactivate:
// unlocking
if currentPin != pinCodePreferences.pin {
// no match
@@ -131,6 +131,8 @@ final class EnterPinCodeViewModel: EnterPinCodeViewModelType {
self.coordinatorDelegate?.enterPinCodeViewModelDidComplete(self)
}
}
default:
break
}
return
}
@@ -141,10 +143,12 @@ final class EnterPinCodeViewModel: EnterPinCodeViewModelType {
switch viewMode {
case .setPin:
update(viewState: .choosePin)
case .unlockByPin:
update(viewState: .unlockByPin)
case .unlock:
update(viewState: .unlock)
case .confirmPinToDeactivate:
update(viewState: .confirmPinToDisable)
default:
break
}
}
@@ -23,7 +23,7 @@ enum EnterPinCodeViewState {
case choosePin // creating pin for the first time, enter for first
case confirmPin // creating pin for the first time, confirm
case pinsDontMatch // pins don't match
case unlockByPin // after pin has been set, enter pin to unlock
case unlock // after pin has been set, enter pin to unlock
case wrongPin // after pin has been set, pin entered wrongly
case wrongPinTooManyTimes // after pin has been set, pin entered wrongly too many times
case forgotPin // after pin has been set, user tapped forgot pin
+91 -13
View File
@@ -28,6 +28,7 @@ final class SetPinCoordinator: SetPinCoordinatorType {
private let navigationRouter: NavigationRouterType
private let session: MXSession?
private var viewMode: SetPinCoordinatorViewMode
private let pinCodePreferences: PinCodePreferences
// MARK: Public
@@ -38,23 +39,34 @@ final class SetPinCoordinator: SetPinCoordinatorType {
// MARK: - Setup
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode) {
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode, pinCodePreferences: PinCodePreferences) {
self.navigationRouter = NavigationRouter(navigationController: RiotNavigationController())
self.session = session
self.viewMode = viewMode
}
self.pinCodePreferences = pinCodePreferences
}
private func getRootCoordinator() -> Coordinator & Presentable {
switch viewMode {
case .unlock:
if pinCodePreferences.isBiometricsSet {
return createSetupBiometricsCoordinator()
} else {
return createEnterPinCodeCoordinator()
}
case .setPin, .confirmPinToDeactivate:
return createEnterPinCodeCoordinator()
case .setupBiometricsAfterLogin, .setupBiometricsFromSettings, .confirmBiometricsToDeactivate:
return createSetupBiometricsCoordinator()
}
}
// MARK: - Public methods
func start() {
let rootCoordinator = self.createEnterPinCodeCoordinator()
rootCoordinator.start()
self.add(childCoordinator: rootCoordinator)
self.navigationRouter.setRootModule(rootCoordinator)
let rootCoordinator = getRootCoordinator()
setRootCoordinator(rootCoordinator)
}
func toPresentable() -> UIViewController {
@@ -62,6 +74,14 @@ final class SetPinCoordinator: SetPinCoordinatorType {
}
// MARK: - Private methods
private func setRootCoordinator(_ coordinator: Coordinator & Presentable) {
coordinator.start()
self.add(childCoordinator: coordinator)
self.navigationRouter.setRootModule(coordinator)
}
private func createEnterPinCodeCoordinator() -> EnterPinCodeCoordinator {
let coordinator = EnterPinCodeCoordinator(session: self.session, viewMode: self.viewMode)
@@ -69,12 +89,26 @@ final class SetPinCoordinator: SetPinCoordinatorType {
return coordinator
}
private func createSetupBiometricsCoordinator() -> SetupBiometricsCoordinator {
let coordinator = SetupBiometricsCoordinator(session: self.session, viewMode: self.viewMode)
coordinator.delegate = self
return coordinator
}
private func storePin(_ pin: String) {
PinCodePreferences.shared.pin = pin
pinCodePreferences.pin = pin
}
private func removePin() {
PinCodePreferences.shared.reset()
pinCodePreferences.pin = nil
}
private func setupBiometrics() {
pinCodePreferences.biometricsEnabled = true
}
private func removeBiometrics() {
pinCodePreferences.biometricsEnabled = nil
}
}
@@ -94,10 +128,54 @@ extension SetPinCoordinator: EnterPinCodeCoordinatorDelegate {
func enterPinCodeCoordinator(_ coordinator: EnterPinCodeCoordinatorType, didCompleteWithPin pin: String) {
storePin(pin)
self.delegate?.setPinCoordinatorDidComplete(self)
if pinCodePreferences.forcePinProtection && pinCodePreferences.isBiometricsAvailable {
viewMode = .setupBiometricsAfterLogin
setRootCoordinator(createSetupBiometricsCoordinator())
} else {
self.delegate?.setPinCoordinatorDidComplete(self)
}
}
func enterPinCodeCoordinatorDidCancel(_ coordinator: EnterPinCodeCoordinatorType) {
self.delegate?.setPinCoordinatorDidCancel(self)
}
}
extension SetPinCoordinator: SetupBiometricsCoordinatorDelegate {
func setupBiometricsCoordinatorDidComplete(_ coordinator: SetupBiometricsCoordinatorType) {
switch viewMode {
case .setupBiometricsAfterLogin, .setupBiometricsFromSettings:
setupBiometrics()
case .confirmBiometricsToDeactivate:
removeBiometrics()
default:
break
}
self.delegate?.setPinCoordinatorDidComplete(self)
}
func setupBiometricsCoordinatorDidCompleteWithReset(_ coordinator: SetupBiometricsCoordinatorType) {
self.delegate?.setPinCoordinatorDidCompleteWithReset(self)
}
func setupBiometricsCoordinatorDidCancel(_ coordinator: SetupBiometricsCoordinatorType) {
if viewMode == .unlock {
// if trying to unlock
if pinCodePreferences.isPinSet {
// and user also has set a pin, so fallback to it
setRootCoordinator(createEnterPinCodeCoordinator())
} else {
// no pin set, cascade cancellation
self.delegate?.setPinCoordinatorDidCancel(self)
}
return
}
if viewMode == .setupBiometricsAfterLogin {
self.delegate?.setPinCoordinatorDidComplete(self)
} else {
self.delegate?.setPinCoordinatorDidCancel(self)
}
}
}
@@ -20,8 +20,11 @@ import Foundation
@objc enum SetPinCoordinatorViewMode: Int {
case setPin
case unlockByPin
case unlock
case confirmPinToDeactivate
case setupBiometricsAfterLogin
case setupBiometricsFromSettings
case confirmBiometricsToDeactivate
}
@objc protocol SetPinCoordinatorBridgePresenterDelegate {
@@ -65,7 +68,7 @@ final class SetPinCoordinatorBridgePresenter: NSObject {
// }
func present(from viewController: UIViewController, animated: Bool) {
let setPinCoordinator = SetPinCoordinator(session: self.session, viewMode: self.viewMode)
let setPinCoordinator = SetPinCoordinator(session: self.session, viewMode: self.viewMode, pinCodePreferences: .shared)
setPinCoordinator.delegate = self
viewController.present(setPinCoordinator.toPresentable(), animated: animated, completion: nil)
setPinCoordinator.start()
@@ -74,7 +77,7 @@ final class SetPinCoordinatorBridgePresenter: NSObject {
}
func present(in window: UIWindow) {
let setPinCoordinator = SetPinCoordinator(session: self.session, viewMode: self.viewMode)
let setPinCoordinator = SetPinCoordinator(session: self.session, viewMode: self.viewMode, pinCodePreferences: .shared)
setPinCoordinator.delegate = self
guard let view = setPinCoordinator.toPresentable().view else { return }
window.addSubview(view)
@@ -0,0 +1,77 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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 SetupBiometricsCoordinator: SetupBiometricsCoordinatorType {
// MARK: - Properties
// MARK: Private
private let session: MXSession?
private var setupBiometricsViewModel: SetupBiometricsViewModelType
private let setupBiometricsViewController: SetupBiometricsViewController
private let viewMode: SetPinCoordinatorViewMode
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
weak var delegate: SetupBiometricsCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode, pinCodePreferences: PinCodePreferences = .shared) {
self.session = session
self.viewMode = viewMode
let setupBiometricsViewModel = SetupBiometricsViewModel(session: self.session, viewMode: viewMode, pinCodePreferences: pinCodePreferences)
let setupBiometricsViewController = SetupBiometricsViewController.instantiate(with: setupBiometricsViewModel)
self.setupBiometricsViewModel = setupBiometricsViewModel
self.setupBiometricsViewController = setupBiometricsViewController
}
// MARK: - Public methods
func start() {
self.setupBiometricsViewModel.coordinatorDelegate = self
}
func toPresentable() -> UIViewController {
return self.setupBiometricsViewController
}
}
// MARK: - SetupBiometricsViewModelCoordinatorDelegate
extension SetupBiometricsCoordinator: SetupBiometricsViewModelCoordinatorDelegate {
func setupBiometricsViewModelDidComplete(_ viewModel: SetupBiometricsViewModelType) {
self.delegate?.setupBiometricsCoordinatorDidComplete(self)
}
func setupBiometricsViewModelDidCompleteWithReset(_ viewModel: SetupBiometricsViewModelType) {
self.delegate?.setupBiometricsCoordinatorDidCompleteWithReset(self)
}
func setupBiometricsViewModelDidCancel(_ viewModel: SetupBiometricsViewModelType) {
self.delegate?.setupBiometricsCoordinatorDidCancel(self)
}
}
@@ -0,0 +1,30 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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 SetupBiometricsCoordinatorDelegate: class {
func setupBiometricsCoordinatorDidComplete(_ coordinator: SetupBiometricsCoordinatorType)
func setupBiometricsCoordinatorDidCompleteWithReset(_ coordinator: SetupBiometricsCoordinatorType)
func setupBiometricsCoordinatorDidCancel(_ coordinator: SetupBiometricsCoordinatorType)
}
/// `SetupBiometricsCoordinatorType` is a protocol describing a Coordinator that handle key backup setup passphrase navigation flow.
protocol SetupBiometricsCoordinatorType: Coordinator, Presentable {
var delegate: SetupBiometricsCoordinatorDelegate? { get }
}
@@ -0,0 +1,28 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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
/// SetupBiometricsViewController view actions exposed to view model
enum SetupBiometricsViewAction {
case loadData
case enableDisableTapped
case skipOrCancel
case unlock
case cantUnlockedAlertResetAction
}
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="V8j-Lb-PgC">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Setup Biometrics View Controller-->
<scene sceneID="mt5-wz-YKA">
<objects>
<viewController extendedLayoutIncludesOpaqueBars="YES" automaticallyAdjustsScrollViewInsets="NO" id="V8j-Lb-PgC" customClass="SetupBiometricsViewController" 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="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView hidden="YES" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="callkit_icon" translatesAutoresizingMaskIntoConstraints="NO" id="m4e-3p-cc0">
<rect key="frame" x="187" y="114" width="40" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="RH5-eK-akg"/>
<constraint firstAttribute="width" constant="40" id="eZM-v6-0MP"/>
</constraints>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" alignment="center" spacing="24" translatesAutoresizingMaskIntoConstraints="NO" id="ua6-xA-2ZZ">
<rect key="frame" x="0.0" y="83" width="414" height="771"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Npd-Cl-ROl">
<rect key="frame" x="87" y="0.0" width="240" height="100"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Unlock with Face ID" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5d9-9G-mVC">
<rect key="frame" x="21.5" y="16" width="197" height="26.5"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="22"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Save yourself time" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Hwi-Me-q2e">
<rect key="frame" x="50" y="71.5" width="140.5" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="Hwi-Me-q2e" secondAttribute="bottom" constant="8" id="5t1-u8-gqI"/>
<constraint firstItem="5d9-9G-mVC" firstAttribute="centerX" secondItem="Npd-Cl-ROl" secondAttribute="centerX" id="mVK-Yb-6bE"/>
<constraint firstAttribute="height" constant="100" id="t0f-ZB-PN8"/>
<constraint firstItem="5d9-9G-mVC" firstAttribute="top" secondItem="Npd-Cl-ROl" secondAttribute="top" constant="16" id="xbZ-CM-RwY"/>
<constraint firstItem="Hwi-Me-q2e" firstAttribute="centerX" secondItem="Npd-Cl-ROl" secondAttribute="centerX" id="yV3-O4-JOm"/>
</constraints>
</view>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="faceid_icon" translatesAutoresizingMaskIntoConstraints="NO" id="BCZ-BS-w0E">
<rect key="frame" x="171" y="374.5" width="72" height="72"/>
<constraints>
<constraint firstAttribute="width" constant="72" id="enO-Nr-Kis"/>
<constraint firstAttribute="height" constant="72" id="hqB-9S-FMJ"/>
</constraints>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5JR-LV-ZwX">
<rect key="frame" x="16" y="721" width="382" height="50"/>
<color key="backgroundColor" systemColor="systemGreenColor" red="0.20392156859999999" green="0.78039215689999997" blue="0.34901960780000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="d9h-bD-uYQ"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="17"/>
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<state key="normal" title="Enable Face ID"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<integer key="value" value="10"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="enableButtonAction:" destination="V8j-Lb-PgC" eventType="touchUpInside" id="BQi-xW-jkZ"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="5JR-LV-ZwX" firstAttribute="leading" secondItem="ua6-xA-2ZZ" secondAttribute="leading" constant="16" id="oS7-Jp-f31"/>
<constraint firstAttribute="trailing" secondItem="5JR-LV-ZwX" secondAttribute="trailing" constant="16" id="sO9-Cj-gAf"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" red="0.94509803921568625" green="0.96078431372549022" blue="0.97254901960784312" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="bFg-jh-JZB" firstAttribute="bottom" secondItem="ua6-xA-2ZZ" secondAttribute="bottom" constant="8" id="0Wf-Mp-exA"/>
<constraint firstItem="m4e-3p-cc0" firstAttribute="top" secondItem="bFg-jh-JZB" secondAttribute="top" constant="70" id="8fB-R8-Nae"/>
<constraint firstItem="ua6-xA-2ZZ" firstAttribute="leading" secondItem="bFg-jh-JZB" secondAttribute="leading" id="BhU-bi-ycC"/>
<constraint firstItem="bFg-jh-JZB" firstAttribute="trailing" secondItem="ua6-xA-2ZZ" secondAttribute="trailing" id="Jsz-dV-9qw"/>
<constraint firstItem="m4e-3p-cc0" firstAttribute="centerX" secondItem="EL9-GA-lwo" secondAttribute="centerX" id="Sqv-do-u5P"/>
<constraint firstItem="ua6-xA-2ZZ" firstAttribute="top" secondItem="bFg-jh-JZB" secondAttribute="top" constant="39" id="zV3-48-Oov"/>
</constraints>
<viewLayoutGuide key="safeArea" id="bFg-jh-JZB"/>
</view>
<connections>
<outlet property="biometricsIconImageView" destination="BCZ-BS-w0E" id="NUF-eV-lgU"/>
<outlet property="enableButton" destination="5JR-LV-ZwX" id="yjp-uu-gX8"/>
<outlet property="itemsStackView" destination="ua6-xA-2ZZ" id="S9f-ag-Trx"/>
<outlet property="logoImageView" destination="m4e-3p-cc0" id="4kL-Mi-frU"/>
<outlet property="subtitleLabel" destination="Hwi-Me-q2e" id="mvA-tq-Yp4"/>
<outlet property="titleLabel" destination="5d9-9G-mVC" id="fiW-V0-wVA"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="zK0-v6-7Wt" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3198.5507246376815" y="-647.54464285714278"/>
</scene>
</scenes>
<resources>
<image name="callkit_icon" width="40" height="40"/>
<image name="faceid_icon" width="72" height="72"/>
</resources>
</document>
@@ -0,0 +1,224 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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 SetupBiometricsViewController: UIViewController {
// MARK: - Constants
private enum Constants {
static let aConstant: Int = 666
}
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var logoImageView: UIImageView!
@IBOutlet private weak var itemsStackView: UIStackView!
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var subtitleLabel: UILabel!
@IBOutlet private weak var biometricsIconImageView: UIImageView!
@IBOutlet private weak var enableButton: UIButton!
// MARK: Private
private var viewModel: SetupBiometricsViewModelType!
private var theme: Theme!
private var errorPresenter: MXKErrorPresentation!
private var activityPresenter: ActivityIndicatorPresenter!
// MARK: - Setup
class func instantiate(with viewModel: SetupBiometricsViewModelType) -> SetupBiometricsViewController {
let viewController = StoryboardScene.SetupBiometricsViewController.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.setupViews()
self.activityPresenter = ActivityIndicatorPresenter()
self.errorPresenter = MXKErrorAlertPresentation()
self.registerThemeServiceDidChangeThemeNotification()
self.update(theme: self.theme)
self.viewModel.viewDelegate = self
if #available(iOS 13.0, *) {
modalPresentationStyle = .fullScreen
isModalInPresentation = true
}
self.viewModel.process(viewAction: .loadData)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.theme.statusBarStyle
}
// 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.titleLabel.textColor = theme.textPrimaryColor
self.subtitleLabel.textColor = theme.textSecondaryColor
self.enableButton.backgroundColor = theme.tintColor
self.enableButton.tintColor = theme.baseTextPrimaryColor
self.enableButton.setTitleColor(theme.baseTextPrimaryColor, for: .normal)
}
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() {
self.title = ""
}
private func showSkipButton() {
self.navigationItem.rightBarButtonItem = MXKBarButtonItem(title: VectorL10n.skip, style: .plain) { [weak self] in
self?.skipCancelButtonAction()
}
}
private func showCancelButton() {
self.navigationItem.rightBarButtonItem = MXKBarButtonItem(title: VectorL10n.cancel, style: .plain) { [weak self] in
self?.skipCancelButtonAction()
}
}
private func hideSkipCancelButton() {
self.navigationItem.rightBarButtonItem = nil
}
private func render(viewState: SetupBiometricsViewState) {
switch viewState {
case .setupAfterLogin:
renderSetupAfterLogin()
case .setupFromSettings:
renderSetupFromSettings()
case .unlock:
renderUnlock()
case .confirmToDisable:
renderConfirmToDisable()
case .cantUnlocked:
renderCantUnlocked()
}
}
private func renderSetupAfterLogin() {
showSkipButton()
guard let biometricsName = viewModel.localizedBiometricsName() else { return }
self.titleLabel.text = VectorL10n.biometricsSetupTitleX(biometricsName)
self.subtitleLabel.text = VectorL10n.biometricsSetupSubtitle
self.biometricsIconImageView.image = viewModel.biometricsIcon()
self.enableButton.setTitle(VectorL10n.biometricsSetupEnableButtonTitleX(biometricsName), for: .normal)
}
private func renderSetupFromSettings() {
showCancelButton()
guard let biometricsName = viewModel.localizedBiometricsName() else { return }
self.titleLabel.text = VectorL10n.biometricsSetupTitleX(biometricsName)
self.subtitleLabel.text = VectorL10n.biometricsSetupSubtitle
self.biometricsIconImageView.image = viewModel.biometricsIcon()
self.enableButton.setTitle(VectorL10n.biometricsSetupEnableButtonTitleX(biometricsName), for: .normal)
}
private func renderUnlock() {
hideSkipCancelButton()
self.logoImageView.isHidden = false
// hide all items but the logo
self.itemsStackView.isHidden = true
self.viewModel.process(viewAction: .unlock)
}
private func renderConfirmToDisable() {
showCancelButton()
guard let biometricsName = viewModel.localizedBiometricsName() else { return }
self.titleLabel.text = VectorL10n.biometricsDesetupTitleX(biometricsName)
self.subtitleLabel.text = VectorL10n.biometricsDesetupSubtitle
self.biometricsIconImageView.image = viewModel.biometricsIcon()
self.enableButton.setTitle(VectorL10n.biometricsDesetupDisableButtonTitleX(biometricsName), for: .normal)
}
private func renderCantUnlocked() {
guard let biometricsName = viewModel.localizedBiometricsName() else { return }
let controller = UIAlertController(title: VectorL10n.biometricsCantUnlockedAlertTitle,
message: VectorL10n.biometricsCantUnlockedAlertMessageX(biometricsName, biometricsName),
preferredStyle: .alert)
let resetAction = UIAlertAction(title: VectorL10n.biometricsCantUnlockedAlertMessageLogin, style: .default) { (_) in
self.viewModel.process(viewAction: .cantUnlockedAlertResetAction)
}
let retryAction = UIAlertAction(title: VectorL10n.biometricsCantUnlockedAlertMessageRetry, style: .cancel) { (_) in
self.viewModel.process(viewAction: .unlock)
}
controller.addAction(resetAction)
controller.addAction(retryAction)
self.present(controller, animated: true, completion: nil)
}
// MARK: - Actions
@IBAction private func enableButtonAction(_ sender: Any) {
self.viewModel.process(viewAction: .enableDisableTapped)
}
private func skipCancelButtonAction() {
self.viewModel.process(viewAction: .skipOrCancel)
}
}
// MARK: - SetupBiometricsViewModelViewDelegate
extension SetupBiometricsViewController: SetupBiometricsViewModelViewDelegate {
func setupBiometricsViewModel(_ viewModel: SetupBiometricsViewModelType, didUpdateViewState viewSate: SetupBiometricsViewState) {
self.render(viewState: viewSate)
}
}
@@ -0,0 +1,135 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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 LocalAuthentication
final class SetupBiometricsViewModel: SetupBiometricsViewModelType {
// MARK: - Properties
// MARK: Private
private let session: MXSession?
private let viewMode: SetPinCoordinatorViewMode
private let pinCodePreferences: PinCodePreferences
// MARK: Public
weak var viewDelegate: SetupBiometricsViewModelViewDelegate?
weak var coordinatorDelegate: SetupBiometricsViewModelCoordinatorDelegate?
// MARK: - Setup
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode, pinCodePreferences: PinCodePreferences) {
self.session = session
self.viewMode = viewMode
self.pinCodePreferences = pinCodePreferences
}
deinit {
}
// MARK: - Public
func localizedBiometricsName() -> String? {
return pinCodePreferences.localizedBiometricsName()
}
func biometricsIcon() -> UIImage? {
return pinCodePreferences.biometricsIcon()
}
func process(viewAction: SetupBiometricsViewAction) {
switch viewAction {
case .loadData:
loadData()
case .enableDisableTapped:
enableDisableBiometrics()
case .skipOrCancel:
coordinatorDelegate?.setupBiometricsViewModelDidCancel(self)
case .unlock:
unlockWithBiometrics()
case .cantUnlockedAlertResetAction:
coordinatorDelegate?.setupBiometricsViewModelDidCompleteWithReset(self)
}
}
// MARK: - Private
private func enableDisableBiometrics() {
LAContext().evaluatePolicy(.deviceOwnerAuthentication, localizedReason: VectorL10n.biometricsUsageReason) { (success, error) in
if success {
// complete after a little delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
}
}
}
}
private func unlockWithBiometrics() {
LAContext().evaluatePolicy(.deviceOwnerAuthentication, localizedReason: VectorL10n.biometricsUsageReason) { (success, error) in
if success {
// complete after a little delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
}
} else {
if let error = error as NSError?, error.code == LAError.Code.userCancel.rawValue || error.code == LAError.Code.userFallback.rawValue {
self.userCancelledUnlockWithBiometrics()
}
}
}
}
private func userCancelledUnlockWithBiometrics() {
if pinCodePreferences.isPinSet {
// cascade this cancellation, coordinator should take care of it
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.coordinatorDelegate?.setupBiometricsViewModelDidCancel(self)
}
} else {
// show an alert to nowhere to go from here
DispatchQueue.main.async {
self.update(viewState: .cantUnlocked)
}
}
}
private func loadData() {
switch viewMode {
case .setupBiometricsAfterLogin:
self.update(viewState: .setupAfterLogin)
case .setupBiometricsFromSettings:
self.update(viewState: .setupFromSettings)
case .unlock:
self.update(viewState: .unlock)
case .confirmBiometricsToDeactivate:
self.update(viewState: .confirmToDisable)
default:
break
}
}
private func update(viewState: SetupBiometricsViewState) {
self.viewDelegate?.setupBiometricsViewModel(self, didUpdateViewState: viewState)
}
}
@@ -0,0 +1,40 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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 SetupBiometricsViewModelViewDelegate: class {
func setupBiometricsViewModel(_ viewModel: SetupBiometricsViewModelType, didUpdateViewState viewSate: SetupBiometricsViewState)
}
protocol SetupBiometricsViewModelCoordinatorDelegate: class {
func setupBiometricsViewModelDidComplete(_ viewModel: SetupBiometricsViewModelType)
func setupBiometricsViewModelDidCompleteWithReset(_ viewModel: SetupBiometricsViewModelType)
func setupBiometricsViewModelDidCancel(_ viewModel: SetupBiometricsViewModelType)
}
/// Protocol describing the view model used by `SetupBiometricsViewController`
protocol SetupBiometricsViewModelType {
var viewDelegate: SetupBiometricsViewModelViewDelegate? { get set }
var coordinatorDelegate: SetupBiometricsViewModelCoordinatorDelegate? { get set }
func localizedBiometricsName() -> String?
func biometricsIcon() -> UIImage?
func process(viewAction: SetupBiometricsViewAction)
}
@@ -0,0 +1,28 @@
// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/SetupBiometrics SetupBiometrics
/*
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
/// SetupBiometricsViewController view state
enum SetupBiometricsViewState {
case setupAfterLogin
case setupFromSettings
case unlock
case confirmToDisable
case cantUnlocked
}