CNLiveTips.swift 3.07 KB
//
//  CNLiveTips.swift
//  metaCode
//
//  Created by open on 2023/6/5.
//

import Foundation
import UIKit
import YYCategories
import MBProgressHUD

class tip {
    
    static func showMessage(message : String) {
        
        if !message.isNotBlank() {
            return
        }
        
        let delegate = UIApplication.shared.delegate as! AppDelegate
        
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor(white: 0.1, alpha: 0.7)
        bgView.layer.cornerRadius = 8.0
        bgView.clipsToBounds = true
        delegate.window?.addSubview(bgView)
        
        let remindLab = UILabel.init()
        remindLab.textColor = .white
        remindLab.font = Font_15_Fit
        remindLab.textAlignment = .center
        remindLab.numberOfLines = 1
        remindLab.backgroundColor = .clear
        remindLab.text = message
        bgView.addSubview(remindLab)
        
        var width = message.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height:  delegate.window?.bounds.size.height ?? 0), options:.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:Font_15_Fit], context: nil).size.width
        if width >  (delegate.window?.bounds.size.width ?? 0)/4.0 * 3.0 {
            width =  (delegate.window?.bounds.size.width ?? 0)/4.0 * 3.0
        }
        
        let height = message.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:Font_15_Fit], context: nil).size.height
        
        bgView.bounds = CGRect(x: 0, y: 0, width: width + 20, height: height + 20)
        bgView.layer.position = CGPoint(x:  (delegate.window?.bounds.size.width ?? 0) / 2.0, y: ( (delegate.window?.bounds.size.height ?? 0) - CNLiveConfigManager.shared.keyboardDisplayHeight) / 2.0)
        
        remindLab.bounds = CGRect(x: 0, y: 0, width: width, height: height)
        remindLab.layer.position = CGPoint(x: bgView.bounds.size.width / 2, y: bgView.bounds.size.height / 2)
        
        UIView.animate(withDuration: 1, delay: 1, options: .curveEaseOut) {
            bgView.alpha = 0
        } completion: { finish in
            bgView.removeFromSuperview()
        }
    }
    
    static func showLoading(message: String) {
        
        let delegate = UIApplication.shared.delegate as! AppDelegate
        
        delegate.window?.subviews.forEach({ view in
            if view.isKind(of: MBProgressHUD.layerClass) {
                let hub = view as! MBProgressHUD
                hub.removeFromSuperview()
            }
        })
        
        let hub = MBProgressHUD.showAdded(to: delegate.window!, animated: true)
        hub.mode = .indeterminate
        hub.animationType = .fade
        hub.contentColor = .white
        hub.bezelView.backgroundColor = .black
        hub.label.textColor = .white
        if message.isNotBlank() {
            hub.label.text = message
        }
    }
    
    static func hiddenLoading() {
        let delegate = UIApplication.shared.delegate as! AppDelegate
        MBProgressHUD.hide(for: delegate.window!, animated: true)
    }
    
}