无故事王国
2024-03-14 a478b668ca1a5d4f6d2d9b1075d292cbbef90de5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
//  StudentUpdateInfoView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/14.
//
 
import UIKit
import JQTools
import QMUIKit
import RxSwift
 
let StudentUpdate_Nofi = Notification.Name.init("StudentUpdate_Nofi")
class StudentUpdateInfoView: UIView,JQNibView{
 
                @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 var updateUserProfile:Bool = false
                private let disposeBag = DisposeBag()
                private var profileImg:UIImage?
 
                override func awakeFromNib() {
                                super.awakeFromNib()
                                cons_bottom.constant = -(JQ_ScreenW / 2)
                                alpha = 0
                                layoutIfNeeded()
 
                                let tap = UITapGestureRecognizer(target: self, action: #selector(userImgUploadAction))
                                img_profile.isUserInteractionEnabled = true
                                img_profile.addGestureRecognizer(tap)
 
                                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}
 
                                guard tf_height.text!.int != 0 else {alertError(msg: "请输入正确的身高");return}
                                guard tf_weight.text!.int != 0 else {alertError(msg: "请输入正确的体重");return}
 
                                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()
                }
 
 
                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)
                }
}