| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- import SwiftDate
- import SwiftUI
- import Swinject
- extension Home {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State var isStatusPopupPresented = false
- private var numberFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var targetFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 1
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let dateFormatter = DateFormatter()
- dateFormatter.timeStyle = .short
- return dateFormatter
- }
- var header: some View {
- HStack(alignment: .bottom) {
- Spacer()
- VStack(alignment: .leading, spacing: 12) {
- HStack {
- Text("IOB").font(.caption2).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" U", comment: "Insulin unit")
- )
- .font(.system(size: 12, weight: .bold))
- }
- HStack {
- Text("COB").font(.caption2).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" g", comment: "gram of carbs")
- )
- .font(.system(size: 12, weight: .bold))
- }
- }
- Spacer()
- CurrentGlucoseView(
- recentGlucose: $state.recentGlucose,
- delta: $state.glucoseDelta,
- units: $state.units
- )
- .onTapGesture {
- state.openCGM()
- }
- Spacer()
- PumpView(
- reservoir: $state.reservoir,
- battery: $state.battery,
- name: $state.pumpName,
- expiresAtDate: $state.pumpExpiresAtDate,
- timerDate: $state.timerDate
- )
- .onTapGesture {
- if state.pumpDisplayState != nil {
- state.setupPump = true
- }
- }
- .sheet(isPresented: $state.setupPump) {
- if let pumpManager = state.provider.apsManager.pumpManager {
- PumpConfig.PumpSettingsView(pumpManager: pumpManager, completionDelegate: state)
- }
- }
- Spacer()
- LoopView(
- suggestion: $state.suggestion,
- enactedSuggestion: $state.enactedSuggestion,
- closedLoop: $state.closedLoop,
- timerDate: $state.timerDate,
- isLooping: $state.isLooping,
- lastLoopDate: $state.lastLoopDate
- ).onTapGesture {
- isStatusPopupPresented = true
- }.onLongPressGesture {
- state.runLoop()
- }
- Spacer()
- }.frame(maxWidth: .infinity)
- }
- var infoPanal: some View {
- HStack(alignment: .center) {
- if state.pumpSuspended {
- Text("Pump suspended")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGray)
- .padding(.leading, 8)
- } else if let tempRate = state.tempRate {
- Text(
- (numberFormatter.string(from: tempRate as NSNumber) ?? "0") +
- NSLocalizedString(" U/hr", comment: "Unit per hour with space")
- )
- .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
- .padding(.leading, 8)
- }
- if let tempTarget = state.tempTarget {
- Text(tempTarget.displayName).font(.caption).foregroundColor(.secondary)
- if state.units == .mmolL {
- Text(
- targetFormatter
- .string(from: (tempTarget.targetBottom?.asMmolL ?? 0) as NSNumber)!
- )
- .font(.caption)
- .foregroundColor(.secondary)
- if tempTarget.targetBottom != tempTarget.targetTop {
- Text("-").font(.caption)
- .foregroundColor(.secondary)
- Text(
- targetFormatter
- .string(from: (tempTarget.targetTop?.asMmolL ?? 0) as NSNumber)! +
- " \(state.units.rawValue)"
- )
- .font(.caption)
- .foregroundColor(.secondary)
- } else {
- Text(state.units.rawValue).font(.caption)
- .foregroundColor(.secondary)
- }
- } else {
- Text(targetFormatter.string(from: (tempTarget.targetBottom ?? 0) as NSNumber)!)
- .font(.caption)
- .foregroundColor(.secondary)
- if tempTarget.targetBottom != tempTarget.targetTop {
- Text("-").font(.caption)
- .foregroundColor(.secondary)
- Text(
- targetFormatter
- .string(from: (tempTarget.targetTop ?? 0) as NSNumber)! + " \(state.units.rawValue)"
- )
- .font(.caption)
- .foregroundColor(.secondary)
- } else {
- Text(state.units.rawValue).font(.caption)
- .foregroundColor(.secondary)
- }
- }
- }
- Spacer()
- if let progress = state.bolusProgress {
- Text("Bolusing")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
- ProgressView(value: Double(progress))
- .progressViewStyle(BolusProgressViewStyle())
- .padding(.trailing, 8)
- .onTapGesture {
- state.cancelBolus()
- }
- }
- }
- .frame(maxWidth: .infinity, maxHeight: 30)
- }
- var legendPanal: some View {
- HStack(alignment: .center) {
- Group {
- Circle().fill(Color.loopGreen).frame(width: 8, height: 8)
- Text("BG")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.loopGreen)
- }
- Group {
- Circle().fill(Color.insulin).frame(width: 8, height: 8)
- .padding(.leading, 8)
- Text("IOB")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.insulin)
- }
- Group {
- Circle().fill(Color.zt).frame(width: 8, height: 8)
- .padding(.leading, 8)
- Text("ZT")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.zt)
- }
- Group {
- Circle().fill(Color.loopYellow).frame(width: 8, height: 8)
- .padding(.leading, 8)
- Text("COB")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.loopYellow)
- }
- Group {
- Circle().fill(Color.uam).frame(width: 8, height: 8)
- .padding(.leading, 8)
- Text("UAM")
- .font(.system(size: 12, weight: .bold)).foregroundColor(.uam)
- }
- if let eventualBG = state.eventualBG {
- Text(
- "⇢ " + numberFormatter.string(
- from: (state.units == .mmolL ? eventualBG.asMmolL : Decimal(eventualBG)) as NSNumber
- )!
- )
- .font(.system(size: 12, weight: .bold)).foregroundColor(.secondary)
- }
- }
- .frame(maxWidth: .infinity, maxHeight: 30)
- }
- var body: some View {
- GeometryReader { geo in
- VStack(spacing: 0) {
- header
- .frame(maxHeight: 70)
- .padding(.top, geo.safeAreaInsets.top)
- .background(Color.gray.opacity(0.2))
- infoPanal
- MainChartView(
- glucose: $state.glucose,
- suggestion: $state.suggestion,
- tempBasals: $state.tempBasals,
- boluses: $state.boluses,
- suspensions: $state.suspensions,
- hours: .constant(state.filteredHours),
- maxBasal: $state.maxBasal,
- autotunedBasalProfile: $state.autotunedBasalProfile,
- basalProfile: $state.basalProfile,
- tempTargets: $state.tempTargets,
- carbs: $state.carbs,
- timerDate: $state.timerDate,
- units: $state.units
- )
- .padding(.bottom)
- .modal(for: .dataTable, from: self)
- legendPanal
- ZStack {
- Rectangle().fill(Color.gray.opacity(0.2)).frame(height: 50 + geo.safeAreaInsets.bottom)
- HStack {
- Button { state.showModal(for: .addCarbs) }
- label: {
- ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
- Image("carbs")
- .renderingMode(.template)
- .resizable()
- .frame(width: 24, height: 24)
- .foregroundColor(.loopGreen)
- .padding(8)
- if let carbsReq = state.carbsRequired {
- Text(numberFormatter.string(from: carbsReq as NSNumber)!)
- .font(.caption)
- .foregroundColor(.white)
- .padding(4)
- .background(Capsule().fill(Color.red))
- }
- }
- }
- Spacer()
- Button { state.showModal(for: .addTempTarget) }
- label: {
- Image("target")
- .renderingMode(.template)
- .resizable()
- .frame(width: 24, height: 24)
- .padding(8)
- }.foregroundColor(.loopYellow)
- Spacer()
- Button { state.showModal(for: .bolus(waitForSuggestion: false)) }
- label: {
- Image("bolus")
- .renderingMode(.template)
- .resizable()
- .frame(width: 24, height: 24)
- .padding(8)
- }.foregroundColor(.insulin)
- Spacer()
- if state.allowManualTemp {
- Button { state.showModal(for: .manualTempBasal) }
- label: {
- Image("bolus1")
- .renderingMode(.template)
- .resizable()
- .frame(width: 24, height: 24)
- .padding(8)
- }.foregroundColor(.insulin)
- Spacer()
- }
- Button { state.showModal(for: .settings) }
- label: {
- Image("settings1")
- .renderingMode(.template)
- .resizable()
- .frame(width: 24, height: 24)
- .padding(8)
- }.foregroundColor(.loopGray)
- }
- .padding(.horizontal, 24)
- .padding(.bottom, geo.safeAreaInsets.bottom)
- }
- }
- .edgesIgnoringSafeArea(.vertical)
- }
- .onAppear(perform: configureView)
- .navigationTitle("Home")
- .navigationBarHidden(true)
- .ignoresSafeArea(.keyboard)
- .popup(isPresented: isStatusPopupPresented, alignment: .top, direction: .top) {
- VStack(alignment: .leading) {
- Text(state.statusTitle).foregroundColor(.white)
- .padding(.bottom, 4)
- Text(state.suggestion?.reason ?? "No sugestion found").font(.caption).foregroundColor(.white)
- if let errorMessage = state.errorMessage, let date = state.errorDate {
- Text("Error at \(dateFormatter.string(from: date))").foregroundColor(.white)
- .padding(.bottom, 4)
- .padding(.top, 8)
- Text(errorMessage).font(.caption).foregroundColor(.white)
- }
- }
- .padding()
- .background(
- RoundedRectangle(cornerRadius: 8, style: .continuous)
- .fill(Color(UIColor.darkGray))
- )
- .onTapGesture {
- isStatusPopupPresented = false
- }
- .gesture(
- DragGesture(minimumDistance: 10, coordinateSpace: .local)
- .onEnded { value in
- if value.translation.height < 0 {
- isStatusPopupPresented = false
- }
- }
- )
- }
- }
- }
- }
|