Explorar o código

[CGM Config View] - Nightscout button

Add a link to configure Nightscout if not defined.
Improve minor code
Pierre L %!s(int64=2) %!d(string=hai) anos
pai
achega
9f8430dbbd

+ 4 - 2
FreeAPS/Sources/Modules/CGM/CGMStateModel.swift

@@ -134,18 +134,20 @@ extension CGM {
                 .store(in: &lifetime)
         }
 
-        func displayNameOfApp() -> String {
+        func displayNameOfApp() -> String? {
+            guard cgmManager != nil else { return nil }
             var nameOfApp = "Open Application"
             switch cgmManager.cgmGlucoseSourceType {
             case .plugin:
                 nameOfApp = "Open " + (cgmManager.cgmManager?.localizedTitle ?? "Application")
             default:
-                nameOfApp = "Open " + (cgmManager.cgmGlucoseSourceType.displayName ?? "Application")
+                nameOfApp = "Open " + cgmManager.cgmGlucoseSourceType.displayName
             }
             return nameOfApp
         }
 
         func urlOfApp() -> URL? {
+            guard cgmManager != nil else { return nil }
             switch cgmManager.cgmGlucoseSourceType {
             case .plugin:
                 return cgmManager.cgmManager?.appURL

+ 30 - 18
FreeAPS/Sources/Modules/CGM/View/CGMRootView.swift

@@ -30,29 +30,29 @@ extension CGM {
                         }
                     }
 
-                    if let cgmFetchManager = state.cgmManager {
-                        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))
-                                        }
+                    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) }
-                                    .frame(maxWidth: .infinity, alignment: .center)
-                                    .buttonStyle(.bordered)
                             }
-                            .listRowBackground(Color.clear)
-                        } else if state.cgmCurrent.type == .nightscout && state.url != nil {
+
+                            label: {
+                                Label(state.displayNameOfApp() ?? "-", systemImage: "waveform.path.ecg.rectangle").font(.title3) }
+                                .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(state.url!, options: [:]) { success in
+                                    UIApplication.shared.open(url, options: [:]) { success in
                                         if !success {
                                             self.router.alertMessage
                                                 .send(MessageContent(content: "No URL available", type: .warning))
@@ -64,6 +64,18 @@ extension CGM {
                                     .buttonStyle(.bordered)
                             }
                             .listRowBackground(Color.clear)
+                        } else {
+                            Section {
+                                Button {
+                                    state.showModal(for: .nighscoutConfigDirect)
+                                    // router.mainSecondaryModalView.send(router.view(for: .nighscoutConfigDirect))
+                                }
+                                label: { Label("Config Nightscout", systemImage: "waveform.path.ecg.rectangle").font(.title3)
+                                }
+                                .frame(maxWidth: .infinity, alignment: .center)
+                                .buttonStyle(.bordered)
+                            }
+                            .listRowBackground(Color.clear)
                         }
                     }
 

+ 2 - 0
FreeAPS/Sources/Modules/NightscoutConfig/View/NightscoutConfigRootView.swift

@@ -5,6 +5,7 @@ import Swinject
 extension NightscoutConfig {
     struct RootView: BaseView {
         let resolver: Resolver
+        let displayClose: Bool
         @StateObject var state = StateModel()
         @State var importAlert: Alert?
         @State var isImportAlertPresented = false
@@ -134,6 +135,7 @@ extension NightscoutConfig {
             .onAppear(perform: configureView)
             .navigationBarTitle("Nightscout Config")
             .navigationBarTitleDisplayMode(.automatic)
+            .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
             .alert(isPresented: $isImportAlertPresented) {
                 importAlert!
             }

+ 4 - 1
FreeAPS/Sources/Router/Screen.swift

@@ -7,6 +7,7 @@ enum Screen: Identifiable, Hashable {
     case settings
     case configEditor(file: String)
     case nighscoutConfig
+    case nighscoutConfigDirect
     case pumpConfig
     case pumpSettingsEditor
     case basalProfileEditor
@@ -48,7 +49,9 @@ extension Screen {
         case let .configEditor(file):
             ConfigEditor.RootView(resolver: resolver, file: file)
         case .nighscoutConfig:
-            NightscoutConfig.RootView(resolver: resolver)
+            NightscoutConfig.RootView(resolver: resolver, displayClose: false)
+        case .nighscoutConfigDirect:
+            NightscoutConfig.RootView(resolver: resolver, displayClose: true)
         case .pumpConfig:
             PumpConfig.RootView(resolver: resolver)
         case .pumpSettingsEditor: