Blame view

HDFwear/Home/HomePageSortVC.swift 6.37 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  //
  //  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": "运动记录", "Pressure": "压力", "Mett": "梅脱"]
45b9dc3a   daifengyi   feat:sort page UI
21
      let iconDic: [String: String] = ["BloodPressure": "home_bp", "BloodOxygen": "home_bo", "HeartRate": "home_hr", "Sleep": "home_sleep", "Train": "home_train",  "WomenHealth": "home_women_health", "MotionRecord": "home_motion"]
f2cf74c7   yangbin   1.0.20(4)
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
      
      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()
84bd9999   daifengyi   feat:home page ic...
47
          title = LocString("编辑健康功能")
f2cf74c7   yangbin   1.0.20(4)
48
49
          tableView.setEditing(true, animated: false)
          tableView.contentInset = UIEdgeInsets.init(top: 5, left: 0, bottom: 0, right: 0)
45b9dc3a   daifengyi   feat:sort page UI
50
51
52
          tableView.separatorStyle = .none
          tableView.register(UINib(nibName: "SortPageSectionHeader", bundle: .main), forHeaderFooterViewReuseIdentifier: "SortPageSectionHeader")
          tableView.register(UINib(nibName: "SortPageCell", bundle: .main), forCellReuseIdentifier: "SortPageCell")
f2cf74c7   yangbin   1.0.20(4)
53
54
55
          
          ary2 = device.allFunctions
          testDate()
45b9dc3a   daifengyi   feat:sort page UI
56
  //        tableView.tableFooterView = footerView
f2cf74c7   yangbin   1.0.20(4)
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
  
          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 {
45b9dc3a   daifengyi   feat:sort page UI
88
          let cell  = tableView.dequeueReusableCell(withIdentifier: "SortPageCell", for: indexPath) as! SortPageCell
f2cf74c7   yangbin   1.0.20(4)
89
          if indexPath.section == 1 {
45b9dc3a   daifengyi   feat:sort page UI
90
91
              cell.iconVIew.image = UIImage(named: iconDic[ary2[indexPath.row]]!)
              cell.nameLabel.text = LocString(dic[ary2[indexPath.row]]!)
f2cf74c7   yangbin   1.0.20(4)
92
          } else {
45b9dc3a   daifengyi   feat:sort page UI
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
              cell.iconVIew.image = UIImage(named: iconDic[ary1[indexPath.row]]!)
              cell.nameLabel.text = LocString(dic[ary1[indexPath.row]]!)
          }
          return cell
      }
      
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SortPageSectionHeader") as! SortPageSectionHeader
          if section == 0 {
              header.topLabelTopConstraint.constant = 8
              header.topLabel.text = LocString("显示在首页")
              header.bottomLabel.text = LocString("长按并拖拽可调整排序")
          }else {
              header.topLabelTopConstraint.constant = 15
              header.topLabel.text = LocString("不显示在首页")
              header.bottomLabel.text = ""
f2cf74c7   yangbin   1.0.20(4)
109
          }
45b9dc3a   daifengyi   feat:sort page UI
110
          return header
f2cf74c7   yangbin   1.0.20(4)
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
      }
      
      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 {
45b9dc3a   daifengyi   feat:sort page UI
155
          return 72
f2cf74c7   yangbin   1.0.20(4)
156
157
      }
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
45b9dc3a   daifengyi   feat:sort page UI
158
          return 0
f2cf74c7   yangbin   1.0.20(4)
159
160
      }
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
45b9dc3a   daifengyi   feat:sort page UI
161
          return 57
f2cf74c7   yangbin   1.0.20(4)
162
163
164
165
166
167
168
169
170
171
172
173
174
175
      }
  //    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  //        if section == 1 {
  //            let view = UIView()
  //            let label = UILabel()
  //            label.font =
  //            return view
  //        } else {
  //            return nil
  //        }
  //    }
      
      
  }