Meshtastic-Apple/Meshtastic/Views/Settings/Config/Module/RtttlConfig.swift

108 lines
3.7 KiB
Swift
Raw Normal View History

2023-03-25 14:30:18 -07:00
//
// RingtoneConfig.swift
// Meshtastic
//
// Created by Garth Vander Houwen on 3/25/23.
//
import SwiftUI
import OSLog
2023-03-25 14:30:18 -07:00
struct RtttlConfig: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
var node: NodeInfoEntity?
@State private var isPresentingSaveConfirm: Bool = false
@State var hasChanges = false
@State var ringtone: String = ""
var body: some View {
VStack {
Form {
2025-05-08 22:50:44 -07:00
ConfigHeader(title: "Ringtone", config: \.rtttlConfig, node: node, onAppear: setRtttLConfigValue)
2025-04-26 16:05:09 -07:00
Section(header: Text("Options")) {
2023-03-25 14:30:18 -07:00
HStack {
2025-05-08 22:50:44 -07:00
Label("Ringtone", systemImage: "music.quarternote.3")
TextField("Ringtone Transfer Language", text: $ringtone, axis: .vertical)
2023-03-25 14:30:18 -07:00
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
2024-10-06 08:50:12 -07:00
.onChange(of: ringtone) {
2024-09-22 08:03:18 -07:00
var totalBytes = ringtone.utf8.count
2023-03-25 14:30:18 -07:00
// Only mess with the value if it is too big
2024-09-22 08:03:18 -07:00
while totalBytes > 228 {
ringtone = String(ringtone.dropLast())
2024-09-22 08:03:18 -07:00
totalBytes = ringtone.utf8.count
2023-03-25 14:30:18 -07:00
}
2024-10-06 08:50:12 -07:00
}
2023-03-25 14:30:18 -07:00
.foregroundColor(.gray)
}
.keyboardType(.default)
.listRowSeparator(.hidden)
2025-05-08 22:50:44 -07:00
Text("Ringtone Transfer Language(RTTTL) Ringtone String used by supported buzzers in external notifications.")
.foregroundColor(.gray)
.font(.callout)
2023-03-25 14:30:18 -07:00
}
}
.disabled(self.bleManager.connectedPeripheral == nil || node?.rtttlConfig == nil)
SaveConfigButton(node: node, hasChanges: $hasChanges) {
2023-03-25 14:30:18 -07:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
if connectedNode != nil {
let adminMessageId = bleManager.saveRtttlConfig(ringtone: ringtone.trimmingCharacters(in: .whitespacesAndNewlines), fromUser: connectedNode!.user!, toUser: node!.user!, 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()
2023-03-25 14:30:18 -07:00
}
}
}
2025-05-08 22:50:44 -07:00
.navigationTitle("Ringtone Config")
.navigationBarItems(
trailing: ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: bleManager.connectedPeripheral?.shortName ?? "?"
)
}
)
.onFirstAppear {
// Need to request a RtttlConfig from the remote node before allowing changes
if let connectedPeripheral = bleManager.connectedPeripheral, let node {
let connectedNode = getNodeInfo(id: connectedPeripheral.num, context: context)
if let connectedNode {
if node.num != connectedNode.num {
if UserDefaults.enableAdministration && node.num != connectedNode.num {
/// 2.5 Administration with session passkey
let expiration = node.sessionExpiration ?? Date()
if expiration < Date() || node.rtttlConfig == nil {
Logger.mesh.info("⚙️ Empty or expired ringtone module config requesting via PKI admin")
_ = bleManager.requestRtttlConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
}
} else {
/// Legacy Administration
2025-05-13 06:19:27 -07:00
Logger.mesh.info("☠️ Using insecure legacy admin that is no longer supported, please upgrade your firmware.")
}
}
2023-03-25 14:30:18 -07:00
}
}
}
2024-10-06 08:50:12 -07:00
.onChange(of: ringtone) { _, newRingtone in
2023-03-25 14:30:18 -07:00
if node != nil && node!.rtttlConfig != nil {
if newRingtone != node!.rtttlConfig!.ringtone { hasChanges = true }
}
}
}
}
func setRtttLConfigValue() {
self.ringtone = node?.rtttlConfig?.ringtone ?? ""
self.hasChanges = false
}
}