Skip to content

Commit 6364a23

Browse files
committed
Add mark thread read logic to ChatChannelViewModel
1 parent 79f2afe commit 6364a23

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
238238
if canMarkRead {
239239
sendReadEventIfNeeded(for: first)
240240
}
241+
if shouldMarkThreadRead {
242+
sendThreadReadEvent()
243+
}
241244
}
242245

243246
public func scrollToLastMessage() {
@@ -346,6 +349,9 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
346349
sendReadEventIfNeeded(for: message)
347350
}
348351
}
352+
if index == 0 && shouldMarkThreadRead {
353+
sendThreadReadEvent()
354+
}
349355
}
350356

351357
open func groupMessages() {
@@ -655,7 +661,24 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
655661
}
656662
}
657663
}
658-
664+
665+
private var shouldMarkThreadRead: Bool {
666+
guard UIApplication.shared.applicationState == .active else {
667+
return false
668+
}
669+
guard messageController?.replies.isEmpty == false else {
670+
return false
671+
}
672+
673+
return channelDataSource.hasLoadedAllNextMessages
674+
}
675+
676+
private func sendThreadReadEvent() {
677+
throttler.throttle { [weak self] in
678+
self?.messageController?.markThreadRead()
679+
}
680+
}
681+
659682
private func handleDateChange() {
660683
guard showScrollToLatestButton == true, let currentDate = currentDate else {
661684
currentDateString = nil

0 commit comments

Comments
 (0)