Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snoozer improvements #266

Merged
merged 5 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions LoopFollow/Application/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@
<nil key="textColor"/>
<color key="highlightedColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<variation key="heightClass=regular-widthClass=compact">
<fontDescription key="fontDescription" type="system" pointSize="40"/>
<fontDescription key="fontDescription" type="system" pointSize="65"/>
</variation>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="MinAgo" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="HNC-P8-PbV">
Expand All @@ -377,7 +377,7 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
<variation key="heightClass=regular-widthClass=compact">
<fontDescription key="fontDescription" type="system" pointSize="40"/>
<fontDescription key="fontDescription" type="system" pointSize="65"/>
</variation>
</label>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Alert Name" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsLetterSpacingToFitWidth="YES" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="tiA-fs-ZRV">
Expand Down
43 changes: 27 additions & 16 deletions LoopFollow/Controllers/Nightscout/BGData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import UIKit
extension MainViewController {
// Dex Share Web Call
func webLoadDexShare() {
Expand Down Expand Up @@ -213,25 +214,26 @@ extension MainViewController {
}

// NS BG Data Front end updater
func viewUpdateNSBG (sourceName: String) {
func viewUpdateNSBG(sourceName: String) {
DispatchQueue.main.async {
if UserDefaultsRepository.debugLog.value {
self.writeDebugLog(value: "Display: BG")
self.writeDebugLog(value: "Num BG: " + self.bgData.count.description)
}

let entries = self.bgData
if entries.count < 2 { return } // protect index out of bounds
if entries.count < 2 { return } // Protect index out of bounds

self.updateBGGraph()
self.updateStats()

let latestEntryi = entries.count - 1
let latestBG = entries[latestEntryi].sgv
let priorBG = entries[latestEntryi - 1].sgv
let deltaBG = latestBG - priorBG as Int
let lastBGTime = entries[latestEntryi].date
let latestEntryIndex = entries.count - 1
let latestBG = entries[latestEntryIndex].sgv
let priorBG = entries[latestEntryIndex - 1].sgv
let deltaBG = latestBG - priorBG
let lastBGTime = entries[latestEntryIndex].date

let deltaTime = (TimeInterval(Date().timeIntervalSince1970)-lastBGTime) / 60
let deltaTime = (TimeInterval(Date().timeIntervalSince1970) - lastBGTime) / 60
var userUnit = " mg/dL"
if self.mmol {
userUnit = " mmol/L"
Expand All @@ -243,40 +245,49 @@ extension MainViewController {
var snoozerDirection = ""
var snoozerDelta = ""

// Set BGText with the latest BG value
self.BGText.text = bgUnits.toDisplayUnits(String(latestBG))
snoozerBG = bgUnits.toDisplayUnits(String(latestBG))
self.setBGTextColor()

if let directionBG = entries[latestEntryi].direction {
// Direction handling
if let directionBG = entries[latestEntryIndex].direction {
self.DirectionText.text = self.bgDirectionGraphic(directionBG)
snoozerDirection = self.bgDirectionGraphic(directionBG)
self.latestDirectionString = self.bgDirectionGraphic(directionBG)
}
else
{
} else {
self.DirectionText.text = ""
snoozerDirection = ""
self.latestDirectionString = ""
}

// Delta handling
if deltaBG < 0 {
self.DeltaText.text = bgUnits.toDisplayUnits(String(deltaBG))
snoozerDelta = bgUnits.toDisplayUnits(String(deltaBG))
self.latestDeltaString = String(deltaBG)
}
else
{
} else {
self.DeltaText.text = "+" + bgUnits.toDisplayUnits(String(deltaBG))
snoozerDelta = "+" + bgUnits.toDisplayUnits(String(deltaBG))
self.latestDeltaString = "+" + String(deltaBG)
}

// Apply strikethrough to BGText based on the staleness of the data
let bgTextStr = self.BGText.text ?? ""
let attributeString = NSMutableAttributedString(string: bgTextStr)
attributeString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributeString.length))
if deltaTime >= 12 { // Data is stale
attributeString.addAttribute(.strikethroughColor, value: UIColor.systemRed, range: NSRange(location: 0, length: attributeString.length))
} else { // Data is fresh
attributeString.addAttribute(.strikethroughColor, value: UIColor.clear, range: NSRange(location: 0, length: attributeString.length))
}
self.BGText.attributedText = attributeString

// Snoozer Display
guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
snoozer.BGLabel.text = snoozerBG
snoozer.DirectionLabel.text = snoozerDirection
snoozer.DeltaLabel.text = snoozerDelta

}
}
}
53 changes: 32 additions & 21 deletions LoopFollow/Controllers/Timers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ extension MainViewController {
repeats: true)
}

// Updates Min Ago display
@objc func minAgoTimerDidEnd(_ timer:Timer) {

// print("min ago timer ended")
@objc func minAgoTimerDidEnd(_ timer: Timer) {
if bgData.count > 0 {
let bgSeconds = bgData.last!.date
let now = Date().timeIntervalSince1970
Expand All @@ -54,36 +51,50 @@ extension MainViewController {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale

if secondsAgo < 270 {
formatter.allowedUnits = [ .minute] // Units to display in the formatted string
if secondsAgo >= 720 { // 720 seconds = 12 minutes
formatter.allowedUnits = [.minute] // Only show minutes after 12 minutes have passed
} else if secondsAgo < 270 { // Less than 4.5 minutes
formatter.allowedUnits = [.minute] // Show only minutes if less than 4.5 minutes
} else {
formatter.allowedUnits = [ .minute, .second] // Units to display in the formatted string
formatter.allowedUnits = [.minute, .second] // Show minutes and seconds otherwise
}

let formattedDuration = formatter.string(from: secondsAgo) ?? ""
let minAgoDisplayText = formattedDuration + " min ago"

//formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
let formattedDuration = formatter.string(from: secondsAgo)

MinAgoText.text = formattedDuration ?? ""
MinAgoText.text! += " min ago"
latestMinAgoString = formattedDuration ?? ""
latestMinAgoString += " min ago"
MinAgoText.text = minAgoDisplayText
latestMinAgoString = minAgoDisplayText

if let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController {
snoozer.MinAgoLabel.text = formattedDuration ?? ""
snoozer.MinAgoLabel.text! += " min ago"
} else { return }

snoozer.MinAgoLabel.text = minAgoDisplayText

// Start with the current BGLabel text
let bgLabelText = snoozer.BGLabel.text ?? ""
let attributeString = NSMutableAttributedString(string: bgLabelText)

// Always apply the strikethrough style
attributeString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributeString.length))

// Conditionally set the strikethrough color based on the freshness of the data
if secondsAgo >= 720 { // Data is stale
attributeString.addAttribute(.strikethroughColor, value: UIColor.systemRed, range: NSRange(location: 0, length: attributeString.length))
} else { // Data is fresh
attributeString.addAttribute(.strikethroughColor, value: UIColor.clear, range: NSRange(location: 0, length: attributeString.length))
}

snoozer.BGLabel.attributedText = attributeString
}
} else {
MinAgoText.text = ""
latestMinAgoString = ""

if let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController {
snoozer.MinAgoLabel.text = ""
} else { return }

// Reset BGLabel to ensure no formatting is carried over
snoozer.BGLabel.text = ""
snoozer.BGLabel.attributedText = NSAttributedString(string: "")
}
}

}

// Runs a 60 second timer when an alarm is snoozed
Expand Down
20 changes: 0 additions & 20 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,26 +425,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
showHideNSDetails()
}



//update Min Ago Text. We need to call this separately because it updates between readings
func updateMinAgo(){
if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Update min ago text") }
guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
if bgData.count > 0 {
let deltaTime = (TimeInterval(Date().timeIntervalSince1970)-bgData[bgData.count - 1].date) / 60
minAgoBG = Double(TimeInterval(Date().timeIntervalSince1970)-bgData[bgData.count - 1].date)
MinAgoText.text = String(Int(deltaTime)) + " min ago"
snoozer.MinAgoLabel.text = String(Int(deltaTime)) + " min ago"
latestMinAgoString = String(Int(deltaTime)) + " min ago"
} else {
MinAgoText.text = ""
snoozer.MinAgoLabel.text = ""
latestMinAgoString = ""
}

}

//Clear the info data before next pull. This ensures we aren't displaying old data if something fails.
func clearLastInfoData(index: Int){
tableData[index].value = ""
Expand Down