forked from nightscout/nightguard
-
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.
Added new complication target for the new SwiftUI Complications
- Loading branch information
Showing
15 changed files
with
612 additions
and
130 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
nightguard Complication/Assets.xcassets/AccentColor.colorset/Contents.json
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,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
nightguard Complication/Assets.xcassets/AppIcon.appiconset/Contents.json
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,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "watchos", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
nightguard Complication/Assets.xcassets/WidgetBackground.colorset/Contents.json
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,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,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> |
59 changes: 59 additions & 0 deletions
59
nightguard Complication/nightguard_Complication.intentdefinition
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,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> |
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,53 @@ | ||
// | ||
// nightguard_Complication_Extension.swift | ||
// nightguard Complication Extension | ||
// | ||
// Created by Dirk Hermanns on 02.04.23. | ||
// Copyright © 2023 private. All rights reserved. | ||
// | ||
|
||
import WidgetKit | ||
import SwiftUI | ||
import Intents | ||
|
||
struct nightguard_Complication_ExtensionEntryView : View { | ||
|
||
@Environment(\.widgetFamily) var widgetFamily | ||
var entry: NightscoutDataEntry | ||
|
||
var body: some View { | ||
switch widgetFamily { | ||
case .accessoryCorner: | ||
AccessoryInlineView(entry: entry) | ||
case .accessoryCircular: | ||
AccessoryInlineView(entry: entry) | ||
case .accessoryInline: | ||
AccessoryInlineView(entry: entry) | ||
case .accessoryRectangular: | ||
AccessoryRectangularView(entry: entry) | ||
@unknown default: | ||
//mandatory as there are more widget families as in lockscreen widgets etc | ||
Text("Not an implemented widget yet") | ||
} | ||
} | ||
} | ||
|
||
@main | ||
struct nightguard_Complication_Extension: Widget { | ||
let kind: String = "nightguard_Complication_Extension" | ||
|
||
var body: some WidgetConfiguration { | ||
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: NightguardTimelineProvider()) { entry in | ||
nightguard_Complication_ExtensionEntryView(entry: entry) | ||
} | ||
.configurationDisplayName("BG Values") | ||
.description("Nightguard BG Values Complication") | ||
} | ||
} | ||
|
||
struct nightguard_Complication_Extension_Previews: PreviewProvider { | ||
static var previews: some View { | ||
nightguard_Complication_ExtensionEntryView(entry: NightscoutDataEntry(date: Date(), configuration: ConfigurationIntent())) | ||
.previewContext(WidgetPreviewContext(family: .accessoryRectangular)) | ||
} | ||
} |
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,23 @@ | ||
// | ||
// AccessoryInlineView.swift | ||
// nightguard | ||
// | ||
// Created by Dirk Hermanns on 02.04.23. | ||
// Copyright © 2023 private. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import WidgetKit | ||
|
||
struct AccessoryInlineView : View { | ||
|
||
var entry: NightscoutDataEntry | ||
|
||
var body: some View { | ||
Text("| \(Date(timeIntervalSince1970: entry.time.doubleValue / 1000).toLocalTimeString()) " + "\(UnitsConverter.mgdlToDisplayUnits(entry.sgv))\(UnitsConverter.mgdlToDisplayUnits(entry.bgdeltaString))\(entry.bgdeltaArrow)") | ||
|
||
.widgetAccentable(true) | ||
.unredacted() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
nightguard Widget Extension/AccessoryRectangularView.swift
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,23 @@ | ||
// | ||
// AccessoryRectangularView.swift | ||
// nightguard | ||
// | ||
// Created by Dirk Hermanns on 02.04.23. | ||
// Copyright © 2023 private. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
import WidgetKit | ||
|
||
struct AccessoryRectangularView : View { | ||
|
||
var entry: NightscoutDataEntry | ||
|
||
var body: some View { | ||
Text("| \(Date(timeIntervalSince1970: entry.time.doubleValue / 1000).toLocalTimeString()) " + "\(UnitsConverter.mgdlToDisplayUnits(entry.sgv))\(UnitsConverter.mgdlToDisplayUnits(entry.bgdeltaString))\(entry.bgdeltaArrow)") | ||
|
||
.widgetAccentable(true) | ||
.unredacted() | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
nightguard Widget Extension/NightguardTimelineProvider.swift
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,58 @@ | ||
// | ||
// NightguardTimelineProvider.swift | ||
// nightguard | ||
// | ||
// Created by Dirk Hermanns on 02.04.23. | ||
// Copyright © 2023 private. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import WidgetKit | ||
|
||
struct NightguardTimelineProvider: IntentTimelineProvider { | ||
|
||
func recommendations() -> [IntentRecommendation<ConfigurationIntent>] { | ||
return [ | ||
IntentRecommendation(intent: ConfigurationIntent(), description: "BG Values") | ||
] | ||
} | ||
|
||
|
||
func placeholder(in context: Context) -> NightscoutDataEntry { | ||
|
||
NightscoutDataEntry(configuration: ConfigurationIntent()) | ||
} | ||
|
||
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (NightscoutDataEntry) -> ()) { | ||
|
||
completion(getTimelineData(configuration: configuration)) | ||
} | ||
|
||
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<NightscoutDataEntry>) -> ()) { | ||
|
||
var entries: [NightscoutDataEntry] = [] | ||
|
||
entries.append(getTimelineData(configuration: configuration)) | ||
|
||
let timeline = Timeline(entries: entries, policy: .atEnd) | ||
completion(timeline) | ||
} | ||
|
||
private func getTimelineData(configuration: ConfigurationIntent) -> NightscoutDataEntry { | ||
let data = NightscoutDataRepository.singleton.loadCurrentNightscoutData() | ||
|
||
let entry = NightscoutDataEntry( | ||
date: Date(timeIntervalSince1970: data.time.doubleValue / 1000), | ||
sgv: data.sgv, | ||
bgdeltaString: data.bgdeltaString, | ||
bgdeltaArrow: data.bgdeltaArrow, | ||
bgdelta: data.bgdelta, | ||
time: data.time, | ||
battery: data.battery, | ||
iob: data.iob, | ||
cob: data.cob, | ||
configuration: configuration) | ||
|
||
return entry | ||
} | ||
} |
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,58 @@ | ||
// | ||
// NightscoutDataEntry.swift | ||
// nightguard | ||
// | ||
// Created by Dirk Hermanns on 02.04.23. | ||
// Copyright © 2023 private. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import WidgetKit | ||
import Intents | ||
|
||
struct NightscoutDataEntry: TimelineEntry { | ||
|
||
var date: Date = Date() | ||
|
||
var sgv : String = "---" | ||
// the delta Value in Display Units | ||
var bgdeltaString : String = "---" | ||
var bgdeltaArrow : String = "-" | ||
// the delta value in mgdl | ||
var bgdelta : Float = 0.0 | ||
var hourAndMinutes : String { | ||
get { | ||
if time == 0 { | ||
return "??:??" | ||
} | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "HH:mm" | ||
|
||
let date = Date.init(timeIntervalSince1970: Double(time.int64Value / 1000)) | ||
return formatter.string(from: date) | ||
} | ||
} | ||
var timeString : String { | ||
get { | ||
if time == 0 { | ||
return "-min" | ||
} | ||
|
||
// trick: when displaying the time, we'll add 30 seconds to current time for showing the difference like Nightscout does (0-30 seconds: "0 mins", 31-90 seconds: "1 min", ...) | ||
let thirtySeconds = Int64(30 * 1000) | ||
|
||
// calculate how old the current data is | ||
let currentTime = Int64(Date().timeIntervalSince1970 * 1000) + thirtySeconds | ||
let difference = (currentTime - time.int64Value) / 60000 | ||
if difference > 59 { | ||
return ">1Hr" | ||
} | ||
return String(difference) + "min" | ||
} | ||
} | ||
var time : NSNumber = 0 | ||
var battery : String = "---" | ||
var iob : String = "" | ||
var cob : String = "" | ||
let configuration: ConfigurationIntent | ||
} |
Oops, something went wrong.