197 lines
8.2 KiB
Swift
197 lines
8.2 KiB
Swift
//
|
|
// AccountsListController.swift
|
|
// Malinka
|
|
//
|
|
// Created by user on 26.08.2022.
|
|
// Copyright © 2022 NUT.Tech. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import WalletFoundation
|
|
|
|
enum AccountStatus: Equatable {
|
|
case accepted(selected: Bool)
|
|
case creating(selected: Bool)
|
|
case pending
|
|
case declined
|
|
|
|
init(account: Account.Model) {
|
|
let selected = account.username == Accounts().current?.username
|
|
|
|
switch account.state {
|
|
case .accepted:
|
|
self = .accepted(selected: selected)
|
|
case .creating:
|
|
self = .creating(selected: selected)
|
|
case .pending:
|
|
self = .pending
|
|
case .declined:
|
|
self = .declined
|
|
}
|
|
}
|
|
}
|
|
|
|
final class AccountsListController: UIViewController {
|
|
|
|
private enum Constants {
|
|
enum Geometry {
|
|
static let fontSize: CGFloat = 14
|
|
static let buttonCornerRadius: CGFloat = 5
|
|
static let buttonHeight: CGFloat = 48
|
|
static let buttonMargin: CGFloat = 12
|
|
static let buttonTitlePadding: CGFloat = 5
|
|
static let stackMargin: CGFloat = 16
|
|
}
|
|
}
|
|
|
|
private lazy var scrollView: UIScrollView = {
|
|
let scrollview = UIScrollView()
|
|
scrollview.translatesAutoresizingMaskIntoConstraints = false
|
|
return scrollview
|
|
}()
|
|
|
|
private lazy var addAccountButton: UIButton = {
|
|
let button = UIButton()
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
button.setTitle(L10n.Main.Account.add, for: .normal)
|
|
button.setTitleColor(Asset.deepWater.color, for: .normal)
|
|
button.setImage(Asset.chatsAddPlus.image, for: .normal)
|
|
button.clipsToBounds = true
|
|
if let font = FontFamily.GolosUI.medium.font(size: Constants.Geometry.fontSize) {
|
|
button.titleLabel?.font = font
|
|
}
|
|
button.layer.cornerRadius = Constants.Geometry.buttonCornerRadius
|
|
let borderColor = Asset.deepWater.color.withAlphaComponent(0.2)
|
|
button.layer.borderColor = borderColor.cgColor
|
|
button.layer.borderWidth = 1.0
|
|
button.contentEdgeInsets = UIEdgeInsets(top: 0,
|
|
left: 0,
|
|
bottom: 0,
|
|
right: Constants.Geometry.buttonTitlePadding)
|
|
button.titleEdgeInsets = UIEdgeInsets(top: 0,
|
|
left: Constants.Geometry.buttonTitlePadding,
|
|
bottom: 0,
|
|
right: -Constants.Geometry.buttonTitlePadding)
|
|
return button
|
|
}()
|
|
|
|
private lazy var accountsStack: UIStackView = {
|
|
let stackView = UIStackView()
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
stackView.axis = .vertical
|
|
stackView.spacing = Constants.Geometry.stackMargin
|
|
return stackView
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.title = L10n.Main.Account.title
|
|
self.navigationItem.leftBarButtonItem = .pop(self, { self.dismiss(animated: true) })
|
|
self.setupViews()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
Accounts().isLocked = true
|
|
self.navigationController >>- {
|
|
$0.navigationBar.isTranslucent = false
|
|
$0.navigationBar.backgroundColor = Asset.snow.color
|
|
$0.setNavigationBarHidden(false, animated: true)
|
|
}
|
|
self.configureView()
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
super.viewWillDisappear(animated)
|
|
Accounts().isLocked = false
|
|
}
|
|
|
|
private func setupViews() {
|
|
self.view.backgroundColor = Asset.snow.color
|
|
self.scrollView.addSubview(self.accountsStack)
|
|
self.view.addSubview(self.scrollView)
|
|
self.view.addSubview(self.addAccountButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
self.scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
|
|
self.scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
|
|
self.scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
|
|
|
|
self.accountsStack.topAnchor.constraint(equalTo: self.scrollView.topAnchor),
|
|
self.accountsStack.leftAnchor.constraint(equalTo: self.scrollView.leftAnchor, constant: Constants.Geometry.stackMargin),
|
|
self.accountsStack.rightAnchor.constraint(equalTo: self.scrollView.rightAnchor, constant: -Constants.Geometry.stackMargin),
|
|
self.accountsStack.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
|
|
self.accountsStack.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor, constant: -2 * Constants.Geometry.stackMargin),
|
|
|
|
self.addAccountButton.heightAnchor.constraint(equalToConstant: Constants.Geometry.buttonHeight),
|
|
self.addAccountButton.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Constants.Geometry.buttonMargin),
|
|
self.addAccountButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Constants.Geometry.buttonMargin),
|
|
self.addAccountButton.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor, constant: Constants.Geometry.buttonMargin),
|
|
self.addAccountButton.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -Constants.Geometry.buttonMargin)
|
|
])
|
|
|
|
self.addAccountButton.addTarget(self, action: #selector(addAccountPressed), for: .touchUpInside)
|
|
}
|
|
|
|
private func configureView() {
|
|
|
|
self.configureLeftBarButtonItem()
|
|
self.accountsStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
|
|
Accounts().collection.forEach { account in
|
|
let status = AccountStatus(account: account)
|
|
|
|
let accountItem = AccountListCard(title: account.username,
|
|
keyType: account.keyType,
|
|
status: status) { [unowned self] in
|
|
AccountController.showPrivateKeyPopup(in: self, account: account)
|
|
} deleteAction: { [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
|
|
)
|
|
} refreshBlock: { [unowned self] in
|
|
UIView.animate(withDuration: .slowest) {
|
|
self.accountsStack.arrangedSubviews
|
|
.compactMap({ $0 as? AccountListCard })
|
|
.forEach {
|
|
switch $0.status {
|
|
case .accepted(selected: true):
|
|
$0.status = .accepted(selected: false)
|
|
case .creating(selected: true):
|
|
$0.status = .creating(selected: false)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
self.view.layoutIfNeeded()
|
|
Accounts().current = account
|
|
self.mainController.content.pop(animated: true)
|
|
}
|
|
}
|
|
|
|
self.accountsStack.addArrangedSubview(accountItem)
|
|
}
|
|
self.scrollView.layoutIfNeeded()
|
|
}
|
|
|
|
private func configureLeftBarButtonItem() {
|
|
guard let leftBarButtonItem = self.navigationItem.leftBarButtonItem else { return }
|
|
leftBarButtonItem.isEnabled = Accounts().hasActiveAccounts
|
|
leftBarButtonItem.image?.withTintColor(Accounts().hasActiveAccounts ? Asset.dark.color : Asset.disabled.color)
|
|
}
|
|
|
|
@objc
|
|
private func addAccountPressed() {
|
|
AccountController.showPopup(in: self)
|
|
}
|
|
}
|