Files
raspberry/iOS/Wallet/Sources/Common/Popup/CommonPopup.swift
T

116 lines
4.1 KiB
Swift

//
// CommonPopup.swift
// Wallet
//
// Created by Igor on 23.03.2021.
// Copyright © 2021 AM. All rights reserved.
//
import UIKit
enum Popup {
@discardableResult static func show(
title: String? = nil,
text: String? = nil,
image: UIImage? = nil,
description: String? = nil,
views: [UIView] = [],
buttons: [CommonButtonAction] = [],
buttonsActions: [((UIViewController) -> Void)?] = [],
spacing: CGFloat = 12,
in parent: UIViewController = .topController(),
animated: Bool = true,
_ completion: (() -> Void)? = nil
) -> UIViewController {
var subviews = [UIView]()
if let image = image {
let imgView = UIImageView(image: image)
imgView.contentMode = .center
subviews.append(imgView)
}
if let description = description {
subviews.append(.label(text: description, style: .regular, size: 14, color: Asset.textGranite.color, numberOfLines: 0))
}
for (index, view) in views.enumerated() {
if index != 0 {
subviews.append(.view(height: spacing))
}
subviews.append(view)
}
var ctrl: UIViewController!
if buttons.count > 0 {
subviews.append(.view(height: 24))
for (index, button) in buttons.enumerated() {
if index != 0 {
subviews.append(.view(height: 16))
}
if index < buttonsActions.count, let action = buttonsActions[index] {
button.action = .action() { [weak button] _ in
button?.isUserInteractionEnabled = false
action(ctrl)
}
} else {
button.action = .action() { _ in Popup.hide(in: parent) }
}
subviews.append(button)
}
}
subviews.append(.view(height: 12))
ctrl = CommonControllerPopup(title: title, text: text, views: subviews, spacing: 0, completion: completion)
parent.present(ctrl, animated: true)
return ctrl
}
@discardableResult
static func show(
title: String? = nil,
text: String? = nil,
image: UIImage? = nil,
description: String? = nil,
views: [UIView] = [],
submit: String? = nil,
submitStyle: UIButton.Style = .primary,
cancel: String? = nil,
cancelStyle: UIButton.Style = .primary,
cancelSize: UIButton.Size = .large,
submitEnabled: Bool = true,
spacing: CGFloat = 12,
in parent: UIViewController = .topController(),
animated: Bool = true,
_ submitCompletion: ((UIViewController) -> Void)? = nil,
_ completion: (() -> Void)? = nil
) -> UIViewController {
var buttons: [CommonButtonAction] = []
var buttonsActions: [((UIViewController) -> Void)?] = []
if let submit = submit {
buttons.append(.button(style: submitStyle, title: submit, isEnabled: submitEnabled, action: {}))
buttonsActions.append(submitCompletion)
}
if let cancel = cancel {
buttons.append(.button(style: cancelStyle, title: cancel, image: nil, size: cancelSize, action: {}))
buttonsActions.append(nil)
}
return show(title: title, text: text, image: image, description: description, views: views,
buttons: buttons, buttonsActions: buttonsActions,
spacing: spacing, in: parent, animated: animated, completion)
}
static func show(
content: UIViewController,
in parent: UIViewController = .topController(),
contentScrollIsEnabled: Bool = true,
animated: Bool = true,
_ completion: (() -> Void)? = nil
) { parent.present(CommonControllerPopup(contentViewController: content, contentScrollIsEnabled: contentScrollIsEnabled, completion: completion), animated: animated) }
static func hide(in parent: UIViewController = .topController()) { parent.dismiss(animated: true) }
}