Timers.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // min Ago Timer
  12. func startMinAgoTimer(time: TimeInterval) {
  13. minAgoTimer = Timer.scheduledTimer(timeInterval: time,
  14. target: self,
  15. selector: #selector(MainViewController.minAgoTimerDidEnd(_:)),
  16. userInfo: nil,
  17. repeats: true)
  18. }
  19. // Updates Min Ago display
  20. @objc func minAgoTimerDidEnd(_ timer:Timer) {
  21. // print("min ago timer ended")
  22. if bgData.count > 0 {
  23. let bgSeconds = bgData.last!.date
  24. let now = Date().timeIntervalSince1970
  25. let secondsAgo = now - bgSeconds
  26. // Update Min Ago Displays
  27. let formatter = DateComponentsFormatter()
  28. formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
  29. if secondsAgo < 270 {
  30. formatter.allowedUnits = [ .minute] // Units to display in the formatted string
  31. } else {
  32. formatter.allowedUnits = [ .minute, .second] // Units to display in the formatted string
  33. }
  34. //formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
  35. let formattedDuration = formatter.string(from: secondsAgo)
  36. MinAgoText.text = formattedDuration ?? ""
  37. MinAgoText.text! += " min ago"
  38. latestMinAgoString = formattedDuration ?? ""
  39. latestMinAgoString += " min ago"
  40. guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
  41. snoozer.MinAgoLabel.text = formattedDuration ?? ""
  42. snoozer.MinAgoLabel.text! += " min ago"
  43. } else {
  44. MinAgoText.text = ""
  45. latestMinAgoString = ""
  46. guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
  47. snoozer.MinAgoLabel.text = ""
  48. }
  49. }
  50. // Main Download Timer
  51. func startTimer(time: TimeInterval) {
  52. timer = Timer.scheduledTimer(timeInterval: time,
  53. target: self,
  54. selector: #selector(MainViewController.timerDidEnd(_:)),
  55. userInfo: nil,
  56. repeats: true)
  57. }
  58. // Check for new data when timer ends
  59. @objc func timerDidEnd(_ timer:Timer) {
  60. nightscoutLoader()
  61. }
  62. // Runs a 60 second timer when an alarm is snoozed
  63. // Prevents the alarm from triggering again while saving the snooze time to settings
  64. // End function needs nothing done
  65. func startCheckAlarmTimer(time: TimeInterval = 60) {
  66. checkAlarmTimer = Timer.scheduledTimer(timeInterval: time,
  67. target: self,
  68. selector: #selector(MainViewController.checkAlarmTimerDidEnd(_:)),
  69. userInfo: nil,
  70. repeats: false)
  71. }
  72. @objc func checkAlarmTimerDidEnd(_ timer:Timer) {
  73. }
  74. // Cancel and reset the playing alarm if it has not been snoozed after 4 min 50 seconds.
  75. // This allows the next BG reading to either start the timer going or not fire if the situation has been resolved
  76. func startAlarmPlayingTimer(time: TimeInterval = 290) {
  77. let alarmPlayingTimer = Timer.scheduledTimer(timeInterval: time,
  78. target: self,
  79. selector: #selector(MainViewController.alarmPlayingTimerDidEnd(_:)),
  80. userInfo: nil,
  81. repeats: false)
  82. }
  83. @objc func alarmPlayingTimerDidEnd(_ timer:Timer) {
  84. if AlarmSound.isPlaying {
  85. stopAlarmAtNextReading()
  86. }
  87. }
  88. // NS Loader Timer
  89. func startViewTimer(time: TimeInterval) {
  90. viewTimer = Timer.scheduledTimer(timeInterval: time,
  91. target: self,
  92. selector: #selector(MainViewController.viewTimerDidEnd(_:)),
  93. userInfo: nil,
  94. repeats: false)
  95. }
  96. // This delays a few things to hopefully all all data to arrive.
  97. @objc func viewTimerDidEnd(_ timer:Timer) {
  98. if bgData.count > 0 {
  99. self.checkAlarms(bgs: bgData)
  100. //self.updateMinAgo()
  101. // self.updateBadge(val: bgData[bgData.count - 1].sgv)
  102. //self.viewUpdateNSBG()
  103. if UserDefaultsRepository.writeCalendarEvent.value {
  104. self.writeCalendar()
  105. }
  106. }
  107. }
  108. // Timer to allow us to write min ago calendar entries but not update them every 30 seconds
  109. func startCalTimer(time: TimeInterval) {
  110. calTimer = Timer.scheduledTimer(timeInterval: time,
  111. target: self,
  112. selector: #selector(MainViewController.calTimerDidEnd(_:)),
  113. userInfo: nil,
  114. repeats: false)
  115. }
  116. // Nothing should be done when this timer ends because it just blocks the calendar from writing when it's active
  117. @objc func calTimerDidEnd(_ timer:Timer) {
  118. }
  119. }