admin管理员组

文章数量:1122832

I am building an app for an oil and gas company. The app will be used in the field to connect to and manage devices that inject lubricants into pumps. The app should function as follows: User selects a device to manage from the DeviceListView, the app will navigate to the HomeView where the user can set up the system any way they want to. The issue I am working on is with the navigation. As is, my NavigationLink will navigate when the users tap on the green part of the row (blue highlight), but it will not connect with a device. Meaning, all the values to be displayed in HomeView will not show up. However, when I tap on the text (Red/Orange highlight) the app will connect to the device and load the characteristics in the console, but it will not navigate to the HomeView. I thought this was a UI issue. I have tried a whole bunch of combinations of H-Stack, Z-stack, modifiers and such, but the result remains the same. Is there a way to set the NavigationLink to do both, connect and navigate at the same time or with the same tapping gesture? DeviceListView

    import SwiftUI
    import CoreData
    import CoreBluetooth
    import Foundation
    
    //Defines the model for an item in the device list.
    struct DeviceViewItem: Identifiable{
        var id = UUID()
        var uuid: String?
        var sn: String?
        var discovered: Bool
        var name: String?
    }
    
    // Making a shared property to store savedArray
    class DeviceDataManager: ObservableObject {
        static let shared = DeviceDataManager() // Singleton instance
        @Published var savedArray: [DeviceViewItem] = []
    }
    
    struct DeviceListView: View {
        //CoreData vars:
        @FetchRequest(entity: Device.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)]) var coreDataDevices: FetchedResults<Device>
        @EnvironmentObject var centralManager: CentralManager
        @Environment(\.managedObjectContext) var viewContext
        
        // State for managing deletion confirmation alert
        @State private var showingDeleteAlert = false
        @State private var deviceToDelete: Device? = nil
    
        var body: some View {
            NavigationStack {
                ZStack{
                    Color("Green 1").ignoresSafeArea()
                    VStack{
                        Image("HeliosLogoSmall")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 200, height: 100)
                            .padding(.top, 40)
                        Text("CONNECT TO A DEVICE")
                            .textStyle(TextStyles.headerBold)
                            .padding()
                    List {
                        let deviceViewList = buildDeviceListView(savedDevice: coreDataDevices, discoveredDevice: centralManager.discoveredPeripherals)
                        ForEach(deviceViewList, id: \.id) { device in
                            NavigationLink(destination: HomeView(), label:{
                                HStack {
                                    Text(device.name ?? "Unknown Device")
                                        .textStyle(TextStyles.display4)
                                }
                                .foregroundColor(device.discovered ? .black : .gray)
                                .onTapGesture {
                                    signalConnect(device: device, manager: centralManager)
                                    updateSaveDevice(deviceViewItem: device, context: viewContext)
                                }
                            }
                            )
                            .listRowBackground(Color("Green 1"))
                        }
                        // Swipe to delete functionality
                        .onDelete(perform: showDeleteAlert)
                    }
                    .listStyle(.plain)
                    .font(Font.custom("InstrumentSans-Regular", size:22))
                    .foregroundColor(Color("Grey 5"))
                    .alert(isPresented: $showingDeleteAlert) {
                        Alert(
                            title: Text("Delete Device"),
                            message: Text("Are you sure you want to delete this device? This action cannot be undone."),
                            primaryButton: .destructive(Text("Delete")) {
                                if let device = deviceToDelete {
                                    deleteDevice(device)
                                }
                            },
                            secondaryButton: .cancel()
                        )
                    }
                    ScanButtonView() //button to begin scanning
                }//Vstack
                .padding([.leading, .trailing], 20)
              }//Zstack
            } //NavigationStack
        }
        
        // Functions to delete a saved device
        
        // Save the context after deleting the device
    }
    
    
    //compares the serial number that we are trying to connect to with the serial number that we have stored
    private func signalConnect(device: DeviceViewItem, manager: CentralManager) {
        print("Tapped " + device.sn!)
        manager.connectBySN(sn: device.sn ?? "Serial1234")
    }
    
    private func buildDeviceListView(savedDevice: FetchedResults<Device>, discoveredDevice: [CBPeripheral] ) -> Array<DeviceViewItem> {
        var savedArray = Array<DeviceViewItem>()
        var discoveredArray = Array<DeviceViewItem>()
        
        // Start by adding all devices from CoreData with discovered = false
        for device in savedDevice {
            var item = DeviceViewItem(discovered: false)
            item.sn = device.serialNumber
            item.uuid = device.uuid
            item.name = device.name
            savedArray.append(item)
        }
        
        // Parse through every discovered device
        for device in discoveredDevice {
            var item = DeviceViewItem(discovered: false)
          //  var item = DeviceViewItem(discovered: true, isConnected: centralManager.isConnected(device.identifier.uuidString)) 

本文标签: iosIs there a way to get a NavigationLink to connect and navigate at the same timeStack Overflow