NCHud.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // NCHud.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import JGProgressHUD
  11. class NCHud: NSObject {
  12. private let hud = JGProgressHUD()
  13. private var view: UIView?
  14. public init(_ view: UIView? = nil) {
  15. if let view {
  16. self.view = view
  17. }
  18. super.init()
  19. }
  20. func initHud(view: UIView? = nil, text: String? = nil, detailText: String? = nil) {
  21. DispatchQueue.main.async {
  22. if let view {
  23. self.view = view
  24. }
  25. self.hud.indicatorView = JGProgressHUDIndicatorView()
  26. self.hud.textLabel.text = text
  27. self.hud.textLabel.textColor = NCBrandColor.shared.iconImageColor
  28. self.hud.detailTextLabel.text = detailText
  29. self.hud.detailTextLabel.textColor = NCBrandColor.shared.iconImageColor2
  30. if let view = self.view {
  31. self.hud.show(in: view)
  32. }
  33. }
  34. }
  35. func initHudRing(view: UIView? = nil, text: String? = nil, detailText: String? = nil, tapToCancelDetailText: Bool = false, tapOperation: (() -> Void)? = nil) {
  36. DispatchQueue.main.async {
  37. self.hud.tapOnHUDViewBlock = { hud in
  38. if let tapOperation {
  39. tapOperation()
  40. hud.dismiss()
  41. }
  42. }
  43. if let view {
  44. self.view = view
  45. }
  46. self.hud.indicatorView = JGProgressHUDRingIndicatorView()
  47. self.hud.progress = 0.0
  48. let indicatorView = self.hud.indicatorView as? JGProgressHUDRingIndicatorView
  49. indicatorView?.ringWidth = 1.5
  50. indicatorView?.ringColor = NCBrandColor.shared.iconImageColor
  51. self.hud.textLabel.text = text
  52. self.hud.textLabel.textColor = NCBrandColor.shared.iconImageColor
  53. if tapToCancelDetailText {
  54. self.hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  55. } else {
  56. self.hud.detailTextLabel.text = detailText
  57. }
  58. self.hud.detailTextLabel.textColor = NCBrandColor.shared.iconImageColor2
  59. if let view = self.view {
  60. self.hud.show(in: view)
  61. }
  62. }
  63. }
  64. func dismiss() {
  65. DispatchQueue.main.async {
  66. self.hud.dismiss()
  67. }
  68. }
  69. func show() {
  70. DispatchQueue.main.async {
  71. if let view = self.view {
  72. self.hud.show(in: view)
  73. }
  74. }
  75. }
  76. func progress(num: Float, total: Float) {
  77. DispatchQueue.main.async {
  78. self.hud.progress = num / total
  79. }
  80. }
  81. func progress(_ progress: Double) {
  82. DispatchQueue.main.async {
  83. self.hud.progress = Float(progress)
  84. }
  85. }
  86. func success() {
  87. DispatchQueue.main.async {
  88. self.hud.indicatorView = JGProgressHUDSuccessIndicatorView()
  89. self.hud.indicatorView?.tintColor = .green
  90. self.hud.textLabel.text = NSLocalizedString("_success_", comment: "")
  91. self.hud.detailTextLabel.text = nil
  92. self.hud.dismiss(afterDelay: 1.0)
  93. }
  94. }
  95. func error(text: String?) {
  96. DispatchQueue.main.async {
  97. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  98. self.hud.indicatorView?.tintColor = .red
  99. self.hud.textLabel.text = text
  100. self.hud.dismiss(afterDelay: 2.0)
  101. }
  102. }
  103. func setText(text: String?, detailText: String? = nil) {
  104. DispatchQueue.main.async {
  105. self.hud.textLabel.text = text
  106. self.hud.detailTextLabel.text = detailText
  107. }
  108. }
  109. }