// // CommonMenuSelect.swift // Wallet // // Created by Igor on 27.02.2021. // Copyright © 2021 AM. All rights reserved. // import UIKit #warning("REWRITE THIS *") var navigationGlobalHack: UINavigationController? class CommonMenuSelect: CommonMenuAction { enum Kind { case sheet case cards } let kind: Kind private let completion: ((Common.Model.Menu) -> Void)? private let collection: [Common.Model.Menu] private let title: String? private let update: Bool var selectedItem: Common.Model.Menu? { selected } init( icon: UIImage? = Asset.commonArrowRight.image, kind: Kind = .sheet, title: String? = nil, empty: Common.Model.Menu? = nil, selected: Common.Model.Menu? = nil, collection: [Common.Model.Menu], update: Bool = true, completion: ((Common.Model.Menu) -> Void)? = nil ) { self.kind = kind self.collection = collection self.completion = completion self.title = title self.update = update super.init(icon: icon, empty: empty, selected: selected) } override func perform(sender: CommonMenuSender) { switch kind { case .cards: Popup.show( title: title, views: collection.map({ menu in let card = CommonViewCard(action: .action(icon: menu == sender.action?.selected ? Asset.commonSelected.image : nil, empty: menu) { _ in if self.update { sender.action?.selected = menu sender.update(animated: true) } Popup.hide() self.completion?(menu) }) card.update(animated: true) card.rightAction = { } return card // UIView(height: 150, color: UIColor(red255: CGFloat.random(in: 0..<250), green255: CGFloat.random(in: 0..<250), blue255: CGFloat.random(in: 0..<250), alpha: 1)) }) + (title == L10n.Common.Card.Card.select ? [ UIView.button(style: .outline, title: L10n.Cards.newCard) { [weak self] in let ctrl = StoryboardScene.Cards.card.instantiate() ctrl.completion = { self?.completion?($0.toMenu()) } Popup.hide() navigationGlobalHack?.pushViewController(ctrl, animated: true) } ] : []), spacing: 14 ) case .sheet: Alert.system(title: title, actions: collection.map({ menu in .custom(title: menu.title) { if self.update { sender.action?.selected = menu sender.update(animated: true) } self.completion?(menu) } }) + [.cancel], style: .actionSheet) } } } extension CommonMenuAction { static func select( icon: UIImage? = Asset.commonArrowRight.image, kind: CommonMenuSelect.Kind = .sheet, title: String? = nil, empty: Common.Model.Menu? = nil, collection: [Common.Model.Menu], selected: Common.Model.Menu? = nil, update: Bool = true, completion: ((Common.Model.Menu) -> Void)? = nil ) -> CommonMenuSelect { .init(icon: icon, kind: kind, title: title, empty: empty, selected: selected, collection: collection, update: update, completion: completion) } }