NCActivityIndicator.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // NCActivityIndicator.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 11/08/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. class NCActivityIndicator: NSObject {
  26. @objc static let shared: NCActivityIndicator = {
  27. let instance = NCActivityIndicator()
  28. return instance
  29. }()
  30. private var activityIndicator: UIActivityIndicatorView?
  31. private var viewActivityIndicator: UIView?
  32. private var viewBackgroundActivityIndicator: UIView?
  33. @objc func startActivity(backgroundView: UIView? = nil, style: UIActivityIndicatorView.Style, blurEffect: Bool = true) {
  34. start(backgroundView: backgroundView, style: style, blurEffect: blurEffect)
  35. }
  36. func start(backgroundView: UIView? = nil, bottom: CGFloat? = nil, top: CGFloat? = nil, style: UIActivityIndicatorView.Style = .large, blurEffect: Bool = true) {
  37. if self.activityIndicator != nil { stop() }
  38. DispatchQueue.main.async {
  39. self.activityIndicator = UIActivityIndicatorView(style: style)
  40. guard let activityIndicator = self.activityIndicator, self.viewBackgroundActivityIndicator == nil else { return }
  41. activityIndicator.color = NCBrandColor.shared.textColor
  42. activityIndicator.hidesWhenStopped = true
  43. activityIndicator.translatesAutoresizingMaskIntoConstraints = false
  44. var sizeActivityIndicator = activityIndicator.frame.height
  45. if backgroundView == nil {
  46. sizeActivityIndicator += 50
  47. }
  48. self.viewActivityIndicator = UIView(frame: CGRect(x: 0, y: 0, width: sizeActivityIndicator, height: sizeActivityIndicator))
  49. self.viewActivityIndicator?.translatesAutoresizingMaskIntoConstraints = false
  50. self.viewActivityIndicator?.layer.cornerRadius = 10
  51. self.viewActivityIndicator?.layer.masksToBounds = true
  52. self.viewActivityIndicator?.backgroundColor = .clear
  53. #if !EXTENSION
  54. if backgroundView == nil {
  55. if let window = (UIApplication.shared.connectedScenes.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }.first { $0.isKeyWindow }) {
  56. self.viewBackgroundActivityIndicator?.removeFromSuperview()
  57. self.viewBackgroundActivityIndicator = NCViewActivityIndicator(frame: window.bounds)
  58. window.addSubview(self.viewBackgroundActivityIndicator!)
  59. self.viewBackgroundActivityIndicator?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  60. self.viewBackgroundActivityIndicator?.backgroundColor = .clear
  61. }
  62. } else {
  63. self.viewBackgroundActivityIndicator = backgroundView
  64. }
  65. #else
  66. self.viewBackgroundActivityIndicator = backgroundView
  67. #endif
  68. // VIEW ACTIVITY INDICATOR
  69. guard let viewActivityIndicator = self.viewActivityIndicator else { return }
  70. viewActivityIndicator.addSubview(activityIndicator)
  71. if backgroundView == nil, blurEffect {
  72. let blurEffect = UIBlurEffect(style: .regular)
  73. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  74. blurEffectView.frame = viewActivityIndicator.frame
  75. viewActivityIndicator.insertSubview(blurEffectView, at: 0)
  76. }
  77. NSLayoutConstraint.activate([
  78. viewActivityIndicator.widthAnchor.constraint(equalToConstant: sizeActivityIndicator),
  79. viewActivityIndicator.heightAnchor.constraint(equalToConstant: sizeActivityIndicator),
  80. activityIndicator.centerXAnchor.constraint(equalTo: viewActivityIndicator.centerXAnchor),
  81. activityIndicator.centerYAnchor.constraint(equalTo: viewActivityIndicator.centerYAnchor)
  82. ])
  83. // BACKGROUD VIEW ACTIVITY INDICATOR
  84. guard let viewBackgroundActivityIndicator = self.viewBackgroundActivityIndicator else { return }
  85. viewBackgroundActivityIndicator.addSubview(viewActivityIndicator)
  86. if let constant = bottom {
  87. viewActivityIndicator.bottomAnchor.constraint(equalTo: viewBackgroundActivityIndicator.bottomAnchor, constant: constant).isActive = true
  88. } else if let constant = top {
  89. viewActivityIndicator.topAnchor.constraint(equalTo: viewBackgroundActivityIndicator.topAnchor, constant: constant).isActive = true
  90. } else {
  91. viewActivityIndicator.centerYAnchor.constraint(equalTo: viewBackgroundActivityIndicator.centerYAnchor).isActive = true
  92. }
  93. viewActivityIndicator.centerXAnchor.constraint(equalTo: viewBackgroundActivityIndicator.centerXAnchor).isActive = true
  94. activityIndicator.startAnimating()
  95. }
  96. }
  97. @objc func stop() {
  98. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  99. self.activityIndicator?.stopAnimating()
  100. self.activityIndicator?.removeFromSuperview()
  101. self.activityIndicator = nil
  102. self.viewActivityIndicator?.removeFromSuperview()
  103. self.viewActivityIndicator = nil
  104. if self.viewBackgroundActivityIndicator is NCViewActivityIndicator {
  105. self.viewBackgroundActivityIndicator?.removeFromSuperview()
  106. }
  107. self.viewBackgroundActivityIndicator = nil
  108. }
  109. }
  110. }
  111. class NCViewActivityIndicator: UIView {
  112. // MARK: - View Life Cycle
  113. override init(frame: CGRect) {
  114. super.init(frame: frame)
  115. }
  116. required init?(coder: NSCoder) {
  117. fatalError("init(coder:) has not been implemented")
  118. }
  119. }