TSGeneratorloadingView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // TSGeneratorView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/2.
  6. //
  7. import Kingfisher
  8. let kTextLeading = 24.0
  9. class TSGeneratorView: TSBaseView {
  10. enum Style {
  11. case loading //loading
  12. case generalError //通用错误
  13. case sensitiveError //敏感类的错误
  14. case netWorkError //网络错误
  15. }
  16. var style:Style = .generalError
  17. var clickBackstageBlock:(()->Void)?
  18. var clickErrorBlock:((Style)->Void)?
  19. var isUploadImage:Bool = true{
  20. didSet {
  21. errorView.isUploadImage = isUploadImage
  22. }
  23. }
  24. lazy var animationView: TSGeneratoringAnimationView = {
  25. let animationView = TSGeneratoringAnimationView()
  26. animationView.backgroundGenerateBtn.addTarget(self, action: #selector(clickBackstageBtn), for: .touchUpInside)
  27. return animationView
  28. }()
  29. lazy var errorView: TSGeneratorErrorView = {
  30. let errorView = TSGeneratorErrorView()
  31. errorView.isUploadImage = isUploadImage
  32. errorView.isHidden = true
  33. errorView.submitBtn.addTarget(self, action: #selector(clickErrorBtn), for: .touchUpInside)
  34. return errorView
  35. }()
  36. lazy var blurEffect: TSDynamicBlurView = {
  37. return TSDynamicBlurView()
  38. }()
  39. lazy var xBtn: UIButton = {
  40. let xBtn = UIButton.createButton(image: UIImage(named: "close_gray")) { [weak self] in
  41. guard let self = self else { return }
  42. self.isHidden = true
  43. }
  44. xBtn.isHidden = true
  45. return xBtn
  46. }()
  47. override func creatUI() {
  48. contentView.addSubview(blurEffect)
  49. blurEffect.snp.makeConstraints { make in
  50. make.edges.equalToSuperview()
  51. }
  52. contentView.addSubview(animationView)
  53. animationView.snp.makeConstraints { make in
  54. make.leading.trailing.centerY.equalToSuperview()
  55. }
  56. contentView.addSubview(errorView)
  57. errorView.snp.makeConstraints { make in
  58. make.leading.trailing.centerY.equalToSuperview()
  59. }
  60. //关闭按钮
  61. contentView.addSubview(xBtn)
  62. xBtn.snp.makeConstraints { make in
  63. make.top.equalTo(k_Height_StatusBar + 4)
  64. make.leading.equalTo(16)
  65. make.width.equalTo(36)
  66. make.height.equalTo(36)
  67. }
  68. }
  69. override func dealThings() {
  70. // 监听应用生命周期事件
  71. NotificationCenter.default.addObserver(
  72. self,
  73. selector: #selector(handleAppDidEnterBackground),
  74. name: UIApplication.didEnterBackgroundNotification,
  75. object: nil
  76. )
  77. NotificationCenter.default.addObserver(
  78. self,
  79. selector: #selector(handleAppWillEnterForeground),
  80. name: UIApplication.willEnterForegroundNotification,
  81. object: nil
  82. )
  83. }
  84. lazy var isRotating = false{
  85. didSet{
  86. if isRotating == true{
  87. animationView.startAnimation()
  88. }else{
  89. animationView.stopAnimation()
  90. }
  91. }
  92. }
  93. @objc private func handleAppDidEnterBackground() {
  94. animationView.stopAnimation()
  95. }
  96. @objc private func handleAppWillEnterForeground() {
  97. isRotating = isRotating
  98. }
  99. func showLoading(text:String){
  100. isRotating = true
  101. animationView.isHidden = false
  102. errorView.isHidden = true
  103. animationView.setProgressText(text: text)
  104. }
  105. func showError(text:String){
  106. isRotating = false
  107. animationView.isHidden = true
  108. errorView.isHidden = false
  109. errorView.style = style
  110. errorView.textLabel.text = text
  111. }
  112. func setBackgroundColor(color:UIColor){
  113. blurEffect.removeFromSuperview()
  114. contentView.backgroundColor = color
  115. }
  116. //后台生成
  117. @objc func clickBackstageBtn() {
  118. clickBackstageBlock?()
  119. }
  120. @objc func clickErrorBtn() {
  121. clickErrorBlock?(self.style)
  122. }
  123. }
  124. extension TSGeneratorView{
  125. func updateShowProgress(text:String) {
  126. isHidden = false
  127. showLoading(text: text)
  128. isRotating = true
  129. }
  130. func updateShowLoading(text:String){
  131. isHidden = false
  132. showLoading(text: text)
  133. isRotating = true
  134. }
  135. func updateShowError(text:String,code:Int){
  136. self.style = TSNetWorkCode.getGeneratorStyle(code: code)
  137. isHidden = false
  138. showError(text: text.isEmpty ? kGenerateFailed : text)
  139. isRotating = false
  140. setBackgroundGenerateBtnHidden(true)
  141. }
  142. func updateShowSuccess(){
  143. isHidden = true
  144. isRotating = false
  145. setBackgroundGenerateBtnHidden(true)
  146. }
  147. func setBackgroundGenerateBtnHidden(_ isHidden:Bool){
  148. self.animationView.isShowBackGeneration = !isHidden
  149. }
  150. }