// // AccountControllerConfirmChangeKey.swift // Wallet // // Created by Alexandr Serpokrylow on 18.02.2022. // Copyright © 2022 AM. All rights reserved. // import UIKit final class AccountControllerConfirmChangeKey: UIViewController { @IBOutlet weak var warningView: CommonViewWarning! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var signalView: UIView! @IBOutlet weak var checkBoxButton: UIButton! @IBOutlet weak var confirmButton: CommonButtonAction! @IBOutlet weak var cancelButton: CommonButtonAction! private let service = Account.Service.ChangeKey() var changeKeyHasBeenProcessed: (() -> Void)? var collection: [ChangeKeyModel] = [] override func viewDidLoad() { super.viewDidLoad() title = L10n.Account.Onboarding.Changing.Private.key if navigationController != nil { navigationItem.leftBarButtonItem = .pop(self) navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() navigationController?.navigationBar.isTranslucent = true } self.configureView() } private func configureView() { self.warningView.makeCritical() self.tableView.delegate = self self.tableView.dataSource = self self.tableView.register(cell: ConfirmChangeKeyCell.self) self.checkBoxButton.setImage(Asset.checkboxOff.image, for: .normal) self.confirmButton.isEnabled = true } @IBAction func onCheckBoxClicked(_ sender: Any) { if self.checkBoxButton.image(for: .normal) == Asset.checkboxOff.image { self.checkBoxButton.setImage(Asset.checkboxOn.image, for: .normal) } else { self.checkBoxButton.setImage(Asset.checkboxOff.image, for: .normal) } } @IBAction func onConfirmClicked(_ sender: Any) { if self.checkBoxButton.image(for: .normal) == Asset.checkboxOff.image { UIView.animate(withDuration: 0.3, animations: { self.signalView.alpha = 1 }) DispatchQueue.main.asyncAfter(deadline: .now() + 1.5, execute: { UIView.animate(withDuration: 0.3, animations: { self.signalView.alpha = 0 }) }) return } AccountViewAuthorize.showGetPassword(in: self, showPinBackButton: true, { password in Loader.show() self.service.change(for: self.collection, using: password) { [weak self] result in Loader.hide() guard let self else { return } DispatchQueue.main.async { switch result { case let .success(transitionID): self.showSuccessfulPopup(transactionId: transitionID) { self.dismiss(animated: true) self.changeKeyHasBeenProcessed?() } case let .failure(error): Alert.error(error) } } } }) } @IBAction func onCancelClicked(_ sender: Any) { dismiss(animated: true, completion: nil) } } extension AccountControllerConfirmChangeKey: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { self.collection.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 230 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(ConfirmChangeKeyCell.self, indexPath: indexPath) let account = self.collection[indexPath.row] cell.configureView(account: account) return cell } } extension AccountControllerConfirmChangeKey { fileprivate func showSuccessfulPopup(transactionId: String, _ completion: @escaping () -> Void) { let views: [UIView] = [ .field( title: L10n.Swap.Main.Success.id, text: transactionId, rightButtonAvailbale: true, rightButtonImage: Asset.commonCopy.image, rightButtonAction: { Alert.copy(transactionId) } ) ] let description = "\(L10n.Account.Onboarding.Keys.Changed.description) (\(self.collection.count))" Popup.show( title: L10n.Account.Onboarding.Keys.Changed.title, description: description, views: views, submit: L10n.Common.Button.checkOnBloksIO, submitStyle: .outline, cancel: L10n.Common.Button.done, in: self, { _ in guard let checkTransactionUrl = URL(transactionId: transactionId) else { return } UIApplication.shared.open(checkTransactionUrl, options: [:], completionHandler: nil) }, { completion() }) } }