Skip to content

Commit 4f2cda8

Browse files
fixed issue with jumping keyboard
1 parent 9f8cff8 commit 4f2cda8

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
3131
var onMessageSent: () -> Void
3232

3333
public var body: some View {
34-
VStack {
34+
VStack(spacing: 0) {
3535
HStack(alignment: .bottom) {
3636
factory.makeLeadingComposerView(state: $viewModel.pickerTypeState)
3737

@@ -98,7 +98,7 @@ public struct ComposerInputView<Factory: ViewFactory>: View {
9898
var addedAssets: [AddedAsset]
9999
var addedFileURLs: [URL]
100100
var addedCustomAttachments: [CustomAttachment]
101-
var onCustomAttachmentTap: (CustomAttachment) -> ()
101+
var onCustomAttachmentTap: (CustomAttachment) -> Void
102102

103103
var removeAttachmentWithId: (String) -> Void
104104

@@ -145,7 +145,8 @@ public struct ComposerInputView<Factory: ViewFactory>: View {
145145
if !addedCustomAttachments.isEmpty {
146146
factory.makeCustomAttachmentPreviewView(
147147
addedCustomAttachments: addedCustomAttachments,
148-
onCustomAttachmentTap: onCustomAttachmentTap)
148+
onCustomAttachmentTap: onCustomAttachmentTap
149+
)
149150
}
150151

151152
ComposerTextInputView(

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
2525
@State private var width: CGFloat?
2626
@State private var height: CGFloat?
2727
@State private var keyboardShown = false
28+
@State private var pendingKeyboardUpdate: Bool?
2829

2930
private var dateFormatter: DateFormatter {
3031
utils.dateFormatter
@@ -101,7 +102,13 @@ struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
101102
}
102103
}
103104
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { value in
104-
showScrollToLatestButton = value ?? 0 < -20
105+
DispatchQueue.main.async {
106+
let offsetValue = value ?? 0
107+
showScrollToLatestButton = offsetValue < -20
108+
if keyboardShown {
109+
resignFirstResponder()
110+
}
111+
}
105112
}
106113
.onPreferenceChange(HeightPreferenceKey.self) { value in
107114
if let value = value, value != height {
@@ -132,8 +139,18 @@ struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
132139
}
133140
}
134141
.onReceive(keyboardPublisher) { visible in
135-
keyboardShown = visible
142+
if currentDateString != nil {
143+
pendingKeyboardUpdate = visible
144+
} else {
145+
keyboardShown = visible
146+
}
136147
}
148+
.onChange(of: currentDateString, perform: { dateString in
149+
if dateString == nil, let keyboardUpdate = pendingKeyboardUpdate {
150+
keyboardShown = keyboardUpdate
151+
pendingKeyboardUpdate = nil
152+
}
153+
})
137154
.modifier(HideKeyboardOnTapGesture(shouldAdd: keyboardShown))
138155
}
139156

0 commit comments

Comments
 (0)