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
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import HealthKit
// Code in this extension is duplicated from:
// https://github.com/LoopKit/LoopKit/blob/master/LoopKit/HKUnit.swift
// to avoid pulling in the LoopKit extension since it's not extension-API safe.
public extension HKUnit {
extension HKUnit {
// A formatting helper for determining the preferred decimal style for a given unit
var preferredMinimumFractionDigits: Int {
var preferredFractionDigits: Int {
if self.unitString == "mg/dL" {
return 0
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ extension Bundle {
return String(format: NSLocalizedString("%1$@ v%2$@", comment: "The format string for the app name and version number. (1: bundle name)(2: bundle version)"), bundleDisplayName, shortVersionString)
}

private var mainAppBundleIdentifier: String? {
return object(forInfoDictionaryKey: "MainAppBundleIdentifier") as? String
}

var appGroupSuiteName: String {
return object(forInfoDictionaryKey: "AppGroupIdentifier") as! String
}

var mainAppUrl: URL? {
if let mainAppBundleIdentifier = mainAppBundleIdentifier {
return URL(string: "\(mainAppBundleIdentifier)://")
} else {
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ extension UserDefaults {
case StatusExtensionContext = "com.loopkit.Loop.StatusExtensionContext"
}

var statusExtensionContextObservableKey: String {
return Key.StatusExtensionContext.rawValue
}

var statusExtensionContext: StatusExtensionContext? {
get {
if let rawValue = dictionary(forKey: Key.StatusExtensionContext.rawValue) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation


enum GlucoseTrend: Int {
public enum GlucoseTrend: Int {
case upUpUp = 1
case upUp = 2
case up = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import Foundation
import HealthKit
import LoopUI

struct ReservoirContext {
let startDate: Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation
import HealthKit


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

Expand All @@ -19,7 +18,7 @@ final class WatchContext: NSObject, RawRepresentable {
var maxBolus: Double?

var glucose: HKQuantity?
var glucoseTrend: GlucoseTrend?
var glucoseTrendRawValue: Int?
var eventualGlucose: HKQuantity?
var glucoseDate: Date?

Expand Down Expand Up @@ -64,9 +63,7 @@ final class WatchContext: NSObject, RawRepresentable {
}
}

if let rawTrend = rawValue["gt"] as? Int {
glucoseTrend = GlucoseTrend(rawValue: rawTrend)
}
glucoseTrendRawValue = rawValue["gt"] as? Int
glucoseDate = rawValue["gd"] as? Date

IOB = rawValue["iob"] as? Double
Expand Down Expand Up @@ -98,7 +95,7 @@ final class WatchContext: NSObject, RawRepresentable {
raw["gv"] = glucose?.doubleValue(for: unit)
}

raw["gt"] = glucoseTrend?.rawValue
raw["gt"] = glucoseTrendRawValue
raw["gd"] = glucoseDate
raw["iob"] = IOB
raw["ld"] = loopLastRunDate
Expand Down
278 changes: 4 additions & 274 deletions Loop Status Extension/Base.lproj/MainInterface.storyboard

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Loop Status Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>MainAppBundleIdentifier</key>
<string>$(MAIN_APP_BUNDLE_IDENTIFIER)</string>
<key>CFBundleDisplayName</key>
<string>Loop</string>
<key>CFBundleExecutable</key>
Expand Down
107 changes: 87 additions & 20 deletions Loop Status Extension/StatusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,115 @@
// Copyright © 2016 LoopKit Authors. All rights reserved.
//

import UIKit
import NotificationCenter
import HealthKit
import CoreData
import HealthKit
import LoopUI
import NotificationCenter
import UIKit

class StatusViewController: UIViewController, NCWidgetProviding {

@IBOutlet weak var loopCompletionHUD: LoopCompletionHUDView!
@IBOutlet weak var glucoseHUD: GlucoseHUDView!
@IBOutlet weak var basalRateHUD: BasalRateHUDView!
@IBOutlet weak var reservoirVolumeHUD: ReservoirVolumeHUDView!
@IBOutlet weak var batteryHUD: BatteryLevelHUDView!

@IBOutlet weak var hudView: HUDView!
@IBOutlet weak var subtitleLabel: UILabel!


var statusExtensionContext: StatusExtensionContext?
var defaults: UserDefaults?
final var observationContext = 1

var loopCompletionHUD: LoopCompletionHUDView! {
get {
return hudView.loopCompletionHUD
}
}

var glucoseHUD: GlucoseHUDView! {
get {
return hudView.glucoseHUD
}
}

var basalRateHUD: BasalRateHUDView! {
get {
return hudView.basalRateHUD
}
}

var reservoirVolumeHUD: ReservoirVolumeHUDView! {
get {
return hudView.reservoirVolumeHUD
}
}

var batteryHUD: BatteryLevelHUDView! {
get {
return hudView.batteryHUD
}
}

override func viewDidLoad() {
super.viewDidLoad()
subtitleLabel.alpha = 0
subtitleLabel.textColor = UIColor.secondaryLabelColor

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(openLoopApp(_:)))
view.addGestureRecognizer(tapGestureRecognizer)

defaults = UserDefaults(suiteName: Bundle.main.appGroupSuiteName)
if let defaults = defaults {
defaults.addObserver(
self,
forKeyPath: defaults.statusExtensionContextObservableKey,
options: [],
context: &observationContext)
}
}

deinit {
if let defaults = defaults {
defaults.removeObserver(self, forKeyPath: defaults.statusExtensionContextObservableKey, context: &observationContext)
}
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observationContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}

update()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


@objc private func openLoopApp(_: Any) {
if let url = Bundle.main.mainAppUrl {
self.extensionContext?.open(url)
}
}

func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
let result = update()
completionHandler(result)
}

@discardableResult
func update() -> NCUpdateResult {
guard
let context = UserDefaults(suiteName: Bundle.main.appGroupSuiteName)?.statusExtensionContext
let context = defaults?.statusExtensionContext
else {
completionHandler(NCUpdateResult.failed)
return
return NCUpdateResult.failed
}

// We should never have the case where there's glucose values but no preferred
// unit. However, if that case were to happen we might show quantities against
// the wrong units and that could be very harmful. So unless there's a preferred
// unit, assume that none of the rest of the data is reliable.
guard
let preferredUnitString = context.preferredUnitString
else {
completionHandler(NCUpdateResult.failed)
return
return NCUpdateResult.failed
}

if let glucose = context.latestGlucose {
Expand Down Expand Up @@ -88,10 +156,9 @@ class StatusViewController: UIViewController, NCWidgetProviding {
} else {
subtitleLabel.alpha = 0
}

// Right now we always act as if there's new data.
// TODO: keep track of data changes and return .noData if necessary
completionHandler(NCUpdateResult.newData)
return NCUpdateResult.newData
}

}
2 changes: 1 addition & 1 deletion Loop.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

// Change this on first setup to your own unique organization identifier in
// reverse-domain name syntax.
MAIN_APP_BUNDLE_IDENTIFIER = com.loopkit
MAIN_APP_BUNDLE_IDENTIFIER = com.jslev9
Loading