Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Common/Extensions/NSUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ extension UserDefaults {
glucoseTargetRangeSchedule = nil
}

let suspendThreshold: GlucoseThreshold?
let suspendThreshold: GlucoseThresholdSetting?
if let rawValue = dictionary(forKey: "com.loopkit.Loop.MinimumBGGuard") {
suspendThreshold = GlucoseThreshold(rawValue: rawValue)
suspendThreshold = GlucoseThresholdSetting(rawValue: rawValue)
} else {
suspendThreshold = nil
}
Expand Down
4 changes: 2 additions & 2 deletions Common/Extensions/UserDefaults+CGM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ extension UserDefaults {
case cgmSettings = "com.loopkit.Loop.cgmSettings"
}

var cgm: CGM? {
var cgm: CGMType? {
get {
if let rawValue = dictionary(forKey: Key.cgmSettings.rawValue) {
return CGM(rawValue: rawValue)
return CGMType(rawValue: rawValue)
} else {
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions Common/Models/Contexts/GlucoseContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// GlucoseContext.swift
// Loop
//
// Created by Bharat Mediratta on 6/25/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//

import Foundation
import HealthKit


struct GlucoseContext {
let value: Double
let unit: HKUnit
let startDate: Date

var quantity: HKQuantity {
return HKQuantity(unit: unit, doubleValue: value)
}
}
45 changes: 45 additions & 0 deletions Common/Models/Contexts/NetBasalContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// NetBasalContext.swift
// Loop
//
// Created by Bharat Mediratta on 6/25/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//

import Foundation

struct NetBasalContext {
let rate: Double
let percentage: Double
let start: Date
let end: Date
}

extension NetBasalContext: RawRepresentable {
typealias RawValue = [String: Any]

var rawValue: RawValue {
return [
"rate": rate,
"percentage": percentage,
"start": start,
"end": end
]
}

init?(rawValue: RawValue) {
guard
let rate = rawValue["rate"] as? Double,
let percentage = rawValue["percentage"] as? Double,
let start = rawValue["start"] as? Date,
let end = rawValue["end"] as? Date
else {
return nil
}

self.rate = rate
self.percentage = percentage
self.start = start
self.end = end
}
}
56 changes: 56 additions & 0 deletions Common/Models/Contexts/PredictedGlucoseContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// PredictedGlucoseContext.swift
// Loop
//
// Created by Bharat Mediratta on 6/25/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//

import Foundation
import HealthKit


struct PredictedGlucoseContext {
let values: [Double]
let unit: HKUnit
let startDate: Date
let interval: TimeInterval

var samples: [GlucoseContext] {
var result: [GlucoseContext] = []
for (i, v) in values.enumerated() {
result.append(GlucoseContext(value: v, unit: unit, startDate: startDate.addingTimeInterval(Double(i) * interval)))
}
return result
}
}


extension PredictedGlucoseContext: RawRepresentable {
typealias RawValue = [String: Any]

var rawValue: RawValue {
return [
"values": values,
"unit": unit.unitString,
"startDate": startDate,
"interval": interval
]
}

init?(rawValue: RawValue) {
guard
let values = rawValue["values"] as? [Double],
let unitString = rawValue["unit"] as? String,
let startDate = rawValue["startDate"] as? Date,
let interval = rawValue["interval"] as? TimeInterval
else {
return nil
}

self.values = values
self.unit = HKUnit(from: unitString)
self.startDate = startDate
self.interval = interval
}
}
61 changes: 61 additions & 0 deletions Common/Models/Contexts/SensorDisplayableContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// SensorDisplayableContext.swift
// Loop
//
// Created by Bharat Mediratta on 6/25/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//

import Foundation
import LoopUI


struct SensorDisplayableContext: SensorDisplayable {
let isStateValid: Bool
let stateDescription: String
let trendType: GlucoseTrendType?
let isLocal: Bool
}


extension SensorDisplayableContext: RawRepresentable {
typealias RawValue = [String: Any]

var rawValue: RawValue {
var raw: RawValue = [
"isStateValid": isStateValid,
"stateDescription": stateDescription,
"isLocal": isLocal
]
raw["trendType"] = trendType?.rawValue

return raw
}

init(_ other: SensorDisplayable) {
isStateValid = other.isStateValid
stateDescription = other.stateDescription
isLocal = other.isLocal
trendType = other.trendType
}

init?(rawValue: RawValue) {
guard
let isStateValid = rawValue["isStateValid"] as? Bool,
let stateDescription = rawValue["stateDescription"] as? String,
let isLocal = rawValue["isLocal"] as? Bool
else {
return nil
}

self.isStateValid = isStateValid
self.stateDescription = stateDescription
self.isLocal = isLocal

if let rawValue = rawValue["trendType"] as? GlucoseTrendType.RawValue {
trendType = GlucoseTrendType(rawValue: rawValue)
} else {
trendType = nil
}
}
}
67 changes: 67 additions & 0 deletions Common/Models/Contexts/StatusExtensionContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// StatusExtensionContext.swift
// Loop Status Extension
//
// Created by Bharat Mediratta on 11/25/16.
// Copyright © 2016 LoopKit Authors. All rights reserved.
//
// This class allows Loop to pass context data to the Loop Status Extension.

import Foundation
import HealthKit
import LoopKit


struct StatusExtensionContext: RawRepresentable {
var predictedGlucose: PredictedGlucoseContext?
var lastLoopCompleted: Date?
var netBasal: NetBasalContext?
var batteryPercentage: Double?
var sensor: SensorDisplayableContext?
}


extension StatusExtensionContext {
typealias RawValue = [String: Any]
static let version = 4

init?(rawValue: RawValue) {
guard let version = rawValue["version"] as? Int, version == type(of: self).version else {
return nil
}

if let rawValue = rawValue["predictedGlucose"] as? PredictedGlucoseContext.RawValue {
predictedGlucose = PredictedGlucoseContext(rawValue: rawValue)
}

if let rawValue = rawValue["netBasal"] as? NetBasalContext.RawValue {
netBasal = NetBasalContext(rawValue: rawValue)
}

lastLoopCompleted = rawValue["lastLoopCompleted"] as? Date
batteryPercentage = rawValue["batteryPercentage"] as? Double

if let rawValue = rawValue["sensor"] as? SensorDisplayableContext.RawValue {
sensor = SensorDisplayableContext(rawValue: rawValue)
}
}

var rawValue: RawValue {
var raw: RawValue = [
"version": type(of: self).version
]

raw["predictedGlucose"] = predictedGlucose?.rawValue
raw["lastLoopCompleted"] = lastLoopCompleted
raw["netBasal"] = netBasal?.rawValue
raw["batteryPercentage"] = batteryPercentage
raw["sensor"] = sensor?.rawValue
return raw
}
}

extension StatusExtensionContext: CustomDebugStringConvertible {
var debugDescription: String {
return String(reflecting: rawValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import Foundation
import HealthKit

final class WatchContext: NSObject, RawRepresentable {
typealias RawValue = [String: Any]

private let version = 3

struct WatchContext {
var preferredGlucoseUnit: HKUnit?
var maxBolus: Double?

Expand Down Expand Up @@ -42,16 +38,16 @@ final class WatchContext: NSObject, RawRepresentable {
var reservoirPercentage: Double?
var batteryPercentage: Double?

var cgm: CGM?
var cgm: CGMType?
}

override init() {
super.init()
}
extension WatchContext: RawRepresentable {
typealias RawValue = [String: Any]

required init?(rawValue: RawValue) {
super.init()
static let version = 3

guard rawValue["v"] as? Int == version else {
init?(rawValue: RawValue) {
guard rawValue["v"] as? Int == type(of: self).version else {
return nil
}

Expand Down Expand Up @@ -93,14 +89,14 @@ final class WatchContext: NSObject, RawRepresentable {
COB = rawValue["cob"] as? Double
maxBolus = rawValue["mb"] as? Double

if let cgmRawValue = rawValue["cgm"] as? CGM.RawValue {
cgm = CGM(rawValue: cgmRawValue)
if let cgmRawValue = rawValue["cgm"] as? CGMType.RawValue {
cgm = CGMType(rawValue: cgmRawValue)
}
}

var rawValue: RawValue {
var raw: [String: Any] = [
"v": version
"v": type(of: self).version
]

raw["ba"] = lastNetTempBasalDose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
import Foundation
import HealthKit

struct GlucoseThreshold: RawRepresentable {
typealias RawValue = [String: Any]

let value: Double

struct GlucoseThresholdSetting {
let unit: HKUnit

public var quantity: HKQuantity {
let value: Double

var quantity: HKQuantity {
return HKQuantity(unit: unit, doubleValue: value)
}
public init(unit: HKUnit, value: Double) {
self.value = value
self.unit = unit
}
}


extension GlucoseThresholdSetting: RawRepresentable {
typealias RawValue = [String: Any]

init?(rawValue: RawValue) {
guard let unitsStr = rawValue["units"] as? String, let value = rawValue["value"] as? Double else {
return nil
Expand All @@ -41,8 +40,8 @@ struct GlucoseThreshold: RawRepresentable {
}


extension GlucoseThreshold: Equatable {
static func ==(lhs: GlucoseThreshold, rhs: GlucoseThreshold) -> Bool {
extension GlucoseThresholdSetting: Equatable {
static func ==(lhs: GlucoseThresholdSetting, rhs: GlucoseThresholdSetting) -> Bool {
return lhs.value == rhs.value
}
}
Loading