Files
2023-01-27 11:23:51 +03:00

102 lines
3.6 KiB
Swift

//
// CardsController.swift
// Wallet
//
// Created by Igor on 15.12.2020.
// Copyright © 2020 AM. All rights reserved.
//
import UIKit
import WalletFoundation
class CardsController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
@IBOutlet private weak var emptyView: CommonViewEmptyContainer!
private let service = Cards.shared
var didSelect: ((Cards.Model) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(emptyView)
title = L10n.Cards.title
navigationItem.leftBarButtonItem = .pop(self)
emptyView.action = .action(empty: .menu(title: L10n.Cards.Empty.submit)) { [weak self]_ in
self?.show()
}
tableView.register(cell: CommonCellCard.self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController >>- {
$0.navigationBar.isTranslucent = false
$0.navigationBar.backgroundColor = Asset.snow.color
$0.setNavigationBarHidden(false, animated: true)
}
Accounts().isLocked = true
reload()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
func reload() {
tableView.reloadData()
emptyView.isEmpty = service.collection.isEmpty
navigationItem.rightBarButtonItem = service.collection.isEmpty ? nil : .init(
customView: .button(style: .outline, title: L10n.Cards.new, image: Asset.commonPlus.image, size: .small) { [weak self] in self?.show() }
)
}
private func show(card: Cards.Model? = nil) {
let ctrl = StoryboardScene.Cards.card.instantiate()
if let card = card { ctrl.service = .init(card: card) }
ctrl.completion = { [weak self] _ in self?.reload() }
navigationController?.pushViewController(ctrl, animated: true)
}
}
extension CardsController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { service.collection.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(CommonCellCard.self, indexPath: indexPath)
cell.cardView.style = .data
cell.cardView.action = .select(
icon: Asset.commonMenuHorizontal.image,
empty: service.collection[indexPath.row].toMenu(),
collection: service.menu(for: service.collection[indexPath.row]).map({ $0.toMenu() }),
update: false
) { [weak self] menu in
guard let self,
let serviceMenu = Cards.Service.Menu(rawValue: menu.uuid) else { return }
switch serviceMenu {
case .edit:
self.show(card: self.service.collection[indexPath.row])
case .primary:
self.service.default = self.service.collection[indexPath.row]
self.reload()
case .delete:
Alert.system(
title: L10n.Common.Button.delete,
text: L10n.Cards.delete(self.service.collection[indexPath.row].number),
actions: [
.yes {
self.service.delete(card: self.service.collection[indexPath.row])
self.reload()
},
.cancel
],
in: self
)
}
}
return cell
}
}