wallet/Tokenary macOS/Screens/ImportViewController.swift

84 lines
2.8 KiB
Swift
Raw Normal View History

2021-11-30 15:56:00 +03:00
// Copyright © 2021 Tokenary. All rights reserved.
2021-06-12 13:32:27 +03:00
import Cocoa
import WalletCore
2021-06-12 13:32:27 +03:00
2021-06-13 02:12:03 +03:00
class ImportViewController: NSViewController {
2021-06-12 17:39:42 +03:00
2021-08-01 19:42:27 +03:00
private let walletsManager = WalletsManager.shared
var selectAccountAction: SelectAccountAction?
2021-08-01 19:42:27 +03:00
private var inputValidationResult = WalletsManager.InputValidationResult.invalid
2021-06-13 06:30:20 +03:00
2021-06-13 02:24:02 +03:00
@IBOutlet weak var textField: NSTextField! {
didSet {
textField.delegate = self
2022-05-08 21:58:03 +03:00
textField.placeholderString = Strings.importWalletTextFieldPlaceholder
2021-06-13 02:24:02 +03:00
}
}
@IBOutlet weak var okButton: NSButton!
2021-06-12 17:39:42 +03:00
2021-06-12 13:32:27 +03:00
override func viewDidLoad() {
super.viewDidLoad()
}
2021-06-12 19:33:24 +03:00
@IBAction func actionButtonTapped(_ sender: Any) {
2021-07-23 21:11:54 +03:00
if inputValidationResult == .requiresPassword {
showPasswordAlert()
} else {
2021-07-23 21:11:54 +03:00
importWith(input: textField.stringValue, password: nil)
2021-06-12 20:37:56 +03:00
}
2021-06-12 19:33:24 +03:00
}
2021-07-23 21:11:54 +03:00
private func showPasswordAlert() {
let alert = Alert()
2021-08-30 23:39:36 +03:00
alert.messageText = Strings.enterKeystorePassword
2021-07-23 21:11:54 +03:00
alert.alertStyle = .informational
2021-08-26 21:17:23 +03:00
alert.addButton(withTitle: Strings.ok)
alert.addButton(withTitle: Strings.cancel)
2021-07-23 21:11:54 +03:00
let passwordTextField = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 160, height: 20))
passwordTextField.bezelStyle = .roundedBezel
alert.accessoryView = passwordTextField
passwordTextField.isAutomaticTextCompletionEnabled = false
passwordTextField.alignment = .center
DispatchQueue.main.async {
passwordTextField.becomeFirstResponder()
}
if alert.runModal() == .alertFirstButtonReturn {
importWith(input: textField.stringValue, password: passwordTextField.stringValue)
}
}
private func importWith(input: String, password: String?) {
2021-08-01 19:42:27 +03:00
do {
let wallet = try walletsManager.addWallet(input: input, inputPassword: password)
showAccountsList(newWalletId: wallet.id)
2021-08-01 19:42:27 +03:00
} catch {
Alert.showWithMessage(Strings.failedToImportWallet, style: .critical)
2021-07-23 21:11:54 +03:00
}
}
private func showAccountsList(newWalletId: String?) {
2021-06-13 05:45:54 +03:00
let accountsListViewController = instantiate(AccountsListViewController.self)
accountsListViewController.selectAccountAction = selectAccountAction
accountsListViewController.newWalletId = newWalletId
2021-06-13 05:45:54 +03:00
view.window?.contentViewController = accountsListViewController
}
@IBAction func cancelButtonTapped(_ sender: NSButton) {
showAccountsList(newWalletId: nil)
}
2021-06-12 19:33:24 +03:00
2021-06-12 13:32:27 +03:00
}
2021-06-13 02:24:02 +03:00
extension ImportViewController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
2021-08-01 19:42:27 +03:00
inputValidationResult = walletsManager.validateWalletInput(textField.stringValue)
2021-07-23 21:11:54 +03:00
okButton.isEnabled = inputValidationResult != .invalid
2021-06-13 02:24:02 +03:00
}
}