无故事王国
2024-03-14 a478b668ca1a5d4f6d2d9b1075d292cbbef90de5
WanPai/Root/Activity/View/StudentUpdateInfoView.swift
@@ -13,94 +13,137 @@
let StudentUpdate_Nofi = Notification.Name.init("StudentUpdate_Nofi")
class StudentUpdateInfoView: UIView,JQNibView{
    @IBOutlet weak var view_container: UIView!
    @IBOutlet weak var cons_bottom: NSLayoutConstraint!
    @IBOutlet weak var tf_height: QMUITextField!
    @IBOutlet weak var tf_weight: QMUITextField!
    @IBOutlet weak var tf_phone: QMUITextField!
            @IBOutlet weak var img_profile: UIImageView!
            @IBOutlet weak var view_profile: UIView!
            @IBOutlet weak var view_container: UIView!
            @IBOutlet weak var cons_bottom: NSLayoutConstraint!
            @IBOutlet weak var tf_height: QMUITextField!
            @IBOutlet weak var tf_weight: QMUITextField!
            @IBOutlet weak var tf_phone: QMUITextField!
    private var model:ActivityDetailPartModel?
    private let disposeBag = DisposeBag()
    override func awakeFromNib() {
        super.awakeFromNib()
        cons_bottom.constant = -(JQ_ScreenW / 2)
        alpha = 0
        layoutIfNeeded()
            private var model:ActivityDetailPartModel?
            private var updateUserProfile:Bool = false
            private let disposeBag = DisposeBag()
            private var profileImg:UIImage?
        NotificationCenter.default.rx.notification(UIApplication.keyboardWillHideNotification).subscribe(onNext: {noti in
            if let userInfo = noti.userInfo{
                self.cons_bottom.constant =  0
                let duration = (userInfo["UIKeyboardAnimationDurationUserInfoKey"] as? Double) ?? 0
                UIView.animate(withDuration: duration) {
                    self.layoutIfNeeded()
                }
            }
        }).disposed(by: disposeBag)
            override func awakeFromNib() {
                        super.awakeFromNib()
                        cons_bottom.constant = -(JQ_ScreenW / 2)
                        alpha = 0
                        layoutIfNeeded()
        NotificationCenter.default.rx.notification(UIApplication.keyboardWillShowNotification).subscribe(onNext: { noti in
            if let userInfo = noti.userInfo{
                self.cons_bottom.constant =  (userInfo["UIKeyboardFrameEndUserInfoKey"] as? CGRect)?.height ?? 0
                let duration = (userInfo["UIKeyboardAnimationDurationUserInfoKey"] as? Double) ?? 0
                UIView.animate(withDuration: duration) {
                    self.layoutIfNeeded()
                }
            }
        }).disposed(by: disposeBag)
    }
    static func show(_ model:ActivityDetailPartModel){
        let storesView = StudentUpdateInfoView.jq_loadNibView()
        storesView.model = model
        storesView.frame = sceneDelegate?.window?.frame ?? .zero
      sceneDelegate?.window?.addSubview(storesView)
        storesView.cons_bottom.constant = 0
      storesView.tf_phone.text = model.phone
      storesView.tf_height.text = "\(model.height.int)"
      storesView.tf_weight.text = "\(model.weight.int)"
                        let tap = UITapGestureRecognizer(target: self, action: #selector(userImgUploadAction))
                        img_profile.isUserInteractionEnabled = true
                        img_profile.addGestureRecognizer(tap)
        UIView.animate(withDuration: 0.4) {
            storesView.alpha = 1
            storesView.layoutIfNeeded()
        }
    }
                        NotificationCenter.default.rx.notification(UIApplication.keyboardWillHideNotification).subscribe(onNext: {noti in
                                    if let userInfo = noti.userInfo{
                                                self.cons_bottom.constant =  0
                                                let duration = (userInfo["UIKeyboardAnimationDurationUserInfoKey"] as? Double) ?? 0
                                                UIView.animate(withDuration: duration) {
                                                            self.layoutIfNeeded()
                                                }
                                    }
                        }).disposed(by: disposeBag)
                        NotificationCenter.default.rx.notification(UIApplication.keyboardWillShowNotification).subscribe(onNext: { noti in
                                    if let userInfo = noti.userInfo{
                                                self.cons_bottom.constant =  (userInfo["UIKeyboardFrameEndUserInfoKey"] as? CGRect)?.height ?? 0
                                                let duration = (userInfo["UIKeyboardAnimationDurationUserInfoKey"] as? Double) ?? 0
                                                UIView.animate(withDuration: duration) {
                                                            self.layoutIfNeeded()
                                                }
                                    }
                        }).disposed(by: disposeBag)
            }
            static func show(_ model:ActivityDetailPartModel,updateUserProfile:Bool = false){
                        let storesView = StudentUpdateInfoView.jq_loadNibView()
                        storesView.model = model
                        storesView.updateUserProfile = updateUserProfile
                        storesView.view_profile.isHidden = !updateUserProfile
                        storesView.frame = sceneDelegate?.window?.frame ?? .zero
                        sceneDelegate?.window?.addSubview(storesView)
                        storesView.cons_bottom.constant = 0
                        storesView.tf_phone.text = model.phone
                        storesView.tf_height.text = "\(model.height.int)"
                        storesView.tf_weight.text = "\(model.weight.int)"
                        if updateUserProfile{
                                    storesView.img_profile.sd_setImage(with: URL(string: model.headImg),placeholderImage: UIImage(named: "btn_add_large_2"))
                        }
                        UIView.animate(withDuration: 0.4) {
                                    storesView.alpha = 1
                                    storesView.layoutIfNeeded()
                        }
            }
            @objc func userImgUploadAction(){
                        JQ_ImagePickerTool.getSharedInstance().singleImage({ [weak self] image in
                                    self?.img_profile.image = image
                                    self?.profileImg = image
                        }, clipSize: CGSize(width: JQ_ScreenW, height: JQ_ScreenW))
            }
    @IBAction func saveAction(_ sender: UIButton) {
        guard !tf_height.text!.isEmpty else {alertError(msg: "请输入身高");return}
        guard !tf_weight.text!.isEmpty else {alertError(msg: "请输入体重");return}
            @IBAction func saveAction(_ sender: UIButton) {
                        guard !tf_height.text!.isEmpty else {alertError(msg: "请输入身高");return}
                        guard !tf_weight.text!.isEmpty else {alertError(msg: "请输入体重");return}
        guard tf_height.text!.int != 0 else {alertError(msg: "请输入正确的身高");return}
        guard tf_weight.text!.int != 0 else {alertError(msg: "请输入正确的体重");return}
                        guard tf_height.text!.int != 0 else {alertError(msg: "请输入正确的身高");return}
                        guard tf_weight.text!.int != 0 else {alertError(msg: "请输入正确的体重");return}
        self.endEditing(true)
      Services.editParticipant(id: model!.id, height: tf_height.text!.int ?? 0, weight: tf_weight.text!.int ?? 0, phone: tf_phone.text,isStudent: model!.isStudent).subscribe(onNext: {data in
            alertSuccess(msg: "修改成功")
            DispatchQueue.main.asyncAfter(deadline: .now()+1) {
                NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                self.close()
            }
        }).disposed(by: disposeBag)
    }
                        guard profileImg != nil || !(model?.headImg.isEmpty ?? true) else {
                                    alertError(msg: "请上传头像");return
                        }
                        self.endEditing(true)
                        if profileImg != nil{
                                    showHUD("正在上传头像")
                                    profileImg!.uploadImgToService().subscribe(onNext: { [weak self] imgUrl in
                                                guard let weakSelf = self else { return }
                                                weakSelf.model?.headImg = imgUrl
                                                hiddenHUD()
                                                weakSelf.saveEdit()
                                    }, onError: { error in
                                                hiddenHUD()
                                                alertError(msg: error.localizedDescription)
                                    }).disposed(by: disposeBag)
                        }else{
                                    saveEdit()
                        }
            }
            private func saveEdit(){
                        Services.editParticipant(id: model!.id, height: tf_height.text!.int ?? 0, weight: tf_weight.text!.int ?? 0, phone: tf_phone.text,isStudent: model!.isStudent,headImg: model!.headImg).subscribe(onNext: {data in
                                    alertSuccess(msg: "修改成功")
                                    DispatchQueue.main.asyncAfter(deadline: .now()+1) {
                                                NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                                                self.close()
                                    }
                        }).disposed(by: disposeBag)
            }
    @IBAction func closeAction(_ sender: UIButton) {
        close()
    }
            @IBAction func closeAction(_ sender: UIButton) {
                        close()
            }
    private func close(){
        self.cons_bottom.constant = -(JQ_ScreenW / 2)
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
   override func layoutSubviews() {
      super.layoutSubviews()
      view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20,width: JQ_ScreenW,height: JQ_ScreenW)
   }
            private func close(){
                        self.cons_bottom.constant = -(JQ_ScreenW / 2)
                        UIView.animate(withDuration: 0.4) {
                                    self.alpha = 0
                                    self.layoutIfNeeded()
                        } completion: { _ in
                                    self.removeFromSuperview()
                        }
            }
            override func layoutSubviews() {
                        super.layoutSubviews()
                        view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20,width: JQ_ScreenW,height: JQ_ScreenW)
            }
}