Fix edit device name alert for macOS 12

This commit is contained in:
Leon Böttger committed 2025-12-20 21:05:01 +01:00
1 parent ad1ab30b94
commit 7cdfdcd474
2 files changed
+46 -26

No files matched your search

+42 -22
View File
@@ -8,13 +8,51 @@
import SwiftUI
import LUI
struct ChangeDeviceNameAlertModifier: ViewModifier {
@Binding var isPresented: Bool
@State var customName = ""
let maxLength = NearbyConnectionManager.maxNameChars
func body(content: Content) -> some View {
content
.alert("ChangeDeviceName", isPresented: $isPresented) {
TextField("DeviceName", text: $customName)
.onChange(of: customName) { newValue in
if newValue.count > maxLength {
customName = String(newValue.prefix(maxLength))
}
}
Button("Save") {
let trimmed = customName.trimmingCharacters(in: .whitespacesAndNewlines)
NearbyConnectionManager.shared.setCustomDeviceName(to: trimmed)
}
Button("Cancel", role: .cancel) { }
} message: {
Text("ChangeDeviceNameDescription")
}
}
}
extension View {
func changeDeviceNameAlert(
isPresented: Binding<Bool>,
) -> some View {
modifier(ChangeDeviceNameAlertModifier(
isPresented: isPresented
))
}
}
struct ChangeDeviceNameModifier: ViewModifier {
let isEnabled: Bool
private let maxLength = NearbyConnectionManager.maxNameChars
@State private var isPresented = false
@State private var customName = ""
func body(content: Content) -> some View {
content
@@ -22,32 +60,14 @@ struct ChangeDeviceNameModifier: ViewModifier {
guard isEnabled else { return }
lightVibration()
customName = NearbyConnectionManager.getCustomDeviceName() ?? ""
isPresented = true
}
.alert("ChangeDeviceName", isPresented: $isPresented) {
TextField("DeviceName", text: $customName)
.onChange(of: customName) { newValue in
if newValue.count > maxLength {
customName = String(newValue.prefix(maxLength))
}
}
Button("Save") {
NearbyConnectionManager.shared
.setCustomDeviceName(to: customName.trimmingCharacters(in: .whitespacesAndNewlines))
}
Button("Cancel", role: .cancel) { }
} message: {
Text("ChangeDeviceNameDescription")
}
.changeDeviceNameAlert(isPresented: $isPresented)
}
}
extension View {
func changeDeviceNameAlert(isEnabled: Bool = true) -> some View {
modifier(ChangeDeviceNameModifier(isEnabled: isEnabled))
+4 -4
View File
@@ -14,6 +14,8 @@ struct SettingsView: View {
@ObservedObject var settings = Settings.shared
@ObservedObject var connectionManager = NearbyConnectionManager.shared
@State private var isChangeDeviceNameAlertPresented = false
var body: some View {
LargeAppIconView(title: "Settings") {
@@ -46,11 +48,9 @@ struct SettingsView: View {
.foregroundColor(.gray)
Button("Edit") {
isChangeDeviceNameAlertPresented.toggle()
}
.overlay(
Color.clear
.contentShape(Rectangle()).changeDeviceNameAlert()
)
.changeDeviceNameAlert(isPresented: $isChangeDeviceNameAlertPresented)
}
Divider()