TimestampedHistoryEvent.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // TimestampedPumpEvent.swift
  3. // RileyLink
  4. //
  5. // Created by Nate Racklyeft on 6/15/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. // Boxes a TimestampedPumpEvent, storing its reconciled date components
  10. public struct TimestampedHistoryEvent {
  11. public let pumpEvent: PumpEvent
  12. public let date: Date
  13. public init(pumpEvent: PumpEvent, date: Date) {
  14. self.pumpEvent = pumpEvent
  15. self.date = date
  16. }
  17. public func isMutable(atDate date: Date = Date(), forPump model: PumpModel) -> Bool {
  18. guard let bolus = self.pumpEvent as? BolusNormalPumpEvent else {
  19. return false
  20. }
  21. let deliveryFinishDate = date.addingTimeInterval(bolus.deliveryTime)
  22. return model.appendsSquareWaveToHistoryOnStartOfDelivery && bolus.type == .square && deliveryFinishDate > date
  23. }
  24. }
  25. extension TimestampedHistoryEvent: DictionaryRepresentable {
  26. public var dictionaryRepresentation: [String : Any] {
  27. var dict = pumpEvent.dictionaryRepresentation
  28. dict["timestamp"] = DateFormatter.ISO8601DateFormatter().string(from: date)
  29. return dict
  30. }
  31. }