Meshtastic-Apple/Meshtastic/Views/Settings/Config/Module/PaxCounterConfig.swift
Blake McAnally 58da532d32 Extract the generated protobufs into its own Swift package
This change modifies the process for generating and integrating the Meshtastic protobufs into the client application.

* The generated Swift code is now in a local SPM package `MeshtasticProtobufs`
* An Xcode Workspace file `Meshtastic.xcworkspace` was created to more easily manage the new build targets.
* The code generation script for the protos was modified to generate the Swift code into the new location.
* The README.md was updated to reflect these changes.

NOTE: After merging this PR, do not open the project file `Meshtastic.xcodeproj`. You must use the workspace `Meshtastic.xcworkspace`

Extracting out the generated protobuf code into its own library enables several opportunities for the project. This is just a first step, but with some more modularization, a standalone Apple Watch app or other targets starts to become a little bit more achievable to implement.

After extracting the protobufs into a Swift package, I validate these changes by building and running the Meshtastic app to an iPhone 15 Pro Max, and tried changing some settings on a local node. I then messaged back and forth using two local nodes connected to two different iOS devices.
2024-06-28 11:11:01 -05:00

115 lines
3.5 KiB
Swift

//
// PaxCounterConfig.swift
// Meshtastic
//
// Copyright Garth Vander Houwen 2/25/24.
//
import MeshtasticProtobufs
import SwiftUI
struct PaxCounterConfig: View {
@Environment(\.managedObjectContext) private var context
@EnvironmentObject private var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
let node: NodeInfoEntity?
@State private var enabled = false
@State private var paxcounterUpdateInterval = 0
@State private var hasChanges: Bool = false
var body: some View {
Form {
ConfigHeader(title: "config.module.paxcounter.title", config: \.powerConfig, node: node, onAppear: setPaxValues)
Section {
Toggle(isOn: $enabled) {
Label("enabled", systemImage: "figure.walk.motion")
Text("config.module.paxcounter.enabled.description")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
.listRowSeparator(.visible)
if enabled {
Picker("config.module.paxcounter.updateinterval", selection: $paxcounterUpdateInterval) {
ForEach(UpdateIntervals.allCases) { at in
if at.rawValue >= 300 {
Text(at.description)
}
}
}
.pickerStyle(DefaultPickerStyle())
.listRowSeparator(.hidden)
Text("config.module.paxcounter.updateinterval.description")
.foregroundColor(.gray)
.font(.callout)
}
} header: {
Text("options")
}
}
.disabled(self.bleManager.connectedPeripheral == nil || node?.powerConfig == nil)
.navigationTitle("config.module.paxcounter.title")
.navigationBarItems(trailing: ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: "\(bleManager.connectedPeripheral?.shortName ?? "?")"
)
})
.onAppear {
if self.bleManager.context == nil {
self.bleManager.context = context
}
setPaxValues()
// Need to request a PAX Counter module config from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.paxCounterConfig == nil {
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral?.num ?? 0, context: context)
if node != nil && connectedNode != nil {
_ = bleManager.requestPaxCounterModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
}
}
}
.onChange(of: enabled) {
if let val = node?.paxCounterConfig?.enabled {
hasChanges = $0 != val
}
}
.onChange(of: paxcounterUpdateInterval) {
if let val = node?.paxCounterConfig?.updateInterval {
hasChanges = $0 != val
}
}
SaveConfigButton(node: node, hasChanges: $hasChanges) {
guard let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context),
let fromUser = connectedNode.user,
let toUser = node?.user else {
return
}
var config = ModuleConfig.PaxcounterConfig()
config.enabled = enabled
config.paxcounterUpdateInterval = UInt32(paxcounterUpdateInterval)
let adminMessageId = bleManager.savePaxcounterModuleConfig(
config: config,
fromUser: fromUser,
toUser: toUser,
adminIndex: connectedNode.myInfo?.adminIndex ?? 0
)
if adminMessageId > 0 {
// Should show a saved successfully alert once I know that to be true
// for now just disable the button after a successful save
hasChanges = false
goBack()
}
}
}
private func setPaxValues() {
enabled = node?.paxCounterConfig?.enabled ?? enabled
paxcounterUpdateInterval = Int(node?.paxCounterConfig?.updateInterval ?? 900)
}
}