Files
raspberry/iOS/Wallet/Sources/Inheritance/Controller/InheritanceControllerSetup.swift

110 lines
4.2 KiB
Swift

//
// InheritanceControllerSetup.swift
// Wallet
//
// Created by Igor on 15.03.2021.
// Copyright © 2021 AM. All rights reserved.
//
import UIKit
import WalletFoundation
class InheritanceControllerSetup: UIViewController {
@IBOutlet private weak var headerLbl: UILabel!
@IBOutlet private weak var periodTxtFld: CommonTextField!
@IBOutlet private weak var addBtn: UIButton!
@IBOutlet private weak var stackView: UIStackView!
@IBOutlet private weak var submitBtn: UIButton!
var service: Inheritance.Service.Setup!
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Inheritance.Setup.title
navigationItem.leftBarButtonItem = .pop(self)
switch service.contract {
case .cash:
headerLbl.attributedText = L10n.Inheritance.Setup.header(L10n.Inheritance.Menu.cash)
.attributed(style: .bold, size: 24, color: Asset.textCoal.color)
case .list:
headerLbl.attributedText = L10n.Inheritance.Setup.header(L10n.Inheritance.Menu.list)
.attributed(style: .bold, size: 24, color: Asset.textCoal.color)
case .swap:
headerLbl.attributedText = L10n.Inheritance.Setup.header(L10n.Inheritance.Menu.swap)
.attributed(style: .bold, size: 24, color: Asset.textCoal.color)
default:
return
}
periodTxtFld.keyboardType = .numberPad
stackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
service.collection.forEach({ stackView.addArrangedSubview(InheritanceViewShare(share: $0, delete: delete())) })
service.didReload = { [weak self] in self?.reload(animated: true) }
service.didUpdate = { [weak self] in self?.update(animated: true) }
reload(animated: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Accounts().isLocked = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
private func reload(animated: Bool) {
periodTxtFld.set(value: service.period > 0 ? "\(service.period)" : "", animated: animated)
var shares = stackView.arrangedSubviews.compactMap({ $0 as? InheritanceViewShare })
if shares.count > service.collection.count {
shares.filter({ view in !service.collection.contains(where: { $0.uuid == view.share.uuid }) }).forEach({ $0.removeFromSuperview() })
} else if shares.count < service.collection.count {
service.collection.last >>- {
stackView.addArrangedSubview(InheritanceViewShare(share: $0, delete: delete()))
}
}
shares = stackView.arrangedSubviews.compactMap({ $0 as? InheritanceViewShare })
shares.enumerated().forEach({ $0.element.share = self.service.collection[$0.offset] })
shares.enumerated().forEach({ $0.element.showDelete = $0.offset != 0 })
update(animated: animated)
}
private func update(animated: Bool) {
addBtn.isEnabled = service.canAdd
submitBtn.isEnabled = service.isValid
stackView.arrangedSubviews.compactMap({ $0 as? InheritanceViewShare }).forEach({ $0.update(error: service.errors[.percent]) })
view.animateIfNeeded(duration: animated ? .medium : .none)
}
private func delete() -> (Inheritance.Model.Share) -> Void {{ share in
Alert.system(text: L10n.Inheritance.Setup.delete(share.account), actions: [.delete { self.service.delete(share: share) }, .cancel], in: self)
}}
@IBAction func onAdd(_: AnyObject?) {
service.add()
}
@IBAction func onSubmit(_: AnyObject?) {
view.endEditing(true)
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show()
self.service.submit(privateKey: $0) {
Loader.hide()
Alert.notify($0, pop: self)
}
}
}
}
extension InheritanceControllerSetup: CommonTextFieldDelegate {
func textFieldDidChange(_ textField: CommonTextField) {
service.period = Int(textField.value) ?? 0
textField.error = service.errors[.period]
}
}