Files
2022-12-16 13:06:02 +03:00

151 lines
5.1 KiB
Swift

//
// 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
self.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 {
self.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()
self.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
self.hide(in: ctrl.contentView)
} else {
self.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()
self.loaders.removeAll(where: { $0 == view })
}
}
static func hideAll() {
self.loaders.compactMap({ $0.superview }).forEach({ hide(in: $0) })
self.loaders = []
}
}
final 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))
self.backgroundColor = .clear
self.translatesAutoresizingMaskIntoConstraints = true
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.alphaView.frame = bounds
self.alphaView.translatesAutoresizingMaskIntoConstraints = true
self.alphaView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(alphaView)
self.imgView.translatesAutoresizingMaskIntoConstraints = false
self.imgView.contentMode = .scaleAspectFit
self.imgView.loadGif(name: "loader")
self.imgView.frame.size = .init(width: 100, height: 100)
self.addSubview(imgView)
}
convenience init(showImmediatelyWithSize size: CGFloat) {
self.init()
self.startAnimation(withSize: size)
}
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)
self.alphaView.backgroundColor = color
self.startAnimation(withSize: 100)
} else {
self.layoutIfNeeded()
}
}
func hide() {
UIView.animate(withDuration: Animation.fast) {
self.alphaView.alpha = 0
} completion: { _ in
self.removeFromSuperview()
}
}
private func startAnimation(withSize size: CGFloat) {
self.alphaView.alpha = 0
NSLayoutConstraint.activate([
self.imgView.widthAnchor.constraint(equalToConstant: size),
self.imgView.heightAnchor.constraint(equalToConstant: size),
self.imgView.centerXAnchor.constraint(equalTo: centerXAnchor),
self.imgView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
self.layoutIfNeeded()
UIView.animate(withDuration: Animation.fast) {
self.alphaView.alpha = 1
}
}
}