-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autoISF - display/edit settings in the preferred units (#1023)
- Loading branch information
Showing
5 changed files
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import SwiftUI | ||
|
||
struct BGTextField: View { | ||
let placeholder: String | ||
@Binding var mgdlValue: Decimal | ||
@Binding var units: GlucoseUnits | ||
var isDisabled: Bool | ||
|
||
init( | ||
_ placeholder: String, | ||
mgdlValue: Binding<Decimal>, | ||
units: Binding<GlucoseUnits>, | ||
isDisabled: Bool | ||
) { | ||
self.placeholder = placeholder | ||
_mgdlValue = mgdlValue | ||
_units = units | ||
self.isDisabled = isDisabled | ||
} | ||
|
||
private var mmolLFormatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
formatter.minimumFractionDigits = 1 | ||
formatter.maximumFractionDigits = 1 | ||
return formatter | ||
} | ||
|
||
private var mgdLFormatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
formatter.maximumFractionDigits = 0 | ||
return formatter | ||
} | ||
|
||
private var displayValue: Binding<Decimal> { | ||
Binding( | ||
get: { units == .mmolL ? mgdlValue.asMmolL : mgdlValue }, | ||
set: { newValue in mgdlValue = units == .mmolL ? newValue.asMgdL : newValue } | ||
) | ||
} | ||
|
||
var body: some View { | ||
HStack { | ||
if units == .mmolL { | ||
DecimalTextField(placeholder, value: displayValue, formatter: mmolLFormatter) | ||
.disabled(isDisabled) | ||
} else { | ||
DecimalTextField(placeholder, value: displayValue, formatter: mgdLFormatter) | ||
.disabled(isDisabled) | ||
} | ||
|
||
Text(units == .mmolL ? LocalizedStringKey("mmol/L") : LocalizedStringKey("mg/dL")) | ||
.foregroundStyle(.secondary) | ||
} | ||
} | ||
} |