Blame view

Twear/Tools/ScanView.swift 17 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
28
29
  //
  //  ScanView.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/6.
  //
  
  import UIKit
  import SnapKit
  
  
  enum ScanViewType: Int {
      case ScanStart = 0
      case ScanComplete
      case Pairing
      case PairSuccess
      case PairFail
      case WearTip
      case FailReason
  }
  
  
  class ScanView: UIView {
      
      typealias clickActionClosure = (_ index: Int, _ action: Any?) -> Void
      var clickClosure: clickActionClosure?
      
      var deviceArray: [ScanDevice] = [] {
          didSet {
582f536d   yangbin   common:2022.1.28
30
              if type == .ScanStart {
75d24c15   yangbin   123
31
32
33
34
35
36
37
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(min(75 + max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0))
              }
              tableView.snp.updateConstraints { make in
                  make.height.equalTo(min(max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0-75))
              }
              tableView.reloadData()
582f536d   yangbin   common:2022.1.28
38
              }
75d24c15   yangbin   123
39
40
41
          }
      }
      
582f536d   yangbin   common:2022.1.28
42
      var type: ScanViewType = .ScanStart
75d24c15   yangbin   123
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
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
  
      private let bgView = UIView() //白色框
      private var tableViewHeight: CGFloat = 0
      
  //    var
      
      
      private lazy var titleLabel: UILabel = {
          let label = UILabel()
          label.font = BoldFont(13)
          label.textColor = .black
          label.textAlignment = .left
          label.text = LocString("正在搜索...")
          return label
      }()
      
      private lazy var detailLabel: UILabel = {
          let label = UILabel()
          label.font = RegularFont(11)
          label.numberOfLines = 0
          label.textColor = .black
          return label
      }()
      
      
      private lazy var cancelBtn: UIButton = {
          let button = UIButton()
          button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
          button.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
          button.titleLabel?.font = BoldFont(16)
          button.setTitle(LocString("暂不配对"), for: .normal)
          button.tag = 2
          return button
      }()
      
      private lazy var reasonBtn: UIButton = {
          let button = UIButton()
          button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
          button.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal)
          button.titleLabel?.font = RegularFont(14)
          button.setTitle(LocString("为什么配对失败?"), for: .normal)
          button.tag = 5
          return button
      }()
      
      private lazy var tableView: UITableView = {
          let tableView = UITableView(frame: CGRect(x: 0, y: 31, width: SCREEN_WIDTH-20, height: 150))
          tableView.bounces = false
          tableView.delegate = self
          tableView.dataSource = self
          tableView.separatorStyle = .none
          return tableView
      }()
      
      private lazy var splitLine: UIView = {
          let view = UIView()
          view.backgroundColor = LineColor
          return view
      }()
      
      private lazy var imageView: UIImageView = {
          let view = UIImageView()
          return view
      }()
      
      
      private lazy var loadButton: LoaderButton = {
          let button = LoaderButton()
          button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
          button.setTitle(LocString("重新搜索"), for: .normal)
          button.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal)
          button.titleLabel?.font = BoldFont(11)
          button.tag = 4
  //        button.layer.cornerRadius = 8
          
          return button
      }()
      
      
      
      init() {
          super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
          createView()
      }
      
      func createView() {
          backgroundColor = UIColor.black.withAlphaComponent(0.1)
          bgView.layer.cornerRadius = 10
          bgView.clipsToBounds = true
          bgView.backgroundColor = .white
          
          addSubview(bgView)
          bgView.addSubview(titleLabel)
          bgView.addSubview(cancelBtn)
          bgView.addSubview(tableView)
          bgView.addSubview(splitLine)
          bgView.addSubview(imageView)
          bgView.addSubview(detailLabel)
          bgView.addSubview(reasonBtn)
          bgView.addSubview(loadButton)
          imageView.isHidden = true
          detailLabel.isHidden = true
          reasonBtn.isHidden = true
          
          bgView.snp.makeConstraints { (make) in
              make.left.equalToSuperview().offset(10)
              make.right.equalToSuperview().offset(-10)
              make.bottom.equalToSuperview().offset(isiPhoneX() ? -42.5 : -22.5)
              make.height.equalTo(150+31+44)
              make.top.equalTo(titleLabel).offset(-13)
          }
          
          titleLabel.snp.makeConstraints { make in
              make.left.equalToSuperview().offset(10)
              make.top.equalToSuperview().offset(13)
              make.right.equalToSuperview().offset(-10)
          }
          
          loadButton.snp.makeConstraints { make in
              make.right.equalToSuperview()
              make.centerY.equalTo(titleLabel)
              make.width.equalTo(80)
              make.height.equalTo(26)
          }
          
          imageView.snp.makeConstraints { make in
              make.top.equalTo(titleLabel.snp_bottom).offset(23)
              make.centerX.equalToSuperview()
          }
          
          detailLabel.snp.makeConstraints { make in
              make.top.equalTo(imageView.snp_bottom).offset(25)
              make.left.equalToSuperview().offset(20)
              make.right.equalToSuperview().offset(-20)
          }
          
          tableView.snp.makeConstraints { make in
              make.left.right.equalToSuperview()
              make.top.equalToSuperview().offset(31)
              make.bottom.equalTo(splitLine.snp_top)
              make.height.equalTo(150)
          }
          
          reasonBtn.snp.makeConstraints { make in
              make.left.equalToSuperview().offset(20)
              make.bottom.equalTo(splitLine.snp_top).offset(-12)
          }
          
          splitLine.snp.makeConstraints { make in
              make.height.equalTo(0.5)
              make.right.equalToSuperview().offset(-10)
              make.left.equalToSuperview().offset(10)
              make.bottom.equalToSuperview().offset(-44)
          }
          
          cancelBtn.snp.makeConstraints { make in
              make.left.right.equalToSuperview()
              make.bottom.equalToSuperview()
              make.top.equalTo(splitLine.snp_bottom)
          }
          
  //        loadButton.toggle()
          loadButton.cornerRadius = 1
          loadButton.loaderNormalImage = UIImage(named: "微信运动")
          loadButton.startAnimate(loaderType: .circleRotate, loaderColor: UIColor.rgbColorFromHex(0x00993E), complete: nil)
      }
      
      
      func updateUI(_ type: ScanViewType) {
          
          splitLine.isHidden = false
          reasonBtn.isHidden = true
          loadButton.isHidden = true
582f536d   yangbin   common:2022.1.28
216
          self.type = type
75d24c15   yangbin   123
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
          switch type {
          case .ScanStart:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(225)
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
              cancelBtn.setTitle(LocString("暂不配对"), for: .normal)
              cancelBtn.tag = 2
              cancelBtn.isHidden = false
              
              titleLabel.font = BoldFont(13)
              titleLabel.textAlignment = .left
              titleLabel.text = LocString("正在搜索...")
              
              imageView.isHidden = true
              
              tableView.isHidden = false
              
              detailLabel.isHidden = true
              
              loadButton.isHidden = false
              loadButton.startAnimate(loaderType: .circleRotate, loaderColor: UIColor.rgbColorFromHex(0x00993E), complete: nil)
              
          case .ScanComplete:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(min(75 + max(CGFloat(deviceArray.count) * 50.0, 150), SCREEN_HEIGHT/2.0))
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal)
              cancelBtn.setTitle(LocString("暂不配对"), for: .normal)
              cancelBtn.tag = 2
              cancelBtn.isHidden = false
              
              titleLabel.font = BoldFont(13)
              titleLabel.textAlignment = .left
              titleLabel.text = LocString("搜索完成")
              
              imageView.isHidden = true
              
              tableView.isHidden = false
              
              detailLabel.isHidden = true
              
              loadButton.isHidden = false
              loadButton.stopAnimate(complete: nil)
              
          case .Pairing:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(220)
              }
              
              
              cancelBtn.isHidden = true
              
              titleLabel.font = BoldFont(15)
              titleLabel.textAlignment = .center
              titleLabel.text = LocString("正在配对...")
              
              imageView.isHidden = false
              imageView.image = UIImage(named: "pairing")
              
              tableView.isHidden = true
              
              splitLine.isHidden = true
              
              detailLabel.snp.remakeConstraints { make in
                  make.top.equalTo(imageView.snp_bottom).offset(25)
                  make.left.equalToSuperview().offset(20)
                  make.right.equalToSuperview().offset(-20)
              }
              detailLabel.text = LocString("请勿操作手机和手表")
              detailLabel.isHidden = false
              
          case .PairSuccess:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(220)
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
              cancelBtn.setTitle(LocString("完成"), for: .normal)
              cancelBtn.tag = 6
              cancelBtn.isHidden = false
              
              titleLabel.font = BoldFont(15)
              titleLabel.textAlignment = .center
              titleLabel.text = LocString("配对成功")
              
              imageView.isHidden = false
              imageView.image = UIImage(named: "pair_success")
              
              tableView.isHidden = true
              
              detailLabel.snp.remakeConstraints { make in
                  make.top.equalTo(imageView.snp_bottom).offset(25)
                  make.left.equalToSuperview().offset(20)
                  make.right.equalToSuperview().offset(-20)
              }
              detailLabel.text = LocString("恭喜你,配对成功!")
              detailLabel.isHidden = false
              
          case .PairFail:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(250)
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0xD00000), for: .normal)
              cancelBtn.setTitle(LocString("重试"), for: .normal)
              cancelBtn.tag = 3
              cancelBtn.isHidden = false
              
              
              titleLabel.font = BoldFont(15)
              titleLabel.textAlignment = .center
              titleLabel.text = LocString("配对失败")
              
              imageView.isHidden = false
              imageView.image = UIImage(named: "pair_fail")
              
              tableView.isHidden = true
              
              reasonBtn.isHidden = false
              
              detailLabel.snp.remakeConstraints { make in
                  make.top.equalTo(imageView.snp_bottom).offset(23)
                  make.left.equalToSuperview().offset(20)
                  make.right.equalToSuperview().offset(-20)
              }
              detailLabel.text = LocString("配对失败,请重试")
              detailLabel.isHidden = false
              
          case .WearTip:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(250)
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
              cancelBtn.setTitle(LocString("完成"), for: .normal)
              cancelBtn.tag = 2
              cancelBtn.isHidden = false
              
              titleLabel.font = BoldFont(15)
              titleLabel.textAlignment = .center
              titleLabel.text = LocString("佩戴提示")
              
              imageView.isHidden = false
              imageView.image = UIImage(named: "wear_tip")
              
              tableView.isHidden = true
              
              detailLabel.snp.remakeConstraints { make in
                  make.top.equalTo(imageView.snp_bottom).offset(20)
                  make.left.equalToSuperview().offset(58)
                  make.right.equalToSuperview().offset(-58)
              }
              detailLabel.text = LocString("为了准确的测量数据,和不影响APP的正常使用,请您尽量日常佩戴手表。")
              detailLabel.isHidden = false
              
          case .FailReason:
              bgView.snp.updateConstraints { make in
                  make.height.equalTo(250)
              }
              
              cancelBtn.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
              cancelBtn.setTitle(LocString("我知道了"), for: .normal)
              cancelBtn.tag = 2
              cancelBtn.isHidden = false
              
              titleLabel.font = BoldFont(13)
              titleLabel.textAlignment = .center
              titleLabel.text = LocString("可能原因")
              
              imageView.isHidden = true
              
              tableView.isHidden = true
              
              detailLabel.snp.remakeConstraints { make in
                  make.top.equalToSuperview().offset(55)
                  make.left.equalToSuperview().offset(20)
                  make.right.equalToSuperview().offset(-20)
              }
              detailLabel.text = LocString("1.距离太远,请将手表靠近手机;\n2.当前APP版本太低,请更新升级新版本后重试;\n3.您的手机此时存在短暂性网络通讯故障,请稍后重试;\n4.关闭蓝牙并删除当前已连接的蓝牙设备后,再次开启蓝牙重试;\n5.重启手表和手机后重试。")
              detailLabel.isHidden = false
              
          }
          
      }
      
      
      @objc private func clickBtnAction(_ sender: UIButton) {
582f536d   yangbin   common:2022.1.28
407
  
75d24c15   yangbin   123
408
409
410
411
412
413
414
415
416
417
418
419
420
421
          
          if sender.tag == 2 {
              dismiss()
          } else if sender.tag == 3 {
              deviceArray = []
              updateUI(.ScanStart)
          } else if sender.tag == 4 {
              deviceArray = []
              updateUI(.ScanStart)
          } else if sender.tag == 5 {
              updateUI(.FailReason)
          } else if sender.tag == 6 {
              updateUI(.WearTip)
          }
582f536d   yangbin   common:2022.1.28
422
423
424
425
          
          if (clickClosure != nil) {
              clickClosure!(sender.tag, nil)
          }
75d24c15   yangbin   123
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
      }
      
      
      
      func show() {
          let window = KeyWindow
          window.addSubview(self)
          self.alpha = 0
          UIView.animate(withDuration: 0.25, animations: {
  //            self.bgView.frame.origin.y = SCREEN_HEIGHT - self.tableViewHeight
              self.alpha = 1
          }, completion: { (finish) -> Void in
              
          })
      }
      
      @objc func dismiss() {
          UIView.animate(withDuration: 0.25, animations: { () -> Void in
              self.alpha = 0
          }, completion: { (finish) -> Void in
              if finish {
                  self.removeFromSuperview()
              }
          })
          if GCDTimer.shared.isExistTimer(WithTimerName: "StartScan") {
              GCDTimer.shared.cancleTimer(WithTimerName: "StartScan")
          }
          if GCDTimer.shared.isExistTimer(WithTimerName: "DevicePairing") {
              GCDTimer.shared.cancleTimer(WithTimerName: "DevicePairing")
          }
      }
      
      deinit {
          print("ScanView dealloc")
      }
      
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
      /*
      // Only override draw() if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      override func draw(_ rect: CGRect) {
          // Drawing code
      }
      */
  
  }
  
  
  extension ScanView: UITableViewDelegate, UITableViewDataSource {
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return deviceArray.count
      }
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          var cell = tableView.dequeueReusableCell(withIdentifier: "ScanView")
          if cell == nil {
              cell = UITableViewCell(style: .subtitle, reuseIdentifier: "ScanView")
          }
          cell?.accessoryType = .disclosureIndicator
          cell?.textLabel?.text = deviceArray[indexPath.row].peripheral.name ?? " "
          cell?.textLabel?.font = BoldFont(15)
          cell?.detailTextLabel?.text = "mac: \(deviceArray[indexPath.row].mac)"
          cell?.detailTextLabel?.font = RegularFont(11)
          return cell!
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let cell = tableView.cellForRow(at: indexPath)
          cell?.isSelected = false
          
          if (clickClosure != nil) {
              clickClosure!(6, deviceArray[indexPath.row].peripheral)
          }
      }
      
  }