Files
2022-12-21 18:22:56 +03:00

105 lines
4.2 KiB
Swift

//
// AccountUIViewController.swift
// List
//
// Created by Igor Danich on 25.06.2020.
// Copyright © 2020 Igor Danich. All rights reserved.
//
import UIKit
import WalletKit
class AccountController: UIViewController {
typealias Completion = (WalletType) -> Void
enum WalletType {
case new
case key
case failed
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Account"
}
}
extension AccountController {
static func showPopup(in viewController: UIViewController, completion: Completion? = nil) {
let ctrl = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Add Private key
ctrl.addAction(.init(title: L10n.Account.Alert.manual, style: .default) { _ in
let manualController = StoryboardScene.Account.manual.instantiate()
let ctrl = UINavigationController(rootViewController: manualController)
manualController.privateKeyHasBeenProcessed = {
ctrl.dismiss(animated: true)
}
ctrl.modalPresentationStyle = .fullScreen
viewController.present(ctrl, animated: true)
completion?(.key)
})
// Create new one
ctrl.addAction(.init(title: L10n.Account.Alert.create, style: .default) { _ in
Task {
do {
let vc = try await AttestateWalletService.createController()
let navVc = UINavigationController(rootViewController: vc)
navVc.view.backgroundColor = .white
navVc.modalPresentationStyle = .fullScreen
DispatchQueue.main.async {
viewController.present(navVc, animated: true)
completion?(.new)
}
} catch {
Popup.show(title: "account.create.error.title".localized,
description: "account.create.error.description".localized,
submit: "account.create.error.button.title".localized,
submitStyle: .primary) { ctrl in
ctrl.dismiss(animated: true, completion: nil)
completion?(.failed)
}
}
}
})
ctrl.addAction(.init(title: L10n.Common.Button.cancel, style: .cancel, handler: nil))
viewController.present(ctrl, animated: true)
}
static func showPrivateKeyPopup(in viewController: UIViewController, wallet: WalletKit.Wallet) {
Alert.system(
actions: [
.init(title: L10n.Main.Account.export, style: .default) { _ in
Account.Service.Authorize.shared.update(password: nil)
AccountViewAuthorize.showGetPrivateKey(in: viewController, description: L10n.Main.Account.exportDescription) {
let ctrl = AccountViewControllerPrivateKey()
ctrl.subView.keyLbl.lzText = $0
ctrl.subView.qrImgView.image = $0.generateQRCode()
viewController.mainController.content.push(ctrl, animated: true)
}
},
.init(title: L10n.Main.Account.change, style: .default) { _ in
Account.Service.Authorize.shared.update(password: nil)
AccountViewAuthorize.showGetPrivateKey(in: viewController, description: L10n.Main.Account.exportDescription) {
let changeKeyController = StoryboardScene.Account.changeKey.instantiate()
changeKeyController.wallet = wallet
changeKeyController.privateKey = $0
let ctrl = UINavigationController(rootViewController: changeKeyController)
ctrl.modalPresentationStyle = .fullScreen
viewController.present(ctrl, animated: true)
}
},
.init(title: L10n.Common.Button.cancel, style: .cancel)
],
style: .actionSheet,
in: viewController
)
}
}