Blame view

Twear/Setting/AlarmClockVC.swift 3.4 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  //
  //  AlarmClockVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/24.
  //
  
  import UIKit
  import MBProgressHUD
  
  class AlarmClockVC: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var noClockView: UIView!
      
      var alarmClockArray = CurDevice.alarmClocks
      
      override func viewWillAppear(_ animated: Bool) {
582f536d   yangbin   common:2022.1.28
19
          super.viewWillAppear(animated)
75d24c15   yangbin   123
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
          alarmClockArray = CurDevice.alarmClocks
          noClockView.isHidden = alarmClockArray.count > 0
          tableView.isHidden = alarmClockArray.count == 0
          tableView.reloadData()
      }
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("闹钟提醒")
          
          let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          addButton.setImage(UIImage(named: "device_add"), for: .normal)
          addButton.addTarget(self, action: #selector(add), for: .touchUpInside)
          navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: addButton)
          
          
          tableView.register(UINib.init(nibName: "AlarmClockCell", bundle: Bundle.main), forCellReuseIdentifier: "AlarmClockCell")
          tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
          tableView.tableFooterView = UIView(frame: CGRect.zero)
      }
      
      
      @objc private func add() {
          if alarmClockArray.count >= 5 {
              MBProgressHUD.showh(LocString("您最多可设置5个闹钟"))
              return
          }
          let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "EditAlarmClockVC")
          navigationController?.pushViewController(vc, animated: true)
      }
  
      private func switchValueChanged(_ isOn: Bool, index: Int) {
          alarmClockArray[index].isOn = isOn
          let device = CurDevice
          device.alarmClocks = alarmClockArray
          BluetoothManager.shared.setAlarmClock(alarmClockArray) { error in
              if error == nil {
                  AdminHelper.shared.updateDevice(device)
              }
          }
      }
      
      deinit {
          print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
      }
  }
  
  
  extension AlarmClockVC: UITableViewDelegate, UITableViewDataSource {
  
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 85
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return alarmClockArray.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "AlarmClockCell", for: indexPath) as! AlarmClockCell
          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)
      }
  }