319 lines
11 KiB
Swift
319 lines
11 KiB
Swift
//
|
|
// OnboardingControllerManual.swift
|
|
// List
|
|
//
|
|
// Created by Igor Danich on 22.07.2020.
|
|
// Copyright © 2020 Igor Danich. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import EosioSwift
|
|
import WalletKit
|
|
import Combine
|
|
import WalletUIComponents
|
|
|
|
final class AccountControllerManual: UIViewController {
|
|
|
|
struct AccountModelAdd {
|
|
let purse: Purse
|
|
var isAdded: Bool = false
|
|
var isChecked: Bool = true
|
|
}
|
|
|
|
@IBOutlet private weak var textField: CommonTextField!
|
|
@IBOutlet private weak var submitBtn: UIButton!
|
|
@IBOutlet weak var tableView: UITableView!
|
|
@IBOutlet weak var containerView: CommonViewContainer!
|
|
|
|
var privateKeyHasBeenProcessed: (() -> Void)?
|
|
|
|
private var accounts = [AccountModelAdd]()
|
|
private var accountsToShow = [AccountModelAdd]()
|
|
|
|
private let viewModel = TextFieldViewModel(placeholder: L10n.Account.Manual.search)
|
|
|
|
private var header: SearchAccountView { SearchAccountView(viewModel: self.viewModel) }
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
self.title = L10n.Account.Manual.title
|
|
self.image = Asset.accountManualLogo.image
|
|
|
|
if let navigationController = self.navigationController {
|
|
|
|
self.navigationItem.leftBarButtonItem = .pop(self, {
|
|
if !Accounts().collection.isEmpty { $0.dismiss(animated: true) }
|
|
})
|
|
|
|
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
navigationController.navigationBar.shadowImage = UIImage()
|
|
navigationController.navigationBar.isTranslucent = true
|
|
}
|
|
|
|
self.tableView.separatorStyle = .none
|
|
self.tableView.showsVerticalScrollIndicator = false
|
|
self.tableView.delegate = self
|
|
self.tableView.dataSource = self
|
|
self.tableView.register(cell: SearchAccountCell.self)
|
|
|
|
self.textField.delegate = self
|
|
|
|
self.submitBtn.isEnabled = false
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
self.navigationController?.setNavigationBarHidden(false, animated: true)
|
|
|
|
self.viewModel.objectWillChange
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] in
|
|
guard let self else { return }
|
|
self.textFieldDidChange(self.viewModel.text)
|
|
if self.viewModel.shouldClear { self.textFieldShouldClear() }
|
|
}
|
|
.store(in: &self.cancellables)
|
|
}
|
|
|
|
private func textFieldShouldClear() {
|
|
self.accountsToShow = self.accounts
|
|
self.tableView.reloadData()
|
|
}
|
|
|
|
private func textFieldDidChange(_ text: String?) {
|
|
let text = text ?? ""
|
|
self.accountsToShow = text.isEmpty ? self.accounts : self.accounts.filter { $0.purse.name.starts(with: text) }
|
|
self.tableView.reloadData()
|
|
}
|
|
|
|
private func fetchPrivateKey() {
|
|
|
|
Loader.show(in: self)
|
|
|
|
let keyString = self.textField.value
|
|
self.textField.value = ""
|
|
|
|
Task {
|
|
|
|
do {
|
|
let keys = try WalletKey.create(private: keyString)
|
|
let holder = try await Accounts().bank.restore(using: keys)
|
|
|
|
if holder.purses.count == 0 {
|
|
throw PurseHolder.PurseError.empty
|
|
}
|
|
|
|
let existed = Accounts().collection
|
|
let accounts: [AccountModelAdd] = holder.purses.compactMap { purse in
|
|
let isAdded = existed.contains(where: { $0.name == purse.name && $0.keyType == purse.keyType })
|
|
return AccountModelAdd(purse: purse, isAdded: isAdded)
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
Loader.hide(in: self)
|
|
|
|
self.textField.value = keyString
|
|
|
|
self.accounts = accounts
|
|
self.accountsToShow = accounts
|
|
self.containerView.isHidden = false
|
|
self.tableView.reloadData()
|
|
}
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
Loader.hide(in: self)
|
|
|
|
self.containerView.isHidden = true
|
|
|
|
switch error {
|
|
case WalletKeyError.invalidPrivate: Alert.error(L10n.Error.Accounts.keyInvalid)
|
|
case PurseHolder.PurseError.empty: Alert.error(L10n.Error.Accounts.empty)
|
|
default: break
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private func readQrData(string: String) {
|
|
self.submitBtn.isEnabled = string.count > 0
|
|
self.textField.value = string
|
|
self.fetchPrivateKey()
|
|
}
|
|
|
|
@IBAction private func onQR(_ : AnyObject?) {
|
|
|
|
guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else {
|
|
let ctrl = ScannerViewController()
|
|
ctrl.navigationItem.leftBarButtonItem = .close { [weak self] in self?.dismiss(animated: true) }
|
|
ctrl.didCapture = { [weak self] string in
|
|
self?.dismiss(animated: true)
|
|
self?.readQrData(string: string)
|
|
}
|
|
|
|
let navCtrl = UINavigationController(rootViewController: ctrl)
|
|
navCtrl.modalPresentationStyle = .fullScreen
|
|
self.present(navCtrl, animated: true)
|
|
return
|
|
}
|
|
|
|
Alert.system(
|
|
actions: [
|
|
.init(title: L10n.Wallet.Send.qrCamera, style: .default) { [weak self] _ in
|
|
guard let self else { return }
|
|
let ctrl = ScannerViewController()
|
|
ctrl.navigationItem.leftBarButtonItem = .close(self)
|
|
ctrl.didCapture = { [weak self] value in
|
|
self?.dismiss(animated: true)
|
|
self?.readQrData(string: value)
|
|
}
|
|
let navCtrl = UINavigationController(rootViewController: ctrl)
|
|
navCtrl.modalPresentationStyle = .fullScreen
|
|
UIViewController.topController().present(navCtrl, animated: true)
|
|
},
|
|
.init(title: L10n.Wallet.Send.qrLibrary, style: .default) { [weak self] _ in
|
|
guard let self else { return }
|
|
let ctrl = UIImagePickerController()
|
|
ctrl.delegate = self
|
|
ctrl.sourceType = .photoLibrary
|
|
UIViewController.topController().present(ctrl, animated: true)
|
|
}
|
|
],
|
|
style: .actionSheet,
|
|
in: self
|
|
)
|
|
}
|
|
|
|
@IBAction private func onSubmit(_ : AnyObject?) {
|
|
|
|
let accountsToAdd = Dictionary(grouping: accounts.filter { $0.isChecked && !$0.isAdded },
|
|
by: { $0.purse.keyType })
|
|
|
|
Loader.show(in: self)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
|
|
|
for (_, accounts) in accountsToAdd {
|
|
|
|
do {
|
|
if let password = Account.Service.Authorize.shared.password {
|
|
try Accounts().addAccounts(purses: accounts.map(\.purse), password: password)
|
|
}
|
|
Loader.hide(in: self)
|
|
} catch {
|
|
Loader.hide(in: self)
|
|
Alert.error(error)
|
|
}
|
|
}
|
|
|
|
self.dismiss(animated: true)
|
|
self.privateKeyHasBeenProcessed?()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AccountControllerManual: UITableViewDelegate, UITableViewDataSource {
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return accountsToShow.count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
return 52
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
return UITableView.automaticDimension
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
|
return self.header
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
submitBtn.isEnabled = accounts.contains(where: { $0.isChecked && !$0.isAdded })
|
|
|
|
let cell = tableView.dequeueReusableCell(SearchAccountCell.self, indexPath: indexPath)
|
|
let account = accountsToShow[indexPath.row]
|
|
cell.configureView(account: account)
|
|
|
|
return cell
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
let accountToChange = accountsToShow[indexPath.row]
|
|
guard let index = accounts.firstIndex(where: {
|
|
$0.purse.name == accountToChange.purse.name &&
|
|
$0.purse.keyType == accountToChange.purse.keyType
|
|
}) else { return }
|
|
accountsToShow[indexPath.row].isChecked = !accountsToShow[indexPath.row].isChecked
|
|
accounts[index].isChecked = !accounts[index].isChecked
|
|
|
|
tableView.reloadRows(at: [indexPath], with: .automatic)
|
|
submitBtn.isEnabled = accounts.contains(where: { $0.isChecked && !$0.isAdded })
|
|
}
|
|
}
|
|
|
|
extension AccountControllerManual: CommonTextFieldDelegate {
|
|
func textFieldDidChange(_ textField: CommonTextField) {
|
|
if textField.value.count == Constants.maxPrivateKeyTextCount {
|
|
Loader.show(in: self)
|
|
fetchPrivateKey()
|
|
}
|
|
}
|
|
|
|
func textField(_ textField: CommonTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
|
let currentText = textField.value
|
|
|
|
guard let stringRange = Range(range, in: currentText) else { return false }
|
|
|
|
let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
|
|
let shouldChange = updatedText.count <= Constants.maxPrivateKeyTextCount
|
|
|
|
return shouldChange
|
|
}
|
|
}
|
|
|
|
extension AccountControllerManual {
|
|
enum Constants {
|
|
static let maxPrivateKeyTextCount = 51
|
|
}
|
|
}
|
|
|
|
extension AccountControllerManual: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
|
|
|
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
|
|
|
picker.dismiss(animated: true)
|
|
|
|
guard
|
|
let selectedImage = info[.originalImage] as? UIImage,
|
|
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]),
|
|
let ciImage = CIImage(image: selectedImage) else {
|
|
return
|
|
}
|
|
|
|
var qrCodeLink = ""
|
|
for feature in (detector.features(in: ciImage) as? [CIQRCodeFeature]) ?? [] {
|
|
qrCodeLink += feature.messageString ?? ""
|
|
}
|
|
|
|
print(qrCodeLink) // Your result from QR Code
|
|
|
|
if !qrCodeLink.isEmpty {
|
|
self.readQrData(string: qrCodeLink)
|
|
} else {
|
|
Alert.error(L10n.Main.Qr.notValidError)
|
|
}
|
|
}
|
|
}
|