Skip to content

Commit

Permalink
#198: Lockscreen Widget and new optimized Watch complications
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermanns committed Sep 18, 2022
1 parent cb999f6 commit aa00c2b
Show file tree
Hide file tree
Showing 30 changed files with 710 additions and 139 deletions.
6 changes: 3 additions & 3 deletions nightguard WatchKit App/Base.lproj/Interface.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="19162" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="zZT-1m-QPq">
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="21225" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="zZT-1m-QPq">
<device id="watch44"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19144"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="19044"/>
<deployment identifier="watchOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="21040"/>
</dependencies>
<scenes>
<!--Main Controller-->
Expand Down
2 changes: 1 addition & 1 deletion nightguard WatchKit App/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>779</string>
<string>785</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
6 changes: 3 additions & 3 deletions nightguard WatchKit Extension/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
81 changes: 61 additions & 20 deletions nightguard WatchKit Extension/ComplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
handler([])
}

fileprivate func createLine1And2TextProvider(_ currentNightscoutData: NightscoutData) -> (CLKTextProvider, CLKTextProvider) {

var line1TextProvider : CLKTextProvider
var line2TextProvider : CLKTextProvider

if useRelativeTimeWhenPossible {
line1TextProvider = CLKSimpleTextProvider(text: getSgvAndArrow(currentNightscoutData, " "))
line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
line2TextProvider = getRelativeDateTextProvider(for: currentNightscoutData.time)
} else {
line1TextProvider = CLKSimpleTextProvider(text: "\(currentNightscoutData.hourAndMinutes)")
line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
line2TextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))\(currentNightscoutData.bgdeltaString)\(currentNightscoutData.bgdeltaArrow)")
}

return (line1TextProvider, line2TextProvider)
}

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {

let currentNightscoutData = NightscoutCacheService.singleton.getCurrentNightscoutData()
Expand All @@ -40,19 +60,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
switch complication.family {
case .modularSmall:

var line1TextProvider : CLKTextProvider
var line2TextProvider : CLKTextProvider
if useRelativeTimeWhenPossible {
line1TextProvider = CLKSimpleTextProvider(text: getSgvAndArrow(currentNightscoutData, " "))
line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
line2TextProvider = getRelativeDateTextProvider(for: currentNightscoutData.time)
} else {
line1TextProvider = CLKSimpleTextProvider(text: "\(currentNightscoutData.hourAndMinutes)")
line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
line2TextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))\(currentNightscoutData.bgdeltaString)\(currentNightscoutData.bgdeltaArrow)")
}
let (line1TextProvider, line2TextProvider) = createLine1And2TextProvider(currentNightscoutData)
let modTemplate = CLKComplicationTemplateModularSmallStackText(line1TextProvider: line1TextProvider,
line2TextProvider: line2TextProvider)
template = modTemplate
Expand Down Expand Up @@ -110,6 +118,23 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
textProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
template = CLKComplicationTemplateUtilitarianLargeFlat(textProvider: textProvider, imageProvider: imageProvider)
case .extraLarge:
let row1Col1 = CLKSimpleTextProvider(text: getOneLine(currentNightscoutData))
row1Col1.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
let row1Col2 = getRelativeDateTextProvider(for: currentNightscoutData.time)
var row2Col1 = CLKSimpleTextProvider(text: "")
var row2Col2 = CLKSimpleTextProvider(text: "")
if self.oldNightscoutData.count > 1 {
let nightscoutData = self.oldNightscoutData[1]
row2Col1 = CLKSimpleTextProvider(text: getOneLine(nightscoutData))
row2Col2 = getRelativeDateTextProvider(for: nightscoutData.time) as? CLKSimpleTextProvider ?? row2Col2
}

template = CLKComplicationTemplateExtraLargeColumnsText(
row1Column1TextProvider: row1Col1, row1Column2TextProvider: row1Col2,
row2Column1TextProvider: row2Col1, row2Column2TextProvider: row2Col2)

case .circularSmall:

let textProvider = CLKSimpleTextProvider(text:
Expand All @@ -136,13 +161,15 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
case .graphicCircular:
if #available(watchOSApplicationExtension 5.0, *) {

let sgvLong = "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))"
let sgvShort = "\(UnitsConverter.mgdlToShortDisplayUnits(currentNightscoutData.sgv))"
let centerTextProvider = CLKSimpleTextProvider(text: sgvLong, shortText: sgvShort)
centerTextProvider.tintColor = UIColorChanger.getBgColor(
let line2TextProvider = CLKSimpleTextProvider(text: "\(UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))")
line2TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))

let line1TextProvider = CLKSimpleTextProvider(text: "\(currentNightscoutData.hourAndMinutes)")
line1TextProvider.tintColor = UIColorChanger.getBgColor(
UnitsConverter.mgdlToDisplayUnits(currentNightscoutData.sgv))
let gaugeProvider = CLKSimpleGaugeProvider(style: .fill, gaugeColor: UIColor.black, fillFraction: CLKSimpleGaugeProviderFillFractionEmpty)
template = CLKComplicationTemplateGraphicCircularClosedGaugeText(gaugeProvider: gaugeProvider, centerTextProvider: centerTextProvider)

template = CLKComplicationTemplateGraphicCircularStackText(line1TextProvider: line1TextProvider, line2TextProvider: line2TextProvider)
} else {
abort()
}
Expand All @@ -157,6 +184,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
} else {
abort()
}
case .graphicExtraLarge:

let (line1TextProvider, line2TextProvider) = createLine1And2TextProvider(currentNightscoutData)

let modTemplate = CLKComplicationTemplateGraphicExtraLargeCircularStackText(
line1TextProvider: line1TextProvider, line2TextProvider: line2TextProvider)

template = modTemplate
default: break
}

Expand Down Expand Up @@ -242,10 +277,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
var template: CLKComplicationTemplate? = nil
switch complication.family {
case .graphicExtraLarge:
template = nil
case .modularSmall:
template = nil
case .modularLarge:
template = nil
case .extraLarge:
template = nil
case .utilitarianSmall:
template = nil
case .utilitarianLarge:
Expand All @@ -260,13 +299,15 @@ class ComplicationController: NSObject, CLKComplicationDataSource {

func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
let descriptors = [
CLKComplicationDescriptor(identifier: "nightguardComplication", displayName: "Nightguard",
CLKComplicationDescriptor(identifier: "nightguardComplication", displayName: "BG Values",
supportedFamilies: [CLKComplicationFamily.circularSmall,
CLKComplicationFamily.graphicBezel,
CLKComplicationFamily.graphicCorner,
CLKComplicationFamily.graphicCircular,
CLKComplicationFamily.graphicExtraLarge,
CLKComplicationFamily.modularLarge,
CLKComplicationFamily.modularSmall,
CLKComplicationFamily.extraLarge,
CLKComplicationFamily.utilitarianLarge,
CLKComplicationFamily.utilitarianSmall])
]
Expand Down
2 changes: 1 addition & 1 deletion nightguard WatchKit Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>779</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>NSAppTransportSecurity</key>
Expand Down
6 changes: 4 additions & 2 deletions nightguard WatchKit Extension/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ class MainViewModel: ObservableObject, Identifiable {

fileprivate func updateComplication() {
let complicationServer = CLKComplicationServer.sharedInstance()
for complication in complicationServer.activeComplications! {
complicationServer.reloadTimeline(for: complication)
if complicationServer.activeComplications != nil {
for complication in complicationServer.activeComplications! {
complicationServer.reloadTimeline(for: complication)
}
}
}

Expand Down
20 changes: 0 additions & 20 deletions nightguard WatchKit Extension/UserDefaultsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ class UserDefaultsRepository {
key: "alarmSoundFileName",
default: "")

static let showBGOnAppBadge = UserDefaultsValue<Bool>(
key: "showBGOnAppBadge",
default: false,
onChange: { show in
#if os(iOS)
if show {
UIApplication.shared.setCurrentBGValueOnAppBadge()
} else {
UIApplication.shared.clearAppBadge()
}
#endif
})

static let alarmNotificationState = UserDefaultsValue<Bool>(key: "alarmNotificationState", default: false)

// If this is set to true, you can override the default units setting from your backend
Expand All @@ -101,13 +88,6 @@ class UserDefaultsRepository {
static let maximumBloodGlucoseDisplayed = UserDefaultsValue<Float>(key: "maximumBloodGlucoseDisplayed", default: 350)

#if os(iOS)
static let screenlockSwitchState = UserDefaultsValue<Bool>(
key: "screenlockSwitchState",
default: UIApplication.shared.isIdleTimerDisabled,
onChange: { screenlock in
UIApplication.shared.isIdleTimerDisabled = screenlock
})

static let nightscoutUris = UserDefaultsValue<[String]>(key: "nightscoutUris", default: [])

// minutes of idle (user inactivity) before dimming the screen (0 means never)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions nightguard Widget Extension/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
11 changes: 11 additions & 0 deletions nightguard Widget Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.widgetkit-extension</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>INEnums</key>
<array/>
<key>INIntentDefinitionModelVersion</key>
<string>1.2</string>
<key>INIntentDefinitionNamespace</key>
<string>88xZPY</string>
<key>INIntentDefinitionSystemVersion</key>
<string>20A294</string>
<key>INIntentDefinitionToolsBuildVersion</key>
<string>12A6144</string>
<key>INIntentDefinitionToolsVersion</key>
<string>12.0</string>
<key>INIntents</key>
<array>
<dict>
<key>INIntentCategory</key>
<string>information</string>
<key>INIntentDescriptionID</key>
<string>tVvJ9c</string>
<key>INIntentEligibleForWidgets</key>
<true/>
<key>INIntentIneligibleForSuggestions</key>
<true/>
<key>INIntentName</key>
<string>Configuration</string>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
<dict>
<key>INIntentResponseCodeName</key>
<string>failure</string>
</dict>
</array>
</dict>
<key>INIntentTitle</key>
<string>Configuration</string>
<key>INIntentTitleID</key>
<string>gpCwrM</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentVerb</key>
<string>View</string>
</dict>
</array>
<key>INTypes</key>
<array/>
</dict>
</plist>
Loading

0 comments on commit aa00c2b

Please sign in to comment.