Skip to content

Commit 8a66431

Browse files
Added docs for read indicators
1 parent b2541cf commit 8a66431

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ struct MessageContainerView<Factory: ViewFactory>: View {
143143
} else if message.isSentByCurrentUser {
144144
HStack(spacing: 4) {
145145
factory.makeMessageReadIndicatorView(
146-
readUsers: channel.readUsers(
147-
currentUserId: chatClient.currentUserId
148-
),
146+
channel: channel,
147+
message: message,
149148
showReadCount: isInGroup
150149
)
151150
MessageDateView(message: message)

Sources/StreamChatSwiftUI/ChatChannel/Utils/ChatChannelExtensions.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import StreamChat
77

88
extension ChatChannel {
99

10+
/// Returns the online info text for a channel.
11+
/// - Parameters:
12+
/// - currentUserId: the id of the current user.
13+
/// - Returns: the online info text string.
1014
func onlineInfoText(currentUserId: String) -> String {
1115
if isDirectMessageChannel {
1216
guard let member = lastActiveMembers
@@ -28,12 +32,20 @@ extension ChatChannel {
2832
return L10n.Message.Title.group(memberCount, watcherCount)
2933
}
3034

35+
/// Returns the currently typing users, without the current user.
36+
/// - Parameters:
37+
/// - currentUserId: the id of the current user.
38+
/// - Returns: Array of users that are currently typing.
3139
func currentlyTypingUsersFiltered(currentUserId: UserId?) -> [ChatUser] {
3240
currentlyTypingUsers.filter { user in
3341
user.id != currentUserId
3442
}
3543
}
3644

45+
/// Returns the typing indicator string.
46+
/// - Parameters:
47+
/// - currentUserId: the id of the current user.
48+
/// - Returns: the typing indicator string.
3749
func typingIndicatorString(currentUserId: UserId?) -> String {
3850
let typingUsers = currentlyTypingUsersFiltered(currentUserId: currentUserId)
3951
if let user = typingUsers.first(where: { user in user.name != nil }), let name = user.name {
@@ -44,7 +56,11 @@ extension ChatChannel {
4456
}
4557
}
4658

47-
func readUsers(currentUserId: UserId?) -> [ChatUser] {
59+
/// Returns users that have read the channel's latest message.
60+
/// - Parameters:
61+
/// - currentUserId: the id of the current user.
62+
/// - Returns: The list of users that read the channel.
63+
public func readUsers(currentUserId: UserId?) -> [ChatUser] {
4864
guard let message = latestMessages.first else {
4965
return []
5066
}

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,14 @@ extension ViewFactory {
555555
}
556556

557557
public func makeMessageReadIndicatorView(
558-
readUsers: [ChatUser],
558+
channel: ChatChannel,
559+
message: ChatMessage,
559560
showReadCount: Bool
560561
) -> some View {
561-
MessageReadIndicatorView(
562+
let readUsers = channel.readUsers(
563+
currentUserId: chatClient.currentUserId
564+
)
565+
return MessageReadIndicatorView(
562566
readUsers: readUsers,
563567
showReadCount: showReadCount
564568
)

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,13 @@ public protocol ViewFactory: AnyObject {
545545
associatedtype MessageReadIndicatorViewType: View
546546
/// Creates the message read indicator view.
547547
/// - Parameters:
548-
/// - readUsers: list of users who read the message.
548+
/// - channel: the channel where the message was sent.
549+
/// - message: the sent message.
549550
/// - showReadCount: whether read count should be shown.
550551
/// - Returns: view shown in the message read indicator slot.
551552
func makeMessageReadIndicatorView(
552-
readUsers: [ChatUser],
553+
channel: ChatChannel,
554+
message: ChatMessage,
553555
showReadCount: Bool
554556
) -> MessageReadIndicatorViewType
555557
}

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ class ViewFactory_Tests: XCTestCase {
527527

528528
// When
529529
let view = viewFactory.makeMessageReadIndicatorView(
530-
readUsers: [],
530+
channel: .mockDMChannel(),
531+
message: .mock(id: .unique, cid: .unique, text: "Test", author: .mock(id: .unique)),
531532
showReadCount: false
532533
)
533534

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Read Indicators
3+
---
4+
5+
## Read Indicators Overview
6+
7+
The SwiftUI SDK supports read indicators, which indicate whether a message was read by the other channel participants.
8+
9+
The read indicators are available in both the channel and the message list. The default implementation shows one gray checkmark if a message was sent, but not read by anyone and two blue (depending on tint color) checkmarks for a read message. If a message is sent in a group, the number of readers of the message is also shown.
10+
11+
It is possible to customize the read indicator in the message list. For example, some messaging apps show small icons of the users who have read the message.
12+
13+
In order to implement your own version of the read indicator, you will need to implement the `makeMessageReadIndicatorView` in the `ViewFactory` protocol.
14+
15+
```swift
16+
public func makeMessageReadIndicatorView(
17+
channel: ChatChannel,
18+
message: ChatMessage,
19+
showReadCount: Bool
20+
) -> some View {
21+
CustomMessageReadIndicatorView(
22+
channel: ChatChannel,
23+
message: ChatMessage
24+
)
25+
}
26+
```
27+
28+
In this method, you receive the channel and the message as parameters. Additionally, you receive the `showReadCount`, which you can ignore in case you don't want to support conditional display of the number of users that read a message.
29+
30+
You can use the channel to extract the users who have read the message. In order to do this, call the `readUsers(currentUserId:)` method of the channel. If you need more information about the reads (e.g. last read date), you can access the `reads` property of the channel.

docusaurus/sidebars-ios.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"swiftui/components/message-threads",
6868
"swiftui/components/inline-replies",
6969
"swiftui/components/composer-commands",
70-
"swiftui/components/typing-indicators"
70+
"swiftui/components/typing-indicators",
71+
"swiftui/components/read-indicators"
7172
]
7273
}
7374
]

0 commit comments

Comments
 (0)