Skip to content

Commit

Permalink
Use sheet instead of pop-up for manual glucose (Artificial-Pancreas#272)
Browse files Browse the repository at this point in the history
* Add sheet (new View) for adding manual glucose 
* Use glucose meter limits for manual glucose (0.8 - 40 mmol/l)
  • Loading branch information
dnzxy authored and mountrcg committed Oct 27, 2023
1 parent 2d4c40d commit d2dad5c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
8 changes: 3 additions & 5 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

var units: GlucoseUnits = .mmolL

Expand Down Expand Up @@ -149,7 +149,6 @@ extension DataTable {
func deleteGlucose(at index: Int) {
let id = glucose[index].id
provider.deleteGlucose(id: id)
// CoreData
let fetchRequest: NSFetchRequest<NSFetchRequestResult>
fetchRequest = NSFetchRequest(entityName: "Readings")
fetchRequest.predicate = NSPredicate(format: "id == %@", id)
Expand All @@ -166,14 +165,14 @@ extension DataTable {
)
}
} catch { /* To do: handle any thrown errors. */ }
// Manual Glucose
// Deletes Manual Glucose
if (glucose[index].glucose.type ?? "") == GlucoseType.manual.rawValue {
provider.deleteManualGlucose(date: glucose[index].glucose.dateString)
}
}

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

Expand All @@ -193,7 +192,6 @@ extension DataTable {
// Save to Health
var saveToHealth = [BloodGlucose]()
saveToHealth.append(saveToJSON)
healthKitManager.saveIfNeeded(bloodGlucose: saveToHealth)
}
}
}
Expand Down
109 changes: 56 additions & 53 deletions FreeAPS/Sources/Modules/DataTable/View/DataTableRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ 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 isLayered = false
@FocusState private var isFocused: Bool
@State private var showManualGlucose: Bool = false
@State private var isAmountUnconfirmed: Bool = true

@Environment(\.colorScheme) var colorScheme

Expand Down Expand Up @@ -52,12 +51,11 @@ extension DataTable {
}
}
.onAppear(perform: configureView)
.navigationTitle(isLayered ? "" : "History")
.blur(radius: isLayered ? 4.0 : 0)
.navigationTitle("History")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(leading: Button(isLayered ? "" : "Close", action: state.hideModal))
.popup(isPresented: newGlucose, alignment: .center, direction: .top) {
addGlucose
.navigationBarItems(leading: Button("Close", action: state.hideModal))
.sheet(isPresented: $showManualGlucose) {
addGlucoseView
}
}

Expand All @@ -75,61 +73,66 @@ extension DataTable {
Text("Time").foregroundStyle(.secondary)
Spacer()
Text(state.units.rawValue).foregroundStyle(.secondary)
Button {
newGlucose = true
isFocused = true
isLayered.toggle()
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, isManual: item.glucose)
}
.onDelete(perform: deleteGlucose)
} else {
HStack {
Text(NSLocalizedString("No data.", comment: "No data text when no entries in history list"))
}
label: { Image(systemName: "plus.circle.fill").foregroundStyle(.secondary) }
.buttonStyle(.borderless)
}
ForEach(state.glucose) { item in
glucoseView(item, isManual: item.glucose)
}.onDelete(perform: deleteGlucose)
}
}

private var addGlucose: some View {
VStack {
Form {
Section {
HStack {
Text("Glucose").font(.custom("popup", fixedSize: 18))
DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
.focused($isFocused).font(.custom("glucose", fixedSize: 22))
Text(state.units.rawValue).foregroundStyle(.secondary)
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)
}
}
}
header: {
Text("Blood Glucose Test").foregroundColor(.secondary).font(.custom("popupHeader", fixedSize: 12))
.padding(.top)
}
HStack {
Button {
newGlucose = false
isLayered = false
}
label: { Text("Cancel").foregroundColor(.red) }
.frame(maxWidth: .infinity, alignment: .leading)
Spacer()
Button {
state.addManualGlucose()
newGlucose = false
isLayered = false

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

Button {
state.addManualGlucose()
isAmountUnconfirmed = false
showManualGlucose = false
}
label: { Text("Save") }
.frame(maxWidth: .infinity, alignment: .center)
.disabled(state.manualGlucose < limitLow || state.manualGlucose > limitHigh)
}
}
label: { Text("Save") }
.frame(maxWidth: .infinity, alignment: .trailing)
.disabled(state.manualGlcuose <= 0)
}
.buttonStyle(BorderlessButtonStyle())
.font(.custom("popupButtons", fixedSize: 16))
}
.onAppear(perform: configureView)
.navigationTitle("Add Glucose")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(leading: Button("Close", action: { showManualGlucose = false }))
}
.frame(minHeight: 220, maxHeight: 260).cornerRadius(20)
.background(
RoundedRectangle(cornerRadius: 20, style: .continuous)
.fill(Color(.tertiarySystemBackground))
).shadow(radius: 40)
}

@ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
Expand Down

0 comments on commit d2dad5c

Please sign in to comment.