element-ios/Riot/Modules/SetPinCode/EnterPinCode/EnterPinCodeViewModel.swift

213 lines
8.2 KiB
Swift
Raw Normal View History

// File created from ScreenTemplate
// $ createScreen.sh SetPinCode/EnterPinCode EnterPinCode
/*
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 EnterPinCodeViewModel: EnterPinCodeViewModelType {
// MARK: - Properties
// MARK: Private
private let session: MXSession?
2020-09-17 17:37:52 +00:00
private var originalViewMode: SetPinCoordinatorViewMode
2020-07-21 13:16:27 +00:00
private var viewMode: SetPinCoordinatorViewMode
private var firstPin: String = ""
private var currentPin: String = "" {
didSet {
self.viewDelegate?.enterPinCodeViewModel(self, didUpdatePlaceholdersCount: currentPin.count)
}
}
2020-07-21 13:16:27 +00:00
private var numberOfFailuresDuringEnterPIN: Int = 0
// MARK: Public
weak var viewDelegate: EnterPinCodeViewModelViewDelegate?
weak var coordinatorDelegate: EnterPinCodeViewModelCoordinatorDelegate?
private let pinCodePreferences: PinCodePreferences
private let localAuthenticationService: LocalAuthenticationService
// MARK: - Setup
2020-07-22 14:47:45 +00:00
init(session: MXSession?, viewMode: SetPinCoordinatorViewMode, pinCodePreferences: PinCodePreferences) {
self.session = session
2020-09-17 17:37:52 +00:00
self.originalViewMode = viewMode
2020-07-21 13:16:27 +00:00
self.viewMode = viewMode
2020-07-22 14:47:45 +00:00
self.pinCodePreferences = pinCodePreferences
self.localAuthenticationService = LocalAuthenticationService(pinCodePreferences: pinCodePreferences)
}
// MARK: - Public
func process(viewAction: EnterPinCodeViewAction) {
switch viewAction {
2020-07-21 13:16:27 +00:00
case .loadData:
self.loadData()
case .digitPressed(let tag):
self.digitPressed(tag)
2020-07-21 13:16:27 +00:00
case .forgotPinPressed:
self.viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: .forgotPin)
case .cancel:
self.coordinatorDelegate?.enterPinCodeViewModelDidCancel(self)
2020-07-21 13:16:27 +00:00
case .pinsDontMatchAlertAction:
// reset pins
firstPin.removeAll()
currentPin.removeAll()
// go back to first state
self.update(viewState: .choosePin)
2020-07-22 19:45:20 +00:00
case .forgotPinAlertResetAction:
2020-09-29 14:46:07 +00:00
self.coordinatorDelegate?.enterPinCodeViewModelDidCompleteWithReset(self, dueToTooManyErrors: false)
2020-07-22 19:45:20 +00:00
case .forgotPinAlertCancelAction:
// no-op
break
}
}
// MARK: - Private
private func digitPressed(_ tag: Int) {
if tag == -1 {
// delete tapped
if currentPin.isEmpty {
return
} else {
currentPin.removeLast()
2020-09-17 11:07:12 +00:00
// switch to setPin if blocked
2020-09-17 12:57:06 +00:00
if viewMode == .notAllowedPin {
2020-09-17 11:07:12 +00:00
// clear error UI
2020-09-17 17:37:52 +00:00
update(viewState: viewState(for: originalViewMode))
// switch back to original flow
viewMode = originalViewMode
2020-09-17 11:07:12 +00:00
}
}
} else {
// a digit tapped
2020-09-17 11:07:12 +00:00
// switch to setPin if blocked
2020-09-17 12:57:06 +00:00
if viewMode == .notAllowedPin {
2020-09-17 11:07:12 +00:00
// clear old pin first
currentPin.removeAll()
// clear error UI
2020-09-17 17:37:52 +00:00
update(viewState: viewState(for: originalViewMode))
// switch back to original flow
viewMode = originalViewMode
2020-09-17 11:07:12 +00:00
}
// add new digit
2020-07-21 16:32:48 +00:00
currentPin += String(tag)
2020-07-22 14:47:45 +00:00
if currentPin.count == pinCodePreferences.numberOfDigits {
2020-07-21 13:16:27 +00:00
switch viewMode {
2020-09-17 17:37:52 +00:00
case .setPin, .setPinAfterLogin, .setPinAfterRegister:
2020-07-21 13:16:27 +00:00
// choosing pin
if firstPin.isEmpty {
2020-09-17 11:07:12 +00:00
// check if this PIN is allowed
2020-09-17 12:55:41 +00:00
if pinCodePreferences.notAllowedPINs.contains(currentPin) {
2020-09-17 12:57:06 +00:00
viewMode = .notAllowedPin
2020-09-17 12:58:25 +00:00
update(viewState: .notAllowedPin)
2020-09-17 11:07:12 +00:00
return
}
2020-07-21 13:16:27 +00:00
// go to next screen
firstPin = currentPin
currentPin.removeAll()
2020-07-21 16:32:48 +00:00
update(viewState: .confirmPin)
} else {
2020-07-21 13:16:27 +00:00
// check first and second pins
if firstPin == currentPin {
// complete with a little delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
2020-07-21 13:19:14 +00:00
self.coordinatorDelegate?.enterPinCodeViewModel(self, didCompleteWithPin: self.firstPin)
2020-07-21 13:16:27 +00:00
}
} else {
2020-07-21 16:32:48 +00:00
update(viewState: .pinsDontMatch)
2020-07-21 13:16:27 +00:00
}
}
2020-07-24 14:53:23 +00:00
case .unlock, .confirmPinToDeactivate:
2020-07-21 13:16:27 +00:00
// unlocking
2020-07-22 14:47:45 +00:00
if currentPin != pinCodePreferences.pin {
2020-07-21 13:16:27 +00:00
// no match
numberOfFailuresDuringEnterPIN += 1
pinCodePreferences.numberOfPinFailures += 1
if viewMode == .unlock && localAuthenticationService.shouldLogOutUser() {
// log out user
2020-09-29 14:46:07 +00:00
self.coordinatorDelegate?.enterPinCodeViewModelDidCompleteWithReset(self, dueToTooManyErrors: true)
return
}
2020-07-22 14:47:45 +00:00
if numberOfFailuresDuringEnterPIN < pinCodePreferences.allowedNumberOfTrialsBeforeAlert {
2020-07-21 16:32:48 +00:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: .wrongPin)
self.currentPin.removeAll()
}
2020-07-21 13:16:27 +00:00
} else {
2020-07-21 16:32:48 +00:00
viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: .wrongPinTooManyTimes)
2020-07-21 13:16:27 +00:00
numberOfFailuresDuringEnterPIN = 0
2020-07-21 16:32:48 +00:00
currentPin.removeAll()
2020-07-21 13:16:27 +00:00
}
} else {
// match
// we can use biometrics anymore, if set
pinCodePreferences.canUseBiometricsToUnlock = nil
pinCodePreferences.resetCounters()
2020-07-21 13:16:27 +00:00
// complete with a little delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.coordinatorDelegate?.enterPinCodeViewModelDidComplete(self)
}
}
2020-07-24 14:53:23 +00:00
default:
break
}
return
}
}
}
2020-09-17 17:37:52 +00:00
private func viewState(for mode: SetPinCoordinatorViewMode) -> EnterPinCodeViewState {
switch mode {
case .setPin:
return .choosePin
case .setPinAfterLogin:
return .choosePinAfterLogin
case .setPinAfterRegister:
return .choosePinAfterRegister
default:
return .inactive
}
}
private func loadData() {
2020-07-21 13:16:27 +00:00
switch viewMode {
2020-09-17 17:37:52 +00:00
case .setPin, .setPinAfterLogin, .setPinAfterRegister:
update(viewState: viewState(for: viewMode))
2020-08-06 13:35:07 +00:00
self.viewDelegate?.enterPinCodeViewModel(self, didUpdateCancelButtonHidden: pinCodePreferences.forcePinProtection)
2020-07-24 14:53:23 +00:00
case .unlock:
update(viewState: .unlock)
2020-07-21 13:16:27 +00:00
case .confirmPinToDeactivate:
update(viewState: .confirmPinToDisable)
2020-09-07 15:09:47 +00:00
case .inactive:
update(viewState: .inactive)
2020-07-24 14:53:23 +00:00
default:
break
2020-07-21 13:16:27 +00:00
}
}
private func update(viewState: EnterPinCodeViewState) {
self.viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: viewState)
}
}