| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import CoreData
- import SwiftUI
- import Swinject
- extension DataTable {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State private var isRemoveCarbsAlertPresented = false
- @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
- @Environment(\.colorScheme) var colorScheme
- private var glucoseFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 0
- if state.units == .mmolL {
- formatter.maximumFractionDigits = 1
- formatter.roundingMode = .ceiling
- }
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let formatter = DateFormatter()
- formatter.timeStyle = .short
- return formatter
- }
- var body: some View {
- 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(isLayered ? "" : "History")
- .blur(radius: isLayered ? 3.0 : 0)
- .navigationBarTitleDisplayMode(.automatic)
- .navigationBarItems(leading: Button(isLayered ? "" : "Close", action: state.hideModal))
- .popup(isPresented: newGlucose, alignment: .center, direction: .top) {
- addGlucose
- }
- }
- private var treatmentsList: some View {
- List {
- ForEach(state.treatments) { item in
- treatmentView(item)
- }
- }
- }
- private var glucoseList: some View {
- List {
- Button {
- newGlucose = true
- isFocused = true
- isLayered.toggle()
- }
- label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
- .padding(.trailing, 20)
- 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)
- }
- }
- 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
- }
- label: { Text("Save") }
- .frame(maxWidth: .infinity, alignment: .trailing)
- .disabled(state.manualGlcuose <= 0)
- }
- .buttonStyle(BorderlessButtonStyle())
- .font(.custom("popupButtons", fixedSize: 16))
- }
- }
- .frame(maxHeight: 220)
- .background(
- RoundedRectangle(cornerRadius: 8, style: .continuous)
- .fill(Color(.tertiarySystemBackground))
- ).border(.gray).shadow(radius: 40)
- }
- @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.isSMB ?? false) ? "SMB" : item.type.name)
- Text(item.amountText).foregroundColor(.secondary)
- if let duration = item.durationText {
- Text(duration).foregroundColor(.secondary)
- }
- if item.type == .carbs {
- if item.note != "" {
- Spacer()
- Text(item.note ?? "").foregroundColor(.brown)
- }
- 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(item) }
- ),
- secondaryButton: .cancel()
- )
- isRemoveCarbsAlertPresented = true
- }
- .alert(isPresented: $isRemoveCarbsAlertPresented) {
- removeCarbsAlert!
- }
- }
- if item.type == .fpus {
- Spacer()
- Image(systemName: "xmark.circle").foregroundColor(.secondary)
- .contentShape(Rectangle())
- .padding(.vertical)
- .onTapGesture {
- removeCarbsAlert = Alert(
- title: Text("Delete carb equivalents?"),
- message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
- primaryButton: .destructive(
- Text("Delete"),
- action: { state.deleteCarbs(item) }
- ),
- secondaryButton: .cancel()
- )
- isRemoveCarbsAlertPresented = true
- }
- .alert(isPresented: $isRemoveCarbsAlertPresented) {
- removeCarbsAlert!
- }
- }
- if item.type == .bolus {
- Spacer()
- Image(systemName: "xmark.circle").foregroundColor(.secondary)
- .contentShape(Rectangle())
- .padding(.vertical)
- .onTapGesture {
- removeInsulinAlert = Alert(
- title: Text("Delete insulin?"),
- message: Text(item.amountText),
- primaryButton: .destructive(
- Text("Delete"),
- action: { state.deleteInsulin(item) }
- ),
- secondaryButton: .cancel()
- )
- isRemoveInsulinAlertPresented = true
- }
- .alert(isPresented: $isRemoveInsulinAlertPresented) {
- removeInsulinAlert!
- }
- }
- }
- }
- @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> 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)
- if isManual.type == GlucoseType.manual.rawValue {
- Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
- } else {
- Text(item.glucose.direction?.symbol ?? "--")
- }
- }
- }
- }
- private func deleteGlucose(at offsets: IndexSet) {
- state.deleteGlucose(at: offsets[offsets.startIndex])
- }
- }
- }
|