Blame view

HDFwear/Home/WomenHealthVC.swift 9.78 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  //
  //  WomenHealthVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/11/25.
  //
  
  import UIKit
  import MBProgressHUD
  import SwiftDate
  import CoreMedia
  
  class WomenHealthVC: UIViewController {
      
      let menstrual = UserInfo.menstrual
      
1a226870   daifengyi   feat:each functio...
17
  //    @IBOutlet weak var dateLabel: UILabel!
f2cf74c7   yangbin   1.0.20(4)
18
19
20
21
22
23
24
25
      @IBOutlet weak var cycleLabel: UILabel!
      @IBOutlet weak var daysLabel: UILabel!
      
      var setClosure: (() -> ())?
      
      var isFirst: Bool = false
      
      
1a226870   daifengyi   feat:each functio...
26
  //    @IBOutlet weak var firstView: UIView!
f2cf74c7   yangbin   1.0.20(4)
27
28
29
30
31
32
33
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(false, animated: true)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
1a226870   daifengyi   feat:each functio...
34
          title = LocString("生理周期设置")
f2cf74c7   yangbin   1.0.20(4)
35
36
37
38
          
          if menstrual.days == 0 {
              daysLabel.text = "--\(LocString("天"))"
              cycleLabel.text = "--\(LocString("天"))"
1a226870   daifengyi   feat:each functio...
39
  //            dateLabel.text = LocString("未选择")
f2cf74c7   yangbin   1.0.20(4)
40
41
42
          } else {
              daysLabel.text = "\(menstrual.days)\(LocString("天"))"
              cycleLabel.text = "\(menstrual.cycle)\(LocString("天"))"
1a226870   daifengyi   feat:each functio...
43
  //            dateLabel.text = menstrual.lastDate!.toString(.custom("yyyy-MM-dd"))
f2cf74c7   yangbin   1.0.20(4)
44
45
46
47
          }
          
          
          if isFirst {
1a226870   daifengyi   feat:each functio...
48
  //            firstView.isHidden = false
f2cf74c7   yangbin   1.0.20(4)
49
50
51
52
53
54
          }
          
          
          let saveButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          saveButton.setTitle(LocString("保存"), for: .normal)
          saveButton.titleLabel?.font = RegularFont(14)
ebb6ae98   daifengyi   feat:data page na...
55
          saveButton.setTitleColor(TintColor, for: .normal)
f2cf74c7   yangbin   1.0.20(4)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
          saveButton.addTarget(self, action: #selector(save), for: .touchUpInside)
          navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: saveButton)
          
          // Do any additional setup after loading the view.
      }
      
      @objc func save() {
          if daysLabel.text == "--\(LocString("天"))" {
              MBProgressHUD.showh(LocString("经期天数没有填哟"))
              return
          }
          if cycleLabel.text == "--\(LocString("天"))" {
              MBProgressHUD.showh(LocString("周期长度没有填哟"))
              return
          }
1a226870   daifengyi   feat:each functio...
71
72
73
74
  //        if dateLabel.text == LocString("未选择") {
  //            MBProgressHUD.showh(LocString("最近一次月经没有填哟"))
  //            return
  //        }
f2cf74c7   yangbin   1.0.20(4)
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
          var lastDate = menstrual.lastDate!
          lastDate = lastDate - (menstrual.days - 1).days //wypwyp20220305改
          menstrual.lastDate = lastDate
          let result = getDateTypeAndPosition(date: DateInRegion().date)
          let userInfo = UserInfo
          userInfo.menstrual = menstrual
          AdminHelper.shared.updateUser(userInfo)
          let model = MenstrualCalendarModel()
          model.whichDay = result.whichDay
          model.days = result.days
          model.type = result.type
          addHistoryData()
          BluetoothManager.shared.setMenstrual(model) { Error in
              
          }
          setClosure?()
          self.navigationController?.popViewController(animated: true)
      }
      func addHistoryData() {
          let historyData = MenstrualCalendarModel.getAllDate()
          let selectedDate = DateInRegion().dateAtStartOf(.day).date
          if historyData.count == 0 {
              let n: Int = Calendar.current.dateComponents([.day], from: menstrual.lastDate!.dateAtStartOf(.day), to: selectedDate).day ?? 0//(selectedDate - menstrual.lastDate!.dateAtStartOf(.day)).in(.day) ?? 0
              for i in 0..<3*menstrual.cycle+abs(n+1) {
                  let menstrualData = MenstrualCalendarModel()
                  let date = selectedDate - i.days
                  let result = getDateTypeAndPosition(date: date)
                  menstrualData.date = date
                  menstrualData.position = result.position
                  menstrualData.type = result.type
                  menstrualData.dateStr = date.toString(.custom("yyyy.MM.dd"))
                  menstrualData.whichDay = result.whichDay
                  menstrualData.days = result.days
                  menstrualData.add()
              }
          }
      }
      
      @IBAction func setMenstrualDays(_ sender: Any) {
          let values = (3..<9).map { (i) -> Int in
              return i
          }
ebb6ae98   daifengyi   feat:data page na...
117
          let pickerView = ZCPickerView(title: LocString("选择经期天数"), values: values, suffix: LocString("天"))
f2cf74c7   yangbin   1.0.20(4)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
          pickerView.selectedRow = values.firstIndex(of: menstrual.days) ?? 4
          pickerView.show()
          pickerView.clickClosure = {[weak self] value in
              self?.daysLabel.text = "\(value)\(LocString("天"))"
              self?.menstrual.days = value
  //            self?.user.weight = value
  //            self?.updateUserInfo()
          }
      }
   
       @IBAction func setMenstrualCycle(_ sender: Any) {
           let values = (24..<41).map { (i) -> Int in
               return i
           }
ebb6ae98   daifengyi   feat:data page na...
132
           let pickerView = ZCPickerView(title: LocString("选择周期长度"), values: values, suffix: LocString("天"))
f2cf74c7   yangbin   1.0.20(4)
133
134
135
136
137
138
139
140
141
142
143
           pickerView.selectedRow = values.firstIndex(of: menstrual.cycle) ?? 6
           pickerView.show()
           pickerView.clickClosure = {[weak self] value in
   //            self?.user.weight = value
   //            self?.updateUserInfo()
               self?.cycleLabel.text = "\(value)\(LocString("天"))"
               self?.menstrual.cycle = value
           }
       }
  
  
1a226870   daifengyi   feat:each functio...
144
145
146
147
148
149
150
151
152
153
154
  //    @IBAction func setLastMenstrual(_ sender: Any) {
  //        let pickerView = DatePickerView(title: LocString("最近一次月经"))
  //        pickerView.datePicker.datePickerMode = .date
  //        pickerView.datePicker.date = Date()
  //        pickerView.datePicker.maximumDate = Date()
  //        pickerView.show()
  //        pickerView.clickClosure = {[weak self] date in
  //            self?.dateLabel.text = date.toString(.custom("yyyy-MM-dd"))
  //            self?.menstrual.lastDate = date
  //        }
  //    }
f2cf74c7   yangbin   1.0.20(4)
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
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
      
      
      
      func getDateTypeAndPosition(date: Date) -> (type: MenstrualDateModel, position: MenstrualRangePosition, whichDay: Int, days: Int) {
          var type: MenstrualDateModel = .safetyPeriod
          var position: MenstrualRangePosition = .none
          var days: Int = 0
          var whichDay: Int = 0
          var differDays: Int = Calendar.current.dateComponents([.day], from: menstrual.lastDate!.dateAtStartOf(.day), to: date).day ?? 0//(date - menstrual.lastDate!.dateAtStartOf(.day)).in(.day) ?? 0
          differDays = differDays%menstrual.cycle
          var intervalDays = 0
          if differDays < 0 {
              intervalDays = menstrual.cycle + differDays
          } else {
              intervalDays = differDays
          }
  
         
          if intervalDays < menstrual.days { //月经
              whichDay = intervalDays+1
              days = menstrual.days
              if date.isInFuture {
                  type = .predict
              } else {
                  type = .menstrualPeriod
                  if menstrual.days == 1 {
                      position = .full
                  } else {
                      if intervalDays == 0 { //第一天
                          if date.isToday {
                              position = .full
                          } else if date.day == date.monthDays || date.weekday == 7 {
                              position = .full
                          } else {
                              position = .left
                          }
                      } else if intervalDays == menstrual.days-1 {
                          if date.day == 1 || date.weekday == 1 { //最后一天
                              position = .full
                          } else {
                              position = .right
                          }
                      } else {
                          if date.day == date.monthDays || date.weekday == 7 {
                              position = .right
                          } else if date.day == 1 || date.weekday == 1 {
                              position = .left
                          } else {
                              position = .middle
                          }
                      }
                  }
              }
          } else if intervalDays > menstrual.cycle - 10 { //安全期
              whichDay = intervalDays-menstrual.cycle+10
              type = .safetyPeriod
              days = 9
          } else if intervalDays > menstrual.cycle - 20 { //易孕期
              whichDay = intervalDays-menstrual.cycle+20
              days = min(menstrual.cycle-9-menstrual.days, 10)
              type = .fertilePeriod
              //            if menstrual.cycle-19 == menstrual.cycle-10 {
              //                cell.type = .ovulationDay
              //            }
              if date.day == date.monthDays && date.weekday == 1 {
                  position = .full
              } else if date.day == 1 && date.weekday == 7 {
                  position = .full
              } else if intervalDays == menstrual.cycle-10 { //最后一天
                  if date.day == 1 || date.weekday == 1 {
                      position = .full
                  } else {
                      position = .right
                  }
              } else if intervalDays == menstrual.cycle-19 { //第一天
                  if date.day == date.monthDays || date.weekday == 7 {
                      position = .full
                  } else {
                      position = .left
                  }
              } else {
                  if intervalDays == menstrual.cycle-14 {
                      type = .ovulationDay
                  }
                  if date.day == date.monthDays || date.weekday == 7 {
                      position = .right
                  } else if date.day == 1 || date.weekday == 1 {
                      position = .left
                  } else {
                      position = .middle
                  }
              }
          } else { //安全期
              whichDay = intervalDays-menstrual.days+1
              days = menstrual.cycle-19-menstrual.days
              type = .safetyPeriod
          }
          
          
          
          
          return (type, position, whichDay, days)
      }
  }
  
  /*
  
  */