2023-03-25 14:30:18 -07:00
|
|
|
//
|
|
|
|
|
// RingtoneConfig.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Created by Garth Vander Houwen on 3/25/23.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-02-25 15:32:01 -08:00
|
|
|
ConfigHeader(title: "ringtone", config: \.rtttlConfig, node: node, onAppear: setRtttLConfigValue)
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-03-25 14:30:18 -07:00
|
|
|
Section(header: Text("options")) {
|
|
|
|
|
HStack {
|
2023-03-27 10:43:01 -07:00
|
|
|
Label("ringtone", systemImage: "music.quarternote.3")
|
2024-02-25 15:32:01 -08:00
|
|
|
TextField("config.ringtone.label", text: $ringtone, axis: .vertical)
|
2023-03-25 14:30:18 -07:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
.autocapitalization(.none)
|
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
|
.onChange(of: ringtone, perform: { _ in
|
|
|
|
|
|
|
|
|
|
let totalBytes = ringtone.utf8.count
|
|
|
|
|
// Only mess with the value if it is too big
|
|
|
|
|
if totalBytes > 228 {
|
2024-04-04 09:39:03 -07:00
|
|
|
ringtone = String(ringtone.dropLast())
|
2023-03-25 14:30:18 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
}
|
|
|
|
|
.keyboardType(.default)
|
2024-02-21 23:35:28 -08:00
|
|
|
.listRowSeparator(.hidden)
|
2024-02-25 15:32:01 -08:00
|
|
|
Text("config.ringtone.description")
|
2024-02-21 23:35:28 -08:00
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
.font(.callout)
|
2023-03-25 14:30:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.disabled(self.bleManager.connectedPeripheral == nil || node?.rtttlConfig == nil)
|
|
|
|
|
|
2024-02-18 00:03:34 -07:00
|
|
|
SaveConfigButton(node: node, hasChanges: $hasChanges) {
|
2023-03-25 14:30:18 -07:00
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
2024-02-18 00:03:34 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-25 15:32:01 -08:00
|
|
|
.navigationTitle("config.ringtone.title")
|
2023-03-25 14:30:18 -07:00
|
|
|
.navigationBarItems(trailing:
|
|
|
|
|
ZStack {
|
2023-09-02 17:37:35 -07:00
|
|
|
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?")
|
2023-03-25 14:30:18 -07:00
|
|
|
})
|
|
|
|
|
.onAppear {
|
2024-01-14 11:25:00 -08:00
|
|
|
if self.bleManager.context == nil {
|
|
|
|
|
self.bleManager.context = context
|
|
|
|
|
}
|
2023-03-25 14:30:18 -07:00
|
|
|
setRtttLConfigValue()
|
|
|
|
|
// Need to request a Rtttl Config from the remote node before allowing changes
|
2023-03-26 09:46:51 -07:00
|
|
|
if bleManager.connectedPeripheral != nil && (node?.rtttlConfig == nil || node?.rtttlConfig?.ringtone?.count ?? 0 == 0) {
|
2023-03-25 14:30:18 -07:00
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
|
|
|
|
if node != nil && connectedNode != nil {
|
2023-03-25 15:18:44 -07:00
|
|
|
_ = bleManager.requestRtttlConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
|
2023-03-25 14:30:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: ringtone) { newRingtone in
|
|
|
|
|
if node != nil && node!.rtttlConfig != nil {
|
|
|
|
|
if newRingtone != node!.rtttlConfig!.ringtone { hasChanges = true }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func setRtttLConfigValue() {
|
|
|
|
|
self.ringtone = node?.rtttlConfig?.ringtone ?? ""
|
|
|
|
|
self.hasChanges = false
|
|
|
|
|
}
|
|
|
|
|
}
|