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

239 lines
8.8 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 initialPin: String = ""
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
updateAfterPinSet()
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
updateAfterUnlockFailed()
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)
}
}
case .changePin:
// unlocking
if initialPin.isEmpty && currentPin != pinCodePreferences.pin {
// no match
updateAfterUnlockFailed()
} else if initialPin.isEmpty {
// match or already unlocked
// the user can choose a new Pin code
initialPin = currentPin
currentPin.removeAll()
update(viewState: .choosePin)
} else {
// choosing pin
updateAfterPinSet()
}
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
case .changePin:
return .changePin
2020-09-17 17:37:52 +00:00
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)
case .changePin:
update(viewState: .changePin)
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)
}
private func updateAfterUnlockFailed() {
numberOfFailuresDuringEnterPIN += 1
pinCodePreferences.numberOfPinFailures += 1
if viewMode == .unlock && localAuthenticationService.shouldLogOutUser() {
// log out user
self.coordinatorDelegate?.enterPinCodeViewModelDidCompleteWithReset(self, dueToTooManyErrors: true)
return
}
if numberOfFailuresDuringEnterPIN < pinCodePreferences.allowedNumberOfTrialsBeforeAlert {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: .wrongPin)
self.currentPin.removeAll()
}
} else {
viewDelegate?.enterPinCodeViewModel(self, didUpdateViewState: .wrongPinTooManyTimes)
numberOfFailuresDuringEnterPIN = 0
currentPin.removeAll()
}
}
private func updateAfterPinSet() {
if firstPin.isEmpty {
// check if this PIN is allowed
if pinCodePreferences.notAllowedPINs.contains(currentPin) {
viewMode = .notAllowedPin
update(viewState: .notAllowedPin)
return
}
// go to next screen
firstPin = currentPin
currentPin.removeAll()
update(viewState: .confirmPin)
} else if firstPin == currentPin { // check first and second pins
// complete with a little delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.coordinatorDelegate?.enterPinCodeViewModel(self, didCompleteWithPin: self.firstPin)
}
} else {
update(viewState: .pinsDontMatch)
}
}
}