Files
raspberry/iOS/Wallet/Sources/Account/View/Settings/AccountViewSettings.swift
2022-05-27 20:21:01 +03:00

172 lines
7.7 KiB
Swift

//
// AccountViewSettings.swift
// PayCash
//
// Created by Igor Danich on 30.08.2020.
// Copyright © 2020 List. All rights reserved.
//
import UIKit
class AccountViewSettings: CommonViewCustom {
@IBOutlet weak var notificationsView: UIStackView!
@IBOutlet weak var notificationsEnabledLabel: UILabel!
@IBOutlet private weak var biometricksLbl: UILabel!
@IBOutlet private weak var biometricksSwitch: UISwitch!
@IBOutlet private weak var passwordTitlesLbl: UILabel!
@IBOutlet private weak var versionLbl: UILabel!
var parent: UIViewController!
override func setup() {
super.setup()
notificationsView.isUserInteractionEnabled = true
notificationsView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onNotifications)))
biometricksSwitch.isEnabled = Accounts().isBiometricksAvailable
biometricksSwitch.isOn = Accounts().isBiometricksEnabled
if Accounts().isFaceIdAvailable {
biometricksLbl.text = L10n.Account.Settings.faceId
} else {
biometricksLbl.text = L10n.Account.Settings.touchId
}
if UserDefaults.standard.bool(forKey: "isPinPwdMode") {
passwordTitlesLbl.text = L10n.Account.Settings.pin
} else {
passwordTitlesLbl.text = L10n.Account.Settings.password
}
versionLbl.text = "\(Bundle.main.major).\(Bundle.main.minor).\(Bundle.main.patch) (\(Bundle.main.buildNumber))"
}
@objc func onNotifications() {
if let appSettings = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
@IBAction private func onBiometricks(_ : AnyObject?) {
if biometricksSwitch.isOn {
Accounts().checkBiometricks { (success) in
Accounts().isBiometricksEnabled = success
self.biometricksSwitch.isOn = success
}
} else {
Accounts().isBiometricksEnabled = false
}
}
@IBAction private func onPassword(_ : AnyObject?) {
parent.dismiss(animated: true)
if !UserDefaults.standard.bool(forKey: "isPinPwdMode") {
AccountViewPassword.show(in: parent)
} else {
let accountControollerPin = StoryboardScene.Account.accountControllerPin.instantiate()
let ctrl = UINavigationController(rootViewController: accountControollerPin)
accountControollerPin.type = .old
accountControollerPin.pinValidate = { [weak ctrl] (pin: String) -> Bool in
if let password = Accounts().getPassword(password: pin),
!password.isEmpty, password == pin, password.count == 4 {
Account.Service.Authorize.shared.password = password
ctrl?.dismiss(animated: true)
let accountControollerPin = StoryboardScene.Account.accountControllerPin.instantiate()
let ctrl = UINavigationController(rootViewController: accountControollerPin)
accountControollerPin.type = .new
accountControollerPin.pinValidate = { [weak ctrl] (pin: String) -> Bool in
if let oldPassword = Accounts().getPassword(password: password) {
Accounts().update(password: pin, old: oldPassword)
Account.Service.Authorize.shared.password = pin
ctrl?.dismiss(animated: true)
return true
} else {
Alert.error(text: L10n.Account.Password.currentWrong)
return false
}
}
ctrl.modalPresentationStyle = .fullScreen
self.parent.present(ctrl, animated: true)
Account.Service.Authorize.shared.clearPinTries()
return true
} else {
Account.Service.Authorize.shared.savePinTry()
return false
}
}
ctrl.modalPresentationStyle = .fullScreen
parent.present(ctrl, animated: true)
}
}
@IBAction private func onServers(_ : AnyObject?) {
parent.dismiss(animated: true)
Popup.show(title: "Servers", views: [
CommonViewPopUpSelection(
list: Network.servers.collection.map({ $0.name }),
selectedIndex: Network.servers.collection.lastIndex(where: { $0.name == Network.servers.current.name }) ?? -1,
selection: { index in
Network.servers.current = Network.servers.collection[index]
self.parent.dismiss(animated: true)
}
)
], in: parent, animated: true)
}
@IBAction private func onAbout(_ : AnyObject?) {
parent?.navigationController?.pushViewController(AccountControllerAbout(), animated: true)
}
@IBAction func onChangeInterfaceStyle(_ sender: Any) {
parent.dismiss(animated: true)
let vc = StoryboardScene.AccountOnboarding
.accountOnboardingControllerInterfaceType.instantiate()
vc.isStep = false
parent.present(vc, animated: true)
}
@IBAction func onChangeProtection(_ sender: Any) {
parent.dismiss(animated: true)
AccountViewAuthorize.showGetPassword(in: parent, { oldPassword in
let view = AccountViewProtectionLevel()
view.didSubmit = {
self.parent.dismiss(animated: true)
if $0 {
if UserDefaults.standard.bool(forKey: "isPinPwdMode") {
AccountViewPassword.show(in: self.parent, oldPassword: oldPassword)
} else {
let accountControollerPin = StoryboardScene.Account.accountControllerPin.instantiate()
let ctrl = UINavigationController(rootViewController: accountControollerPin)
accountControollerPin.type = .new
accountControollerPin.pinValidate = { [weak ctrl] (pin: String) -> Bool in
if let oldPassword = Accounts().getPassword(password: oldPassword) {
Accounts().update(password: pin, old: oldPassword)
UserDefaults.standard.setValue(!UserDefaults.standard.bool(forKey: "isPinPwdMode"),
forKey: "isPinPwdMode")
Account.Service.Authorize.shared.password = pin
ctrl?.dismiss(animated: true)
return true
} else {
Alert.error(text: L10n.Account.Password.currentWrong)
return false
}
}
ctrl.modalPresentationStyle = .fullScreen
self.parent.present(ctrl, animated: true)
}
}
}
let viewController =
CommonControllerPopup(image: UserDefaults.standard.bool(forKey: "isPinPwdMode")
? Asset.accountProtectionUnlocked.image
: Asset.accountProtectionLocked.image,
title: L10n.Account.Settings.protection,
views: [view], spacing: 0)
self.parent.present(viewController, animated: true)
// Popup.show(content: viewController, in: self.parent)
})
}
}