diff --git a/bitchat/Views/ContentComposerView.swift b/bitchat/Views/ContentComposerView.swift index 7290e93f..537117c3 100644 --- a/bitchat/Views/ContentComposerView.swift +++ b/bitchat/Views/ContentComposerView.swift @@ -91,7 +91,12 @@ struct ContentComposerView: View { .bitchatFont(size: 15) .foregroundColor(palette.primary) .focused(isTextFieldFocused) - .autocorrectionDisabled(true) + .autocorrectionDisabled( + Self.shouldDisableAutocorrection( + for: messageText, + isAutocompleting: conversationUIModel.showAutocomplete + ) + ) #if os(iOS) .textInputAutocapitalization(.sentences) #endif @@ -161,6 +166,12 @@ struct ContentComposerView: View { } } +extension ContentComposerView { + static func shouldDisableAutocorrection(for text: String, isAutocompleting: Bool) -> Bool { + isAutocompleting || text.trimmingCharacters(in: .whitespacesAndNewlines).hasPrefix("/") + } +} + private extension ContentComposerView { /// Static localized strings hoisted out of `body` so each keystroke in the /// composer doesn't perform repeated bundle lookups. diff --git a/bitchatTests/ContentComposerAutocorrectTests.swift b/bitchatTests/ContentComposerAutocorrectTests.swift new file mode 100644 index 00000000..82883c28 --- /dev/null +++ b/bitchatTests/ContentComposerAutocorrectTests.swift @@ -0,0 +1,20 @@ +// +// ContentComposerAutocorrectTests.swift +// bitchatTests +// + +import Testing +@testable import bitchat + +struct ContentComposerAutocorrectTests { + @Test func autocorrectionEnabledForNormalMessages() { + #expect(!ContentComposerView.shouldDisableAutocorrection(for: "hello world", isAutocompleting: false)) + #expect(!ContentComposerView.shouldDisableAutocorrection(for: "", isAutocompleting: false)) + } + + @Test func autocorrectionDisabledForSlashCommandsAndAutocomplete() { + #expect(ContentComposerView.shouldDisableAutocorrection(for: "/nick alice", isAutocompleting: false)) + #expect(ContentComposerView.shouldDisableAutocorrection(for: " /nick qwerty", isAutocompleting: false)) + #expect(ContentComposerView.shouldDisableAutocorrection(for: "hey @al", isAutocompleting: true)) + } +}