Add nickname validation to prevent empty names

- Validate nickname only when field loses focus or on submit
- Replace empty nicknames with anon+random 4 digits (e.g. anon3847)
- Remove auto-save timer to prevent validation while typing
- Add FocusState to detect when user exits the field
This commit is contained in:
jack
2025-07-24 12:16:23 +02:00
parent c4ca38654d
commit cc3a094205
2 changed files with 17 additions and 12 deletions
+8 -3
View File
@@ -25,6 +25,7 @@ struct ContentView: View {
@State private var showMessageActions = false
@State private var selectedMessageSender: String?
@State private var selectedMessageSenderID: String?
@FocusState private var isNicknameFieldFocused: Bool
private var backgroundColor: Color {
colorScheme == .dark ? Color.black : Color.white
@@ -727,11 +728,15 @@ struct ContentView: View {
.font(.system(size: 14, design: .monospaced))
.frame(maxWidth: 100)
.foregroundColor(textColor)
.onChange(of: viewModel.nickname) { _ in
viewModel.saveNickname()
.focused($isNicknameFieldFocused)
.onChange(of: isNicknameFieldFocused) { isFocused in
if !isFocused {
// Only validate when losing focus
viewModel.validateAndSaveNickname()
}
}
.onSubmit {
viewModel.saveNickname()
viewModel.validateAndSaveNickname()
}
}