Blame view

Twear/Mine/UserInfoSettingVC.swift 10.8 KB
75d24c15   yangbin   123
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
  //
  //  UserInfoSettingVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/27.
  //
  
  import UIKit
  import SwiftDate
  import MBProgressHUD
  import TZImagePickerController
  
  class UserInfoSettingVC: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      
      var modifyClosure: ((_ name: String, _ image: UIImage?) -> ())?
      let settingArray: [[String]] = [["头像", "昵称"], ["性别", "生日", "年龄", "身高", "体重"]]
      let user = UserInfo
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(false, animated: true)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
66e7e76d   yangbin   NFC
28
          title = LocString("个人资料")
75d24c15   yangbin   123
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
          
          tableView.contentInset = UIEdgeInsets.init(top: 5, left: 0, bottom: 10, right: 0)
          tableView.register(UINib.init(nibName: "SettingCell4", bundle: Bundle.main), forCellReuseIdentifier: "SettingCell4")
          tableView.register(UINib.init(nibName: "SettingCell5", bundle: Bundle.main), forCellReuseIdentifier: "SettingCell5")
          tableView.tableFooterView = UIView(frame: CGRect.zero)
      }
      
      private func changeAvatar() {
          let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
          let cancelAction = UIAlertAction(title: LocString("取消"), style: .cancel, handler: nil)
          cancelAction.setValue(UIColor.rgbColorFromHex(0x00993E), forKey: "titleTextColor")
          
          
          let archiveAction1 = UIAlertAction(title: LocString("拍照"), style: .default) { (action) in
              self.takePhoto()
          }
          archiveAction1.setValue(UIColor.black, forKey: "titleTextColor")
          let archiveAction2 = UIAlertAction(title: LocString("从手机相册选择"), style: .default) { (action) in
              self.photoAlbum()
          }
          archiveAction2.setValue(UIColor.black, forKey: "titleTextColor")
          alert.addAction(cancelAction)
          alert.addAction(archiveAction1)
          alert.addAction(archiveAction2)
          present(alert, animated: true, completion: nil)
      }
      
      
      private func updateUserInfo() {
          BluetoothManager.shared.setUserInfo(user, completion: nil)
          AdminHelper.shared.updateUser(user)
          tableView.reloadData()
      }
      
      private func updateGender() {
          if UIImage.getImageFromPath("avatar") == nil {
              let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! SettingCell5
              let image = UIImage(named: user.gender == 1 ? "avatar_male" : "avatar_female")
              cell.detailImageView.image = image
              modifyClosure?("", image)
          } else {
              
          }
          updateUserInfo()
      }
  
      private func updateAvatar(_ image: UIImage) {
582f536d   yangbin   common:2022.1.28
76
77
78
79
80
          if image.saveImageToPath("avatar") {
              let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! SettingCell5
              cell.detailImageView.image = image
              modifyClosure?(user.name, image)
          }
75d24c15   yangbin   123
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
      }
      
      deinit {
          print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
      }
  }
  
  
  
  extension UserInfoSettingVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate, TZImagePickerControllerDelegate {
      
      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
          let type:String = (info[UIImagePickerController.InfoKey.mediaType] as! String)
          if type == "public.image" {
              let img = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
              let aImage = img.compressOriginalImage(10.0)
              self.updateAvatar(aImage!)
              picker.dismiss(animated:true, completion:nil)
          }
      }
      
      //相册
      private func photoAlbum() {
          if PHPhotoLibrary.authorizationStatus().rawValue == 2 {
              MBProgressHUD.show(LocString("请在“设置-隐私-照片“选项中,允许'HDF wear'访问你的照片"))
              return
          }
          let picker = TZImagePickerController(maxImagesCount: 1, columnNumber: 3, delegate: self)
          picker?.modalPresentationStyle = .fullScreen
          picker?.allowPickingOriginalPhoto = false //原图
          picker?.allowPickingVideo = false //视频
          picker?.allowCameraLocation = false //位置
          picker?.allowPickingGif = false //GIF
          picker?.allowTakePicture = false //内部拍照
          picker?.allowCrop = true //裁剪
          
          picker?.didFinishPickingPhotosHandle = {[weak self] (photos, assets, isSelectOriginalPhoto) -> Void in
              if let images = photos {
                  let image = images[0].compressOriginalImage(50.0)
                  self?.updateAvatar(image!)
              }
          }
          self.present(picker!, animated: true, completion: nil)
      }
      
      //拍照
      private func takePhoto() {
          let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
          if authStatus == .restricted || authStatus == .denied {
              MBProgressHUD.showh(LocString("请在“设置-隐私-相机”选项中,允许'HDF wear'访问你的相机"))
          } else if PHPhotoLibrary.authorizationStatus().rawValue == 2 {
              MBProgressHUD.showh(LocString("请在“设置-隐私-照片“选项中,允许'HDF wear'访问你的照片"))
          } else {
              let picker = UIImagePickerController()
              picker.modalPresentationStyle = .fullScreen
              picker.sourceType = .camera
              picker.delegate = self
              picker.allowsEditing = true
              self.present(picker, animated: true, completion: nil)
          }
      }
  
      
  }
  
  
  
  
  extension UserInfoSettingVC: UITableViewDelegate, UITableViewDataSource {
      func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
          let view = UIView()
          view.backgroundColor = .clear
          return view
      }
      
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          return 10
      }
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          if indexPath == [0, 0] {
              return 60
          } else {
              return 50
          }
          
      }
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return 2
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return settingArray[section].count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          if indexPath == [0, 0] {
              let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell5", for: indexPath) as! SettingCell5
              cell.titleLabel.text = LocString("头像")
              if let image = UIImage.getImageFromPath("avatar") {
                  cell.detailImageView.image = image
              } else {
                  cell.detailImageView.image = UIImage(named: user.gender == 1 ? "avatar_male" : "avatar_female")
              }
              return cell
          } else {
              let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell4", for: indexPath) as! SettingCell4
              cell.titleLabel.text = LocString(settingArray[indexPath.section][indexPath.row])
              switch settingArray[indexPath.section][indexPath.row] {
              case "昵称":
                  if user.name == "" {
                      cell.detailLabel.text = "\(LocString("用户"))\(user.userId)"
                  } else {
                      cell.detailLabel.text = user.name
                  }
              case "性别":
                  cell.detailLabel.text = user.gender == 1 ? LocString("男") : LocString("女")
              case "生日":
                  cell.detailLabel.text = user.birth.toString(.custom("yyyy-MM-dd"))
              case "年龄":
                  cell.detailLabel.text = "\(user.age)\(LocString("岁"))"
              case "身高":
                  cell.detailLabel.text = "\(user.height)cm"
              case "体重":
                  cell.detailLabel.text = "\(user.weight)kg"
              default:
                  break
              }
              return cell
          }
          
      }
      
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let cell = tableView.cellForRow(at: indexPath)
          cell?.isSelected = false
          switch settingArray[indexPath.section][indexPath.row] {
          case "昵称":
              let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Mine", identifier: "ModifyNicknameVC") as! ModifyNicknameVC
              vc.nameClosure = {[weak self] name in
                  self?.user.name = name
                  AdminHelper.shared.updateUser((self?.user)!)
                  self?.modifyClosure?(name, nil)
                  self?.tableView.reloadRows(at: [indexPath], with: .none)
              }
              navigationController?.pushViewController(vc, animated: true)
          case "性别":
              let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Mine", identifier: "ModifyGenderVC") as! ModifyGenderVC
              vc.selectedRow = 1-user.gender
              vc.genderClosure = {[weak self] gender in
                  self?.user.gender = gender
                  self?.updateGender()
              }
              navigationController?.pushViewController(vc, animated: true)
          case "生日":
              let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Mine", identifier: "ModifyBirthdayVC") as! ModifyBirthdayVC
              vc.birthdayClosure = {[weak self] date, age in
                  self?.user.birth = date
                  self?.user.age = age
                  self?.updateUserInfo()
              }
              navigationController?.pushViewController(vc, animated: true)
          case "体重":
              let values = (40..<121).map { (i) -> Int in
                  return i
              }
              let pickerView = ZCPickerView(title: LocString("体重"), values: values, suffix: "kg")
              pickerView.selectedRow = values.firstIndex(of: user.weight) ?? 26
              pickerView.show()
              pickerView.clickClosure = {[weak self] value in
                  self?.user.weight = value
                  self?.updateUserInfo()
              }
          case "身高":
              let values = (120..<220).map { (i) -> Int in
                  return i
              }
              let pickerView = ZCPickerView(title: LocString("身高"), values: values, suffix: "cm")
              pickerView.selectedRow = values.firstIndex(of: user.height) ?? 56
              pickerView.show()
              pickerView.clickClosure = {[weak self] value in
                  self?.user.height = value
                  self?.updateUserInfo()
              }
          case "头像":
              changeAvatar()
          default:
              break
          }
      }
      
  
  }