// // MainControllerAccountsPopup.swift // List // // Created by Saveliy Stavitsky on 7/16/20. // Copyright © 2020 Igor Danich. All rights reserved. // import UIKit class AccountsSelectionViewController: UIViewController { @IBOutlet weak var mainStackView: UIStackView! @IBOutlet var scrollView: UIScrollView! @IBOutlet var stackView: UIStackView! @IBOutlet var addAccountButton: UIButton! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) Accounts().isLocked = true navigationController?.setNavigationBarHidden(false, animated: true) configureView() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) Accounts().isLocked = false } override func viewDidLoad() { super.viewDidLoad() title = L10n.Main.Account.title navigationItem.leftBarButtonItem = .pop(self, { self.dismiss(animated: true) }) } func configureView() { stackView.arrangedSubviews.forEach({ $0.removeFromSuperview() }) for account in Accounts().collection { let accountButton = AccountViewSelect() accountButton.title = account.username accountButton.keyType = "@\(account.keyType)" accountButton.didTapRemove = { [unowned self]_ in Alert.system( text: L10n.Main.Account.remove, actions: [ .yes { AccountViewAuthorize.showGetPrivateKey(in: self) { _ in Accounts().remove(username: account.username, keyType: account.keyType) self.configureView() } }, .no ], in: self ) } accountButton.didTapReveal = { [unowned self]_ in AccountController.showPrivateKeyPopup(in: self, account: account) } accountButton.completion = { [unowned self] accountButton in UIView.animate(withDuration: .slowest) { self.stackView.arrangedSubviews .compactMap({ $0 as? AccountViewSelect }) .forEach({ $0.isActive = false }) accountButton.isActive = true self.view.layoutIfNeeded() } Accounts().current = account mainController.content.pop(animated: true) } stackView.addArrangedSubview(accountButton) accountButton.isActive = account.username == Accounts().current?.username let currentAccount = Accounts().current let isActiveAccount = account.username == currentAccount?.username && account.keyType == currentAccount?.keyType accountButton.isActive = isActiveAccount } scrollView.layoutIfNeeded() } @IBAction func addAccountPressed(_ sender: Any) { AccountController.showPopup(in: self) } }