forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making schedules generic. Framework organization.
- Loading branch information
Showing
21 changed files
with
527 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// BasalRateSchedule.swift | ||
// Naterade | ||
// | ||
// Created by Nathan Racklyeft on 2/12/16. | ||
// Copyright © 2016 Nathan Racklyeft. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public class BasalRateSchedule: DailyValueSchedule<Double> { | ||
|
||
public override init?(dailyItems: [RepeatingScheduleValue<Double>], timeZone: NSTimeZone? = nil) { | ||
super.init(dailyItems: dailyItems, timeZone: timeZone) | ||
} | ||
|
||
/** | ||
Calculates the total basal delivery for a day | ||
|
||
- returns: The total basal delivery | ||
*/ | ||
public func total() -> Double { | ||
var total: Double = 0 | ||
|
||
for (index, item) in items.enumerate() { | ||
var endTime = maxTimeInterval | ||
|
||
if index < items.endIndex - 1 { | ||
endTime = items[index + 1].startTime | ||
} | ||
|
||
total += (endTime - item.startTime) / NSTimeInterval(hours: 1) * item.value | ||
} | ||
|
||
return total | ||
} | ||
|
||
public override func valueAt(time: NSDate) -> Double { | ||
return super.valueAt(time) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// CarbRatioSchedule.swift | ||
// Naterade | ||
// | ||
// Created by Nathan Racklyeft on 2/12/16. | ||
// Copyright © 2016 Nathan Racklyeft. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import HealthKit | ||
|
||
|
||
public class CarbRatioSchedule: DailyQuantitySchedule { | ||
public override init?(unit: HKUnit, dailyItems: [RepeatingScheduleValue<Double>], timeZone: NSTimeZone? = nil) { | ||
super.init(unit: unit, dailyItems: dailyItems, timeZone: timeZone) | ||
|
||
guard unit == HKUnit.gramUnit() else { | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// DailyQuantitySchedule.swift | ||
// Naterade | ||
// | ||
// Created by Nathan Racklyeft on 2/12/16. | ||
// Copyright © 2016 Nathan Racklyeft. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import HealthKit | ||
|
||
|
||
public class DailyQuantitySchedule: DailyValueSchedule<Double> { | ||
public let unit: HKUnit | ||
|
||
init?(unit: HKUnit, dailyItems: [RepeatingScheduleValue<Double>], timeZone: NSTimeZone?) { | ||
self.unit = unit | ||
|
||
super.init(dailyItems: dailyItems, timeZone: timeZone) | ||
} | ||
|
||
public required convenience init?(rawValue: RawValue) { | ||
guard let | ||
rawUnit = rawValue["unit"] as? String, | ||
timeZoneName = rawValue["timeZone"] as? String, | ||
rawItems = rawValue["items"] as? [RepeatingScheduleValue.RawValue] else | ||
{ | ||
return nil | ||
} | ||
|
||
self.init(unit: HKUnit(fromString: rawUnit), dailyItems: rawItems.flatMap { RepeatingScheduleValue(rawValue: $0) }, timeZone: NSTimeZone(name: timeZoneName)) | ||
} | ||
|
||
public func quantityAt(time: NSDate) -> HKQuantity { | ||
return HKQuantity(unit: unit, doubleValue: valueAt(time)) | ||
} | ||
|
||
func averageValue() -> Double { | ||
var total: Double = 0 | ||
|
||
for (index, item) in items.enumerate() { | ||
var endTime = maxTimeInterval | ||
|
||
if index < items.endIndex - 1 { | ||
endTime = items[index + 1].startTime | ||
} | ||
|
||
total += (endTime - item.startTime) * item.value | ||
} | ||
|
||
return total / repeatInterval | ||
} | ||
|
||
public func averageQuantity() -> HKQuantity { | ||
return HKQuantity(unit: unit, doubleValue: averageValue()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Double.swift | ||
// Naterade | ||
// | ||
// Created by Nathan Racklyeft on 2/12/16. | ||
// Copyright © 2016 Nathan Racklyeft. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
extension Double: RawRepresentable { | ||
public typealias RawValue = NSNumber | ||
|
||
public init?(rawValue: RawValue) { | ||
self = rawValue.doubleValue | ||
} | ||
|
||
public var rawValue: RawValue { | ||
return NSNumber(double: self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// GlucoseQuantitySchedule.swift | ||
// Naterade | ||
// | ||
// Created by Nathan Racklyeft on 2/12/16. | ||
// Copyright © 2016 Nathan Racklyeft. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import HealthKit | ||
|
||
|
||
public class GlucoseQuantitySchedule: DailyQuantitySchedule { | ||
public override init?(unit: HKUnit, dailyItems: [RepeatingScheduleValue<Double>], timeZone: NSTimeZone? = nil) { | ||
super.init(unit: unit, dailyItems: dailyItems, timeZone: timeZone) | ||
|
||
guard unit == HKUnit.milligramsPerDeciliterUnit() || unit == HKUnit.millimolesPerLiterUnit() else { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
|
||
public typealias InsulinSensitivitySchedule = GlucoseQuantitySchedule |
Oops, something went wrong.