Skip to content

Commit

Permalink
Working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski committed Dec 6, 2023
1 parent 1cc870f commit e5c2223
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
if channelController.channel?.unreadCount.messages ?? 0 > 0 {
if channelController.firstUnreadMessageId != nil {
firstUnreadMessageId = channelController.firstUnreadMessageId
} else {
canMarkRead = false
} else if channelController.lastReadMessageId != nil {
lastReadMessageId = channelController.lastReadMessageId
canMarkRead = false
}
canMarkRead = false
}
}

Expand Down Expand Up @@ -239,6 +240,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
//TODO: change to data source.
if let firstUnread = self?.channelController.firstUnreadMessageId,
let message = self?.channelController.dataStore.message(id: firstUnread) {
self?.firstUnreadMessageId = message.messageId
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self?.scrolledId = message.messageId
}
Expand Down Expand Up @@ -400,6 +402,11 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
if !showScrollToLatestButton && scrolledId == nil && !loadingNextMessages {
updateScrolledIdToNewestMessage()
}

if lastMessageRead != nil && firstUnreadMessageId == nil {
//TODO: data source
self.firstUnreadMessageId = channelController.firstUnreadMessageId
}
}

func dataSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
@State private var keyboardShown = false
@State private var pendingKeyboardUpdate: Bool?
@State private var scrollDirection = ScrollDirection.up
@State private var unreadMessagesBannerShown = false

private var messageRenderingUtil = MessageRenderingUtil.shared
private var skipRenderingMessageIds = [String]()
Expand Down Expand Up @@ -176,6 +177,12 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
newMessagesStartId: $firstUnreadMessageId,
count: newMessagesCount(for: index, message: message)
)
.onAppear {
unreadMessagesBannerShown = true
}
.onDisappear {
unreadMessagesBannerShown = false
}
: nil

showsLastInGroupInfo ?
Expand Down Expand Up @@ -282,7 +289,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
}
})
.overlay(
channel.unreadCount.messages > 0 ? VStack {
(channel.unreadCount.messages > 0 && !unreadMessagesBannerShown) ? VStack {
JumpToUnreadButton(unreadCount: channel.unreadCount.messages) {
_ = onJumpToMessage?(firstUnreadMessageId ?? "unknown")
} onClose: {
Expand Down

0 comments on commit e5c2223

Please sign in to comment.