Timers.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Timers.swift
  3. // LoopFollow
  4. //
  5. // Created by Jon Fawcett on 9/3/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension MainViewController {
  11. func restartAllTimers() {
  12. if !bgTimer.isValid { startBGTimer(time: 2) }
  13. if !profileTimer.isValid { startProfileTimer(time: 3) }
  14. if !deviceStatusTimer.isValid { startDeviceStatusTimer(time: 4) }
  15. if !treatmentsTimer.isValid { startTreatmentsTimer(time: 5) }
  16. if !minAgoTimer.isValid { startMinAgoTimer(time: minAgoTimeInterval) }
  17. if !calendarTimer.isValid { startCalendarTimer(time: 15) }
  18. if !alarmTimer.isValid { startAlarmTimer(time: 30) }
  19. }
  20. func invalidateTimers() {
  21. bgTimer.invalidate()
  22. profileTimer.invalidate()
  23. deviceStatusTimer.invalidate()
  24. treatmentsTimer.invalidate()
  25. minAgoTimer.invalidate()
  26. calendarTimer.invalidate()
  27. alarmTimer.invalidate()
  28. }
  29. // min Ago Timer
  30. func startMinAgoTimer(time: TimeInterval) {
  31. minAgoTimer = Timer.scheduledTimer(timeInterval: time,
  32. target: self,
  33. selector: #selector(MainViewController.minAgoTimerDidEnd(_:)),
  34. userInfo: nil,
  35. repeats: true)
  36. }
  37. @objc func minAgoTimerDidEnd(_ timer: Timer) {
  38. if bgData.count > 0 {
  39. let bgSeconds = bgData.last!.date
  40. let now = Date().timeIntervalSince1970
  41. let secondsAgo = now - bgSeconds
  42. // Update Min Ago Displays
  43. let formatter = DateComponentsFormatter()
  44. formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
  45. if secondsAgo >= 720 { // 720 seconds = 12 minutes
  46. formatter.allowedUnits = [.minute] // Only show minutes after 12 minutes have passed
  47. } else if secondsAgo < 270 { // Less than 4.5 minutes
  48. formatter.allowedUnits = [.minute] // Show only minutes if less than 4.5 minutes
  49. } else {
  50. formatter.allowedUnits = [.minute, .second] // Show minutes and seconds otherwise
  51. }
  52. let formattedDuration = formatter.string(from: secondsAgo) ?? ""
  53. let minAgoDisplayText = formattedDuration + " min ago"
  54. MinAgoText.text = minAgoDisplayText
  55. latestMinAgoString = minAgoDisplayText
  56. if let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController {
  57. snoozer.MinAgoLabel.text = minAgoDisplayText
  58. // Start with the current BGLabel text
  59. let bgLabelText = snoozer.BGLabel.text ?? ""
  60. let attributeString = NSMutableAttributedString(string: bgLabelText)
  61. // Always apply the strikethrough style
  62. attributeString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributeString.length))
  63. // Conditionally set the strikethrough color based on the freshness of the data
  64. if secondsAgo >= 720 { // Data is stale
  65. attributeString.addAttribute(.strikethroughColor, value: UIColor.systemRed, range: NSRange(location: 0, length: attributeString.length))
  66. } else { // Data is fresh
  67. attributeString.addAttribute(.strikethroughColor, value: UIColor.clear, range: NSRange(location: 0, length: attributeString.length))
  68. }
  69. snoozer.BGLabel.attributedText = attributeString
  70. }
  71. } else {
  72. MinAgoText.text = ""
  73. latestMinAgoString = ""
  74. if let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController {
  75. snoozer.MinAgoLabel.text = ""
  76. // Reset BGLabel to ensure no formatting is carried over
  77. snoozer.BGLabel.text = ""
  78. snoozer.BGLabel.attributedText = NSAttributedString(string: "")
  79. }
  80. }
  81. }
  82. // Runs a 60 second timer when an alarm is snoozed
  83. // Prevents the alarm from triggering again while saving the snooze time to settings
  84. // End function needs nothing done
  85. func startGraphNowTimer(time: TimeInterval = 60) {
  86. graphNowTimer = Timer.scheduledTimer(timeInterval: time,
  87. target: self,
  88. selector: #selector(MainViewController.graphNowTimerDidEnd(_:)),
  89. userInfo: nil,
  90. repeats: true)
  91. }
  92. @objc func graphNowTimerDidEnd(_ timer:Timer) {
  93. createVerticalLines()
  94. }
  95. // Runs a 60 second timer when an alarm is snoozed
  96. // Prevents the alarm from triggering again while saving the snooze time to settings
  97. // End function needs nothing done
  98. func startCheckAlarmTimer(time: TimeInterval = 60) {
  99. checkAlarmTimer = Timer.scheduledTimer(timeInterval: time,
  100. target: self,
  101. selector: #selector(MainViewController.checkAlarmTimerDidEnd(_:)),
  102. userInfo: nil,
  103. repeats: false)
  104. }
  105. @objc func checkAlarmTimerDidEnd(_ timer:Timer) {
  106. }
  107. // BG Timer
  108. // Runs to 5:10 after last reading timestamp
  109. // Failed or no reading re-attempts after 10 second delay
  110. // Changes to 30 second increments after 7:00
  111. // Changes to 1 minute increments after 10:00
  112. // Changes to 5 minute increments after 20:00 stale data
  113. func startBGTimer(time: TimeInterval = 60 * 5) {
  114. bgTimer = Timer.scheduledTimer(timeInterval: time,
  115. target: self,
  116. selector: #selector(MainViewController.bgTimerDidEnd(_:)),
  117. userInfo: nil,
  118. repeats: false)
  119. }
  120. @objc func bgTimerDidEnd(_ timer:Timer) {
  121. // reset timer to 1 minute if settings aren't entered
  122. if UserDefaultsRepository.shareUserName.value == "" && UserDefaultsRepository.sharePassword.value == "" && UserDefaultsRepository.url.value == "" {
  123. startBGTimer(time: 60)
  124. return
  125. }
  126. if UserDefaultsRepository.shareUserName.value != "" && UserDefaultsRepository.sharePassword.value != "" {
  127. webLoadDexShare()
  128. } else {
  129. webLoadNSBGData()
  130. }
  131. BackgroundAlertManager.shared.scheduleBackgroundAlert()
  132. }
  133. // Device Status Timer
  134. // Runs to 5:10 after last reading timestamp
  135. // Failed or no update re-attempts after 10 second delay
  136. // Changes to 30 second increments after 7:00
  137. // Changes to 1 minute increments after 10:00
  138. // Changes to 5 minute increments after 20:00 stale data
  139. func startDeviceStatusTimer(time: TimeInterval = 60 * 5) {
  140. deviceStatusTimer = Timer.scheduledTimer(timeInterval: time,
  141. target: self,
  142. selector: #selector(MainViewController.deviceStatusTimerDidEnd(_:)),
  143. userInfo: nil,
  144. repeats: false)
  145. }
  146. @objc func deviceStatusTimerDidEnd(_ timer:Timer) {
  147. // reset timer to 1 minute if settings aren't entered
  148. if UserDefaultsRepository.url.value == "" {
  149. startDeviceStatusTimer(time: 60)
  150. return
  151. }
  152. if UserDefaultsRepository.url.value != "" {
  153. webLoadNSDeviceStatus()
  154. }
  155. }
  156. // Treatments Timer
  157. // Runs on 2 minute intervals
  158. // Pauses with stale BG data
  159. func startTreatmentsTimer(time: TimeInterval = 60 * 2) {
  160. treatmentsTimer = Timer.scheduledTimer(timeInterval: time,
  161. target: self,
  162. selector: #selector(MainViewController.treatmentsTimerDidEnd(_:)),
  163. userInfo: nil,
  164. repeats: false)
  165. }
  166. @objc func treatmentsTimerDidEnd(_ timer:Timer) {
  167. // reset timer to 1 minute if settings aren't entered
  168. if UserDefaultsRepository.url.value == "" {
  169. startTreatmentsTimer(time: 60)
  170. return
  171. }
  172. if UserDefaultsRepository.url.value != "" && UserDefaultsRepository.downloadTreatments.value {
  173. WebLoadNSTreatments()
  174. }
  175. startTreatmentsTimer()
  176. }
  177. // Profile Timer
  178. // Runs on 10 minute intervals
  179. // Pauses with stale BG data
  180. func startProfileTimer(time: TimeInterval = 60 * 10) {
  181. profileTimer = Timer.scheduledTimer(timeInterval: time,
  182. target: self,
  183. selector: #selector(MainViewController.profileTimerDidEnd(_:)),
  184. userInfo: nil,
  185. repeats: false)
  186. }
  187. @objc func profileTimerDidEnd(_ timer:Timer) {
  188. // reset timer to 1 minute if settings aren't entered
  189. if UserDefaultsRepository.url.value == "" {
  190. startProfileTimer(time: 60)
  191. return
  192. }
  193. if !isStaleData() && UserDefaultsRepository.url.value != "" {
  194. webLoadNSProfile()
  195. startProfileTimer()
  196. }
  197. }
  198. // Cancel and reset the playing alarm if it has not been snoozed after 4 min 50 seconds.
  199. // This allows the next BG reading to either start the timer going or not fire if the situation has been resolved
  200. func startAlarmPlayingTimer(time: TimeInterval = 290) {
  201. let alarmPlayingTimer = Timer.scheduledTimer(timeInterval: time,
  202. target: self,
  203. selector: #selector(MainViewController.alarmPlayingTimerDidEnd(_:)),
  204. userInfo: nil,
  205. repeats: false)
  206. }
  207. @objc func alarmPlayingTimerDidEnd(_ timer:Timer) {
  208. if AlarmSound.isPlaying {
  209. stopAlarmAtNextReading()
  210. }
  211. }
  212. // Alarm Timer
  213. // Run the alarm checker every 15 seconds
  214. func startAlarmTimer(time: TimeInterval) {
  215. alarmTimer = Timer.scheduledTimer(timeInterval: time,
  216. target: self,
  217. selector: #selector(MainViewController.alarmTimerDidEnd(_:)),
  218. userInfo: nil,
  219. repeats: true)
  220. }
  221. @objc func alarmTimerDidEnd(_ timer:Timer) {
  222. if bgData.count > 0 {
  223. self.checkAlarms(bgs: bgData)
  224. }
  225. if overrideGraphData.count > 0 {
  226. self.checkOverrideAlarms()
  227. }
  228. }
  229. // Calendar Timer
  230. // Run the calendar writer every 30 seconds
  231. func startCalendarTimer(time: TimeInterval) {
  232. calendarTimer = Timer.scheduledTimer(timeInterval: time,
  233. target: self,
  234. selector: #selector(MainViewController.calendarTimerDidEnd(_:)),
  235. userInfo: nil,
  236. repeats: true)
  237. }
  238. @objc func calendarTimerDidEnd(_ timer:Timer) {
  239. if UserDefaultsRepository.writeCalendarEvent.value && UserDefaultsRepository.calendarIdentifier.value != "" {
  240. self.writeCalendar()
  241. }
  242. }
  243. // Timer to allow us to write min ago calendar entries but not update them every 30 seconds
  244. func startCalTimer(time: TimeInterval) {
  245. calTimer = Timer.scheduledTimer(timeInterval: time,
  246. target: self,
  247. selector: #selector(MainViewController.calTimerDidEnd(_:)),
  248. userInfo: nil,
  249. repeats: false)
  250. }
  251. // Nothing should be done when this timer ends because it just blocks the calendar from writing when it's active
  252. @objc func calTimerDidEnd(_ timer:Timer) {
  253. }
  254. }