| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import LoopKitUI
- import SwiftUI
- import Swinject
- extension CGM {
- struct RootView: BaseView {
- let resolver: Resolver
- let displayClose: Bool
- @StateObject var state = StateModel()
- @State private var setupCGM = false
- @State private var shouldDisplayHint: Bool = false
- @State var hintDetent = PresentationDetent.large
- @State var selectedVerboseHint: AnyView?
- @State var hintLabel: String?
- @State private var decimalPlaceholder: Decimal = 0.0
- @State private var booleanPlaceholder: Bool = false
- @Environment(\.colorScheme) var colorScheme
- var color: LinearGradient {
- colorScheme == .dark ? LinearGradient(
- gradient: Gradient(colors: [
- Color.bgDarkBlue,
- Color.bgDarkerDarkBlue
- ]),
- startPoint: .top,
- endPoint: .bottom
- )
- :
- LinearGradient(
- gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
- startPoint: .top,
- endPoint: .bottom
- )
- }
- var body: some View {
- NavigationView {
- Form {
- Section(
- header: Text("CGM Integration to Trio"),
- content: {
- VStack {
- Picker("Type", selection: $state.cgmCurrent) {
- ForEach(state.listOfCGM) { type in
- VStack(alignment: .leading) {
- Text(type.displayName)
- Text(type.subtitle).font(.caption).foregroundColor(.secondary)
- }.tag(type)
- }
- }.padding(.top)
- HStack(alignment: .top) {
- Text(
- "Select your CGM"
- )
- .font(.footnote)
- .foregroundColor(.secondary)
- .lineLimit(nil)
- Spacer()
- Button(
- action: {
- hintLabel = "Available CGM Types for Trio"
- selectedVerboseHint =
- AnyView(
- Text(
- "• Dexcom G5 \n• Dexcom G6/ONE \n• Dexcom G7/ONE+ \n• Dexcom Share \n• Freestyle Libre \n• Freestyle Libre Demo \n• Glucose Simulator \n• Medtronic Enlite \n• Nightscout \n• xDrip4iOS"
- )
- )
- shouldDisplayHint.toggle()
- },
- label: {
- HStack {
- Image(systemName: "questionmark.circle")
- }
- }
- ).buttonStyle(BorderlessButtonStyle())
- }.padding(.top)
- }.padding(.bottom)
- if let link = state.cgmCurrent.type.externalLink {
- Button {
- UIApplication.shared.open(link, options: [:], completionHandler: nil)
- } label: {
- HStack {
- Text("About this source")
- Spacer()
- Image(systemName: "chevron.right")
- }
- }
- .frame(maxWidth: .infinity, alignment: .leading)
- }
- if state.cgmCurrent.type == .plugin {
- Button {
- setupCGM.toggle()
- } label: {
- HStack {
- Text("CGM Configuration")
- Spacer()
- Image(systemName: "chevron.right")
- }
- }
- .frame(maxWidth: .infinity, alignment: .leading)
- }
- }
- ).listRowBackground(Color.chart)
- if let appURL = state.urlOfApp() {
- Section {
- Button {
- UIApplication.shared.open(appURL, options: [:]) { success in
- if !success {
- self.router.alertMessage
- .send(MessageContent(content: "Unable to open the app", type: .warning))
- }
- }
- }
- label: {
- Label(state.displayNameOfApp() ?? "-", systemImage: "waveform.path.ecg.rectangle").font(.title3)
- .padding() }
- .frame(maxWidth: .infinity, alignment: .center)
- .buttonStyle(.bordered)
- }
- .listRowBackground(Color.clear)
- } else if state.cgmCurrent.type == .nightscout {
- if let url = state.url {
- Section {
- Button {
- UIApplication.shared.open(url, options: [:]) { success in
- if !success {
- self.router.alertMessage
- .send(MessageContent(content: "No URL available", type: .warning))
- }
- }
- }
- label: { Label("Open URL", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding() }
- .frame(maxWidth: .infinity, alignment: .center)
- .buttonStyle(.bordered)
- }
- .listRowBackground(Color.clear)
- } else {
- Section {
- Button {
- state.showModal(for: .nighscoutConfigDirect)
- }
- label: {
- Label("Config Nightscout", systemImage: "waveform.path.ecg.rectangle").font(.title3).padding()
- }
- .frame(maxWidth: .infinity, alignment: .center)
- .buttonStyle(.bordered)
- }
- .listRowBackground(Color.clear)
- }
- }
- if state.cgmCurrent.type == .xdrip {
- Section(header: Text("Heartbeat")) {
- VStack(alignment: .leading) {
- if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
- Text("CGM address :").padding(.top)
- Text(cgmTransmitterDeviceAddress)
- } else {
- Text("CGM is not used as heartbeat.").padding(.top)
- }
- HStack(alignment: .top) {
- Text(
- "A heartbeat tells Trio to start a loop cycle. \nThis is required to keep looping."
- )
- .font(.footnote)
- .foregroundColor(.secondary)
- .lineLimit(nil)
- Spacer()
- Button(
- action: {
- hintLabel = "CGM Heartbeat"
- selectedVerboseHint =
- AnyView(
- Text(
- "The CGM Heartbeat can come from either a CGM or a pump to wake up Trio when phone is locked or in the background. If CGM is on the same phone as Trio and xDrip4iOS is configured to use the same AppGroup as Trio and the heartbeat feature is turned on in xDrip4iOS, then the CGM can provide a heartbeat to wake up Trio when phone is locked or app is in the background."
- )
- )
- shouldDisplayHint.toggle()
- },
- label: {
- HStack {
- Image(systemName: "questionmark.circle")
- }
- }
- ).buttonStyle(BorderlessButtonStyle())
- }.padding(.vertical)
- }
- }.listRowBackground(Color.chart)
- }
- if state.cgmCurrent.type == .plugin && state.cgmCurrent.id.contains("Libre") {
- Section {
- Text("Libre Calibrations").navigationLink(to: .calibrations, from: self)
- }.listRowBackground(Color.chart)
- }
- SettingInputSection(
- decimalValue: $decimalPlaceholder,
- booleanValue: $state.smoothGlucose,
- shouldDisplayHint: $shouldDisplayHint,
- selectedVerboseHint: Binding(
- get: { selectedVerboseHint },
- set: {
- selectedVerboseHint = $0.map { AnyView($0) }
- hintLabel = "Smooth Glucose Value"
- }
- ),
- units: state.units,
- type: .boolean,
- label: "Smooth Glucose Value",
- miniHint: "Smooth CGM readings using Savitzky-Golay filtering",
- verboseHint: VStack {
- Text("Default: OFF").bold()
- VStack(alignment: .leading, spacing: 10) {
- Text(
- "This filter looks at small groups of nearby readings and fits them to a simple mathematical curve. This process doesn't change the overall pattern of your glucose data but helps smooth out the \"noise\" or irregular fluctuations that could lead to false highs or lows."
- )
- Text(
- "Because your glucose readings are taken at regular intervals, the filter can use a set of pre-calculated \"weights\" to adjust each group of readings, making the calculations fast and efficient. It's designed to keep the important trends in your data while minimizing those small, misleading variations, giving you and Trio a clearer sense of where your blood sugar is really headed."
- )
- Text(
- "This type of filtering is useful in Trio, as it can help prevent over-corrections based on inaccurate glucose readings. This can help reduce the impact of sudden spikes or dips that might not reflect your true blood glucose levels."
- )
- }
- }
- )
- }
- .scrollContentBackground(.hidden).background(color)
- .onAppear(perform: configureView)
- .navigationTitle("CGM")
- .navigationBarTitleDisplayMode(.automatic)
- .sheet(isPresented: $shouldDisplayHint) {
- SettingInputHintView(
- hintDetent: $hintDetent,
- shouldDisplayHint: $shouldDisplayHint,
- hintLabel: hintLabel ?? "",
- hintText: selectedVerboseHint ?? AnyView(EmptyView()),
- sheetTitle: "Help"
- )
- }
- .sheet(isPresented: $setupCGM) {
- if let cgmFetchManager = state.cgmManager,
- let cgmManager = cgmFetchManager.cgmManager,
- state.cgmCurrent.type == cgmFetchManager.cgmGlucoseSourceType,
- state.cgmCurrent.id == cgmFetchManager.cgmGlucosePluginId
- {
- CGMSettingsView(
- cgmManager: cgmManager,
- bluetoothManager: state.provider.apsManager.bluetoothManager!,
- unit: state.settingsManager.settings.units,
- completionDelegate: state
- )
- } else {
- CGMSetupView(
- CGMType: state.cgmCurrent,
- bluetoothManager: state.provider.apsManager.bluetoothManager!,
- unit: state.settingsManager.settings.units,
- completionDelegate: state,
- setupDelegate: state,
- pluginCGMManager: self.state.pluginCGMManager
- )
- }
- }
- .onChange(of: setupCGM) { setupCGM in
- state.setupCGM = setupCGM
- }
- .onChange(of: state.setupCGM) { setupCGM in
- self.setupCGM = setupCGM
- }
- .screenNavigation(self)
- }
- }
- }
- }
|