// // CommonViewLoading.swift // Wallet // // Created by Igor on 27.02.2021. // Copyright © 2021 AM. All rights reserved. // import UIKit enum Loader { static func isLoading(in view: UIView) -> Bool { view.subviews.contains(where: { $0 is CommonViewLoading }) } private static var loaders = [CommonViewLoading]() private static func popup(_ view: UIView) -> CommonControllerPopup? { var ctrl: UIViewController? = view.viewController while ctrl != nil && !(ctrl is CommonControllerPopup) { ctrl = ctrl?.parent } return ctrl as? CommonControllerPopup } static func show(in viewController: UIViewController = .topController(), color: UIColor = Asset.snow.color.withAlphaComponent(0.9)) { var ctrl: UIViewController? = viewController while ctrl != nil && !(ctrl is CommonControllerPopup) { ctrl = ctrl?.parent } if let ctrl = ctrl as? CommonControllerPopup { ctrl.view.isUserInteractionEnabled = false show( in: ctrl.contentView, color: color, frame: .init(x: 0, y: 0, width: ctrl.contentView.frame.width, height: ctrl.contentView.frame.height - ctrl.bottomHeight.constant) ) } else { show(in: viewController.view, color: color) } } static func show(in view: UIView, color: UIColor = Asset.snow.color.withAlphaComponent(0.9), frame: CGRect? = nil) { if let loading = view.subviews.compactMap({ $0 as? CommonViewLoading }).first { loading.show(in: view, color: color, frame: frame) } else { let loader = CommonViewLoading() loaders.append(loader) loader.show(in: view, color: color, frame: frame) } } static func hide(in viewController: UIViewController = .topController()) { var ctrl: UIViewController? = viewController while ctrl != nil && !(ctrl is CommonControllerPopup) { ctrl = ctrl?.parent } if let ctrl = ctrl as? CommonControllerPopup { ctrl.view.isUserInteractionEnabled = true hide(in: ctrl.contentView) } else { hide(in: viewController.view) } } static func hide(in view: UIView) { let views = view.subviews.compactMap({ $0 as? CommonViewLoading }) views.map({ popup($0) }).forEach({ $0?.view.isUserInteractionEnabled = true }) views.forEach({ view in view.hide() loaders.removeAll(where: { $0 == view }) }) } static func hideAll() { loaders.compactMap({ $0.superview }).forEach({ hide(in: $0) }) loaders = [] } } class CommonViewLoading: UIView { private let imgView = UIImageView(frame: .init(x: 0, y: 0, width: 100, height: 100)) private let alphaView = UIView() init() { super.init(frame: .init(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height)) backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = true autoresizingMask = [.flexibleWidth, .flexibleHeight] alphaView.frame = bounds alphaView.translatesAutoresizingMaskIntoConstraints = true alphaView.autoresizingMask = [.flexibleWidth, .flexibleHeight] addSubview(alphaView) imgView.translatesAutoresizingMaskIntoConstraints = false imgView.contentMode = .scaleAspectFit imgView.loadGif(name: "loader") imgView.frame.size = .init(width: 100, height: 100) addSubview(imgView) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func show(in view: UIView, color: UIColor = .clear, frame: CGRect? = nil) { self.frame = frame ?? view.bounds if superview == nil { view.addSubview(self) alphaView.alpha = 0 alphaView.backgroundColor = color imgView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true imgView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true imgView.widthAnchor.constraint(equalToConstant: 100).isActive = true imgView.heightAnchor.constraint(equalToConstant: 100).isActive = true layoutIfNeeded() UIView.animate(withDuration: Animation.fast) { self.alphaView.alpha = 1 } } else { layoutIfNeeded() } } func hide() { UIView.animate(withDuration: Animation.fast) { self.alphaView.alpha = 0 } completion: { _ in self.removeFromSuperview() } } }