Skip to content

Commit ec6d25a

Browse files
Dismissing keyboard only when scrolling down
1 parent 4d1e40e commit ec6d25a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
135135
}
136136

137137
public func scrollToLastMessage() {
138-
if scrolledId != messages.first?.messageId {
139-
scrolledId = messages.first?.messageId
138+
if scrolledId != nil {
139+
scrolledId = nil
140140
}
141+
scrolledId = messages.first?.messageId
141142
}
142143

143144
public func handleMessageAppear(index: Int) {

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
150150
.onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { value in
151151
DispatchQueue.main.async {
152152
let offsetValue = value ?? 0
153+
let diff = offsetValue - utils.messageCachingUtils.scrollOffset
154+
utils.messageCachingUtils.scrollOffset = offsetValue
153155
let scrollButtonShown = offsetValue < -20
154156
if scrollButtonShown != showScrollToLatestButton {
155157
showScrollToLatestButton = scrollButtonShown
156158
}
157-
if keyboardShown {
159+
if keyboardShown && diff < -20 {
158160
keyboardShown = false
159161
resignFirstResponder()
160162
}

Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import Foundation
66
import StreamChat
7+
import UIKit
78

89
/// Caches messages related data to avoid accessing the database.
910
/// Cleared on chat channel view dismiss or memory warning.
@@ -15,6 +16,8 @@ class MessageCachingUtils {
1516
private var checkedMessageIds = Set<String>()
1617
private var quotedMessageMapping = [String: ChatMessage]()
1718

19+
var scrollOffset: CGFloat = 0
20+
1821
func authorId(for message: ChatMessage) -> String {
1922
if let userDisplayInfo = userDisplayInfo(for: message) {
2023
return userDisplayInfo.id
@@ -72,6 +75,7 @@ class MessageCachingUtils {
7275

7376
func clearCache() {
7477
log.debug("Clearing cached message data")
78+
scrollOffset = 0
7579
messageAuthorMapping = [String: String]()
7680
messageAuthors = [String: UserDisplayInfo]()
7781
messageAttachments = [String: Bool]()

0 commit comments

Comments
 (0)