123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- //
- // TSGeneratorView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/2.
- //
- import Kingfisher
- let kTextLeading = 24.0
- class TSGeneratorView: TSBaseView {
-
- enum Style {
- case loading //loading
- case generalError //通用错误
- case sensitiveError //敏感类的错误
- case netWorkError //网络错误
- }
-
- var style:Style = .generalError
- var clickBackstageBlock:(()->Void)?
- var clickErrorBlock:((Style)->Void)?
-
- var isUploadImage:Bool = true{
- didSet {
- errorView.isUploadImage = isUploadImage
- }
- }
- lazy var animationView: TSGeneratoringAnimationView = {
- let animationView = TSGeneratoringAnimationView()
- animationView.backgroundGenerateBtn.addTarget(self, action: #selector(clickBackstageBtn), for: .touchUpInside)
- return animationView
- }()
-
- lazy var errorView: TSGeneratorErrorView = {
- let errorView = TSGeneratorErrorView()
- errorView.isUploadImage = isUploadImage
- errorView.isHidden = true
- errorView.submitBtn.addTarget(self, action: #selector(clickErrorBtn), for: .touchUpInside)
- return errorView
- }()
- lazy var blurEffect: TSDynamicBlurView = {
- return TSDynamicBlurView()
- }()
-
- lazy var xBtn: UIButton = {
- let xBtn = UIButton.createButton(image: UIImage(named: "close_gray")) { [weak self] in
- guard let self = self else { return }
- self.isHidden = true
- }
- xBtn.isHidden = true
- return xBtn
- }()
-
- override func creatUI() {
- contentView.addSubview(blurEffect)
- blurEffect.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- contentView.addSubview(animationView)
- animationView.snp.makeConstraints { make in
- make.leading.trailing.centerY.equalToSuperview()
- }
-
- contentView.addSubview(errorView)
- errorView.snp.makeConstraints { make in
- make.leading.trailing.centerY.equalToSuperview()
- }
-
- //关闭按钮
- contentView.addSubview(xBtn)
- xBtn.snp.makeConstraints { make in
- make.top.equalTo(k_Height_StatusBar + 4)
- make.leading.equalTo(16)
- make.width.equalTo(36)
- make.height.equalTo(36)
- }
- }
-
- override func dealThings() {
- // 监听应用生命周期事件
- NotificationCenter.default.addObserver(
- self,
- selector: #selector(handleAppDidEnterBackground),
- name: UIApplication.didEnterBackgroundNotification,
- object: nil
- )
-
- NotificationCenter.default.addObserver(
- self,
- selector: #selector(handleAppWillEnterForeground),
- name: UIApplication.willEnterForegroundNotification,
- object: nil
- )
- }
-
- lazy var isRotating = false{
- didSet{
- if isRotating == true{
- animationView.startAnimation()
- }else{
- animationView.stopAnimation()
- }
- }
- }
-
- @objc private func handleAppDidEnterBackground() {
- animationView.stopAnimation()
- }
- @objc private func handleAppWillEnterForeground() {
- isRotating = isRotating
- }
-
-
- func showLoading(text:String){
- isRotating = true
- animationView.isHidden = false
- errorView.isHidden = true
-
- animationView.setProgressText(text: text)
- }
-
- func showError(text:String){
- isRotating = false
- animationView.isHidden = true
- errorView.isHidden = false
- errorView.style = style
- errorView.textLabel.text = text
- }
- func setBackgroundColor(color:UIColor){
- blurEffect.removeFromSuperview()
- contentView.backgroundColor = color
- }
-
- //后台生成
- @objc func clickBackstageBtn() {
- clickBackstageBlock?()
- }
-
- @objc func clickErrorBtn() {
- clickErrorBlock?(self.style)
- }
-
- }
- extension TSGeneratorView{
-
- func updateShowProgress(text:String) {
- isHidden = false
- showLoading(text: text)
- isRotating = true
- }
-
- func updateShowLoading(text:String){
- isHidden = false
- showLoading(text: text)
- isRotating = true
- }
-
- func updateShowError(text:String,code:Int){
- self.style = TSNetWorkCode.getGeneratorStyle(code: code)
- isHidden = false
- showError(text: text.isEmpty ? kGenerateFailed : text)
- isRotating = false
- setBackgroundGenerateBtnHidden(true)
- }
-
- func updateShowSuccess(){
- isHidden = true
- isRotating = false
- setBackgroundGenerateBtnHidden(true)
- }
-
- func setBackgroundGenerateBtnHidden(_ isHidden:Bool){
- self.animationView.isShowBackGeneration = !isHidden
- }
- }
|