Skip to content

Commit

Permalink
1.Bug fix for adding insulin without blousing.
Browse files Browse the repository at this point in the history
2. Localize the error strings from oref0.
  • Loading branch information
Jon-b-m committed Jun 14, 2023
1 parent 6139b7e commit 3f8e09f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 56 deletions.
2 changes: 1 addition & 1 deletion FreeAPS/Resources/javascript/bundle/determine-basal.js

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions FreeAPS/Sources/Localizations/Main/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -1305,17 +1305,38 @@ Enact a temp Basal or a temp target */
/* Hide pop-up */
"Hide" = "Hide";

/* Error string 1. Make translation very short! */
"Predicted Glucose, %@ %@, is below threshold (%@!)" = "Predicted Glucose, %@ %@, is below threshold (%@!)";
/* Bolus pop-up / Alert string. Make translations concise! */
"Predicted Glucose, " = "Predicted Glucose, ";

/* Error string 2. Make translation very short! */
"Eventual Glucose > Target Glucose, but glucose is falling faster than expected. Expected: %@. Falling: %@" = "Eventual Glucose > Target Glucose, but glucose is falling faster than expected. Expected: %@. Falling: %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is predicted below threshold " = "is predicted below threshold ";

/* Error string 3. Make translation very short! */
"Minimum predicted Glucose is %@ %@" = "Minimum predicted Glucose is %@ %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is below Threshold of " = "is below Threshold of ";

/* Error string 4. Make translation very short! */
"Predicted Glucose %@ %@, is below Threshold of %@" = "Predicted Glucose %@ %@, is below Threshold of %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is predicted below threshold " = "is predicted below threshold ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: " = "Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ";

//* Bolus pop-up / Alert string. Make translations concise! */
". Climbing: " = ". Climbing: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: " = "Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: ";

/* Bolus pop-up / Alert string. Make translations concise! */
". Falling: " = ". Falling: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: " = "Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ";

/* Bolus pop-up / Alert string. Make translations concise! */
". Changing: " = ". Changing: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Minimum predicted Glucose is " = "Minimum predicted Glucose is ";

/* -------------------------------------------------------------------------------------------
DASH strings
Expand Down
39 changes: 31 additions & 8 deletions FreeAPS/Sources/Localizations/Main/sv.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -1336,18 +1336,41 @@ Enact a temp Basal or a temp target */
/* Hide pop-up */
"Hide" = "Göm";

/* Error string 1. Make translation very short! */
"Predicted Glucose, %@ %@, is below threshold (%@!)" = "Blodsockerprognos, %@ %@, är lägre än (%@!)";
/* Bolus pop-up / Alert string. Make translations concise! */
"Predicted Glucose, " = "Prognos, ";

/* Error string 2. Make translation very short! */
"Eventual Glucose > Target Glucose, but glucose is falling faster than expected. Expected: %@. Falling: %@" = "Blodsockerprognos > målvärde, med blodsockret sjunker snabbare än väntat. Förväntat: %@. Sjunker: %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is predicted below threshold " = "är under tröskelvärde ";

/* Error string 3. Make translation very short! */
"Minimum predicted Glucose is %@ %@" = "Lägsta blodsockerprognos är %@ %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is below Threshold of " = "är under tröskelvärde ";

/* Error string 4. Make translation very short! */
"Predicted Glucose %@ %@, is below Threshold of %@" = "Blodsockerprognos %@ %@, är under tröskelvärde %@";
/* Bolus pop-up / Alert string. Make translations concise! */
"is predicted below threshold " = "är under tröskelvärde ";

/* Bolus pop-up / Alert string. Make translations concise! */
"is predicted below threshold " = "är under tröskelvärde ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: " = "Prognos > Målvärde, men blodsocker stiger snabbare än väntat. Förväntat: ";

//* Bolus pop-up / Alert string. Make translations concise! */
". Climbing: " = ". Stiger: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: " = "Prognos > Målvärde, men blodsocker sjunker snabbare än väntat. Förväntat: ";

/* Bolus pop-up / Alert string. Make translations concise! */
". Falling: " = ". Sjunker: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: " = "Prognos > Målvärde, men blodsocker ändras snabbare än väntat. Förväntat: ";

/* Bolus pop-up / Alert string. Make translations concise! */
". Changing: " = ". Ändras: ";

/* Bolus pop-up / Alert string. Make translations concise! */
"Minimum predicted Glucose is " = "Lägsta prognos är ";
/* -------------------------------------------------------------------------------------------
DASH strings
*/
Expand Down
10 changes: 9 additions & 1 deletion FreeAPS/Sources/Models/Suggestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ struct Suggestion: JSON, Equatable {
let insulin: Insulin?
let current_target: Decimal?
let insulinForManualBolus: Decimal?
let manualBolusErrorString: String?
let manualBolusErrorString: Decimal?
let minDelta: Decimal?
let expectedDelta: Decimal?
let minGuardBG: Decimal?
let minPredBG: Decimal?
}

struct Predictions: JSON, Equatable {
Expand Down Expand Up @@ -65,6 +69,10 @@ extension Suggestion {
case current_target
case insulinForManualBolus
case manualBolusErrorString
case minDelta
case expectedDelta
case minGuardBG
case minPredBG
}
}

Expand Down
24 changes: 21 additions & 3 deletions FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ extension Bolus {
@Published var waitForSuggestion: Bool = false
@Published var manual: Bool = false
@Published var error: Bool = false
@Published var errorString: String = ""
@Published var errorString: Decimal = 0

@Published var evBG: Int = 0
@Published var insulin: Decimal = 0
@Published var target: Decimal = 0
@Published var isf: Decimal = 0
@Published var percentage: Decimal = 0
@Published var threshold: Decimal = 0
@Published var minGuardBG: Decimal = 0
@Published var minDelta: Decimal = 0
@Published var expectedDelta: Decimal = 0
@Published var minPredBG: Decimal = 0

@Published var units: GlucoseUnits = .mmolL

Expand All @@ -30,6 +35,8 @@ extension Bolus {
broadcaster.register(SuggestionObserver.self, observer: self)
units = settingsManager.settings.units
percentage = settingsManager.settings.insulinReqPercentage
threshold = units == .mmolL ? settingsManager.preferences.threshold_setting.asMmolL : settingsManager.preferences
.threshold_setting

if waitForSuggestionInitial {
apsManager.determineBasal()
Expand Down Expand Up @@ -92,6 +99,11 @@ extension Bolus {

// Manual Bolus recommendation screen after a carb entry (normally) yields a higher amount than the insulin reqiured amount computed for SMBs (auto boluses). Carbs combined with a manual bolus threfore now (test) uses the Eventual BG for glucose prediction, whereas the insulinReg for SMBs uses the minPredBG for glucose prediction (typically lower than Eventual BG).

var conversion: Decimal = 1.0
if self.units == .mmolL {
conversion = 0.0555
}

self.evBG = self.provider.suggestion?.eventualBG ?? 0
self.insulin = self.provider.suggestion?.insulinForManualBolus ?? 0
self.target = self.provider.suggestion?.current_target ?? 0
Expand All @@ -101,8 +113,14 @@ extension Bolus {
self.insulinRecommended = self.insulin * (self.settingsManager.settings.insulinReqPercentage / 100)
} else { self.insulinRecommended = self.insulin }

self.errorString = self.provider.suggestion?.manualBolusErrorString ?? ""
if self.errorString.count > 8 { self.error = true }
self.errorString = self.provider.suggestion?.manualBolusErrorString ?? 0
if self.errorString != 0 {
self.error = true
self.minGuardBG = (self.provider.suggestion?.minGuardBG ?? 0) * conversion
self.minDelta = (self.provider.suggestion?.minDelta ?? 0) * conversion
self.expectedDelta = (self.provider.suggestion?.expectedDelta ?? 0) * conversion
self.minPredBG = (self.provider.suggestion?.minPredBG ?? 0) * conversion
} else { self.error = false }

self.insulinRecommended = self.apsManager
.roundBolus(amount: max(self.insulinRecommended, 0))
Expand Down
134 changes: 104 additions & 30 deletions FreeAPS/Sources/Modules/Bolus/View/BolusRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extension Bolus {
let waitForSuggestion: Bool
let manualBolus: Bool
@StateObject var state = StateModel()

@State private var isAddInsulinAlertPresented = false
@State private var presentInfo = false
@State private var displayError = false
Expand All @@ -20,6 +21,12 @@ extension Bolus {
return formatter
}

private var fractionDigits: Int {
if state.units == .mmolL {
return 1
} else { return 0 }
}

var body: some View {
Form {
Section {
Expand Down Expand Up @@ -70,13 +77,11 @@ extension Bolus {
}
}
header: { Text("Bolus") }

Section {
Button { state.add() }
label: { Text("Enact bolus") }
.disabled(state.amount <= 0)
}

Section {
if waitForSuggestion {
Button { state.showModal(for: nil) }
Expand All @@ -87,25 +92,30 @@ extension Bolus {
.disabled(state.amount <= 0)
}
}
.alert(isPresented: $isAddInsulinAlertPresented) {
Alert(
title: Text("Are you sure?"),
message: Text(
"Add" + formatter
.string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") +
NSLocalizedString(" without bolusing", comment: "")
),
primaryButton: .destructive(
Text("Add"),
action: {
state.addWithoutBolus()
isAddInsulinAlertPresented = false
}
),
secondaryButton: .cancel()
)
}
}
}
.alert(isPresented: $isAddInsulinAlertPresented) {
let amount = formatter
.string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
return Alert(
title: Text("Are you sure?"),
message: Text("Add \(amount) without bolusing"),
primaryButton: .destructive(
Text("Add"),
action: { state.addWithoutBolus() }
),
secondaryButton: .cancel()
)
}
.alert(isPresented: $displayError) {
Alert(
title: Text("Warning!"),
message: Text("\n" + NSLocalizedString(state.errorString, comment: "") + NSLocalizedString(
message: Text("\n" + alertString() + NSLocalizedString(
"\n\nTap 'Add' to continue with selected amount.",
comment: "Alert text to confirm bolus amount to add"
)),
Expand All @@ -123,7 +133,6 @@ extension Bolus {
configureView {
state.waitForSuggestionInitial = waitForSuggestion
state.waitForSuggestion = waitForSuggestion
state.manual = manualBolus
}
}
.navigationTitle("Enact Bolus")
Expand All @@ -141,15 +150,13 @@ extension Bolus {
HStack {
Text("Eventual Glucose").foregroundColor(.secondary)
let evg = state.units == .mmolL ? Decimal(state.evBG).asMmolL : Decimal(state.evBG)
let fractionDigit = state.units == .mmolL ? 1 : 0
Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigit))))
Text(evg.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
Text(state.units.rawValue).foregroundColor(.secondary)
}
HStack {
Text("Target Glucose").foregroundColor(.secondary)
let target = state.units == .mmolL ? state.target.asMmolL : state.target
let fractionDigit = state.units == .mmolL ? 1 : 0
Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigit))))
Text(target.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))))
Text(state.units.rawValue).foregroundColor(.secondary)
}
HStack {
Expand Down Expand Up @@ -198,15 +205,15 @@ extension Bolus {
}
}
// Warning
VStack {
Divider()
if state.error, state.insulinRecommended > 0 {
Text("Warning!").font(.callout).foregroundColor(.orange).bold()
Text(NSLocalizedString(state.errorString, comment: "")).font(.caption)
if state.error, state.insulinRecommended > 0 {
VStack(spacing: 5) {
Divider()
}
}.padding(.horizontal, 10)
// Footer. Warning string .
Text("Warning!").font(.callout).bold().foregroundColor(.orange)
Text(alertString()).font(.footnote)
Divider()
}.padding(.horizontal, 10)
}
// Footer.
if !(state.error && state.insulinRecommended > 0) {
VStack {
Text(
Expand All @@ -227,10 +234,77 @@ extension Bolus {
// .fill(Color(.systemGray).gradient) // A more prominent pop-up, but harder to read
)
}

// Localize the Oref0 error/warning strings
private func alertString() -> String {
switch state.errorString {
case 1:
return NSLocalizedString(
"Predicted Glucose, ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) + state.minGuardBG
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
.rawValue + ", " +
NSLocalizedString(
"is predicted below threshold ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) + state
.threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + "!"
case 2:
return NSLocalizedString(
"Predicted Glucose, ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) + state.minGuardBG
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state.units
.rawValue + ", " +
NSLocalizedString(
"is below Threshold of ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) + state
.threshold.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
case 3:
return NSLocalizedString(
"Eventual Glucose > Target Glucose, but glucose is climbing slower than expected. Expected: ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) +
state.expectedDelta
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
NSLocalizedString(". Climbing: ", comment: "Bolus pop-up / Alert string. Make translatons concise!") + state
.minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
case 4:
return NSLocalizedString(
"Eventual Glucose > Target Glucose, but glucose is falling slower than expected. Expected: ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) +
state.expectedDelta
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
NSLocalizedString(". Falling: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
.minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
case 5:
return NSLocalizedString(
"Eventual Glucose > Target Glucose, but glucose is changing faster than expected. Expected: ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) +
state.expectedDelta
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) +
NSLocalizedString(". Changing: ", comment: "Bolus pop-up / Alert string. Make translations concise!") + state
.minDelta.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
case 6:
return NSLocalizedString(
"Minimum predicted Glucose is ",
comment: "Bolus pop-up / Alert string. Make translations concise!"
) + state
.minPredBG
.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits))) + " " + state
.units
.rawValue
default:
return "Ignore Warning..."
}
}
}
}

// fix iOS 15 bug
struct ActivityIndicator: UIViewRepresentable {
@Binding var isAnimating: Bool
let style: UIActivityIndicatorView.Style
Expand Down
Loading

0 comments on commit 3f8e09f

Please sign in to comment.