2021-11-30 15:56:00 +03:00
|
|
|
// Copyright © 2021 Tokenary. All rights reserved.
|
2021-06-12 13:32:27 +03:00
|
|
|
|
|
|
|
import Cocoa
|
2022-05-07 23:38:42 +03:00
|
|
|
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
|
2022-08-23 18:00:48 +03:00
|
|
|
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()
|
2021-07-20 23:26:07 +03:00
|
|
|
} 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-06-13 04:05:42 +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 {
|
2021-08-03 23:37:01 +03:00
|
|
|
let wallet = try walletsManager.addWallet(input: input, inputPassword: password)
|
|
|
|
showAccountsList(newWalletId: wallet.id)
|
2021-08-01 19:42:27 +03:00
|
|
|
} catch {
|
2022-05-07 23:38:42 +03:00
|
|
|
Alert.showWithMessage(Strings.failedToImportWallet, style: .critical)
|
2021-07-23 21:11:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 23:37:01 +03:00
|
|
|
private func showAccountsList(newWalletId: String?) {
|
2021-06-13 05:45:54 +03:00
|
|
|
let accountsListViewController = instantiate(AccountsListViewController.self)
|
2022-08-23 18:00:48 +03:00
|
|
|
accountsListViewController.selectAccountAction = selectAccountAction
|
2021-08-03 23:37:01 +03:00
|
|
|
accountsListViewController.newWalletId = newWalletId
|
2021-06-13 05:45:54 +03:00
|
|
|
view.window?.contentViewController = accountsListViewController
|
2021-06-13 04:05:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func cancelButtonTapped(_ sender: NSButton) {
|
2021-08-03 23:37:01 +03:00
|
|
|
showAccountsList(newWalletId: nil)
|
2021-06-13 04:05:42 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|