Blame view

Twear/Setting/OtherSettingVC.swift 8.2 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  //
  //  OtherSettingVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/22.
  //
  
  import UIKit
  import SnapKit
  import MBProgressHUD
  
  class OtherSettingVC: UIViewController {
  
      @IBOutlet weak var tableView: UITableView!
      
be19e595   yangbin   9
16
17
      var unitSettingClosure: ((_ distanceUnit: DistanceUnit) -> ())?
      
75d24c15   yangbin   123
18
19
20
21
22
      let settingDic: [String: [String]] = ["时间单位设置": ["24小时", "12小时"], "公英制单位设置": ["公制", "英制"], "温度单位": ["摄氏度(℃)", "华氏度(℉)"]]
      let settingArray: [String] = {
          switch CurDevice.platform {
          case ._816:
              return ["公英制单位设置", "温度单位"]
582f536d   yangbin   common:2022.1.28
23
24
          case ._828:
              return  ["时间单位设置", "公英制单位设置"]
75d24c15   yangbin   123
25
26
27
28
29
30
31
32
33
34
35
36
37
38
          default:
              return ["时间单位设置", "公英制单位设置", "温度单位"]
          }
      }()
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("其他设置")
  
          tableView.register(UINib.init(nibName: "SettingCell2", bundle: Bundle.main), forCellReuseIdentifier: "SettingCell2")
          tableView.tableFooterView = UIView(frame: CGRect.zero)
  //        tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 10, right: 0)
  //        tableView.backgroundColor = .red
      }
be19e595   yangbin   9
39
40
41
42
43
44
45
46
47
48
49
50
51
      @IBAction func syncTime(_ sender: Any) {
          let device = CurDevice
          MBProgressHUD.showh(LocString("正在同步中..."))
          BluetoothManager.shared.setTime(format: TimeFormat(rawValue: UInt8(device.timeFormat))!) { error in
              if error == nil {
                  MBProgressHUD.showh(LocString("同步完成"))
              } else {
                  MBProgressHUD.showh(LocString("同步失败"))
              }
          }
         
      }
      
75d24c15   yangbin   123
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
  }
  
  
  extension OtherSettingVC: UITableViewDelegate, UITableViewDataSource {
  
      
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          return 30
      }
  
      
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let view = UIView()
          view.backgroundColor = UIColor.rgbColorFromHex(0xF2F2F2)
          let label = UILabel()
          label.font = RegularFont(11)
          label.text = LocString(settingArray[section])
          view.addSubview(label)
          label.snp.makeConstraints { make in
              make.left.equalToSuperview().offset(12)
              make.top.equalToSuperview().offset(7.5)
          }
          return view
      }
      
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          return 0.5
      }
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return settingArray.count
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return settingDic[settingArray[section]]!.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell2", for: indexPath) as! SettingCell2
          let text = settingDic[settingArray[indexPath.section]]![indexPath.row]
          cell.label.text = LocString(text)
          
          let device = CurDevice
          switch settingArray[indexPath.section] {
          case "时间单位设置":
582f536d   yangbin   common:2022.1.28
101
              if device.timeFormat == 0 && text == "24小时" {
75d24c15   yangbin   123
102
                  cell.selectImageView.image = UIImage(named: "setting_selected")
582f536d   yangbin   common:2022.1.28
103
              } else if device.timeFormat == 1 && text == "12小时" {
75d24c15   yangbin   123
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
                  cell.selectImageView.image = UIImage(named: "setting_selected")
              }
          case "公英制单位设置":
              if text == "公制" {
                  if device.distanceUnit == 0 {
                      cell.selectImageView.image = UIImage(named: "setting_selected")
                  }
                  let str1 = LocString("公制")
                  let str2 = LocString("(米/公里/千克)")
                  let str = NSMutableAttributedString(string: "\(str1)\(str2)")
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(0, str1.count))
                  str.addAttributes([.font: RegularFont(11)], range: NSMakeRange(str1.count, str2.count))
                  cell.label.attributedText = str
              } else if text == "英制" {
                  if device.distanceUnit == 1 {
                      cell.selectImageView.image = UIImage(named: "setting_selected")
                  }
                  let str1 = LocString("英制")
                  let str2 = LocString("(英寸/英里/英镑)")
                  let str = NSMutableAttributedString(string: "\(str1)\(str2)")
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(0, str1.count))
                  str.addAttributes([.font: RegularFont(11)], range: NSMakeRange(str1.count, str2.count))
                  cell.label.attributedText = str
              }
              
          case "温度单位":
              if device.temperatureUnit == 0 && text == "摄氏度(℃)" {
                  cell.selectImageView.image = UIImage(named: "setting_selected")
              } else if device.temperatureUnit == 1 && text == "华氏度(℉)" {
                  cell.selectImageView.image = UIImage(named: "setting_selected")
              }
          default:
              break
          }
          
          return cell
      }
      
      func updateUI(_ indexPath: IndexPath, device: DeviceModel) {
          MBProgressHUD.hide()
          AdminHelper.shared.updateDevice(device)
          let cell = tableView.cellForRow(at: indexPath) as! SettingCell2
          let cell1 = tableView.cellForRow(at: IndexPath(row: 1-indexPath.row, section: indexPath.section)) as! SettingCell2
          cell.selectImageView.image = UIImage(named: "setting_selected")
          cell1.selectImageView.image = UIImage(named: "setting_not_selected")
      }
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let cell = tableView.cellForRow(at: indexPath) as! SettingCell2
          cell.isSelected = false
          _ = MBProgressHUD.showMessage("")
          let device = CurDevice
          switch settingDic[settingArray[indexPath.section]]![indexPath.row] {
          case "24小时":
582f536d   yangbin   common:2022.1.28
158
              BluetoothManager.shared.setTime(format: .hour_24) {[weak self] error in
75d24c15   yangbin   123
159
                  if error == nil {
582f536d   yangbin   common:2022.1.28
160
                      device.timeFormat = 0
75d24c15   yangbin   123
161
162
163
164
                      self?.updateUI(indexPath, device: device)
                  }
              }
          case "12小时":
582f536d   yangbin   common:2022.1.28
165
              BluetoothManager.shared.setTime(format: .hour_12) {[weak self] error in
75d24c15   yangbin   123
166
                  if error == nil {
582f536d   yangbin   common:2022.1.28
167
                      device.timeFormat = 1
75d24c15   yangbin   123
168
169
170
171
172
173
                      self?.updateUI(indexPath, device: device)
                  }
              }
          case "公制":
              BluetoothManager.shared.setUnit(.meter, TemperatureUnit(rawValue: UInt8(device.temperatureUnit))!) {[weak self] error in
                  if error == nil {
582f536d   yangbin   common:2022.1.28
174
                      device.distanceUnit = 0
75d24c15   yangbin   123
175
                      self?.updateUI(indexPath, device: device)
be19e595   yangbin   9
176
                      self?.unitSettingClosure?(.meter)
75d24c15   yangbin   123
177
178
179
180
181
                  }
              }
          case "英制":
              BluetoothManager.shared.setUnit(.foot, TemperatureUnit(rawValue: UInt8(device.temperatureUnit))!) {[weak self] error in
                  if error == nil {
582f536d   yangbin   common:2022.1.28
182
                      device.distanceUnit = 1
75d24c15   yangbin   123
183
                      self?.updateUI(indexPath, device: device)
be19e595   yangbin   9
184
                      self?.unitSettingClosure?(.foot)
75d24c15   yangbin   123
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
                  }
              }
          case "摄氏度(℃)":
              BluetoothManager.shared.setUnit(DistanceUnit(rawValue: UInt8(device.distanceUnit))!, .celsius) {[weak self] error in
                  if error == nil {
                      device.temperatureUnit = 0
                      self?.updateUI(indexPath, device: device)
                  }
              }
          case "华氏度(℉)":
              BluetoothManager.shared.setUnit(DistanceUnit(rawValue: UInt8(device.distanceUnit))!, .fahrenheit) {[weak self] error in
                  if error == nil {
                      device.temperatureUnit = 1
                      self?.updateUI(indexPath, device: device)
                  }
              }
          default:
              break
          }
      }
  }