Files

62 lines
1.8 KiB
Swift

//
// CommonMenu.swift
// Wallet
//
// Created by Igor on 27.02.2021.
// Copyright © 2021 AM. All rights reserved.
//
import UIKit
protocol CommonMenuMappable {
func toMenu() -> Common.Model.Menu
}
extension Array where Element: CommonMenuMappable {
func index(of menu: Common.Model.Menu) -> Int? { index(of: menu.uuid) }
func index(of uuid: String) -> Int? { firstIndex(where: { $0.toMenu().uuid == uuid }) }
func object(menu: Common.Model.Menu) -> Element? { object(uuid: menu.uuid) }
func object(uuid: String) -> Element? { first(where: { $0.toMenu().uuid == uuid }) }
}
protocol CommonMenuSender: AnyObject {
var action: CommonMenuAction? { get }
func update(animated: Bool)
}
class CommonMenuAction {
let icon: UIImage?
let empty: Common.Model.Menu?
var selected: Common.Model.Menu?
var menu: Common.Model.Menu? { selected ?? empty }
private let action: ((CommonMenuSender) -> Void)?
init(
icon: UIImage? = nil,
empty: Common.Model.Menu? = nil,
selected: Common.Model.Menu? = nil,
action: ((CommonMenuSender) -> Void)? = nil
) {
self.icon = icon
self.empty = empty
self.selected = selected
self.action = action
}
func perform(sender: CommonMenuSender) { action?(sender) }
}
extension CommonMenuAction {
static func action(
title: String,
text: String? = nil,
action: ((CommonMenuSender) -> Void)? = nil
) -> CommonMenuAction { .init(empty: .menu(title: title, text: text), action: action) }
static func action(
icon: UIImage? = nil,
empty: Common.Model.Menu? = nil,
selected: Common.Model.Menu? = nil,
action: ((CommonMenuSender) -> Void)? = nil
) -> CommonMenuAction { .init(icon: icon, empty: empty, selected: selected, action: action) }
}