Skip to content

Commit 73d1f23

Browse files
[SWUI-100] Fixed bug with keyboard not shown when scrolling fast to bottom
1 parent 266865a commit 73d1f23

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

DemoAppSwiftUI/CreateGroupView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct CreateGroupView: View, KeyboardReadable {
6868
.alert(isPresented: $viewModel.errorShown) {
6969
Alert.defaultErrorAlert
7070
}
71-
.onReceive(keyboardPublisher) { visible in
71+
.onReceive(keyboardWillChangePublisher) { visible in
7272
keyboardShown = visible
7373
}
7474
.modifier(HideKeyboardOnTapGesture(shouldAdd: keyboardShown))

DemoAppSwiftUI/GroupNameView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct GroupNameView: View, KeyboardReadable {
6565
.alert(isPresented: $viewModel.errorShown) {
6666
Alert.defaultErrorAlert
6767
}
68-
.onReceive(keyboardPublisher) { visible in
68+
.onReceive(keyboardWillChangePublisher) { visible in
6969
keyboardShown = visible
7070
}
7171
.modifier(HideKeyboardOnTapGesture(shouldAdd: keyboardShown))

DemoAppSwiftUI/NewChatView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct NewChatView: View, KeyboardReadable {
9797
}
9898
}
9999
.navigationTitle("New Chat")
100-
.onReceive(keyboardPublisher) { visible in
100+
.onReceive(keyboardWillChangePublisher) { visible in
101101
keyboardShown = visible
102102
}
103103
.modifier(HideKeyboardOnTapGesture(shouldAdd: keyboardShown))

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
130130
self.composerHeight = value
131131
}
132132
}
133-
.onReceive(keyboardPublisher) { visible in
133+
.onReceive(keyboardWillChangePublisher) { visible in
134134
if visible {
135135
withAnimation(.easeInOut(duration: 0.02)) {
136136
if viewModel.composerCommand == nil {

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
130130
let offsetValue = value ?? 0
131131
showScrollToLatestButton = offsetValue < -20
132132
if keyboardShown {
133+
keyboardShown = false
133134
resignFirstResponder()
134135
}
135136
}
@@ -178,11 +179,13 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
178179
)
179180
}
180181
}
181-
.onReceive(keyboardPublisher) { visible in
182+
.onReceive(keyboardDidChangePublisher) { visible in
182183
if currentDateString != nil {
183184
pendingKeyboardUpdate = visible
184185
} else {
185-
keyboardShown = visible
186+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
187+
keyboardShown = visible
188+
}
186189
}
187190
}
188191
.onChange(of: currentDateString, perform: { dateString in

Sources/StreamChatSwiftUI/Utils/KeyboardHandling.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import UIKit
88

99
/// Publisher to read keyboard changes.
1010
public protocol KeyboardReadable {
11-
var keyboardPublisher: AnyPublisher<Bool, Never> { get }
11+
var keyboardWillChangePublisher: AnyPublisher<Bool, Never> { get }
12+
var keyboardDidChangePublisher: AnyPublisher<Bool, Never> { get }
1213
var keyboardHeight: AnyPublisher<CGFloat, Never> { get }
1314
}
1415

1516
/// Default implementation.
1617
extension KeyboardReadable {
17-
public var keyboardPublisher: AnyPublisher<Bool, Never> {
18+
public var keyboardWillChangePublisher: AnyPublisher<Bool, Never> {
1819
Publishers.Merge(
1920
NotificationCenter.default
2021
.publisher(for: UIResponder.keyboardWillShowNotification)
@@ -27,6 +28,19 @@ extension KeyboardReadable {
2728
.eraseToAnyPublisher()
2829
}
2930

31+
public var keyboardDidChangePublisher: AnyPublisher<Bool, Never> {
32+
Publishers.Merge(
33+
NotificationCenter.default
34+
.publisher(for: UIResponder.keyboardDidShowNotification)
35+
.map { _ in true },
36+
37+
NotificationCenter.default
38+
.publisher(for: UIResponder.keyboardDidHideNotification)
39+
.map { _ in false }
40+
)
41+
.eraseToAnyPublisher()
42+
}
43+
3044
public var keyboardHeight: AnyPublisher<CGFloat, Never> {
3145
NotificationCenter
3246
.default

0 commit comments

Comments
 (0)