|
|
@@ -8,6 +8,18 @@ extension DataTable {
|
|
|
@State private var isRemoveCarbsAlertPresented = false
|
|
|
@State private var removeCarbsAlert: Alert?
|
|
|
|
|
|
+ private var glucoseFormatter: NumberFormatter {
|
|
|
+ let formatter = NumberFormatter()
|
|
|
+ formatter.numberStyle = .decimal
|
|
|
+ formatter.maximumFractionDigits = 0
|
|
|
+ if state.units == .mmolL {
|
|
|
+ formatter.minimumFractionDigits = 1
|
|
|
+ formatter.maximumFractionDigits = 1
|
|
|
+ }
|
|
|
+ formatter.roundingMode = .halfUp
|
|
|
+ return formatter
|
|
|
+ }
|
|
|
+
|
|
|
private var dateFormatter: DateFormatter {
|
|
|
let formatter = DateFormatter()
|
|
|
formatter.timeStyle = .short
|
|
|
@@ -15,54 +27,101 @@ extension DataTable {
|
|
|
}
|
|
|
|
|
|
var body: some View {
|
|
|
- Form {
|
|
|
- list
|
|
|
+ VStack {
|
|
|
+ Picker("Mode", selection: $state.mode) {
|
|
|
+ ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
|
|
|
+ Text(item.name).tag(index)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .pickerStyle(SegmentedPickerStyle())
|
|
|
+ .padding(.horizontal)
|
|
|
+
|
|
|
+ Form {
|
|
|
+ switch state.mode {
|
|
|
+ case .treatments: treatmentsList
|
|
|
+ case .glucose: glucoseList
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
.onAppear(perform: configureView)
|
|
|
.navigationTitle("History")
|
|
|
.navigationBarTitleDisplayMode(.automatic)
|
|
|
.navigationBarItems(
|
|
|
- leading: Button("Close", action: state.hideModal)
|
|
|
+ leading: Button("Close", action: state.hideModal),
|
|
|
+ trailing: state.mode == .glucose ? EditButton().asAny() : EmptyView().asAny()
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- private var list: some View {
|
|
|
+ private var treatmentsList: some View {
|
|
|
List {
|
|
|
- ForEach(state.items.indexed(), id: \.1.id) { _, item in
|
|
|
- HStack {
|
|
|
- Image(systemName: "circle.fill").foregroundColor(item.color)
|
|
|
- Text(dateFormatter.string(from: item.date))
|
|
|
- .moveDisabled(true)
|
|
|
- Text(item.type.name)
|
|
|
- Text(item.amountText).foregroundColor(.secondary)
|
|
|
- if let duration = item.durationText {
|
|
|
- Text(duration).foregroundColor(.secondary)
|
|
|
- }
|
|
|
+ ForEach(state.treatments) { item in
|
|
|
+ treatmentView(item)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if item.type == .carbs {
|
|
|
- Spacer()
|
|
|
- Image(systemName: "xmark.circle").foregroundColor(.secondary)
|
|
|
- .contentShape(Rectangle())
|
|
|
- .padding(.vertical)
|
|
|
- .onTapGesture {
|
|
|
- removeCarbsAlert = Alert(
|
|
|
- title: Text("Delete carbs?"),
|
|
|
- message: Text(item.amountText),
|
|
|
- primaryButton: .destructive(
|
|
|
- Text("Delete"),
|
|
|
- action: { state.deleteCarbs(at: item.date) }
|
|
|
- ),
|
|
|
- secondaryButton: .cancel()
|
|
|
- )
|
|
|
- isRemoveCarbsAlertPresented = true
|
|
|
- }
|
|
|
- .alert(isPresented: $isRemoveCarbsAlertPresented) {
|
|
|
- removeCarbsAlert!
|
|
|
- }
|
|
|
+ private var glucoseList: some View {
|
|
|
+ List {
|
|
|
+ ForEach(state.glucose) { item in
|
|
|
+ gluciseView(item)
|
|
|
+ }.onDelete(perform: deleteGlucose)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
|
|
|
+ HStack {
|
|
|
+ Image(systemName: "circle.fill").foregroundColor(item.color)
|
|
|
+ Text(dateFormatter.string(from: item.date))
|
|
|
+ .moveDisabled(true)
|
|
|
+ Text(item.type.name)
|
|
|
+ Text(item.amountText).foregroundColor(.secondary)
|
|
|
+ if let duration = item.durationText {
|
|
|
+ Text(duration).foregroundColor(.secondary)
|
|
|
+ }
|
|
|
+
|
|
|
+ if item.type == .carbs {
|
|
|
+ Spacer()
|
|
|
+ Image(systemName: "xmark.circle").foregroundColor(.secondary)
|
|
|
+ .contentShape(Rectangle())
|
|
|
+ .padding(.vertical)
|
|
|
+ .onTapGesture {
|
|
|
+ removeCarbsAlert = Alert(
|
|
|
+ title: Text("Delete carbs?"),
|
|
|
+ message: Text(item.amountText),
|
|
|
+ primaryButton: .destructive(
|
|
|
+ Text("Delete"),
|
|
|
+ action: { state.deleteCarbs(at: item.date) }
|
|
|
+ ),
|
|
|
+ secondaryButton: .cancel()
|
|
|
+ )
|
|
|
+ isRemoveCarbsAlertPresented = true
|
|
|
+ }
|
|
|
+ .alert(isPresented: $isRemoveCarbsAlertPresented) {
|
|
|
+ removeCarbsAlert!
|
|
|
}
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @ViewBuilder private func gluciseView(_ item: Glucose) -> some View {
|
|
|
+ VStack(alignment: .leading, spacing: 4) {
|
|
|
+ HStack {
|
|
|
+ Text(dateFormatter.string(from: item.glucose.dateString))
|
|
|
+ Spacer()
|
|
|
+ Text(item.glucose.glucose.map {
|
|
|
+ glucoseFormatter.string(from: Double(
|
|
|
+ state.units == .mmolL ? $0.asMmolL : Decimal($0)
|
|
|
+ ) as NSNumber)!
|
|
|
+ } ?? "--")
|
|
|
+ Text(state.units.rawValue)
|
|
|
+ Text(item.glucose.direction?.symbol ?? "--")
|
|
|
+ }
|
|
|
+ Text("ID: " + item.glucose.id).font(.caption2).foregroundColor(.secondary)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private func deleteGlucose(at offsets: IndexSet) {
|
|
|
+ state.deleteGlucose(at: offsets[offsets.startIndex])
|
|
|
+ }
|
|
|
}
|
|
|
}
|