Blame view

Twear/Setting/ContactsViewController.swift 11.5 KB
582f536d   yangbin   common:2022.1.28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  //
  //  ContactsViewController.swift
  //  Twear
  //
  //  Created by yangbin on 2022/1/15.
  //
  
  import UIKit
  import MBProgressHUD
  
  class ContactsViewController: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var noContactsView: UIView!
      
      @IBOutlet weak var syncButton: CustomProgress!
be19e595   yangbin   9
17
      var contacts: [ContactModel] = UserInfo.contacts
582f536d   yangbin   common:2022.1.28
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
  //    var isEdit: Bool = false
      
      
      lazy var footerView: UIView =  {
          let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 50))
          let button = UIButton(frame: CGRect(x: 0, y: 10, width: 54, height: 30))
          button.addTarget(self, action: #selector(add), for: .touchUpInside)
          button.setImage(UIImage(named: "contact_add"), for: .normal)
          view.addSubview(button)
          return view
      }()
      
      lazy var headerView: UIView =  {
          let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 45))
          let label = UILabel(frame: CGRect(x: 12.5, y: 2.5, width: SCREEN_WIDTH-30, height: 40))
          label.font = RegularFont(11)
          label.textColor = UIColor.rgbColorFromHex(0x808080)
          label.numberOfLines = 0
          label.text = LocString("可以选定通讯录中的8个联系人,将其快捷方式显示在手表上。")
          view.addSubview(label)
          return view
      }()
      
      
      lazy var rightButton: UIButton = {
          let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          addButton.setImage(UIImage(named: "device_add"), for: .normal)
          addButton.setTitle(nil, for: .normal)
          addButton.titleLabel?.contentMode = .right
          addButton.titleLabel?.font = RegularFont(14)
          addButton.setTitleColor(.black, for: .normal)
          addButton.addTarget(self, action: #selector(clickRightButton), for: .touchUpInside)
          return addButton
      }()
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
  //        contacts = UserInfo.contacts
  //        noContactsView.isHidden = contacts.count > 0
  //        tableView.isHidden = contacts.count == 0
  //        tableView.reloadData()
      }
      
  
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("快捷通讯")
          
          refreshUI()
      
          navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: rightButton)
          tableView.setEditing(true, animated: true)
          
          tableView.register(UINib.init(nibName: "ContactCell", bundle: Bundle.main), forCellReuseIdentifier: "ContactCell")
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  //        tableView.tableFooterView = UIView(frame: CGRect.zero)
          tableView.tableFooterView = footerView
          tableView.tableHeaderView = headerView
          // Do any additional setup after loading the view.
          
          syncButton.label.text = LocString("传输")
          syncButton.label.textColor = UIColor.white
          syncButton.label.font = RegularFont(15)
  //        syncButton.layer.borderWidth = 1
  //        syncButton.layer.borderColor = UIColor.rgbColorFromHex(0x00993E).cgColor
          syncButton.backView.backgroundColor = UIColor.rgbColorFromHex(0x00993E)
          syncButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(syncContacts(_:))))
582f536d   yangbin   common:2022.1.28
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
          if contacts.count == 0 {
              syncButton.isHidden = true
          }
      }
      
      func updateProgressView(_ value: Int) {
          if contacts.count > 0 {
              syncButton.isHidden = false
          }
          if value == -1 {
              syncButton.value = 0
              syncButton.label.text = LocString("传输")
              syncButton.label.textColor = .white
              syncButton.layer.borderWidth = 0
              syncButton.backView.backgroundColor = UIColor.rgbColorFromHex(0x00993E)
          } else if value < 100 {
              syncButton.value = value
              syncButton.backView.backgroundColor = .white
              syncButton.trackView.backgroundColor = UIColor.rgbColorFromHex(0x64C876)
              syncButton.layer.borderWidth = 1
              syncButton.layer.borderColor = UIColor.rgbColorFromHex(0x00993E).cgColor
              syncButton.label.text = LocString("联系人数据传输中...")
              syncButton.label.textColor = .black
          } else if value == 100 {
              syncButton.value = 100
              syncButton.trackView.backgroundColor = LineColor
              syncButton.layer.borderColor = LineColor.cgColor
              syncButton.layer.borderWidth = 0
              syncButton.label.text = LocString("传输完成")
              syncButton.label.textColor = .white
be19e595   yangbin   9
115
116
117
              let user = UserInfo
              user.contacts = contacts
              AdminHelper.shared.updateUser(user)
582f536d   yangbin   common:2022.1.28
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
          }
      }
      
      
  
      
      @IBAction func syncContacts(_ gestureRecognizer: UITapGestureRecognizer) {
          //0-2 3-5 6-7
          var array = [ContactModel](repeating: ContactModel(), count: 8)
          for (i, model) in contacts.enumerated() {
              array[i] = model
          }
          updateProgressView(0)
          sync1(array)
      }
      
      
      func sync1(_ contactArray: [ContactModel]) {
          BluetoothManager.shared.syncContacts(Array(contactArray[0...2]), num: 1) {[weak self] error in
              if error == nil {
                  print("同步联系人1")
                  self?.sync2(contactArray)
                  self?.updateProgressView(33)
582f536d   yangbin   common:2022.1.28
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
              } else {
                  self?.updateProgressView(-1)
              }
          }
      }
      
      func sync2(_ contactArray: [ContactModel]) {
          BluetoothManager.shared.syncContacts(Array(contactArray[3...5]), num: 2) {[weak self] error in
              if error == nil {
                  print("同步联系人2")
                  self?.updateProgressView(66)
                  self?.sync3(contactArray)
                 
              } else {
                  self?.updateProgressView(-1)
              }
          }
      }
      
      func sync3(_ contactArray: [ContactModel]) {
          BluetoothManager.shared.syncContacts(Array(contactArray[6...7]), num: 3) {[weak self] error in
              if error == nil {
                  print("同步联系人3")
                  self?.updateProgressView(100)
              } else {
                  self?.updateProgressView(-1)
              }
          }
      }
      
      @objc private func add() {
582f536d   yangbin   common:2022.1.28
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
          let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "PhoneContactsVC") as! PhoneContactsVC
          vc.preContacts = contacts
          vc.contactClosure = { [weak self] array in
              self?.selectContacts(array)
              self?.updateProgressView(-1)
          }
          navigationController?.pushViewController(vc, animated: true)
      }
      
      @objc private func clickRightButton() {
  //        if contacts.count == 0 {
              add()
  //        } else {
  //            isEdit = !isEdit
  //            tableView.setEditing(isEdit, animated: true)
  //        }
      }
      
      
      func refreshUI() {
          noContactsView.isHidden = contacts.count > 0
          tableView.isHidden = contacts.count == 0
          footerView.isHidden = contacts.count == 8
          if contacts.count == 0 {
              rightButton.setImage(UIImage(named: "device_add"), for: .normal)
              rightButton.setTitle(nil, for: .normal)
          } else {
              rightButton.setTitle(LocString("全部修改"), for: .normal)
              rightButton.setImage(nil, for: .normal)
              var sos: Bool = false
              for contact in contacts {
                  if contact.isSOS  {
                      sos = true
                  }
              }
              if !sos {
                  contacts[0].isSOS = true
              }
          }
  
          tableView.reloadData()
      }
      
      func selectContacts(_ array: [ContactModel]) {
          contacts = array
          refreshUI()
    
      }
      
      func selectSosContact(_ item: Int) {
          if contacts[item].isSOS {
              contacts[item].isSOS = false
          } else {
              for contact in contacts {
                  contact.isSOS = false
              }
              contacts[item].isSOS = true
          }
          updateProgressView(-1)
          tableView.reloadData()
      }
      
  
      deinit {
          print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
      }
  
  }
  
  
  extension ContactsViewController: UITableViewDelegate, UITableViewDataSource {
  
      func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
          return .delete//UITableViewCell.EditingStyle(rawValue: UITableViewCell.EditingStyle.delete.rawValue | UITableViewCell.EditingStyle.insert.rawValue)!
      }
  //
  //    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
  //        return false
  //    }
  //
  //    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  //        let delete = UITableViewRowAction(style: .normal, title: LocString("删除")) {[weak self] action, index in
  //            self?.contacts.remove(at: indexPath.row)
  //            tableView.deleteRows(at: [indexPath], with: .automatic)
  //         }
  //         delete.backgroundColor = UIColor.red
  //         return [delete]
  //    }
  //
      func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              contacts.remove(at: indexPath.row)
              tableView.deleteRows(at: [indexPath], with: .automatic)
              updateProgressView(-1)
              refreshUI()
          }
      }
      
      func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
          if sourceIndexPath != destinationIndexPath {
              let contact = contacts[sourceIndexPath.row]
              contacts.remove(at: sourceIndexPath.row)
              contacts.insert(contact, at: destinationIndexPath.row)
              updateProgressView(-1)
          }
          tableView.reloadData()
      }
      
      
      func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
          return true
      }
      
      
      func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
          return true
      }
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return contacts.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! ContactCell
          cell.isPhone = false
          cell.contact = contacts[indexPath.row]
          cell.sosClosure = {[weak self] in
              self?.selectSosContact(indexPath.row)
          }
  //        cell.alarmClock = alarmClockArray[indexPath.row]
  //        cell.switchClosure = {[weak self] isOn in
  //            self?.switchValueChanged(isOn, index: indexPath.row)
  //        }
          return cell
      }
      
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  //        let cell = tableView.cellForRow(at: indexPath) as! SettingCell3
  //        cell.isSelected = false
  //        let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "EditAlarmClockVC") as! EditAlarmClockVC
  //        vc.alarmClock = CurDevice.alarmClocks[indexPath.row]
  //        vc.index = indexPath.row
  //        vc.isEdit = true
  //        navigationController?.pushViewController(vc, animated: true)
      }
  }