Skip to content

Commit

Permalink
Rework glucose dialog
Browse files Browse the repository at this point in the history
This ports Artificial-Pancreas/iAPS#272 into Open-iAPS.

This is part of #47

Co-Authored-By: Deniz Cengiz <48965855+dnzxy@users.noreply.github.com>
  • Loading branch information
BrianWieder and dnzxy committed May 6, 2024
1 parent c010fa6 commit dddcd87
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
4 changes: 2 additions & 2 deletions FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension DataTable {
@Published var mode: Mode = .treatments
@Published var treatments: [Treatment] = []
@Published var glucose: [Glucose] = []
@Published var manualGlcuose: Decimal = 0
@Published var manualGlucose: Decimal = 0
@Published var maxBolus: Decimal = 0
@Published var externalInsulinAmount: Decimal = 0
@Published var externalInsulinDate = Date()
Expand Down Expand Up @@ -182,7 +182,7 @@ extension DataTable {
}

func addManualGlucose() {
let glucose = units == .mmolL ? manualGlcuose.asMgdL : manualGlcuose
let glucose = units == .mmolL ? manualGlucose.asMgdL : manualGlucose
let now = Date()
let id = UUID().uuidString

Expand Down
102 changes: 67 additions & 35 deletions FreeAPS/Sources/Modules/DataTable/View/DataTableRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension DataTable {
@State private var removeCarbsAlert: Alert?
@State private var isRemoveInsulinAlertPresented = false
@State private var removeInsulinAlert: Alert?
@State private var newGlucose = false
@State private var showManualGlucose = false
@State private var showExternalInsulin = false
@State private var isAmountUnconfirmed = true

Expand Down Expand Up @@ -74,34 +74,8 @@ extension DataTable {
}) {
addExternalInsulinView
}
.popup(isPresented: newGlucose, alignment: .top, direction: .bottom) {
VStack(spacing: 20) {
HStack {
Text("New Glucose")
DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
Text(state.units.rawValue)
}.padding(.horizontal, 20)
HStack {
let limitLow: Decimal = state.units == .mmolL ? 2.2 : 40
let limitHigh: Decimal = state.units == .mmolL ? 21 : 380
Button { newGlucose = false }
label: { Text("Cancel") }.frame(maxWidth: .infinity, alignment: .leading)

Button {
state.addManualGlucose()
newGlucose = false
}
label: { Text("Save") }
.frame(maxWidth: .infinity, alignment: .trailing)
// .disabled(state.manualGlcuose < limitLow || state.manualGlcuose > limitHigh)

}.padding(20)
}
.frame(maxHeight: 140)
.background(
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill(Color(colorScheme == .dark ? UIColor.systemGray2 : UIColor.systemGray6))
)
.sheet(isPresented: $showManualGlucose) {
addGlucoseView
}
}

Expand Down Expand Up @@ -137,13 +111,71 @@ extension DataTable {

private var glucoseList: some View {
List {
Button { newGlucose = true }
label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.trailing, 20)
HStack {
Text("Time").foregroundStyle(.secondary)
Spacer()
Text(state.units.rawValue).foregroundStyle(.secondary)
Button(
action: {
showManualGlucose = true
state.manualGlucose = 0
},
label: { Image(systemName: "plus.circle.fill").foregroundStyle(.secondary)
}
).buttonStyle(.borderless)
}
if !state.glucose.isEmpty {
ForEach(state.glucose) { item in
glucoseView(item)
}
.onDelete(perform: deleteGlucose)
} else {
HStack {
Text(NSLocalizedString("No data.", comment: "No data text when no entries in history list"))
}
}
}
}

private var addGlucoseView: some View {
NavigationView {
VStack {
Form {
Section {
HStack {
Text("New Glucose")
DecimalTextField(
" ... ",
value: $state.manualGlucose,
formatter: glucoseFormatter,
autofocus: true,
cleanInput: true
)
Text(state.units.rawValue).foregroundStyle(.secondary)
}
}

Section {
HStack {
let limitLow: Decimal = state.units == .mmolL ? 0.8 : 40
let limitHigh: Decimal = state.units == .mmolL ? 14 : 720

ForEach(state.glucose) { item in
glucoseView(item)
}.onDelete(perform: deleteGlucose)
Button {
state.addManualGlucose()
isAmountUnconfirmed = false
showManualGlucose = false
}
label: { Text("Save") }
.frame(maxWidth: .infinity, alignment: .center)
.disabled(state.manualGlucose < limitLow || state.manualGlucose > limitHigh)
}
}
}
}
.onAppear(perform: configureView)
.navigationTitle("Add Glucose")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(leading: Button("Close", action: { showManualGlucose = false }))
}
}

Expand Down

0 comments on commit dddcd87

Please sign in to comment.