ProfileTask.swift 909 B

12345678910111213141516171819202122232425262728293031
  1. // LoopFollow
  2. // ProfileTask.swift
  3. import Foundation
  4. extension MainViewController {
  5. func scheduleProfileTask(initialDelay: TimeInterval = 3) {
  6. let firstRun = Date().addingTimeInterval(initialDelay)
  7. TaskScheduler.shared.scheduleTask(id: .profile, nextRun: firstRun) { [weak self] in
  8. guard let self = self else { return }
  9. self.profileTaskAction()
  10. }
  11. }
  12. func profileTaskAction() {
  13. guard IsNightscoutEnabled() else {
  14. TaskScheduler.shared.rescheduleTask(id: .profile, to: Date().addingTimeInterval(60))
  15. return
  16. }
  17. webLoadNSProfile()
  18. var interval: TimeInterval = 10 * 60
  19. if NightscoutSocketManager.shared.connectionState == .authenticated {
  20. interval = 30 * 60
  21. }
  22. TaskScheduler.shared.rescheduleTask(id: .profile, to: Date().addingTimeInterval(interval))
  23. }
  24. }