Skip to content

Commit

Permalink
Reload messages when author name or profile picture changes
Browse files Browse the repository at this point in the history
  • Loading branch information
laevandus committed Jun 12, 2024
1 parent 71a9e11 commit dc141a9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
### 🐞 Fixed
- Reload messages when author name or profile picture changes [#514](https://github.com/GetStream/stream-chat-swiftui/pull/514)

# [4.57.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.57.0)
_June 07, 2024_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
if !isActive {
return
}
// Message data can change with every callback
utils.messageCachingUtils.clearCache()

let animationState = shouldAnimate(changes: changes)
if animationState == .animated {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public class DefaultMessageIdBuilder: MessageIdBuilder {
if message.textUpdatedAt != nil {
statesId = "edited"
}
return message.baseId + statesId + message.reactionScoresId
+ message.repliesCountId + "\(message.updatedAt)" + message.pinStateId
let authorDisplayInfo = message.authorDisplayInfo
let author = authorDisplayInfo.name + String(authorDisplayInfo.imageURL?.hashValue ?? 0)

return message.baseId +
statesId +
message.reactionScoresId +
message.repliesCountId +
"\(message.updatedAt)" +
message.pinStateId +
author
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,47 @@ class ChatChannelViewModel_Tests: StreamChatTestCase {
// Then
XCTAssert(shouldJump == false)
}

func test_chatChannelVM_messageAuthorChanged() {
// Given
let channelId = ChannelId.unique
let channelController = makeChannelController(messages: [
ChatMessage.mock(
id: "1",
cid: channelId,
text: "A",
author: .mock(id: "a1", name: "Name")
)
])
let viewModel = ChatChannelViewModel(channelController: channelController)
let initialMesssageIds = viewModel.messages.map(\.messageId)

// When
channelController.messages_mock = [
ChatMessage.mock(
id: "1",
cid: channelId,
text: "A",
author: .mock(id: "a1", name: "Name Updated")
)
]
let changes = channelController.messages
.enumerated()
.map {
ListChange.update(
$0.element,
index: IndexPath(item: $0.offset, section: 0)
)
}
viewModel.dataSource(
channelDataSource: ChatChannelDataSource(controller: channelController),
didUpdateMessages: channelController.messages,
changes: changes
)
// Then
let updatedMessageIds = viewModel.messages.map(\.messageId)
XCTAssertNotEqual(initialMesssageIds, updatedMessageIds, "Message id should not be cached when author changes")
}

// MARK: - private

Expand Down

0 comments on commit dc141a9

Please sign in to comment.