Skip to content

Commit 41ff6a0

Browse files
Fix marked read while the app is in the background
1 parent 833a1db commit 41ff6a0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6-
### 🔄 Changed
6+
### 🐞 Fixed
7+
- Fix marked read while the app is in the background
78

89
# [4.41.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.41.0)
910
_November 03, 2023_

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
139139
object: nil
140140
)
141141

142+
NotificationCenter.default.addObserver(
143+
self,
144+
selector: #selector(applicationWillEnterForeground),
145+
name: UIApplication.willEnterForegroundNotification,
146+
object: nil
147+
)
148+
142149
if messageController == nil {
143150
NotificationCenter.default.addObserver(
144151
self,
@@ -166,6 +173,12 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
166173
messageCachingUtils.clearCache()
167174
}
168175

176+
@objc
177+
private func applicationWillEnterForeground() {
178+
guard let first = messages.first else { return }
179+
maybeSendReadEvent(for: first)
180+
}
181+
169182
public func scrollToLastMessage() {
170183
if scrolledId != nil {
171184
scrolledId = nil
@@ -184,7 +197,10 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
184197
save(lastDate: message.createdAt)
185198
}
186199
if index == 0 {
187-
maybeSendReadEvent(for: message)
200+
let isActive = UIApplication.shared.applicationState == .active
201+
if isActive {
202+
maybeSendReadEvent(for: message)
203+
}
188204
}
189205
}
190206

0 commit comments

Comments
 (0)