Files
raspberry/iOS/Wallet/Sources/Common/Extension/CommonUIBarButtonItem.swift

87 lines
3.3 KiB
Swift

//
// UIBarButtonItem.swift
// List
//
// Created by Igor Danich on 29.06.2020.
// Copyright © 2020 Igor Danich. All rights reserved.
//
import UIKit
private class BarButtonItem: UIBarButtonItem {
var completion: (() -> Void)?
init(image: UIImage, completion: @escaping () -> Void) {
super.init()
self.completion = completion
self.image = image
target = self
action = #selector(onAction)
}
init(title: String, titleTextAttributes: [NSAttributedString.Key: Any]? = nil, completion: @escaping () -> Void) {
super.init()
self.completion = completion
self.title = title
self.setTitleTextAttributes(titleTextAttributes ?? [
.font: UIFont.font(style: .medium, size: 16),
.foregroundColor: Asset.textDeepWater.color
], for: .normal)
target = self
action = #selector(onAction)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func onAction() {
completion?()
}
}
extension UIBarButtonItem {
static func custom(title: String, titleTextAttributes: [NSAttributedString.Key: Any]? = nil, _ completion: @escaping () -> Void) -> UIBarButtonItem {
let item = BarButtonItem(title: title, completion: completion)
return item
}
static func custom(image: UIImage, _ completion: @escaping () -> Void) -> UIBarButtonItem {
let item = BarButtonItem(image: image, completion: completion)
item.imageInsets.left = 2
return item
}
static func custom(image: ImageAsset, _ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: image.image, completion)
}
static func menu(_ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: Asset.commonMenu.image, completion)
}
static func qr(image: UIImage? = nil, _ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: image ?? Asset.commonQr.image, completion)
}
static func back(_ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: Asset.commonBack.image, completion)
}
static func pop(_ viewController: UIViewController, animated: Bool = true, _ completion: ((UIViewController) -> Void)? = nil) -> UIBarButtonItem {
.back { [weak viewController] in
guard let vc = viewController else { return }
completion?(vc)
vc.navigationController?.popViewController(animated: animated)
}
}
static func popToRoot(_ viewController: UIViewController, animated: Bool = true, _ completion: (() -> Void)? = nil) -> UIBarButtonItem {
.back { [unowned viewController] in
completion?()
viewController.navigationController?.popToRootViewController(animated: animated)
}
}
static func close(_ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: Asset.commonClose.image, completion)
}
static func close(_ viewController: UIViewController, _ completion: (() -> Void)? = nil) -> UIBarButtonItem {
.close { [unowned viewController] in
viewController.dismiss(animated: true)
completion?()
}
}
static func info(_ completion: @escaping () -> Void) -> UIBarButtonItem {
.custom(image: Asset.commonInfo.image, completion)
}
}