BGTask.swift 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // LoopFollow
  2. // BGTask.swift
  3. import Foundation
  4. extension MainViewController {
  5. func scheduleBGTask(initialDelay: TimeInterval = 2) {
  6. let firstRun = Date().addingTimeInterval(initialDelay)
  7. TaskScheduler.shared.scheduleTask(id: .fetchBG, nextRun: firstRun) { [weak self] in
  8. guard let self = self else { return }
  9. self.bgTaskAction()
  10. }
  11. }
  12. func bgTaskAction() {
  13. // If anything goes wrong, try again in 60 seconds.
  14. TaskScheduler.shared.rescheduleTask(
  15. id: .fetchBG,
  16. to: Date().addingTimeInterval(60)
  17. )
  18. // If no Dexcom credentials and no Nightscout, schedule a retry in 60 seconds.
  19. if Storage.shared.shareUserName.value == "",
  20. Storage.shared.sharePassword.value == "",
  21. !IsNightscoutEnabled()
  22. {
  23. return
  24. }
  25. // If Dexcom credentials exist, fetch from DexShare
  26. if Storage.shared.shareUserName.value != "",
  27. Storage.shared.sharePassword.value != ""
  28. {
  29. webLoadDexShare()
  30. } else {
  31. webLoadNSBGData()
  32. }
  33. }
  34. }