231 lines
9.4 KiB
Swift
231 lines
9.4 KiB
Swift
//
|
|
// SetPrivateKeyViewController.swift
|
|
// Malinka
|
|
//
|
|
// Created by Nut.Tech on 29.07.2022.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
import WalletKit
|
|
|
|
final class SetPrivateKeyViewController: UIViewController {
|
|
|
|
private enum WalletNavigation {
|
|
case account(WalletKit.Wallet)
|
|
}
|
|
|
|
@IBOutlet weak var warningView: CommonViewWarning!
|
|
@IBOutlet var headerTitleLabel: UILabel!
|
|
@IBOutlet private weak var submitBtn: CommonButtonAction!
|
|
|
|
private let accountService = AccountOnboarding.Service.CreateAccount()
|
|
private let iapService = AccountOnboarding.Service.IAP.shared
|
|
|
|
@IBOutlet var signalView: UIView!
|
|
@IBOutlet var checkboxButton: UIButton!
|
|
@IBOutlet private weak var usernameView: CommonViewCopy!
|
|
@IBOutlet private weak var privateKeyView: CommonViewCopy!
|
|
|
|
var showCreateManual: Bool!
|
|
var walletService: KeysWalletService!
|
|
|
|
private var service: UpdateWalletStateService?
|
|
private var refreshSubscription: AnyCancellable?
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.title = L10n.Account.Onboarding.freeTitle
|
|
self.checkboxButton.setImage(Asset.checkboxOff.image, for: .normal)
|
|
|
|
self.usernameView.text = self.walletService.walletName
|
|
self.privateKeyView.text = self.walletService.walletPrivateKey
|
|
self.warningView.makeCritical()
|
|
|
|
self.submitBtn.lzTitle = L10n.Account.Onboarding.Username.create
|
|
self.submitBtn.isEnabled = false
|
|
|
|
if let navigationController = self.navigationController {
|
|
|
|
self.navigationItem.leftBarButtonItem = .pop(self)
|
|
|
|
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
navigationController.navigationBar.shadowImage = UIImage()
|
|
navigationController.navigationBar.isTranslucent = true
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
if Accounts().current != nil,
|
|
self.navigationController == nil {
|
|
self.dismiss(animated: false)
|
|
}
|
|
}
|
|
|
|
@IBAction private func onSubmit(_ : AnyObject?) {
|
|
|
|
if checkboxButton.image(for: .normal) == Asset.checkboxOff.image {
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
self.signalView.alpha = 1
|
|
})
|
|
DispatchQueue.main.async {
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
self.signalView.alpha = 0
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
Popup.showWithLoader(in: self, with: "account.create.progress".localized)
|
|
|
|
let service = self.walletService.createService()
|
|
|
|
AccountViewAuthorize.showGetPassword(in: self) { password in
|
|
self.createWallet(with: service, password: password)
|
|
}
|
|
}
|
|
|
|
private func createWallet(with service: CreateWalletService, password: String) {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
let service = try await service.create(for: password)
|
|
self.service = service
|
|
|
|
var newWallet: WalletKit.Wallet?
|
|
self.refreshSubscription = service.statusPublisher
|
|
.receive(on: DispatchQueue.main)
|
|
.sink(receiveCompletion: { [weak self] _ in
|
|
guard let self,
|
|
let wallet = newWallet else {
|
|
return
|
|
}
|
|
|
|
switch wallet.state {
|
|
case .accepted:
|
|
self.showErrorPopup(title: "account.create.success.title".localized,
|
|
description: "account.create.success.description".localized,
|
|
button: "account.create.success.button".localized,
|
|
navigate: .account(wallet))
|
|
case .creating, .pending:
|
|
self.showErrorPopup(title: "account.create.needMoreTime.title".localized,
|
|
description: "account.create.needMoreTime.description".localized,
|
|
button: "account.create.needMoreTime.button".localized,
|
|
navigate: .account(wallet))
|
|
case .declined:
|
|
self.showErrorPopup(title: "account.create.failed.general.title".localized,
|
|
description: "account.create.failed.general.description".localized,
|
|
button: "account.create.failed.general.button".localized)
|
|
}
|
|
}, receiveValue: { wallet in
|
|
if !newWallet.isExist { newWallet = wallet }
|
|
})
|
|
|
|
} catch WalletError.dailyLimit {
|
|
self.showErrorPopup(title: "account.create.failed.daily.title".localized,
|
|
description: "account.create.failed.daily.description".localized,
|
|
button: "account.create.failed.daily.button".localized)
|
|
} catch WalletError.deviceLimit {
|
|
self.showErrorPopup(title: "account.create.failed.device.title".localized,
|
|
description: "account.create.failed.device.description".localized,
|
|
button: "account.create.failed.device.button".localized)
|
|
} catch WalletError.exists {
|
|
self.showErrorPopup(title: "account.create.failed.exists.title".localized,
|
|
description: "account.create.failed.exists.description".localized,
|
|
button: "account.create.failed.exists.button".localized)
|
|
} catch {
|
|
self.showErrorPopup(title: "account.create.failed.general.title".localized,
|
|
description: "account.create.failed.general.description".localized,
|
|
button: "account.create.failed.general.button".localized)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
@IBAction func onGenerateKeyClicked(_ sender: Any) {
|
|
self.walletService.generateKey()
|
|
privateKeyView.text = self.walletService.walletPrivateKey
|
|
}
|
|
|
|
@IBAction func onUseOwnKeyClicked(_ sender: Any) {
|
|
let view = AccountOnboardingAddPrivateKey()
|
|
view.onSubmit = { key in
|
|
|
|
self.walletService.update(key: key)
|
|
self.privateKeyView.text = self.walletService.walletPrivateKey
|
|
|
|
Popup.hide()
|
|
// TODO: - need remove ?? ""
|
|
Popup.show(title: L10n.Account.Onboarding.Keys.Use.Own.added, views: [
|
|
.field(title: L10n.Account.Onboarding.Username.accountNameTitle, text: self.walletService.walletName),
|
|
.field(title: L10n.Account.Onboarding.Keys.Use.Own.privateKeyAdded, text: key.privateKey ?? "")
|
|
], submit: L10n.Common.Button.continue, in: self)
|
|
}
|
|
|
|
Popup.show(content: CommonViewControllerViewPopUp(
|
|
title: L10n.Account.Onboarding.Enter.Private.key,
|
|
description: L10n.Account.Onboarding.Keys.Use.Own.description,
|
|
view: view
|
|
))
|
|
}
|
|
|
|
@IBAction func onCheckbox(_ sender: Any) {
|
|
self.signalView.alpha = 0
|
|
if self.checkboxButton.image(for: .normal) == Asset.checkboxOff.image {
|
|
self.checkboxButton.setImage(Asset.checkboxOn.image, for: .normal)
|
|
self.submitBtn.isEnabled = true
|
|
} else {
|
|
self.checkboxButton.setImage(Asset.checkboxOff.image, for: .normal)
|
|
self.submitBtn.isEnabled = false
|
|
}
|
|
}
|
|
|
|
@IBAction func haveWalletPrssed(_: AnyObject?) {
|
|
let ctrl = UINavigationController(rootViewController: StoryboardScene.Account.manual.instantiate())
|
|
self.navigationController?.pushViewController(ctrl, animated: true)
|
|
}
|
|
|
|
private func showErrorPopup(title: String, description: String, button: String, navigate: WalletNavigation? = nil) {
|
|
|
|
let views: [UIView] = [
|
|
.field(text: description, textStyle: "regular_16")
|
|
]
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
guard let self = self else { return }
|
|
|
|
Popup.hide(in: self) {
|
|
Popup.show(title: title,
|
|
views: views,
|
|
submit: button,
|
|
submitStyle: .primary, in: self) { ctrl in
|
|
|
|
ctrl.dismiss(animated: true)
|
|
self.processAccountCreation(navigation: navigate)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func processAccountCreation(navigation: WalletNavigation?) {
|
|
switch navigation {
|
|
case .account(let wallet):
|
|
self.dismiss(animated: true) { [weak self] in
|
|
guard let self = self else { return }
|
|
let vc = AccountsListController()
|
|
self.mainController.content.push(vc, animated: false)
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|