diff --git a/QuickDrop/View/ChangeDeviceNameModifier.swift b/QuickDrop/View/ChangeDeviceNameModifier.swift index d8510a9..f33aa06 100644 --- a/QuickDrop/View/ChangeDeviceNameModifier.swift +++ b/QuickDrop/View/ChangeDeviceNameModifier.swift @@ -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, + ) -> 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)) diff --git a/QuickDrop/View/SettingsView.swift b/QuickDrop/View/SettingsView.swift index e0caf7f..7712743 100644 --- a/QuickDrop/View/SettingsView.swift +++ b/QuickDrop/View/SettingsView.swift @@ -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()