GRIViewModel.swift 985 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // LoopFollow
  2. // GRIViewModel.swift
  3. import Combine
  4. import Foundation
  5. class GRIViewModel: ObservableObject {
  6. @Published var gri: Double?
  7. @Published var griHypoComponent: Double?
  8. @Published var griHyperComponent: Double?
  9. @Published var griDataPoints: [(date: Date, value: Double)] = []
  10. private let dataService: StatsDataService
  11. init(dataService: StatsDataService) {
  12. self.dataService = dataService
  13. calculateGRI()
  14. }
  15. func calculateGRI() {
  16. let bgData = dataService.getBGData()
  17. guard !bgData.isEmpty else { return }
  18. let result = GRICalculator.calculate(bgData: bgData)
  19. gri = result.gri
  20. griHypoComponent = result.hypoComponent
  21. griHyperComponent = result.hyperComponent
  22. griDataPoints = GRICalculator.calculateTimeSeries(bgData: bgData)
  23. }
  24. func clearStats() {
  25. gri = nil
  26. griHypoComponent = nil
  27. griHyperComponent = nil
  28. griDataPoints = []
  29. }
  30. }