Skip to content

Commit

Permalink
Release fix: Truncation related fixes for mark as/jump to unread (#2806)
Browse files Browse the repository at this point in the history
* Reset read for truncated messages

* Allow automatic marking as read when discarding unread messages

* Make sure we can mark as read if the last message was seen or is currently on screen
  • Loading branch information
polqf committed Sep 26, 2023
1 parent f20a028 commit 1eee3de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
updater.markRead(cid: channel.cid, userId: currentUserId) { error in
self.callback {
self.markingRead = false
self.isMarkedAsUnread = false
completion?(error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ struct ChannelTruncatedEventMiddleware: EventMiddleware {
}

do {
if let channelDTO = session.channel(cid: truncatedEvent.channel.cid) {
channelDTO.truncatedAt = truncatedEvent.channel.truncatedAt?.bridgeDate
} else {
throw ClientError.ChannelDoesNotExist(cid: truncatedEvent.channel.cid)
let cid = truncatedEvent.channel.cid
guard let channelDTO = session.channel(cid: cid) else {
throw ClientError.ChannelDoesNotExist(cid: cid)
}

channelDTO.truncatedAt = truncatedEvent.channel.truncatedAt?.bridgeDate

// Until BE returns valid values for user's read after truncation, we are clearing them.
if let userId = truncatedEvent.user?.id, let read = session.loadChannelRead(cid: cid, userId: userId) {
read.lastReadMessageId = nil
read.lastReadAt = truncatedEvent.channel.truncatedAt?.bridgeDate ?? DBDate()
read.unreadMessageCount = 0
}
} catch {
log.error("Failed to write the `truncatedAt` field update in the database, error: \(error)")
Expand Down
8 changes: 6 additions & 2 deletions Sources/StreamChatUI/ChatChannel/ChatChannelVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ open class ChatChannelVC: _ViewController,
return false
}

return hasSeenLastMessage && hasSeenFirstUnreadMessage && channelController.hasLoadedAllNextMessages && !hasMarkedMessageAsUnread
return isLastMessageVisibleOrSeen && hasSeenFirstUnreadMessage && channelController.hasLoadedAllNextMessages && !hasMarkedMessageAsUnread
}

private var isLastMessageVisibleOrSeen: Bool {
hasSeenLastMessage || isLastMessageFullyVisible
}

/// A component responsible to handle when to load new or old messages.
Expand All @@ -106,7 +110,7 @@ open class ChatChannelVC: _ViewController,
/// Determines whether first unread message has been seen
private var hasSeenFirstUnreadMessage: Bool = false

/// Determines whether last cell has been seen
/// Determines whether last cell has been seen since the last time it was marked as read
private var hasSeenLastMessage: Bool = false

/// The id of the first unread message
Expand Down
2 changes: 0 additions & 2 deletions Sources/StreamChatUI/ChatMessageList/ChatMessageListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,6 @@ private extension ChatMessageListVC {
self?.adjustContentOffset(oldContentOffset: oldContentOffset, oldContentSize: oldContentSize)
}

self?.updateScrollDependentButtonsVisibility()

UIView.performWithoutAnimation {
self?.scrollToBottomIfNeeded(with: changes, newestChange: newestChange)
self?.reloadMovedMessage(newestChange: newestChange)
Expand Down

0 comments on commit 1eee3de

Please sign in to comment.