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

391 lines
14 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 UIKit
final class EnterPinCodeViewController: UIViewController {
// MARK: - Constants
private enum Constants {
static let aConstant: Int = 666
}
// MARK: - Properties
// MARK: Outlets
2020-09-07 15:09:47 +00:00
@IBOutlet private weak var mainStackView: UIStackView!
@IBOutlet private weak var inactiveView: UIView!
@IBOutlet private weak var inactiveLogoImageView: UIImageView!
2020-07-21 13:16:27 +00:00
@IBOutlet private weak var logoImageView: UIImageView!
@IBOutlet private weak var placeholderStackView: UIStackView!
2020-09-17 13:01:08 +00:00
@IBOutlet private weak var notAllowedPinView: UIView!
@IBOutlet private weak var notAllowedPinLineView: UIView!
@IBOutlet private weak var notAllowedPinLabel: UILabel!
@IBOutlet private weak var digitsStackView: UIStackView!
@IBOutlet private weak var informationLabel: UILabel!
@IBOutlet private weak var explanatoryLabel: UILabel!
2020-07-21 13:16:27 +00:00
@IBOutlet private weak var forgotPinButton: UIButton!
2020-10-02 11:17:52 +00:00
@IBOutlet private weak var bottomView: UIView!
// MARK: Private
private var viewModel: EnterPinCodeViewModelType!
private var theme: Theme!
private var errorPresenter: MXKErrorPresentation!
private var activityPresenter: ActivityIndicatorPresenter!
// MARK: - Setup
class func instantiate(with viewModel: EnterPinCodeViewModelType) -> EnterPinCodeViewController {
let viewController = StoryboardScene.EnterPinCodeViewController.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
2020-07-21 13:16:27 +00:00
self.viewModel.process(viewAction: .loadData)
// force orientation to portrait if phone
if UIDevice.current.isPhone {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
}
2020-09-07 10:51:48 +00:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.vc_closeKeyboard()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return self.theme.statusBarStyle
}
2020-08-06 07:48:19 +00:00
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
// limit orientation to portrait only for phone
if UIDevice.current.isPhone {
return .portrait
}
return super.supportedInterfaceOrientations
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
if UIDevice.current.isPhone {
return .portrait
}
return super.preferredInterfaceOrientationForPresentation
2020-08-06 07:48:19 +00:00
}
// 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.informationLabel.textColor = theme.textPrimaryColor
self.explanatoryLabel.textColor = theme.textSecondaryColor
2020-09-17 13:01:08 +00:00
self.notAllowedPinLineView.backgroundColor = theme.noticeColor
self.notAllowedPinLabel.textColor = theme.noticeColor
2020-07-21 16:32:48 +00:00
updateThemesOfAllImages(in: placeholderStackView, with: theme)
updateThemesOfAllButtons(in: digitsStackView, with: theme)
2020-07-21 16:32:48 +00:00
theme.applyStyle(onButton: forgotPinButton)
}
private func updateThemesOfAllImages(in view: UIView, with theme: Theme) {
if let imageView = view as? UIImageView {
imageView.tintColor = theme.noticeSecondaryColor
} else {
for subview in view.subviews {
updateThemesOfAllImages(in: subview, with: theme)
}
}
}
private func updateThemesOfAllButtons(in view: UIView, with theme: Theme) {
if let button = view as? UIButton {
2020-07-21 16:32:48 +00:00
button.tintColor = theme.textPrimaryColor
button.setTitleColor(theme.textPrimaryColor, for: .normal)
} else {
for subview in view.subviews {
updateThemesOfAllButtons(in: subview, with: theme)
}
}
}
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() {
2020-10-02 11:17:52 +00:00
let cancelBarButtonItem = MXKBarButtonItem(title: VectorL10n.cancel, style: .plain) { [weak self] in
self?.cancelButtonAction()
}
self.navigationItem.rightBarButtonItem = cancelBarButtonItem
2020-07-21 16:32:48 +00:00
showCancelButton()
self.title = ""
2020-09-17 13:22:32 +00:00
notAllowedPinLabel.text = VectorL10n.pinProtectionNotAllowedPin
2020-09-17 11:07:12 +00:00
2020-07-21 16:32:48 +00:00
placeholderStackView.vc_removeAllArrangedSubviews()
for i in 0..<PinCodePreferences.shared.numberOfDigits {
let imageView = UIImageView(image: Asset.Images.selectionUntick.image)
imageView.heightAnchor.constraint(equalToConstant: 24).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 24).isActive = true
imageView.tag = i
placeholderStackView.addArrangedSubview(imageView)
}
}
private func showCancelButton() {
2020-10-02 11:17:52 +00:00
self.navigationController?.navigationBar.isHidden = false
}
2020-07-21 16:32:48 +00:00
private func hideCancelButton() {
2020-10-02 11:17:52 +00:00
self.navigationController?.navigationBar.isHidden = true
2020-07-21 16:32:48 +00:00
}
private func render(viewState: EnterPinCodeViewState) {
switch viewState {
2020-07-21 13:16:27 +00:00
case .choosePin:
self.renderChoosePin()
case .choosePinAfterLogin:
self.renderChoosePinAfterLogin()
case .choosePinAfterRegister:
self.renderChoosePinAfterRegister()
2020-09-17 12:58:25 +00:00
case .notAllowedPin:
self.renderNotAllowedPin()
case .confirmPin:
self.renderConfirmPin()
2020-07-21 13:16:27 +00:00
case .pinsDontMatch:
self.renderPinsDontMatch()
2020-07-24 14:53:23 +00:00
case .unlock:
2020-07-21 13:16:27 +00:00
self.renderUnlockByPin()
case .wrongPin:
self.renderWrongPin()
case .wrongPinTooManyTimes:
self.renderWrongPinTooManyTimes()
case .forgotPin:
self.renderForgotPin()
case .confirmPinToDisable:
self.renderConfirmPinToDisable()
2020-09-07 15:09:47 +00:00
case .inactive:
self.renderInactive()
}
}
2020-07-21 13:16:27 +00:00
private func renderChoosePin() {
2020-09-07 15:09:47 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
2020-07-21 13:16:27 +00:00
self.logoImageView.isHidden = true
self.informationLabel.text = VectorL10n.pinProtectionChoosePin
self.explanatoryLabel.isHidden = false
2020-07-21 13:16:27 +00:00
self.forgotPinButton.isHidden = true
2020-10-02 11:17:52 +00:00
self.bottomView.isHidden = false
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
2020-09-17 11:07:12 +00:00
}
private func renderChoosePinAfterLogin() {
renderChoosePin()
self.informationLabel.text = VectorL10n.pinProtectionChoosePinWelcomeAfterLogin + "\n" + VectorL10n.pinProtectionChoosePin
}
private func renderChoosePinAfterRegister() {
renderChoosePin()
self.informationLabel.text = VectorL10n.pinProtectionChoosePinWelcomeAfterRegister + "\n" + VectorL10n.pinProtectionChoosePin
}
2020-09-17 12:58:25 +00:00
private func renderNotAllowedPin() {
2020-09-17 11:07:12 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
self.logoImageView.isHidden = true
self.forgotPinButton.isHidden = true
2020-10-02 11:17:52 +00:00
self.bottomView.isHidden = false
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = false
2020-09-17 11:07:12 +00:00
renderPlaceholdersCount(.max, error: true)
}
private func renderConfirmPin() {
2020-09-07 15:09:47 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
2020-07-21 13:16:27 +00:00
self.informationLabel.text = VectorL10n.pinProtectionConfirmPin
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
// reset placeholders
renderPlaceholdersCount(0)
}
2020-07-21 13:16:27 +00:00
private func renderPinsDontMatch() {
2020-07-22 07:30:08 +00:00
let error = MXKErrorViewModel(title: VectorL10n.pinProtectionMismatchErrorTitle,
message: VectorL10n.pinProtectionMismatchErrorMessage)
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
2020-07-22 07:30:08 +00:00
self.errorPresenter.presentError(from: self, for: error, animated: true) {
2020-07-21 13:16:27 +00:00
self.viewModel.process(viewAction: .pinsDontMatchAlertAction)
}
}
private func renderUnlockByPin() {
2020-07-21 16:32:48 +00:00
hideCancelButton()
2020-09-07 15:09:47 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
2020-07-21 13:16:27 +00:00
self.logoImageView.isHidden = false
self.informationLabel.text = VectorL10n.pinProtectionEnterPin
2020-10-02 11:17:52 +00:00
self.explanatoryLabel.isHidden = true
2020-07-21 13:16:27 +00:00
self.forgotPinButton.isHidden = false
2020-10-02 11:17:52 +00:00
self.bottomView.isHidden = true
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
2020-07-21 13:16:27 +00:00
}
private func renderWrongPin() {
2020-09-07 15:09:47 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
2020-10-02 11:17:52 +00:00
self.explanatoryLabel.isHidden = true
2020-07-21 13:16:27 +00:00
self.placeholderStackView.vc_shake()
}
private func renderWrongPinTooManyTimes() {
2020-07-22 07:30:08 +00:00
let error = MXKErrorViewModel(title: VectorL10n.pinProtectionMismatchErrorTitle,
message: VectorL10n.pinProtectionMismatchTooManyTimesErrorMessage)
2020-07-21 13:16:27 +00:00
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
2020-07-22 07:30:08 +00:00
self.errorPresenter.presentError(from: self, for: error, animated: true, handler: nil)
2020-07-21 13:16:27 +00:00
}
private func renderForgotPin() {
let controller = UIAlertController(title: VectorL10n.pinProtectionResetAlertTitle,
message: VectorL10n.pinProtectionResetAlertMessage,
preferredStyle: .alert)
let resetAction = UIAlertAction(title: VectorL10n.pinProtectionResetAlertActionReset, style: .default) { (_) in
2020-07-22 19:45:20 +00:00
self.viewModel.process(viewAction: .forgotPinAlertResetAction)
}
let cancelAction = UIAlertAction(title: VectorL10n.cancel, style: .cancel) { (_) in
self.viewModel.process(viewAction: .forgotPinAlertCancelAction)
2020-07-21 13:16:27 +00:00
}
controller.addAction(resetAction)
2020-07-22 19:45:20 +00:00
controller.addAction(cancelAction)
2020-07-21 13:16:27 +00:00
self.present(controller, animated: true, completion: nil)
}
private func renderConfirmPinToDisable() {
2020-09-07 15:09:47 +00:00
self.inactiveView.isHidden = true
self.mainStackView.isHidden = false
2020-07-21 13:16:27 +00:00
self.logoImageView.isHidden = true
self.informationLabel.text = VectorL10n.pinProtectionConfirmPinToDisable
2020-10-02 11:17:52 +00:00
self.explanatoryLabel.isHidden = true
2020-07-21 13:16:27 +00:00
self.forgotPinButton.isHidden = true
2020-10-02 11:17:52 +00:00
self.bottomView.isHidden = false
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
}
2020-09-07 15:09:47 +00:00
private func renderInactive() {
self.hideCancelButton()
self.inactiveView.isHidden = false
self.mainStackView.isHidden = true
2020-09-17 13:01:08 +00:00
self.notAllowedPinView.isHidden = true
2020-10-02 11:17:52 +00:00
self.explanatoryLabel.isHidden = true
2020-09-07 15:09:47 +00:00
}
2020-09-17 11:07:12 +00:00
private func renderPlaceholdersCount(_ count: Int, error: Bool = false) {
UIView.animate(withDuration: 0.3) {
2020-07-22 15:03:21 +00:00
for case let imageView as UIImageView in self.placeholderStackView.arrangedSubviews {
if imageView.tag < count {
2020-09-17 11:07:12 +00:00
if error {
imageView.image = Asset.Images.placeholder.image.vc_tintedImage(usingColor: self.theme.noticeColor)
} else {
imageView.image = Asset.Images.placeholder.image
}
} else {
imageView.image = Asset.Images.selectionUntick.image
}
}
}
}
// MARK: - Actions
@IBAction private func digitButtonAction(_ sender: UIButton) {
self.viewModel.process(viewAction: .digitPressed(sender.tag))
}
2020-07-21 13:16:27 +00:00
@IBAction private func forgotPinButtonAction(_ sender: UIButton) {
self.viewModel.process(viewAction: .forgotPinPressed)
}
private func cancelButtonAction() {
self.viewModel.process(viewAction: .cancel)
}
}
// MARK: - EnterPinCodeViewModelViewDelegate
extension EnterPinCodeViewController: EnterPinCodeViewModelViewDelegate {
func enterPinCodeViewModel(_ viewModel: EnterPinCodeViewModelType, didUpdateViewState viewSate: EnterPinCodeViewState) {
self.render(viewState: viewSate)
}
func enterPinCodeViewModel(_ viewModel: EnterPinCodeViewModelType, didUpdatePlaceholdersCount count: Int) {
self.renderPlaceholdersCount(count)
}
2020-08-06 13:35:07 +00:00
func enterPinCodeViewModel(_ viewModel: EnterPinCodeViewModelType, didUpdateCancelButtonHidden isHidden: Bool) {
if isHidden {
hideCancelButton()
} else {
showCancelButton()
}
}
}