Skip to content

Commit

Permalink
nightscout#215: Fixed some issues with mmol and more optimizations to…
Browse files Browse the repository at this point in the history
… the widgets.
  • Loading branch information
dhermanns committed Apr 20, 2023
1 parent a3fa544 commit 76b11c7
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 42 deletions.
4 changes: 2 additions & 2 deletions nightguard Complication/AccessoryCornerGaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ struct AccessoryCornerGaugeView : View {

var body: some View {

Text("\(UnitsConverter.mgdlToDisplayUnits(entry.sgv))")
Text("\(entry.sgv)")
.font(.system(size: 20))
.foregroundColor(
Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
Color(entry.sgvColor))
.widgetLabel {
ProgressView(value: (Double(calculateAgeInMinutes(from: entry.time)) ?? 100) / 60)
.tint(Color(UIColorChanger.getTimeLabelColor(entry.time)))
Expand Down
8 changes: 4 additions & 4 deletions nightguard Complication/AccessoryCornerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ struct AccessoryCornerView : View {
Text("\(entry.bgdeltaArrow)")
.font(.system(size: 20))
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(entry.bgdelta)))
Color(entry.bgdeltaColor))
.widgetLabel {
Text("\(UnitsConverter.mgdlToDisplayUnits(entry.sgv))" +
"\(UnitsConverter.mgdlToDisplayUnits(entry.bgdeltaString)) " +
Text("\(entry.sgv)" +
"\(entry.bgdeltaString) " +
"\(calculateAgeInMinutes(from: entry.time))m")
.foregroundColor(
Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
Color(entry.sgvColor))
}
.widgetAccentable(true)
.unredacted()
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>817</string>
<string>819</string>
<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>NSAppTransportSecurity</key>
Expand Down
4 changes: 3 additions & 1 deletion nightguard WatchKit Extension/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct MainView: View {

self.viewModel = mainViewModel

//UserDefaultsRepository.units.value = Units.mmol
updateUnits()
viewModel.refreshData(forceRefresh: true, moveToLatestValue: true)
}
Expand Down Expand Up @@ -194,6 +195,8 @@ struct MainView: View {
// Request Data from the main app
// especially the baseUri if missing
WatchSyncRequestMessage().send()
print("*************** \(UserDefaultsRepository.units.value)")
UserDefaults(suiteName: AppConstants.APP_GROUP_ID)?.set(UserDefaultsRepository.units.value.rawValue, forKey: "units")
}
.onReceive(timer) { _ in
viewModel.refreshData(forceRefresh: false, moveToLatestValue: false)
Expand All @@ -203,7 +206,6 @@ struct MainView: View {

fileprivate func updateUnits() {

print(UserDefaultsRepository.units.value)
/*NightscoutService.singleton.readStatus { (result: NightscoutRequestResult<Units>) in

switch result {
Expand Down
6 changes: 6 additions & 0 deletions nightguard WatchKit Extension/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
import SpriteKit
import ClockKit
import WatchConnectivity
import WidgetKit

@available(watchOSApplicationExtension 6.0, *)
class MainViewModel: ObservableObject, Identifiable {
Expand Down Expand Up @@ -77,6 +78,7 @@ class MainViewModel: ObservableObject, Identifiable {

func refreshData(forceRefresh : Bool, moveToLatestValue : Bool) {

WidgetCenter.shared.reloadAllTimelines()
showCareAndLoopData = UserDefaultsRepository.showCareAndLoopData.value

loadCurrentBgData(forceRefresh: forceRefresh)
Expand Down Expand Up @@ -206,6 +208,9 @@ class MainViewModel: ObservableObject, Identifiable {
}

fileprivate func updateComplication() {

WidgetCenter.shared.reloadAllTimelines()

let complicationServer = CLKComplicationServer.sharedInstance()
if complicationServer.activeComplications != nil {
for complication in complicationServer.activeComplications! {
Expand Down Expand Up @@ -295,6 +300,7 @@ class MainViewModel: ObservableObject, Identifiable {
dispatchOnMain { [unowned self] in
if case .data(let newTodaysData) = result {
self.cachedTodaysBgValues = newTodaysData
updateComplication()
paintChartData(todaysData : cachedTodaysBgValues, yesterdaysData : cachedYesterdaysBgValues, moveToLatestValue : true)
}
}
Expand Down
6 changes: 3 additions & 3 deletions nightguard Widget Extension/AccessoryCircularGaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ struct AccessoryCircularGaugeView : View {
Gauge(value: Double(calculateAgeInMinutes(from: entry.time)) ?? 0, in: 0...60) {
Text("\(entry.bgdeltaArrow)")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(entry.bgdelta)))
Color(entry.bgdeltaColor))
} currentValueLabel: {
Text(UnitsConverter.mgdlToDisplayUnits(entry.sgv))
Text(entry.sgv)
.foregroundColor(
Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
Color(entry.sgvColor))
}
.tint(
Color(UIColorChanger.getTimeLabelColor(entry.time)))
Expand Down
7 changes: 3 additions & 4 deletions nightguard Widget Extension/AccessoryCircularView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ struct AccessoryCircularView : View {

var body: some View {
Text("\(calculateAgeInMinutes(from: entry.time))m")
Text("\(UnitsConverter.mgdlToDisplayUnits(entry.sgv))")
.foregroundColor(
Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
Text("\(UnitsConverter.mgdlToDisplayUnits(entry.bgdeltaString))")
Text("\(entry.sgv)")
.foregroundColor(Color(entry.sgvColor))
Text("\(entry.bgdeltaString)")
.foregroundColor(
Color(UIColorChanger.getDeltaLabelColor(entry.bgdelta)))
.widgetAccentable(true)
Expand Down
2 changes: 1 addition & 1 deletion nightguard Widget Extension/AccessoryInlineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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)")
Text("| \(Date(timeIntervalSince1970: entry.time.doubleValue / 1000).toLocalTimeString()) " + "\(entry.sgv)\(entry.bgdeltaString)\(entry.bgdeltaArrow)")

.widgetAccentable(true)
.unredacted()
Expand Down
6 changes: 3 additions & 3 deletions nightguard Widget Extension/AccessoryRectangularView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ struct AccessoryRectangularView : View {
VStack {
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
Text("\(calculateAgeInMinutes(from:NSNumber(value: bgEntry.timestamp)))m")
.foregroundColor(Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
.foregroundColor(Color(entry.sgvColor))
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
VStack {
ForEach(entry.lastBGValues, id: \.self.id) { bgEntry in
Text("\(UnitsConverter.mgdlToDisplayUnits(String(bgEntry.value))) \(UnitsConverter.mgdlToDisplayUnitsWithSign(bgEntry.delta.cleanSignedValue))")
.foregroundColor(Color(UIColorChanger.getBgColor(UnitsConverter.mgdlToDisplayUnits(entry.sgv))))
Text("\(String(bgEntry.value)) \(bgEntry.delta)")
.foregroundColor(Color(entry.sgvColor))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
Expand Down
20 changes: 14 additions & 6 deletions nightguard Widget Extension/NightguardTimelineProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ struct NightguardTimelineProvider: TimelineProvider {
let bloodSugarValues = NightscoutCacheService.singleton.loadTodaysData {_ in }

let bgEntries = bloodSugarValues.map() {bgValue in
return BgEntry(value: bgValue.value, delta: 0, timestamp: bgValue.timestamp)
return BgEntry(
value: UnitsConverter.mgdlToDisplayUnits(String(bgValue.value)),
valueColor: UIColorChanger.getBgColor(String(bgValue.value)),
delta: "0", timestamp: bgValue.timestamp)
}
var reducedEntries = bgEntries
if bgEntries.count > 3 {
Expand All @@ -75,19 +78,21 @@ struct NightguardTimelineProvider: TimelineProvider {
}
}

let reducedEntriesWithDelta = calculateDeltaValues(reducedEntries.reversed())
let reducedEntriesWithDelta = calculateDeltaValues(reducedEntries)

let entry = NightscoutDataEntry(
date: Date(timeIntervalSince1970: data.time.doubleValue / 1000),
sgv: data.sgv,
bgdeltaString: data.bgdeltaString,
sgv: UnitsConverter.mgdlToDisplayUnits(data.sgv),
sgvColor: UIColorChanger.getBgColor(data.sgv),
bgdeltaString: UnitsConverter.mgdlToDisplayUnitsWithSign(data.bgdeltaString),
bgdeltaColor: UIColorChanger.getDeltaLabelColor(data.bgdelta),
bgdeltaArrow: data.bgdeltaArrow,
bgdelta: data.bgdelta,
time: data.time,
battery: data.battery,
iob: data.iob,
cob: data.cob,
lastBGValues: reducedEntriesWithDelta,
lastBGValues: reducedEntriesWithDelta.reversed(),
configuration: ConfigurationIntent())

return entry
Expand All @@ -99,9 +104,12 @@ struct NightguardTimelineProvider: TimelineProvider {
var newEntries: [BgEntry] = []
for bgEntry in reducedEntries {
if preceedingEntry?.value != nil {
let v1AsFloat: Float = Float(bgEntry.value) ?? Float.zero
let v2AsFloat: Float = Float(preceedingEntry?.value ?? bgEntry.value) ?? v1AsFloat
let newEntry = BgEntry(
value: bgEntry.value,
delta: bgEntry.value - (preceedingEntry?.value ?? bgEntry.value),
valueColor: UIColorChanger.getBgColor(bgEntry.value),
delta: Float(v1AsFloat - v2AsFloat).cleanSignedValue,
timestamp: bgEntry.timestamp)
newEntries.append(newEntry)
}
Expand Down
8 changes: 6 additions & 2 deletions nightguard Widget Extension/NightscoutDataEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import Foundation
import WidgetKit
import Intents
import SwiftUI

struct NightscoutDataEntry: TimelineEntry {

var date: Date = Date()

var sgv : String = "---"
var sgvColor : UIColor = UIColor.white
// the delta Value in Display Units
var bgdeltaString : String = "---"
var bgdeltaColor : UIColor = UIColor.white
var bgdeltaArrow : String = "-"
// the delta value in mgdl
var bgdelta : Float = 0.0
Expand Down Expand Up @@ -61,7 +64,8 @@ struct NightscoutDataEntry: TimelineEntry {
struct BgEntry : Identifiable, Hashable {

let id = UUID()
let value: Float
let delta: Float
let value: String
let valueColor: UIColor
let delta: String
let timestamp: Double
}
18 changes: 10 additions & 8 deletions nightguard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@
CODE_SIGN_ENTITLEMENTS = "nightguard WatchKit Extension/scoutwatch WatchKit Extension.entitlements";
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
INFOPLIST_FILE = "nightguard WatchKit App/Info.plist";
Expand Down Expand Up @@ -2472,7 +2472,7 @@
CODE_SIGN_ENTITLEMENTS = "nightguard WatchKit Extension/scoutwatch WatchKit Extension.entitlements";
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
INFOPLIST_FILE = "nightguard WatchKit App/Info.plist";
Expand Down Expand Up @@ -2501,11 +2501,12 @@
CODE_SIGN_ENTITLEMENTS = nightguard/scoutwatch.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = nightguard/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.7;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -2530,11 +2531,12 @@
CODE_SIGN_ENTITLEMENTS = nightguard/scoutwatch.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = nightguard/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.7;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -2646,7 +2648,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEVELOPMENT_TEAM = BSAVUVP8PV;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -2687,7 +2689,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEVELOPMENT_TEAM = BSAVUVP8PV;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -2726,7 +2728,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEVELOPMENT_TEAM = BSAVUVP8PV;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -2769,7 +2771,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 817;
CURRENT_PROJECT_VERSION = 819;
DEVELOPMENT_TEAM = BSAVUVP8PV;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
2 changes: 1 addition & 1 deletion nightguard/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>817</string>
<string>819</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationCategoryType</key>
Expand Down
5 changes: 4 additions & 1 deletion nightguard/PrefsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import Eureka
import WidgetKit

class PrefsViewController: CustomFormViewController {

Expand Down Expand Up @@ -291,6 +292,7 @@ class PrefsViewController: CustomFormViewController {
switch result {
case .data(let units):
UserDefaultsRepository.units.value = units
WidgetCenter.shared.reloadAllTimelines()
completion(nil)

case .error(let error):
Expand All @@ -306,7 +308,8 @@ class PrefsViewController: CustomFormViewController {
}

let bookmarkToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30))
bookmarkToolbar.barStyle = .blackTranslucent
bookmarkToolbar.barStyle = UIBarStyle.black
bookmarkToolbar.isTranslucent = true
bookmarkToolbar.items = [
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(toggleKeyboardAndBookmarks))
Expand Down
6 changes: 3 additions & 3 deletions nightguard/UnitsConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class UnitsConverter {
// if the UI is locked, looks like we are not allowed to access the userdefaults
// so do this on main Thread only:
var units = Units.mmol
dispatchOnMain {
units = UserDefaultsRepository.units.value
}
//dispatchOnMain {
units = Units.fromAny(UserDefaults(suiteName: AppConstants.APP_GROUP_ID)?.value(forKey: "units") ?? Units.mmol.rawValue) ?? Units.mmol
//}

if units == Units.mgdl {
// nothing to do here - just remove decimals
Expand Down
2 changes: 1 addition & 1 deletion nightguardTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>817</string>
<string>819</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion nightguardUITests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>817</string>
<string>819</string>
</dict>
</plist>

0 comments on commit 76b11c7

Please sign in to comment.