Skip to content

Commit 199538b

Browse files
Fixed composer bug with selectedRange updates
1 parent 43431ba commit 199538b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerTextInputView.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ struct ComposerTextInputView: UIViewRepresentable {
3737
func updateUIView(_ uiView: InputTextView, context: Context) {
3838
DispatchQueue.main.async {
3939
if uiView.markedTextRange == nil {
40+
if uiView.text != text {
41+
uiView.text = text
42+
}
4043
uiView.selectedRange.location = selectedRangeLocation
41-
uiView.text = text
4244
uiView.isEditable = editable
4345
uiView.placeholderLabel.text = placeholder
4446
uiView.handleTextChange()
@@ -65,8 +67,8 @@ struct ComposerTextInputView: UIViewRepresentable {
6567
}
6668

6769
func textViewDidChange(_ textView: UITextView) {
68-
textInput.selectedRangeLocation = textView.selectedRange.location
6970
textInput.text = textView.text
71+
textInput.selectedRangeLocation = textView.selectedRange.location
7072
var height = textView.sizeThatFits(textView.bounds.size).height
7173
if height < TextSizeConstants.minThreshold {
7274
height = TextSizeConstants.minimumHeight
@@ -77,6 +79,10 @@ struct ComposerTextInputView: UIViewRepresentable {
7779
}
7880
}
7981
}
82+
83+
func textViewDidChangeSelection(_ textView: UITextView) {
84+
textInput.selectedRangeLocation = textView.selectedRange.location
85+
}
8086

8187
func textView(
8288
_ textView: UITextView,

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
1313
// Initial popup size, before the keyboard is shown.
1414
@State private var popupSize: CGFloat = 350
1515
@State private var composerHeight: CGFloat = 0
16+
@State private var keyboardShown = false
1617

1718
private var factory: Factory
1819
private var channelConfig: ChannelConfig?
@@ -134,13 +135,14 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
134135
}
135136
}
136137
.onReceive(keyboardWillChangePublisher) { visible in
137-
if visible {
138+
if visible && !keyboardShown {
138139
if viewModel.composerCommand == nil {
139140
withAnimation(.easeInOut(duration: 0.02)) {
140141
viewModel.pickerTypeState = .expanded(.none)
141142
}
142143
}
143144
}
145+
keyboardShown = visible
144146
}
145147
.onReceive(keyboardHeight) { height in
146148
if height > 0 && height != popupSize {

0 commit comments

Comments
 (0)