// // CommonPopup.swift // Wallet // // Created by Igor on 23.03.2021. // Copyright © 2021 AM. All rights reserved. // import UIKit enum Popup { private enum Constants { enum PopupWithLoader { static let titleSize: CGFloat = 16 static let loaderSize: CGFloat = 65 static let stackSpacing: CGFloat = 55 static let stackHorizontal: CGFloat = 24 static let stackVertical: CGFloat = 80 } } @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) button?.isUserInteractionEnabled = true } } 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 showWithLoader(in controller: UIViewController, with title: String) { let label = UILabel() label.numberOfLines = 0 label.attributedText = title.attributed(style: .regular, size: Constants.PopupWithLoader.titleSize, alignment: .center) let loader = CommonViewLoading(showImmediatelyWithSize: Constants.PopupWithLoader.loaderSize) let views: [UIView] = [loader, label] let popup = CommonControllerPopup(views: views, spacing: Constants.PopupWithLoader.stackSpacing, leftSpace: Constants.PopupWithLoader.stackHorizontal, rightSpace: Constants.PopupWithLoader.stackHorizontal, topSpace: Constants.PopupWithLoader.stackVertical, bottomSpace: Constants.PopupWithLoader.stackVertical) controller.present(popup, animated: true) } static func show( content: UIViewController, in parent: UIViewController = .topController(), contentScrollIsEnabled: Bool = true, animated: Bool = true, _ completion: (() -> Void)? = nil ) { let popupController = CommonControllerPopup(contentViewController: content, contentScrollIsEnabled: contentScrollIsEnabled, completion: completion) parent.present(popupController, animated: animated) } static func hide(in parent: UIViewController = .topController(), completion: (() -> Void)? = nil) { parent.dismiss(animated: true) { completion?() } } }