BasalRateSchedule.swift 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // BasalRateSchedule.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 2/12/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. public typealias BasalRateSchedule = DailyValueSchedule<Double>
  10. public struct BasalScheduleValidationResult {
  11. let scheduleError: Error?
  12. let itemErrors: [(index: Int, error: Error)]
  13. }
  14. public extension DailyValueSchedule where T == Double {
  15. /**
  16. Calculates the total basal delivery for a day
  17. - returns: The total basal delivery
  18. */
  19. func total() -> Double {
  20. var total: Double = 0
  21. for (index, item) in items.enumerated() {
  22. var endTime = maxTimeInterval
  23. if index < items.endIndex - 1 {
  24. endTime = items[index + 1].startTime
  25. }
  26. total += (endTime - item.startTime).hours * item.value
  27. }
  28. return total
  29. }
  30. }