Skip to content
Merged
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
49 changes: 40 additions & 9 deletions Loop Status Extension/StatusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class StatusViewController: UIViewController, NCWidgetProviding {
@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
Expand Down Expand Up @@ -54,6 +58,30 @@ class StatusViewController: UIViewController, NCWidgetProviding {

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always check the context. If it doesn't match, then you must call super.

}

override func didReceiveMemoryWarning() {
Expand All @@ -67,22 +95,26 @@ class StatusViewController: UIViewController, NCWidgetProviding {
}

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 @@ -124,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
}

}
4 changes: 4 additions & 0 deletions Loop/Extensions/NSUserDefaults+StatusExtension.swift
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