Blame view

Twear/Home/HomePageSortVC.swift 5.16 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
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
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
  //
  //  HomePageSortVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/31.
  //
  
  import UIKit
  
  
  class HomePageSortVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
      @IBOutlet weak var tableView: UITableView!
      var sortClosure: ((_ homePage: [String]) -> ())?
      
      let device = CurDevice
      var ary1: [String] = [String]()
      var ary2 = [String]()
      
      let dic: [String: String] = ["BloodPressure": "血压", "BloodOxygen": "血氧", "HeartRate": "心率", "Sleep": "睡眠", "Train": "训练",  "WomenHealth": "女性健康", "MotionRecord": "运动记录"]
      
      lazy var footerView: UIView = {
         let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 200))
          let label = UILabel(frame: CGRect(x: 40, y: 0, width: SCREEN_WIDTH-80, height: 60))
          label.font = RegularFont(11)
          label.textAlignment = .center
          label.text = LocString("点击左侧减去或者增加按钮自定义卡片,上下拖动右侧图标对卡片进行排序。")
          label.numberOfLines = 0
  
          view.addSubview(label)
          return view
      }()
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          self.navigationController?.setNavigationBarHidden(false, animated: true)
      }
      
      override func viewDidDisappear(_ animated: Bool) {
          super.viewDidDisappear(animated)
          sortClosure?(ary1)
      }
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("自定义首页")
          tableView.setEditing(true, animated: false)
          tableView.contentInset = UIEdgeInsets.init(top: 5, left: 0, bottom: 0, right: 0)
          
          ary2 = device.allFunctions
          testDate()
          tableView.tableFooterView = footerView
  
          for a in ary1 {
              if let i = ary2.firstIndex(of: a) {
                  ary2.remove(at: i)
              }
          }
          
          // Do any additional setup after loading the view.
      }
      
      func testDate() {
          if IsTest {
              if let i = ary2.firstIndex(of: "BloodPressure") {
                  ary2.remove(at: i)
              }
              if let i = ary2.firstIndex(of: "BloodOxygen") {
                  ary2.remove(at: i)
              }
  
          }
      }
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return 2
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          section == 0 ? ary1.count :  ary2.count
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          var cell  = tableView.dequeueReusableCell(withIdentifier: "SortCellId")
          if cell == nil {
              cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "SortCellId")
          }
          cell?.textLabel?.font = RegularFont(15)
          if indexPath.section == 1 {
              cell?.textLabel?.text = LocString(dic[ary2[indexPath.row]]!)
          } else {
              cell?.textLabel?.text = LocString(dic[ary1[indexPath.row]]!)
          }
          return cell!
      }
      
      func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
          return true
      }
      
      func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
          return indexPath.section == 0
      }
  
      
      func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  
          let obj = ary1.remove(at: sourceIndexPath.row)
          if destinationIndexPath.section == 1 {
              ary2.insert(obj, at: destinationIndexPath.row)
          } else {
              ary1.insert(obj, at: destinationIndexPath.row)
          }
          device.homePage = ary1
          AdminHelper.shared.updateDevice(device)
          tableView.reloadData()
      }
      
      func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
          return indexPath.section == 0 ? .delete : .insert
      }
      
      func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              let obj = ary1.remove(at: indexPath.row)
              ary2.append(obj)
              ary2.sort()
          } else {
              let obj = ary2.remove(at: indexPath.row)
              ary1.append(obj)
          }
          device.homePage = ary1
          AdminHelper.shared.updateDevice(device)
          tableView.reloadData()
      }
      
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          return 35
      }
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          return 0.5
      }
  //    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  //        if section == 1 {
  //            let view = UIView()
  //            let label = UILabel()
  //            label.font =
  //            return view
  //        } else {
  //            return nil
  //        }
  //    }
      
      
  }