Blame view

Twear/Home/WomenHealthVC.swift 9.46 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
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
  //
  //  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
      
      @IBOutlet weak var dateLabel: UILabel!
      @IBOutlet weak var cycleLabel: UILabel!
      @IBOutlet weak var daysLabel: UILabel!
      
      var setClosure: (() -> ())?
      
      var isFirst: Bool = false
      
      
      @IBOutlet weak var firstView: UIView!
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(false, animated: true)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("经期设置")
          
          if menstrual.days == 0 {
              daysLabel.text = "--\(LocString("天"))"
              cycleLabel.text = "--\(LocString("天"))"
              dateLabel.text = LocString("未选择")
          } else {
              daysLabel.text = "\(menstrual.days)\(LocString("天"))"
              cycleLabel.text = "\(menstrual.cycle)\(LocString("天"))"
              dateLabel.text = menstrual.lastDate!.toString(.custom("yyyy-MM-dd"))
          }
          
          
          if isFirst {
              firstView.isHidden = false
          }
          
          
          let saveButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          saveButton.setTitle(LocString("保存"), for: .normal)
          saveButton.titleLabel?.font = RegularFont(14)
          saveButton.setTitleColor(UIColor.rgbColorFromHex(0xEF3257), for: .normal)
          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
          }
          if dateLabel.text == LocString("未选择") {
              MBProgressHUD.showh(LocString("最近一次月经没有填哟"))
              return
          }
          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 = (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
          }
          let pickerView = ZCPickerView(title: LocString("选择经期天数"), values: values, suffix: LocString("天"), titleColor: UIColor.rgbColorFromHex(0xEF3257))
          pickerView.selectedRow = values.firstIndex(of: menstrual.days) ?? 4
          pickerView.show()
          pickerView.clickClosure = {[weak self] value in
              self?.daysLabel.text = "\(value)天"
              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
           }
           let pickerView = ZCPickerView(title: LocString("选择周期长度"), values: values, suffix: LocString("天"), titleColor: UIColor.rgbColorFromHex(0xEF3257))
           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)天"
               self?.menstrual.cycle = value
           }
       }
  
  
      @IBAction func setLastMenstrual(_ sender: Any) {
          let pickerView = DatePickerView(title: LocString("最近一次月经"), titleColor: UIColor.rgbColorFromHex(0xEF3257))
          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
          }
      }
      
      
      
      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 = (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)
      }
  }
  
  /*
  
  */