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

Close speak language dialog on selection #279

Merged
merged 5 commits into from
Apr 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions LoopFollow/Controllers/Alarms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,14 @@ extension MainViewController {
// Speaks the current blood glucose value and the change from the previous value.
// Repeated calls to the function within 30 seconds are prevented.
func speakBG(currentValue: Int, previousValue: Int) {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playback, mode: .default)
try audioSession.setActive(true)
} catch {
print("Failed to set up audio session: \(error)")
}

// Get the current time
let currentTime = Date()

Expand Down
2 changes: 1 addition & 1 deletion LoopFollow/Controllers/Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class StatsData {

stdDev = sqrt(partialSum / Float(bgData.count))
if UserDefaultsRepository.units.value != "mg/dL" {
stdDev = Float( bgUnits.toDisplayUnits( String( stdDev ) ) ) ?? 0.0;
stdDev = stdDev / 18
}

if UserDefaultsRepository.useIFCC.value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ class GeneralSettingsViewController: FormViewController {
let speakBGRow: SwitchRow! = form.rowBy(tag: "speakBG")
return !(speakBGRow.value ?? false)
})
row.presentationMode = PresentationMode.presentModally(
controllerProvider: ControllerProvider.callback {
return SelectorViewController<SelectorRow<PushSelectorCell<String>>> { _ in }
},
onDismiss: { vc in
vc.dismiss(animated: true)
})
}.onChange { row in
guard let value = row.value else { return }
UserDefaultsRepository.speakLanguage.value = value
Expand Down
9 changes: 4 additions & 5 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,10 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
DispatchQueue.main.async {
if UserDefaultsRepository.appBadge.value {
let latestBG = String(val)
UIApplication.shared.applicationIconBadgeNumber = Int(bgUnits.removePeriodForBadge(bgUnits.toDisplayUnits(latestBG))) ?? val
} else {
UIApplication.shared.applicationIconBadgeNumber = 0
}
// if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "updated badge") }
UIApplication.shared.applicationIconBadgeNumber = Int(bgUnits.removePeriodAndCommaForBadge(bgUnits.toDisplayUnits(latestBG))) ?? val
} else {
UIApplication.shared.applicationIconBadgeNumber = 0
}
}
}

Expand Down
39 changes: 25 additions & 14 deletions LoopFollow/helpers/bgUnits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,37 @@ import Foundation
class bgUnits {

static func toDisplayUnits(_ value: String) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal

if UserDefaultsRepository.units.value == "mg/dL" {
return removeDecimals(value)
numberFormatter.maximumFractionDigits = 0 // No decimal places for mg/dL
} else {
// convert mg/dL to mmol/l
let floatValue : Float = Float(value)! * 0.0555
return String(floatValue.cleanValue)
numberFormatter.maximumFractionDigits = 1 // Always one decimal place for mmol/L
numberFormatter.minimumFractionDigits = 1 // This ensures even .0 is displayed
}
}

// if a "." is contained, simply takes the left part of the string only
static func removeDecimals(_ value : String) -> String {
if !value.contains(".") {
return value

numberFormatter.locale = Locale.current

if let number = Float(value) {
if UserDefaultsRepository.units.value == "mg/dL" {
let numberValue = NSNumber(value: number)
return numberFormatter.string(from: numberValue) ?? value
} else {
let mmolValue = number / 18
let numberValue = NSNumber(value: mmolValue)
return numberFormatter.string(from: numberValue) ?? value
}
}

return String(value[..<value.firstIndex(of: ".")!])
return value
}

static func removePeriodForBadge(_ value: String) -> String {
return value.replacingOccurrences(of: ".", with: "")

static func removePeriodAndCommaForBadge(_ value: String) -> String {
var modifiedValue = value
modifiedValue = modifiedValue.replacingOccurrences(of: ".", with: "")
modifiedValue = modifiedValue.replacingOccurrences(of: ",", with: "")
return modifiedValue
}
}

Expand Down