Skip to content

Commit

Permalink
Merge pull request #301 from poml88/release
Browse files Browse the repository at this point in the history
Added various features:
  • Loading branch information
dhermanns authored Jun 23, 2024
2 parents ab4a387 + bc419c7 commit 93287ae
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 88 deletions.
57 changes: 41 additions & 16 deletions nightguard Complication/nightguard_Complication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import WidgetKit
import SwiftUI
import Intents

extension View {

func widgetBackground(backgroundView: some View) -> some View {
if #available(watchOS 10.0, *) {
return containerBackground(for: .widget) {
backgroundView
}
} else {
return background(backgroundView)
}
}

}

@main
struct NightguardWidgetsBundle: WidgetBundle {
var body: some Widget {
Expand Down Expand Up @@ -70,21 +84,27 @@ struct nightguard_Complication_Extension_Previews: PreviewProvider {
static var previews: some View {
NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
.previewDisplayName("Acc_Rect")

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryInline))
.previewDisplayName("Acc_Inline")

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCorner))
.previewDisplayName("Acc_Corner")

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
.previewDisplayName("Acc_Circ")

NightguardGaugeEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCorner))
.previewDisplayName("Gauge_Acc_Corner")

NightguardGaugeEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
.previewDisplayName("Gauge_Acc_Circ")
}
}

Expand All @@ -97,12 +117,15 @@ struct NightguardEntryView : View {
switch widgetFamily {
case .accessoryCorner:
AccessoryCornerView(entry: entry)
.widgetBackground(backgroundView: background())
case .accessoryCircular:
AccessoryCircularView(entry: entry)
.widgetBackground(backgroundView: background())
case .accessoryInline:
AccessoryInlineView(entry: entry)
case .accessoryRectangular:
AccessoryRectangularView(entry: entry)
.widgetBackground(backgroundView: background())
@unknown default:
//mandatory as there are more widget families as in lockscreen widgets etc
Text(NSLocalizedString("Not an implemented widget yet", comment: "Gauge Widget Not Implemented Error Message"))
Expand All @@ -119,28 +142,30 @@ struct NightguardGaugeEntryView : View {
switch widgetFamily {
case .accessoryCorner:
AccessoryCornerGaugeView(entry: entry)
.widgetBackground(backgroundView: background())
case .accessoryCircular:
AccessoryCircularGaugeView(entry: entry)
.widgetBackground(backgroundView: background())
default:
//mandatory as there are more widget families as in lockscreen widgets etc
Text(NSLocalizedString("No Gauge Support for this widget!", comment: "Gauge Widget Not Supported Error Message"))
}
}
}

struct NightguardGaugePreviews: PreviewProvider {
static var previews: some View {

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCircular))

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryCorner))

NightguardEntryView(entry: NightscoutDataEntry.previewValues)
.previewContext(WidgetPreviewContext(family: .accessoryInline))
}
}
//struct NightguardGaugePreviews: PreviewProvider {
// static var previews: some View {
//
// NightguardEntryView(entry: NightscoutDataEntry.previewValues)
// .previewContext(WidgetPreviewContext(family: .accessoryRectangular))
//
// NightguardEntryView(entry: NightscoutDataEntry.previewValues)
// .previewContext(WidgetPreviewContext(family: .accessoryCircular))
//
// NightguardEntryView(entry: NightscoutDataEntry.previewValues)
// .previewContext(WidgetPreviewContext(family: .accessoryCorner))
//
// NightguardEntryView(entry: NightscoutDataEntry.previewValues)
// .previewContext(WidgetPreviewContext(family: .accessoryInline))
// }
//}
5 changes: 5 additions & 0 deletions nightguard WatchKit App/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class ExtensionDelegate: NSObject, WKApplicationDelegate {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
AppState.isUIActive = true
}

func applicationWillEnterForeground() {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NotificationCenter.default.post(name: .refreshDataOnAppBecameActive, object: nil)
}

func applicationWillResignActive() {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down
12 changes: 12 additions & 0 deletions nightguard WatchKit App/NotificationExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// NotificationExtension.swift
// nightguard WatchKit App
//
// Created by Philipp Pöml on 29.05.24.
//

import Foundation

extension Notification.Name {
static let refreshDataOnAppBecameActive = Notification.Name("Updates data on wrist raise")
}
4 changes: 4 additions & 0 deletions nightguard WatchKit App/views/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct MainView: View {

// update the ui every 15 seconds:
let timer = Timer.publish(every: 15, on: .current, in: .common).autoconnect()
let refreshDataOnAppBecameActiveNotification = NotificationCenter.default.publisher(for: .refreshDataOnAppBecameActive)

@ObservedObject var viewModel: MainViewModel

Expand Down Expand Up @@ -201,6 +202,9 @@ struct MainView: View {
.onReceive(timer) { _ in
viewModel.refreshData(forceRefresh: false, moveToLatestValue: false)
}
.onReceive(refreshDataOnAppBecameActiveNotification) { _ in
viewModel.refreshData(forceRefresh: false, moveToLatestValue: false)
}
//}
}

Expand Down
42 changes: 22 additions & 20 deletions nightguard Widget Extension/AccessoryCircularGaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ struct AccessoryCircularGaugeView : View {
var entry: NightscoutDataEntry

var body: some View {

Gauge(value: Double(
calculateAgeInMinutes(
fromDouble: entry.lastBGValues.first?.timestamp ?? Date.now.timeIntervalSinceNow - 3600)) ?? 60,
in: 0...60) {
Text("\(entry.lastBGValues.first?.delta ?? "?")")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(
Float(entry.lastBGValues.first?.delta ?? "99") ?? 99)))
} currentValueLabel: {
Text(entry.lastBGValues.first?.value ?? "??")
.foregroundColor(
Color(UIColorChanger.getBgColor(entry.lastBGValues.first?.value ?? "999")))
ZStack {
AccessoryWidgetBackground()
Gauge(value: Double(
calculateAgeInMinutes(
fromDouble: entry.lastBGValues.first?.timestamp ?? Date.now.timeIntervalSinceNow - 3600)) ?? 60,
in: 0...60) {
Text("\(entry.lastBGValues.first?.delta ?? "?")")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(
Float(entry.lastBGValues.first?.delta ?? "99") ?? 99)))
} currentValueLabel: {
Text(entry.lastBGValues.first?.value ?? "??")
.foregroundColor(
Color(UIColorChanger.getBgColor(entry.lastBGValues.first?.value ?? "999")))
}
.tint(
Color(UIColorChanger.getTimeLabelColor(
fromDouble: entry.lastBGValues.first?.timestamp ?? Date().timeIntervalSinceNow - 3600)))

.gaugeStyle(.accessoryCircular)
.widgetAccentable(true)
.unredacted()
}
.tint(
Color(UIColorChanger.getTimeLabelColor(
fromDouble: entry.lastBGValues.first?.timestamp ?? Date().timeIntervalSinceNow - 3600)))

.gaugeStyle(.accessoryCircular)
.widgetAccentable(true)
.unredacted()
}
}

Expand Down
23 changes: 14 additions & 9 deletions nightguard Widget Extension/AccessoryCircularView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ struct AccessoryCircularView : View {
var entry: NightscoutDataEntry

var body: some View {
Text("\(calculateAgeInMinutes(fromDouble: entry.lastBGValues.first?.timestamp ?? Date.now.timeIntervalSinceNow-3600))m")
Text("\(entry.lastBGValues.first?.value ?? "??")")
.foregroundColor(
Color(UIColorChanger.getBgColor(entry.lastBGValues.first?.value ?? "999")))
Text("\(entry.lastBGValues.first?.delta ?? "?")")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(Float(entry.lastBGValues.first?.delta ?? "99") ?? 99.0)))
.widgetAccentable(true)
.unredacted()
ZStack {
AccessoryWidgetBackground()
VStack {
Text("\(calculateAgeInMinutes(fromDouble: entry.lastBGValues.first?.timestamp ?? Date.now.timeIntervalSinceNow-3600))m")
Text("\(entry.lastBGValues.first?.value ?? "??")")
.foregroundColor(
Color(UIColorChanger.getBgColor(entry.lastBGValues.first?.value ?? "999")))
Text("\(entry.lastBGValues.first?.delta ?? "?")")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(Float(entry.lastBGValues.first?.delta ?? "99") ?? 99.0)))
.widgetAccentable(true)
.unredacted()
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions nightguard Widget Extension/AccessoryInlineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct AccessoryInlineView : View {
var entry: NightscoutDataEntry

var body: some View {
//AccessoryWidgetBackground() not supported in this Widget Family
Text("| \(Date(timeIntervalSince1970:entry.lastBGValues.first?.timestamp ?? (Date.now.timeIntervalSinceNow - 3600) / 1000).toLocalTimeString()) " + "\(entry.lastBGValues.first?.value ?? "?")\(entry.lastBGValues.first?.delta ?? "?")")
.widgetAccentable(true)
.unredacted()
Expand Down
70 changes: 47 additions & 23 deletions nightguard Widget Extension/AccessoryRectangularView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,58 @@ struct AccessoryRectangularView : View {
var entry: NightscoutDataEntry

var body: some View {
VStack {
HStack {
VStack {
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
Text("\(calculateAgeInMinutes(from:NSNumber(value: bgEntry.timestamp)))m")
.foregroundColor(Color(entry.sgvColor))
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
VStack {
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
Text("\(String(bgEntry.value)) \(bgEntry.delta)")
.foregroundColor(Color(entry.sgvColor))
.frame(maxWidth: .infinity, alignment: .leading)
ZStack {
//No background on watch
#if os(iOS)
AccessoryWidgetBackground()
.clipShape(RoundedRectangle(cornerRadius: 10))
#endif
VStack {
HStack {
VStack {
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
//Text("\(calculateAgeInMinutes(from:NSNumber(value: bgEntry.timestamp)))m")
if (entry.lastBGValues.first?.id == bgEntry.id) {
Text(Date.now.addingTimeInterval(-(Date.now.timeIntervalSince1970 - (bgEntry.timestamp / 1000))), style: .timer)
//iOS accessory widgets are b/w...
#if os(watchOS)
.foregroundColor(Color(entry.sgvColor))
#endif
.frame(maxWidth: .infinity, alignment: .trailing)
.monospacedDigit()
.multilineTextAlignment(.trailing)
} else {
Text("+\(calculateAgeInMinutes(from:NSNumber(value: Date.now.timeIntervalSince1970 * 1000 + bgEntry.timestamp - (entry.lastBGValues.first?.timestamp ?? 0))))m")
#if os(watchOS)
.foregroundColor(Color(entry.sgvColor))
#endif
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
}
}
if entry.lastBGValues.isEmpty {
VStack {
Text("--- --- ---")
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
// Text("\(String(bgEntry.value)) \(bgEntry.delta)")
Text("\(String(bgEntry.value)) \(bgEntry.delta) \(bgEntry.arrow)")
#if os(watchOS)
.foregroundColor(Color(entry.sgvColor))
#endif
.frame(maxWidth: .infinity, alignment: .leading)
}
}
if entry.lastBGValues.isEmpty {
VStack {
Text("--- --- ---")
}
}
}
if !entry.errorMessage.isEmpty {
Text("\(entry.errorMessage)")
.font(.system(size: 6))
}
//Text(entry.entryDate, style: .time)
//Text("\(snoozedForMinutes(snoozeTimestamp: entry.snoozedUntilTimestamp))min Snoozed")
}
if !entry.errorMessage.isEmpty {
Text("\(entry.errorMessage)")
.font(.system(size: 6))
}
Text(entry.entryDate, style: .time)
//Text("\(snoozedForMinutes(snoozeTimestamp: entry.snoozedUntilTimestamp))min Snoozed")
}
.widgetAccentable(true)
.unredacted()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions nightguard Widget Extension/NightguardTimelineProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ struct NightguardTimelineProvider: TimelineProvider {
return BgEntry(
value: UnitsConverter.mgdlToDisplayUnits(String(bgValue.value)),
valueColor: UIColorChanger.getBgColor(String(bgValue.value)),
delta: "0", timestamp: bgValue.timestamp)
delta: "0", timestamp: bgValue.timestamp, arrow: bgValue.arrow)
}
} else if case .error(let error) = result {
bgEntries = oldEntries.map() {bgValue in
return BgEntry(
value: UnitsConverter.mgdlToDisplayUnits(String(bgValue.value)),
valueColor: UIColorChanger.getBgColor(String(bgValue.value)),
delta: "0", timestamp: bgValue.timestamp)
delta: "0", timestamp: bgValue.timestamp, arrow: bgValue.arrow)
}
errorMessage = error.localizedDescription
} else {
Expand All @@ -92,7 +92,7 @@ struct NightguardTimelineProvider: TimelineProvider {
return BgEntry(
value: UnitsConverter.mgdlToDisplayUnits(String(bgValue.value)),
valueColor: UIColorChanger.getBgColor(String(bgValue.value)),
delta: "0", timestamp: bgValue.timestamp)
delta: "0", timestamp: bgValue.timestamp, arrow: bgValue.arrow)
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ struct NightguardTimelineProvider: TimelineProvider {
value: bgEntry.value,
valueColor: UIColorChanger.getBgColor(bgEntry.value),
delta: Float(v1AsFloat - v2AsFloat).cleanSignedValue,
timestamp: bgEntry.timestamp)
timestamp: bgEntry.timestamp, arrow: bgEntry.arrow)
newEntries.append(newEntry)
}
preceedingEntry = bgEntry
Expand Down
Loading

0 comments on commit 93287ae

Please sign in to comment.