Files
Jura Shikin 1eff29e3b5 Merge branch 'feature/MALINKA-663/Incorrect_switch_to_free_transactions' into 'develop'
MALINKA-663: Reorganised logic of switching transactions type while buying RAM...

See merge request raspberry/mobile/ios!140
2022-12-06 23:40:16 +03:00

113 lines
3.4 KiB
Swift

//
// ResourcesControllerPopup.swift
// Wallet
//
// Created by Igor on 11.02.2021.
// Copyright © 2021 AM. All rights reserved.
//
import UIKit
enum QuotaType: Int {
case freeTransactions
case accountResources
}
class ResourcesControllerPopup: UIViewController, ResourcesControllerPopupStatisticsDelegate {
@IBOutlet private var quotasContainerStackView: UIStackView!
@IBOutlet private weak var quotaView: UIView!
@IBOutlet private weak var statisticsView: UIView!
@IBOutlet private weak var selectedResourcesRadioBattonsGroupView: CommonViewRadioGroup?
private let account = Accounts().current?.name
private var quota: ResourcesControllerPopupQuota { child()! }
private lazy var statistics: ResourcesControllerPopupStatistics = {
let vc: ResourcesControllerPopupStatistics = child()!
vc.delegate = self
return vc
}()
private var quotaType: QuotaType?
override func viewDidLoad() {
super.viewDidLoad()
self.selectedResourcesRadioBattonsGroupView?.collection = [L10n.Resources.Popup.Transactions.title, L10n.Resources.Popup.Statistics.title]
if let account {
self.quota.service = .init(account: account)
self.statistics.service = .init(account: account)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.preload()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.bag.flush()
}
func selectQuota(type: QuotaType) {
if type != self.quotaType {
self.quotaType = type
}
if let radio = self.selectedResourcesRadioBattonsGroupView {
radio.selectedIndex = type.rawValue
}
}
private func preload() {
Loader.show(in: self)
self.quotasContainerStackView.alpha = 0
let group = DispatchGroup()
group.enter()
self.quota.service.fetch { _ in
group.leave()
}
group.enter()
self.statistics.service.fetch { _ in
group.leave()
}
group.notify(queue: .main) {
Loader.hide(in: self)
if let quota = self.quotaType {
self.selectQuota(type: quota)
} else {
self.updateQuotaRadioGroup()
}
UIView.animate(withDuration: 0.3) {
self.quotasContainerStackView.alpha = 1
}
}
}
private func addObserver() {
Notification.subscribe(bag: self.bag, name: .didUpdateAccountQuota) { [weak self] _ in
self?.updateQuotaRadioGroup()
}
}
@IBAction func onResourcesSelect(_ sender: Any) {
let isEnabled = self.selectedResourcesRadioBattonsGroupView?.selectedIndex == 0
self.quotaType = isEnabled ? .freeTransactions : .accountResources
Accounts().quota.isEnabled = isEnabled
}
private func updateQuotaRadioGroup() {
let quotaType: QuotaType = Accounts().quota.isEnabled ? .accountResources
: .freeTransactions
self.selectQuota(type: quotaType)
}
internal func sendSubmit() {
if let type = self.quotaType {
Accounts().quota.isEnabled = type == .freeTransactions
}
self.dismiss(animated: true)
Popup.hide()
}
}