Skip to content

Commit 45b01bc

Browse files
authored
Dismiss keyboard when tapping on the empty message list (#513)
1 parent 2f07f70 commit 45b01bc

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
### ✅ Added
77
- Thread replies shown in channel indicator [#518](https://github.com/GetStream/stream-chat-swiftui/pull/518)
88

9+
### 🐞 Fixed
10+
- Dismiss keyboard when tapping on the empty message list [#513](https://github.com/GetStream/stream-chat-swiftui/pull/513)
11+
912
# [4.57.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.57.0)
1013
_June 07, 2024_
1114

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public struct ChatChannelInfoView: View, KeyboardReadable {
154154
.onReceive(keyboardWillChangePublisher) { visible in
155155
viewModel.keyboardShown = visible
156156
}
157-
.modifier(
158-
HideKeyboardOnTapGesture(shouldAdd: viewModel.keyboardShown)
159-
)
157+
.dismissKeyboardOnTap(enabled: viewModel.keyboardShown)
160158
.background(Color(colors.background).edgesIgnoringSafeArea(.bottom))
161159
}
162160
}

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
7676
} else {
7777
ZStack {
7878
factory.makeEmptyMessagesView(for: channel, colors: colors)
79+
.dismissKeyboardOnTap(enabled: keyboardShown)
7980
if viewModel.shouldShowTypingIndicator {
8081
factory.makeTypingIndicatorBottomView(
8182
channel: channel,

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
304304
) : nil
305305
)
306306
.modifier(factory.makeMessageListContainerModifier())
307-
.modifier(HideKeyboardOnTapGesture(shouldAdd: keyboardShown))
307+
.dismissKeyboardOnTap(enabled: keyboardShown)
308308
.onDisappear {
309309
messageRenderingUtil.update(previousTopMessage: nil)
310310
}

Sources/StreamChatSwiftUI/Utils/KeyboardHandling.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ extension KeyboardReadable {
5757
}
5858
}
5959

60+
extension View {
61+
/// Dismisses the keyboard when tapping on the view.
62+
/// - Parameters:
63+
/// - enabled: If true, tapping on the view dismisses the view, otherwise keyboard stays visible.
64+
/// - onTapped: A closure which is triggered when keyboard is dismissed after tapping the view.
65+
func dismissKeyboardOnTap(enabled: Bool, onKeyboardDismissed: (() -> Void)? = nil) -> some View {
66+
modifier(HideKeyboardOnTapGesture(shouldAdd: enabled))
67+
}
68+
}
69+
6070
/// View modifier for hiding the keyboard on tap.
6171
public struct HideKeyboardOnTapGesture: ViewModifier {
6272
var shouldAdd: Bool

StreamChatSwiftUITestsAppTests/Pages/MessageListPage.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class MessageListPage {
2626
static var list: XCUIElement {
2727
app.scrollViews.matching(NSPredicate(format: "identifier LIKE 'MessageListView' or identifier LIKE 'MessageListScrollView'")).firstMatch
2828
}
29+
30+
static var listEmpty: XCUIElement {
31+
app.otherElements.matching(identifier: "EmptyMessagesView").firstMatch
32+
}
2933

3034
static var typingIndicator: XCUIElement {
3135
app.staticTexts["TypingIndicatorBottomView"].firstMatch

StreamChatSwiftUITestsAppTests/Robots/UserRobot.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ extension UserRobot {
234234
return self
235235
}
236236

237+
@discardableResult
238+
func tapOnComposerTextView() -> Self {
239+
MessageListPage.Composer.textView.wait().safeTap()
240+
return self
241+
}
242+
237243
@discardableResult
238244
func tapOnMessage(at messageCellIndex: Int? = 0) -> Self {
239245
let messageCell = messageCell(withIndex: messageCellIndex)
@@ -251,6 +257,12 @@ extension UserRobot {
251257
MessageListPage.list.safeTap()
252258
return self
253259
}
260+
261+
@discardableResult
262+
func tapOnEmptyMessageList() -> Self {
263+
MessageListPage.listEmpty.safeTap()
264+
return self
265+
}
254266

255267
@discardableResult
256268
func tapOnScrollToBottomButton() -> Self {

StreamChatSwiftUITestsAppTests/Tests/MessageList_Tests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@ final class MessageList_Tests: StreamTestCase {
331331
userRobot.assertMessage(message)
332332
}
333333
}
334+
335+
func test_emptyViewDismissesKeyboard() throws {
336+
GIVEN("user opens the channel") {
337+
userRobot.login().openChannel()
338+
}
339+
WHEN("keyboard is open") {
340+
userRobot.tapOnComposerTextView()
341+
userRobot.assertKeyboard(isVisible: true)
342+
userRobot.tapOnEmptyMessageList()
343+
}
344+
THEN("keyboard is dismissed") {
345+
userRobot.assertKeyboard(isVisible: false)
346+
}
347+
}
334348
}
335349

336350
// MARK: Scroll to bottom

0 commit comments

Comments
 (0)