From 76b11c740d87e79ac985d4cbf2dfc16193092d39 Mon Sep 17 00:00:00 2001 From: Dirk Hermanns Date: Thu, 20 Apr 2023 18:44:50 +0200 Subject: [PATCH] #215: Fixed some issues with mmol and more optimizations to the widgets. --- .../AccessoryCornerGaugeView.swift | 4 ++-- .../AccessoryCornerView.swift | 8 ++++---- nightguard WatchKit App/Info.plist | 2 +- nightguard WatchKit Extension/MainView.swift | 4 +++- .../MainViewModel.swift | 6 ++++++ .../AccessoryCircularGaugeView.swift | 6 +++--- .../AccessoryCircularView.swift | 7 +++---- .../AccessoryInlineView.swift | 2 +- .../AccessoryRectangularView.swift | 6 +++--- .../NightguardTimelineProvider.swift | 20 +++++++++++++------ .../NightscoutDataEntry.swift | 8 ++++++-- nightguard.xcodeproj/project.pbxproj | 18 +++++++++-------- nightguard/Info.plist | 2 +- nightguard/PrefsViewController.swift | 5 ++++- nightguard/UnitsConverter.swift | 6 +++--- nightguardTests/Info.plist | 2 +- nightguardUITests/Info.plist | 2 +- 17 files changed, 66 insertions(+), 42 deletions(-) diff --git a/nightguard Complication/AccessoryCornerGaugeView.swift b/nightguard Complication/AccessoryCornerGaugeView.swift index 4c4458d7..36d86cd8 100644 --- a/nightguard Complication/AccessoryCornerGaugeView.swift +++ b/nightguard Complication/AccessoryCornerGaugeView.swift @@ -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))) diff --git a/nightguard Complication/AccessoryCornerView.swift b/nightguard Complication/AccessoryCornerView.swift index 53ee4793..69b9e907 100644 --- a/nightguard Complication/AccessoryCornerView.swift +++ b/nightguard Complication/AccessoryCornerView.swift @@ -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() diff --git a/nightguard WatchKit App/Info.plist b/nightguard WatchKit App/Info.plist index 29f194b7..11df5bd0 100644 --- a/nightguard WatchKit App/Info.plist +++ b/nightguard WatchKit App/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 817 + 819 CLKComplicationPrincipalClass $(PRODUCT_MODULE_NAME).ComplicationController NSAppTransportSecurity diff --git a/nightguard WatchKit Extension/MainView.swift b/nightguard WatchKit Extension/MainView.swift index d02623d1..06218cb6 100644 --- a/nightguard WatchKit Extension/MainView.swift +++ b/nightguard WatchKit Extension/MainView.swift @@ -25,6 +25,7 @@ struct MainView: View { self.viewModel = mainViewModel + //UserDefaultsRepository.units.value = Units.mmol updateUnits() viewModel.refreshData(forceRefresh: true, moveToLatestValue: true) } @@ -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) @@ -203,7 +206,6 @@ struct MainView: View { fileprivate func updateUnits() { - print(UserDefaultsRepository.units.value) /*NightscoutService.singleton.readStatus { (result: NightscoutRequestResult) in switch result { diff --git a/nightguard WatchKit Extension/MainViewModel.swift b/nightguard WatchKit Extension/MainViewModel.swift index b2fd6d53..13b4602e 100644 --- a/nightguard WatchKit Extension/MainViewModel.swift +++ b/nightguard WatchKit Extension/MainViewModel.swift @@ -11,6 +11,7 @@ import SwiftUI import SpriteKit import ClockKit import WatchConnectivity +import WidgetKit @available(watchOSApplicationExtension 6.0, *) class MainViewModel: ObservableObject, Identifiable { @@ -77,6 +78,7 @@ class MainViewModel: ObservableObject, Identifiable { func refreshData(forceRefresh : Bool, moveToLatestValue : Bool) { + WidgetCenter.shared.reloadAllTimelines() showCareAndLoopData = UserDefaultsRepository.showCareAndLoopData.value loadCurrentBgData(forceRefresh: forceRefresh) @@ -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! { @@ -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) } } diff --git a/nightguard Widget Extension/AccessoryCircularGaugeView.swift b/nightguard Widget Extension/AccessoryCircularGaugeView.swift index 510e4125..6bf32d06 100644 --- a/nightguard Widget Extension/AccessoryCircularGaugeView.swift +++ b/nightguard Widget Extension/AccessoryCircularGaugeView.swift @@ -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))) diff --git a/nightguard Widget Extension/AccessoryCircularView.swift b/nightguard Widget Extension/AccessoryCircularView.swift index e21874e8..bc3467cb 100644 --- a/nightguard Widget Extension/AccessoryCircularView.swift +++ b/nightguard Widget Extension/AccessoryCircularView.swift @@ -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) diff --git a/nightguard Widget Extension/AccessoryInlineView.swift b/nightguard Widget Extension/AccessoryInlineView.swift index 15c6a9de..8301d286 100644 --- a/nightguard Widget Extension/AccessoryInlineView.swift +++ b/nightguard Widget Extension/AccessoryInlineView.swift @@ -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() diff --git a/nightguard Widget Extension/AccessoryRectangularView.swift b/nightguard Widget Extension/AccessoryRectangularView.swift index 2561ea73..6fe7d838 100644 --- a/nightguard Widget Extension/AccessoryRectangularView.swift +++ b/nightguard Widget Extension/AccessoryRectangularView.swift @@ -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) } } diff --git a/nightguard Widget Extension/NightguardTimelineProvider.swift b/nightguard Widget Extension/NightguardTimelineProvider.swift index 1511bec0..b1f0d184 100644 --- a/nightguard Widget Extension/NightguardTimelineProvider.swift +++ b/nightguard Widget Extension/NightguardTimelineProvider.swift @@ -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 { @@ -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 @@ -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) } diff --git a/nightguard Widget Extension/NightscoutDataEntry.swift b/nightguard Widget Extension/NightscoutDataEntry.swift index 575ca9ed..4600ac8c 100644 --- a/nightguard Widget Extension/NightscoutDataEntry.swift +++ b/nightguard Widget Extension/NightscoutDataEntry.swift @@ -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 @@ -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 } diff --git a/nightguard.xcodeproj/project.pbxproj b/nightguard.xcodeproj/project.pbxproj index afbc2ac1..05b4744d 100644 --- a/nightguard.xcodeproj/project.pbxproj +++ b/nightguard.xcodeproj/project.pbxproj @@ -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"; @@ -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"; @@ -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", @@ -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", @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/nightguard/Info.plist b/nightguard/Info.plist index db3f2aa5..8b44eb93 100644 --- a/nightguard/Info.plist +++ b/nightguard/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 817 + 819 ITSAppUsesNonExemptEncryption LSApplicationCategoryType diff --git a/nightguard/PrefsViewController.swift b/nightguard/PrefsViewController.swift index 3b00eb76..16615409 100644 --- a/nightguard/PrefsViewController.swift +++ b/nightguard/PrefsViewController.swift @@ -8,6 +8,7 @@ import UIKit import Eureka +import WidgetKit class PrefsViewController: CustomFormViewController { @@ -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): @@ -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)) diff --git a/nightguard/UnitsConverter.swift b/nightguard/UnitsConverter.swift index 7dc6f962..28c92baa 100644 --- a/nightguard/UnitsConverter.swift +++ b/nightguard/UnitsConverter.swift @@ -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 diff --git a/nightguardTests/Info.plist b/nightguardTests/Info.plist index ab7e5ca8..ea658232 100644 --- a/nightguardTests/Info.plist +++ b/nightguardTests/Info.plist @@ -19,6 +19,6 @@ CFBundleSignature ???? CFBundleVersion - 817 + 819 diff --git a/nightguardUITests/Info.plist b/nightguardUITests/Info.plist index ab7e5ca8..ea658232 100644 --- a/nightguardUITests/Info.plist +++ b/nightguardUITests/Info.plist @@ -19,6 +19,6 @@ CFBundleSignature ???? CFBundleVersion - 817 + 819