TemperatureView.swift 4.03 KB
//
//  TemperatureView.swift
//  Twear
//
//  Created by yangbin on 2022/1/8.
//

import UIKit

class TemperatureView: UIView {
    
    typealias clickAlertClosure = (_ value: String) -> Void
    var clickClosure: clickAlertClosure!
    
    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet private weak var bgView: UIView!
    
    var temperature: Float = 0
    
    private var doInput: Bool = false
    private var text: String = "0"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initFromNib()
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initFromNib()
        setupUI()
    }
    
    private func initFromNib() {
        if let view = Bundle.main.loadNibNamed("TemperatureView", owner: self, options: nil)?.first as? UIView {
            view.frame = bounds
            self.addSubview(view)
        }
    }

    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
//        self.shareImage = image
        initFromNib()
        setupUI()
    }
    
    func setupUI() {
        bgView.layoutIfNeeded()
        bgView.setCorners(corners: [.topLeft, .topRight], radio: 15)
        textLabel.toUnitMode(text: "0", unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
    }
    
    
    @IBAction func clickBtnAction(_ sender: UIButton) {
//        if (clickClosure != nil) {
//            clickClosure!(sender.tag)
//        }
        switch sender.tag {
        case 0..<10:
            let num = sender.tag
            let oldText = text
            let array = text.split(separator: ".")
            if array.count == 2 {
                if array[0].count == 1 && text.count >= 4 {
                    return
                }
            } else {
                if text.count >= 5 {
                    return
                }
            }
            if doInput {
                text += "\(num)"
            } else {
                if text == "0" {
                    text = "\(num)"
                } else {
                    text += "\(num)"
                }
            }
            print(text)
            if let result = Float(text), result < 100 {
                textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
            } else {
                text = oldText
                textLabel.toUnitMode(text: oldText, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
            }
            
        case 10:
            if !doInput {
                text += "."
                doInput = true
            }
//            doInput = !doInput
            textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
        case 20:
            if text.substring(fromIndex: text.count-1) == "." {
                doInput = false
            }
            if text.count == 1 {
                text = "0"
            } else {
                text = text.substring(toIndex: text.count-1)
            }
            textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
        case 30:
            dismiss()
        case 31:
            if let result = Float(text), result < 100, result > 0 {
                if (clickClosure != nil) {
                    clickClosure!(textLabel.text ?? "--℃")
                }
            }
    
            dismiss()
        default:
            break
        }
    }
    
    func show() {
        let wind = KeyWindow
        self.alpha = 0
        
        wind.addSubview(self)
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.alpha = 1
        })
    }
    
    @objc func dismiss() {
//        MBProgressHUD.hide()
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.alpha = 0
        }, completion: { (finish) -> Void in
            if finish {
                self.removeFromSuperview()
            }
        })
    }
    
    deinit {
        print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
    }
}