Skip to content

Commit d676d7b

Browse files
Updates to the composer input view
1 parent f3eaeaa commit d676d7b

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerTextInputView.swift

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ struct ComposerTextInputView: UIViewRepresentable {
5959
func textViewDidChange(_ textView: UITextView) {
6060
textInput.selectedRangeLocation = textView.selectedRange.location
6161
textInput.text = textView.text
62+
var height = textView.sizeThatFits(textView.bounds.size).height
63+
if height < TextSizeConstants.minThreshold {
64+
height = TextSizeConstants.minimumHeight
65+
}
66+
if textInput.height != height {
67+
textInput.height = height
68+
}
6269
}
6370

6471
func textView(
@@ -70,21 +77,5 @@ struct ComposerTextInputView: UIViewRepresentable {
7077
let newMessageLength = textView.text.count + (text.count - range.length)
7178
return newMessageLength <= maxMessageLength
7279
}
73-
74-
func layoutManager(
75-
_ layoutManager: NSLayoutManager,
76-
didCompleteLayoutFor textContainer: NSTextContainer?,
77-
atEnd layoutFinishedFlag: Bool
78-
) {
79-
DispatchQueue.main.async { [weak self] in
80-
guard let view = self?.textView else {
81-
return
82-
}
83-
let size = view.sizeThatFits(view.bounds.size)
84-
if self?.textInput.height != size.height {
85-
self?.textInput.height = size.height
86-
}
87-
}
88-
}
8980
}
9081
}

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
132132
}
133133
.onReceive(keyboardWillChangePublisher) { visible in
134134
if visible {
135-
withAnimation(.easeInOut(duration: 0.02)) {
136-
if viewModel.composerCommand == nil {
135+
if viewModel.composerCommand == nil {
136+
withAnimation(.easeInOut(duration: 0.02)) {
137137
viewModel.pickerTypeState = .expanded(.none)
138138
}
139139
}
140140
}
141141
}
142142
.onReceive(keyboardHeight) { height in
143-
if height > 0 {
143+
if height > 0 && height != popupSize {
144144
self.popupSize = height - bottomSafeArea
145145
}
146146
}
@@ -189,11 +189,11 @@ public struct ComposerInputView<Factory: ViewFactory>: View {
189189
var onCustomAttachmentTap: (CustomAttachment) -> Void
190190
var removeAttachmentWithId: (String) -> Void
191191

192-
@State var textHeight: CGFloat = 34
192+
@State var textHeight: CGFloat = TextSizeConstants.minimumHeight
193193

194194
var textFieldHeight: CGFloat {
195-
let minHeight: CGFloat = 34
196-
let maxHeight: CGFloat = 76
195+
let minHeight: CGFloat = TextSizeConstants.minimumHeight
196+
let maxHeight: CGFloat = TextSizeConstants.maximumHeight
197197

198198
if textHeight < minHeight {
199199
return minHeight

Sources/StreamChatSwiftUI/Utils/Common/InputTextView.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
import UIKit
66

7+
struct TextSizeConstants {
8+
static let minimumHeight = 34.0
9+
static let maximumHeight = 76.0
10+
static let minThreshold = 40.0
11+
}
12+
713
class InputTextView: UITextView {
814
@Injected(\.colors) private var colors
915

@@ -15,7 +21,7 @@ class InputTextView: UITextView {
1521
/// When there is no content in the text view OR the height of the content is less than this value,
1622
/// the text view will be of this height
1723
open var minimumHeight: CGFloat {
18-
34.0
24+
TextSizeConstants.minimumHeight
1925
}
2026

2127
/// The constraint responsible for setting the height of the text view.
@@ -24,7 +30,7 @@ class InputTextView: UITextView {
2430
/// The maximum height of the text view.
2531
/// When the content in the text view is greater than this height, scrolling will be enabled and the text view's height will be restricted to this value
2632
open var maximumHeight: CGFloat {
27-
76.0
33+
TextSizeConstants.maximumHeight
2834
}
2935

3036
override open func didMoveToSuperview() {
@@ -99,17 +105,25 @@ class InputTextView: UITextView {
99105

100106
open func setTextViewHeight() {
101107
var heightToSet = minimumHeight
108+
var contentHeight = contentSize.height
109+
if contentHeight < TextSizeConstants.minThreshold
110+
&& contentHeight != minimumHeight {
111+
contentSize.height = minimumHeight
112+
contentHeight = minimumHeight
113+
}
102114

103-
if contentSize.height <= minimumHeight {
115+
if contentHeight <= minimumHeight {
104116
heightToSet = minimumHeight
105-
} else if contentSize.height >= maximumHeight {
117+
} else if contentHeight >= maximumHeight {
106118
heightToSet = maximumHeight
107119
} else {
108-
heightToSet = contentSize.height
120+
heightToSet = contentHeight
109121
}
110122

111-
heightConstraint?.constant = heightToSet
112-
isScrollEnabled = heightToSet > minimumHeight
113-
layoutIfNeeded()
123+
if heightConstraint?.constant != heightToSet {
124+
heightConstraint?.constant = heightToSet
125+
isScrollEnabled = heightToSet > minimumHeight
126+
layoutIfNeeded()
127+
}
114128
}
115129
}

0 commit comments

Comments
 (0)