I’m constructing an app for an oil and fuel firm. The app might be used within the area to connect with and handle units that inject lubricants into pumps. The app ought to perform as follows: Person selects a tool to handle from the DeviceListView, the app will navigate to the HomeView the place the person can arrange the system any method they wish to.
The difficulty I’m engaged on is with the navigation.
As is, my NavigationLink will navigate when the customers faucet on the inexperienced a part of the row (blue spotlight), however it won’t join with a tool. That means, all of the values to be displayed in HomeView won’t present up.
Nevertheless, once I faucet on the textual content (Crimson/Orange spotlight) the app will hook up with the machine and cargo the traits within the console, however it won’t navigate to the HomeView.
I believed this was a UI challenge. I’ve tried a complete bunch of mixtures of H-Stack, Z-stack, modifiers and such, however the outcome stays the identical.
Is there a strategy to set the NavigationLink to do each, join and navigate on the identical time or with the identical tapping gesture? DeviceListView
import SwiftUI
import CoreData
import CoreBluetooth
import Basis
//Defines the mannequin for an merchandise within the machine checklist.
struct DeviceViewItem: Identifiable{
var id = UUID()
var uuid: String?
var sn: String?
var found: Bool
var identify: String?
}
// Making a shared property to retailer savedArray
class DeviceDataManager: ObservableObject {
static let shared = DeviceDataManager() // Singleton occasion
@Printed var savedArray: [DeviceViewItem] = []
}
struct DeviceListView: View {
//CoreData vars:
@FetchRequest(entity: System.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)]) var coreDataDevices: FetchedResults
@EnvironmentObject var centralManager: CentralManager
@Setting(.managedObjectContext) var viewContext
// State for managing deletion affirmation alert
@State personal var showingDeleteAlert = false
@State personal var deviceToDelete: System? = nil
var physique: some View {
NavigationStack {
ZStack{
Coloration("Inexperienced 1").ignoresSafeArea()
VStack{
Picture("HeliosLogoSmall")
.resizable()
.aspectRatio(contentMode: .match)
.body(width: 200, peak: 100)
.padding(.high, 40)
Textual content("CONNECT TO A DEVICE")
.textStyle(TextStyles.headerBold)
.padding()
Listing {
let deviceViewList = buildDeviceListView(savedDevice: coreDataDevices, discoveredDevice: centralManager.discoveredPeripherals)
ForEach(deviceViewList, id: .id) { machine in
NavigationLink(vacation spot: HomeView(), label:{
HStack {
Textual content(machine.identify ?? "Unknown System")
.textStyle(TextStyles.display4)
}
.foregroundColor(machine.found ? .black : .grey)
.onTapGesture {
signalConnect(machine: machine, supervisor: centralManager)
updateSaveDevice(deviceViewItem: machine, context: viewContext)
}
}
)
.listRowBackground(Coloration("Inexperienced 1"))
}
// Swipe to delete performance
.onDelete(carry out: showDeleteAlert)
}
.listStyle(.plain)
.font(Font.customized("InstrumentSans-Common", measurement:22))
.foregroundColor(Coloration("Gray 5"))
.alert(isPresented: $showingDeleteAlert) {
Alert(
title: Textual content("Delete System"),
message: Textual content("Are you certain you wish to delete this machine? This motion can't be undone."),
primaryButton: .harmful(Textual content("Delete")) {
if let machine = deviceToDelete {
deleteDevice(machine)
}
},
secondaryButton: .cancel()
)
}
ScanButtonView() //button to start scanning
}//Vstack
.padding([.leading, .trailing], 20)
}//Zstack
} //NavigationStack
}
// Features to delete a saved machine
// Save the context after deleting the machine
}
//compares the serial quantity that we try to connect with with the serial quantity that we have now saved
personal func signalConnect(machine: DeviceViewItem, supervisor: CentralManager) {
print("Tapped " + machine.sn!)
supervisor.connectBySN(sn: machine.sn ?? "Serial1234")
}
personal func buildDeviceListView(savedDevice: FetchedResults, discoveredDevice: [CBPeripheral] ) -> Array {
var savedArray = Array()
var discoveredArray = Array()
// Begin by including all units from CoreData with found = false
for machine in savedDevice {
var merchandise = DeviceViewItem(found: false)
merchandise.sn = machine.serialNumber
merchandise.uuid = machine.uuid
merchandise.identify = machine.identify
savedArray.append(merchandise)
}
// Parse via each found machine
for machine in discoveredDevice {
var merchandise = DeviceViewItem(found: false)
// var merchandise = DeviceViewItem(found: true, isConnected: centralManager.isConnected(machine.identifier.uuidString)) 🟡🟣
let sn = CentralManager.buildSn(uuid: machine.identifier.uuidString, identify: machine.identify)
// Discover out if we have linked to this machine earlier than
// Verify if machine is current in array
if let matchIndex = savedArray.firstIndex(the place: { $0.sn == sn }) {
savedArray[matchIndex].found = true
}
else {
// We have by no means saved or linked to this machine earlier than, subsequently add it to the checklist.
merchandise.sn = sn
merchandise.uuid = machine.identifier.uuidString
merchandise.found = true
merchandise.identify = machine.identify
discoveredArray.append(merchandise)
}
}
// Replace the shared savedArray
DeviceDataManager.shared.savedArray = savedArray
return savedArray + discoveredArray //MAKE savedArray worth public and accessible to all.
}