Skip to content

Commit

Permalink
Fixed a crash calculating the chart without clear min/max bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermanns committed May 7, 2021
1 parent f9d00d5 commit 72e5be8
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 68 deletions.
34 changes: 19 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ GEM
artifactory (3.0.15)
atomos (0.1.3)
aws-eventstream (1.1.1)
aws-partitions (1.432.0)
aws-sdk-core (3.113.0)
aws-partitions (1.447.0)
aws-sdk-core (3.114.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.239.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-kms (1.43.0)
aws-sdk-core (~> 3, >= 3.112.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.91.0)
aws-sdk-s3 (1.93.1)
aws-sdk-core (~> 3, >= 3.112.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.1)
Expand All @@ -29,26 +29,29 @@ GEM
commander-fastlane (4.4.6)
highline (~> 1.7.2)
declarative (0.0.20)
declarative-option (0.1.0)
digest-crc (0.6.3)
rake (>= 12.0.0, < 14.0.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.6)
emoji_regex (3.2.2)
excon (0.79.0)
faraday (1.3.0)
excon (0.80.1)
faraday (1.4.1)
faraday-excon (~> 1.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
multipart-post (>= 1.2, < 3)
ruby2_keywords
ruby2_keywords (>= 0.0.4)
faraday-cookie_jar (0.0.7)
faraday (>= 0.8.0)
http-cookie (~> 1.0.0)
faraday-excon (1.1.0)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.1.0)
faraday_middleware (1.0.0)
faraday (~> 1.0)
fastimage (2.2.3)
fastlane (2.178.0)
fastlane (2.181.0)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.3, < 3.0.0)
artifactory (~> 3.0)
Expand Down Expand Up @@ -105,7 +108,7 @@ GEM
rexml
signet (~> 0.14)
webrick
google-apis-iamcredentials_v1 (0.2.0)
google-apis-iamcredentials_v1 (0.3.0)
google-apis-core (~> 0.1)
google-apis-storage_v1 (0.3.0)
google-apis-core (~> 0.1)
Expand All @@ -123,7 +126,7 @@ GEM
google-cloud-core (~> 1.2)
googleauth (~> 0.9)
mini_mime (~> 1.0)
googleauth (0.16.0)
googleauth (0.16.1)
faraday (>= 0.17.3, < 2.0)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
Expand All @@ -136,10 +139,10 @@ GEM
httpclient (2.8.3)
jmespath (1.4.0)
json (2.5.1)
jwt (2.2.2)
jwt (2.2.3)
memoist (0.16.2)
mini_magick (4.11.0)
mini_mime (1.0.2)
mini_mime (1.1.0)
multi_json (1.15.0)
multipart-post (2.0.0)
nanaimo (0.3.0)
Expand All @@ -148,12 +151,12 @@ GEM
plist (3.6.0)
public_suffix (4.0.6)
rake (13.0.3)
representable (3.0.4)
representable (3.1.1)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
rexml (3.2.4)
rexml (3.2.5)
rouge (2.0.7)
ruby2_keywords (0.0.4)
rubyzip (2.3.0)
Expand All @@ -170,6 +173,7 @@ GEM
terminal-notifier (2.0.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
trailblazer-option (0.1.1)
tty-cursor (0.7.1)
tty-screen (0.8.1)
tty-spinner (0.9.3)
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>717</string>
<string>721</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
41 changes: 30 additions & 11 deletions nightguard WatchKit Extension/ChartPainter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ class ChartPainter {
}

fileprivate func paintTreatments(_ context : CGContext, bgValues : [BloodSugar], maxBgValue : CGFloat) {
var isOddTreatment = true
for treatment in TreatmentsStream.singleton.treatments {
if let mealBolusTreatment = treatment as? MealBolusTreatment {
drawMealBolusValue(context,
carbs: mealBolusTreatment.carbs,
insulin: mealBolusTreatment.insulin,
x: calcXValue(Double(mealBolusTreatment.timestamp)),
y: CGFloat(paintableHeight - treatmentsYOffset()))
y: CGFloat(paintableHeight - treatmentsYOffset(isOddTreatment: isOddTreatment)))
}
if let correctionBolusTreatment = treatment as? CorrectionBolusTreatment {
drawCorrectionBolusValue(context,
Expand All @@ -256,27 +257,38 @@ class ChartPainter {
if let bolusWizardTreatment = treatment as? BolusWizardTreatment {
drawBolusWizardValue(context, insulin: bolusWizardTreatment.insulin,
x: calcXValue(Double(bolusWizardTreatment.timestamp)),
y: CGFloat(paintableHeight - treatmentsYOffset()))
y: CGFloat(paintableHeight - treatmentsYOffset(isOddTreatment: isOddTreatment)))
}
if let carbCorrectionTreatment = treatment as? CarbCorrectionTreatment {
drawMealBolusValue(context,
carbs: carbCorrectionTreatment.carbs,
insulin: 0,
x: calcXValue(Double(carbCorrectionTreatment.timestamp)),
y: CGFloat(paintableHeight - treatmentsYOffset()))
y: CGFloat(paintableHeight - treatmentsYOffset(isOddTreatment: isOddTreatment)))
}
// toggle the odd/even marker. This is used to prevent close treatments to be painted
// on the same position in the chart
isOddTreatment = !isOddTreatment
}
}

// determines how low treatments like carbs should be displayed on the
// chart. If on the apple watch, this should differ to the main ios app.
fileprivate func treatmentsYOffset() -> Int {
fileprivate func treatmentsYOffset(isOddTreatment : Bool) -> Int {
if self.canvasHeight < 500 {
// On the watch: use smaller fonts
return 10
if (isOddTreatment) {
return 30
} else {
return 10
}
}

return 20
if (isOddTreatment) {
return 50
} else {
return 25
}
}

fileprivate func nearestBgValueYValue(timestamp : Double, bgs : [BloodSugar]) -> CGFloat {
Expand Down Expand Up @@ -516,7 +528,10 @@ class ChartPainter {
}

fileprivate func paintEverySecondHour(_ context : CGContext, attrs : [NSAttributedString.Key : Any]) {
let halfHours = determineEverySecondHourBetween(minimumXValue, maxTimestamp: maximumXValue)

guard let halfHours = determineEverySecondHourBetween(minimumXValue, maxTimestamp: maximumXValue) else {
return
}

let hourFormat = DateFormatter()
hourFormat.timeStyle = .short
Expand All @@ -533,7 +548,7 @@ class ChartPainter {
}
}

fileprivate func determineEverySecondHourBetween(_ minTimestamp : Double, maxTimestamp : Double) -> [Double] {
fileprivate func determineEverySecondHourBetween(_ minTimestamp : Double, maxTimestamp : Double) -> [Double]? {

let minDate = Date(timeIntervalSince1970: minTimestamp / 1000)
let maxDate = Date(timeIntervalSince1970: maxTimestamp / 1000)
Expand All @@ -543,7 +558,9 @@ class ChartPainter {
var stop : Bool

repeat {
let nextEvenHour = getNextEvenHour(currentDate);
guard let nextEvenHour = getNextEvenHour(currentDate) else {
return nil
}
if nextEvenHour.compare(maxDate) == .orderedAscending {
evenHours.append(nextEvenHour.timeIntervalSince1970 * 1000)
stop = false
Expand Down Expand Up @@ -600,13 +617,15 @@ class ChartPainter {
}

// Returns e.g. 04:00 for 02:00 o'clock.
fileprivate func getNextEvenHour(_ date : Date) -> Date {
fileprivate func getNextEvenHour(_ date : Date) -> Date? {

let cal = Calendar.current

let hour = (cal as NSCalendar).component(NSCalendar.Unit.hour, from: date)

let currentHour = (cal as NSCalendar).date(bySettingHour: hour, minute: 0, second: 0, of: date, options: NSCalendar.Options())!
guard let currentHour = (cal as NSCalendar).date(bySettingHour: hour, minute: 0, second: 0, of: date, options: NSCalendar.Options()) else {
return nil
}
var nextHour = currentHour.addingTimeInterval(
isEven(hour + 1) ? fullHour : 2 * fullHour
)
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>717</string>
<string>721</string>
<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>CLKComplicationSupportedFamilies</key>
Expand Down
12 changes: 6 additions & 6 deletions nightguard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEVELOPMENT_TEAM = BSAVUVP8PV;
INFOPLIST_FILE = "nightguard WatchKit Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -2107,7 +2107,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEVELOPMENT_TEAM = BSAVUVP8PV;
INFOPLIST_FILE = "nightguard WatchKit Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -2139,7 +2139,7 @@
CODE_SIGN_ENTITLEMENTS = "nightguard WatchKit App/nightguard.entitlements";
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
IBSC_MODULE = scoutwatch_WatchKit_Extension;
Expand Down Expand Up @@ -2172,7 +2172,7 @@
CODE_SIGN_ENTITLEMENTS = "nightguard WatchKit App/nightguard.entitlements";
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
IBSC_MODULE = scoutwatch_WatchKit_Extension;
Expand Down Expand Up @@ -2202,7 +2202,7 @@
CODE_SIGN_ENTITLEMENTS = nightguard/scoutwatch.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
ENABLE_BITCODE = NO;
Expand Down Expand Up @@ -2230,7 +2230,7 @@
CODE_SIGN_ENTITLEMENTS = nightguard/scoutwatch.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CURRENT_PROJECT_VERSION = 717;
CURRENT_PROJECT_VERSION = 721;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = BSAVUVP8PV;
ENABLE_BITCODE = NO;
Expand Down
4 changes: 3 additions & 1 deletion nightguard/DateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ extension Date {
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

let isoTimeAsString = String(describing: isoTime)
guard let convertedDate = dateFormatter.date(from: isoTimeAsString) else { return Date() }
guard let convertedDate = dateFormatter.date(from: isoTimeAsString) else {
return Date()
}

return convertedDate
}
Expand Down
5 changes: 5 additions & 0 deletions nightguard/DoubleExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ extension Double {
formatter.maximumFractionDigits = fractionDigits
return formatter.string(from: NSNumber(value: self)) ?? "\(self)"
}

static func fromIsoString(isoTime: String) -> Double {

return Date.fromIsoString(isoTime: isoTime).timeIntervalSince1970 * 1000
}
}
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>717</string>
<string>721</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationCategoryType</key>
Expand Down
30 changes: 6 additions & 24 deletions nightguard/NightscoutData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,17 @@ class NightscoutData : NSObject, NSCoding, Codable {
}
self.bgdeltaString = bgdeltaString

guard let bgdeltaArrow = decoder.decodeObject(forKey: "bgdeltaArrow") as? String else {
return
}
self.bgdeltaArrow = bgdeltaArrow
bgdeltaArrow = decoder.decodeObject(forKey: "bgdeltaArrow") as? String ?? "?"

guard let bgdelta = decoder.decodeObject(forKey: "bgdelta") as? Float else {
return
}
self.bgdelta = bgdelta
self.bgdelta = decoder.decodeObject(forKey: "bgdelta") as? Float ?? 0.0

guard let time = decoder.decodeObject(forKey: "time") as? NSNumber else {
return
}
self.time = time
self.time = decoder.decodeObject(forKey: "time") as? NSNumber ?? 0

guard let battery = decoder.decodeObject(forKey: "battery") as? String else {
return
}
self.battery = battery
self.battery = decoder.decodeObject(forKey: "battery") as? String ?? "?"

guard let iob = decoder.decodeObject(forKey: "iob") as? String else {
return
}
self.iob = iob
self.iob = decoder.decodeObject(forKey: "iob") as? String ?? "?"

guard let cob = decoder.decodeObject(forKey: "cob") as? String else {
return
}
self.cob = cob
self.cob = decoder.decodeObject(forKey: "cob") as? String ?? "?"
}

/*
Expand Down
Loading

0 comments on commit 72e5be8

Please sign in to comment.