tokenary/Tokenary iOS/Screens/ImportViewController.swift

129 lines
4.3 KiB
Swift
Raw Normal View History

2021-12-07 20:35:14 +03:00
// Copyright © 2021 Tokenary. All rights reserved.
import UIKit
class ImportViewController: UIViewController {
2021-12-10 17:11:24 +03:00
var completion: ((Bool) -> Void)?
private let walletsManager = WalletsManager.shared
2021-12-11 00:22:27 +03:00
@IBOutlet weak var placeholderLabel: UILabel! {
didSet {
2022-05-08 21:58:03 +03:00
placeholderLabel.text = Strings.importWalletTextFieldPlaceholder
2021-12-11 00:22:27 +03:00
}
}
2021-12-07 20:35:14 +03:00
@IBOutlet weak var pasteButton: UIButton!
@IBOutlet weak var okButton: UIButton!
2021-12-10 17:11:24 +03:00
@IBOutlet weak var textView: UITextView! {
didSet {
textView.delegate = self
2021-12-11 00:22:27 +03:00
textView.textContainerInset = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 8)
2021-12-10 23:39:40 +03:00
textView.layer.cornerRadius = 5
2021-12-11 00:22:27 +03:00
textView.layer.borderWidth = CGFloat.pixel
2021-12-10 23:39:40 +03:00
textView.layer.borderColor = UIColor.separator.cgColor
2021-12-10 17:11:24 +03:00
}
}
2021-12-11 01:30:45 +03:00
private var isWaiting = false
2021-12-10 17:11:24 +03:00
private var inputValidationResult = WalletsManager.InputValidationResult.invalid
2021-12-07 20:35:14 +03:00
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
2022-05-08 03:00:36 +03:00
navigationItem.title = Strings.importWallet
2021-12-07 20:35:14 +03:00
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismissAnimated))
2021-12-11 01:30:45 +03:00
okButton.configurationUpdateHandler = { [weak self] button in
let isWaiting = self?.isWaiting == true
button.configuration?.title = isWaiting ? "" : Strings.ok
button.configuration?.showsActivityIndicator = isWaiting
}
2021-12-07 20:35:14 +03:00
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.async { [weak self] in
self?.navigationController?.navigationBar.sizeToFit()
2021-12-10 23:39:40 +03:00
self?.textView.becomeFirstResponder()
2021-12-07 20:35:14 +03:00
}
}
@IBAction func pasteButtonTapped(_ sender: Any) {
2021-12-09 19:36:05 +03:00
if let text = UIPasteboard.general.string {
textView.text = text
2021-12-10 17:11:24 +03:00
validateInput(proceedIfValid: false)
2021-12-09 19:36:05 +03:00
}
2021-12-07 20:35:14 +03:00
}
@IBAction func okButtonTapped(_ sender: Any) {
2021-12-10 17:11:24 +03:00
attemptImportWithCurrentInput()
}
private func attemptImportWithCurrentInput() {
if inputValidationResult == .requiresPassword {
2021-12-10 17:21:31 +03:00
askPassword()
2021-12-10 17:11:24 +03:00
} else {
importWith(input: textView.text, password: nil)
}
}
2021-12-10 17:21:31 +03:00
private func askPassword() {
2021-12-10 19:46:44 +03:00
showPasswordAlert(title: Strings.enterKeystorePassword, message: nil) { [weak self] password in
guard let password = password else { return }
2021-12-11 01:30:45 +03:00
self?.setWaiting(true)
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) {
self?.importWith(input: self?.textView.text ?? "", password: password)
}
2021-12-10 17:11:24 +03:00
}
}
private func importWith(input: String, password: String?) {
do {
_ = try walletsManager.addWallet(input: input, inputPassword: password)
completion?(true)
dismissAnimated()
} catch {
2021-12-11 01:30:45 +03:00
setWaiting(false)
2022-05-08 03:00:36 +03:00
showMessageAlert(text: Strings.failedToImportWallet)
2021-12-10 17:11:24 +03:00
}
}
2021-12-11 01:30:45 +03:00
private func setWaiting(_ waiting: Bool) {
guard waiting != self.isWaiting else { return }
self.isWaiting = waiting
view.isUserInteractionEnabled = !waiting
isModalInPresentation = waiting
navigationItem.leftBarButtonItem?.isEnabled = !waiting
okButton.setNeedsUpdateConfiguration()
}
2021-12-10 17:11:24 +03:00
private func validateInput(proceedIfValid: Bool) {
2021-12-11 00:22:27 +03:00
placeholderLabel.isHidden = !textView.text.isEmpty
2021-12-10 17:11:24 +03:00
inputValidationResult = walletsManager.validateWalletInput(textView.text)
let isValid = inputValidationResult != .invalid
okButton.isEnabled = isValid
if isValid && proceedIfValid {
attemptImportWithCurrentInput()
}
}
}
extension ImportViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
validateInput(proceedIfValid: true)
return false
} else {
return true
}
}
func textViewDidChange(_ textView: UITextView) {
validateInput(proceedIfValid: false)
2021-12-07 20:35:14 +03:00
}
}