| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- 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 showExternalInsulin = false
- @State private var isAmountUnconfirmed = true
- @Environment(\.colorScheme) var colorScheme
- private var insulinFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- 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
- 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("History")
- .navigationBarTitleDisplayMode(.automatic)
- .navigationBarItems(
- leading: Button("Close", action: state.hideModal),
- trailing: state.mode == .glucose ? EditButton().asAny() : EmptyView().asAny()
- )
- .sheet(isPresented: $showExternalInsulin, onDismiss: {
- if isAmountUnconfirmed {
- state.externalInsulinAmount = 0
- state.externalInsulinDate = Date()
- }
- }) {
- 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))
- )
- }
- }
- private var treatmentsList: some View {
- List {
- HStack {
- Spacer()
- Button(action: { showExternalInsulin = true
- state.externalInsulinDate = Date() }, label: {
- HStack {
- Text("Add")
- .foregroundColor(Color.secondary)
- .font(.caption)
- Image(systemName: "syringe")
- .foregroundColor(Color.accentColor)
- }.frame(maxWidth: .infinity, alignment: .trailing)
- }).buttonStyle(.borderless)
- }
- if !state.treatments.isEmpty {
- ForEach(state.treatments) { item in
- treatmentView(item)
- }
- } else {
- HStack {
- Text("No data.")
- }
- }
- }
- }
- private var glucoseList: some View {
- List {
- Button { newGlucose = true }
- label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
- .padding(.trailing, 20)
- ForEach(state.glucose) { item in
- glucoseView(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 {
- 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!
- }
- }
- }
- }
- var addExternalInsulinView: some View {
- NavigationView {
- VStack {
- Form {
- Section {
- HStack {
- Text("Amount")
- Spacer()
- DecimalTextField(
- "0",
- value: $state.externalInsulinAmount,
- formatter: insulinFormatter,
- autofocus: true,
- cleanInput: true
- )
- Text("U").foregroundColor(.secondary)
- }
- }
- Section {
- DatePicker("Date", selection: $state.externalInsulinDate, in: ...Date())
- }
- let amountWarningCondition = (state.externalInsulinAmount > state.maxBolus) &&
- (state.externalInsulinAmount <= state.maxBolus * 3)
- Section {
- HStack {
- Button {
- state.addExternalInsulin()
- isAmountUnconfirmed = false
- showExternalInsulin = false
- }
- label: {
- Text("Log external insulin")
- }
- .foregroundColor(amountWarningCondition ? Color.white : Color.accentColor)
- .frame(maxWidth: .infinity, alignment: .center)
- .disabled(
- state.externalInsulinAmount <= 0 || state.externalInsulinAmount > state
- .maxBolus * 3
- )
- }
- }
- header: {
- if amountWarningCondition
- {
- Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
- }
- }
- .listRowBackground(
- amountWarningCondition ? Color
- .red : colorScheme == .dark ? Color(UIColor.secondarySystemBackground) : Color.white
- )
- }
- }
- .onAppear(perform: configureView)
- .navigationTitle("External Insulin")
- .navigationBarTitleDisplayMode(.inline)
- .navigationBarItems(leading: Button("Close", action: { showExternalInsulin = false
- state.externalInsulinAmount = 0 }))
- }
- }
- @ViewBuilder private func glucoseView(_ 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])
- }
- }
- }
|