Skip to content

Commit 422c53a

Browse files
Merged main
2 parents c707fe7 + 4f2f073 commit 422c53a

File tree

9 files changed

+83
-76
lines changed

9 files changed

+83
-76
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,16 @@ struct MessageContainerView<Factory: ViewFactory>: View {
138138
}
139139

140140
if showsAllInfo && !message.isDeleted {
141-
if isInGroup && !message.isSentByCurrentUser {
142-
MessageAuthorAndDateView(message: message)
143-
} else if message.isSentByCurrentUser && channel.config.readEventsEnabled {
141+
if message.isSentByCurrentUser && channel.config.readEventsEnabled {
144142
HStack(spacing: 4) {
145143
factory.makeMessageReadIndicatorView(
146144
channel: channel,
147-
message: message,
148-
showReadCount: isInGroup
145+
message: message
149146
)
150147
MessageDateView(message: message)
151148
}
149+
} else if isInGroup {
150+
MessageAuthorAndDateView(message: message)
152151
} else {
153152
MessageDateView(message: message)
154153
}

Sources/StreamChatSwiftUI/ChatChannel/Utils/ChatChannelExtensions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ extension ChatChannel {
5959
/// Returns users that have read the channel's latest message.
6060
/// - Parameters:
6161
/// - currentUserId: the id of the current user.
62+
/// - message: the current message.
6263
/// - Returns: The list of users that read the channel.
63-
public func readUsers(currentUserId: UserId?) -> [ChatUser] {
64-
guard let message = latestMessages.first else {
64+
public func readUsers(currentUserId: UserId?, message: ChatMessage?) -> [ChatUser] {
65+
guard let message = message else {
6566
return []
6667
}
6768
let readUsers = reads.filter {

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ extension ChatChannel: Identifiable {
9393
}
9494

9595
public var id: String {
96-
"\(cid.id)-\(lastMessageAt ?? createdAt)-\(lastActiveMembersCount)-\(mutedString)-\(unreadCount.messages)-\(typingUsersString)-\(readUsers(currentUserId: nil).count)"
96+
"\(cid.id)-\(lastMessageAt ?? createdAt)-\(lastActiveMembersCount)-\(mutedString)-\(unreadCount.messages)-\(typingUsersString)-\(readUsersId)"
97+
}
98+
99+
private var readUsersId: String {
100+
"\(readUsers(currentUserId: nil, message: latestMessages.first).count)"
97101
}
98102

99103
private var lastActiveMembersCount: Int {

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListItem.swift

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,35 @@ public struct ChatChannelListItem: View {
3333
)
3434

3535
VStack(alignment: .leading, spacing: 4) {
36-
Text(channelName)
37-
.lineLimit(1)
38-
.font(fonts.bodyBold)
39-
.foregroundColor(Color(colors.text))
40-
if let image = image {
41-
HStack(spacing: 4) {
42-
Image(uiImage: image)
43-
.customizable()
44-
.frame(maxHeight: 12)
45-
.foregroundColor(Color(colors.subtitleText))
46-
SubtitleText(text: subtitleText)
47-
Spacer()
48-
}
49-
} else {
50-
HStack(spacing: 4) {
51-
if shouldShowTypingIndicator {
52-
TypingIndicatorView()
53-
}
54-
SubtitleText(text: subtitleText)
55-
Spacer()
56-
}
57-
}
58-
}
59-
60-
Spacer()
61-
62-
VStack(alignment: .trailing, spacing: 4) {
63-
if channel.unreadCount == .noUnread {
36+
HStack {
37+
titleView
38+
6439
Spacer()
65-
} else {
66-
UnreadIndicatorView(
67-
unreadCount: channel.unreadCount.messages
68-
)
40+
41+
if channel.unreadCount != .noUnread {
42+
UnreadIndicatorView(
43+
unreadCount: channel.unreadCount.messages
44+
)
45+
}
6946
}
7047

71-
HStack(spacing: 4) {
72-
if shouldShowReadEvents {
73-
MessageReadIndicatorView(
74-
readUsers: channel.readUsers(
75-
currentUserId: chatClient.currentUserId
76-
),
77-
showReadCount: false
78-
)
48+
HStack {
49+
subtitleView
50+
51+
Spacer()
52+
53+
HStack(spacing: 4) {
54+
if shouldShowReadEvents {
55+
MessageReadIndicatorView(
56+
readUsers: channel.readUsers(
57+
currentUserId: chatClient.currentUserId,
58+
message: channel.latestMessages.first
59+
),
60+
showReadCount: false
61+
)
62+
}
63+
SubtitleText(text: timestampText)
7964
}
80-
SubtitleText(text: timestampText)
8165
}
8266
}
8367
}
@@ -89,20 +73,28 @@ public struct ChatChannelListItem: View {
8973
.id("\(channel.id)-base")
9074
}
9175

92-
private var shouldShowReadEvents: Bool {
93-
if let message = channel.latestMessages.first,
94-
message.isSentByCurrentUser,
95-
!message.isDeleted {
96-
return channel.config.readEventsEnabled
97-
}
98-
99-
return false
76+
private var titleView: some View {
77+
Text(channelName)
78+
.lineLimit(1)
79+
.font(fonts.bodyBold)
80+
.foregroundColor(Color(colors.text))
10081
}
10182

102-
private var shouldShowTypingIndicator: Bool {
103-
!channel.currentlyTypingUsersFiltered(
104-
currentUserId: chatClient.currentUserId
105-
).isEmpty && channel.config.typingEventsEnabled
83+
private var subtitleView: some View {
84+
HStack(spacing: 4) {
85+
if let image = image {
86+
Image(uiImage: image)
87+
.customizable()
88+
.frame(maxHeight: 12)
89+
.foregroundColor(Color(colors.subtitleText))
90+
} else {
91+
if shouldShowTypingIndicator {
92+
TypingIndicatorView()
93+
}
94+
}
95+
SubtitleText(text: subtitleText)
96+
Spacer()
97+
}
10698
}
10799

108100
private var subtitleText: String {
@@ -117,6 +109,22 @@ public struct ChatChannelListItem: View {
117109
}
118110
}
119111

112+
private var shouldShowReadEvents: Bool {
113+
if let message = channel.latestMessages.first,
114+
message.isSentByCurrentUser,
115+
!message.isDeleted {
116+
return channel.config.readEventsEnabled
117+
}
118+
119+
return false
120+
}
121+
122+
private var shouldShowTypingIndicator: Bool {
123+
!channel.currentlyTypingUsersFiltered(
124+
currentUserId: chatClient.currentUserId
125+
).isEmpty && channel.config.typingEventsEnabled
126+
}
127+
120128
private var image: UIImage? {
121129
if channel.isMuted {
122130
return images.muted

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,13 @@ extension ViewFactory {
556556

557557
public func makeMessageReadIndicatorView(
558558
channel: ChatChannel,
559-
message: ChatMessage,
560-
showReadCount: Bool
559+
message: ChatMessage
561560
) -> some View {
562561
let readUsers = channel.readUsers(
563-
currentUserId: chatClient.currentUserId
562+
currentUserId: chatClient.currentUserId,
563+
message: message
564564
)
565+
let showReadCount = channel.memberCount > 2
565566
return MessageReadIndicatorView(
566567
readUsers: readUsers,
567568
showReadCount: showReadCount

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,9 @@ public protocol ViewFactory: AnyObject {
547547
/// - Parameters:
548548
/// - channel: the channel where the message was sent.
549549
/// - message: the sent message.
550-
/// - showReadCount: whether read count should be shown.
551550
/// - Returns: view shown in the message read indicator slot.
552551
func makeMessageReadIndicatorView(
553552
channel: ChatChannel,
554-
message: ChatMessage,
555-
showReadCount: Bool
553+
message: ChatMessage
556554
) -> MessageReadIndicatorViewType
557555
}

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelExtensions_Tests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ChatChannelExtensions_Tests: XCTestCase {
7878
let channel = ChatChannel.mockDMChannel(reads: [read], latestMessages: messages)
7979

8080
// When
81-
let readUsers = channel.readUsers(currentUserId: nil)
81+
let readUsers = channel.readUsers(currentUserId: nil, message: messages[0])
8282

8383
// Then
8484
XCTAssert(readUsers.count == 1)
@@ -90,7 +90,7 @@ class ChatChannelExtensions_Tests: XCTestCase {
9090
let channel = ChatChannel.mockDMChannel(reads: [])
9191

9292
// When
93-
let readUsers = channel.readUsers(currentUserId: nil)
93+
let readUsers = channel.readUsers(currentUserId: nil, message: nil)
9494

9595
// Then
9696
XCTAssert(readUsers.isEmpty)

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,7 @@ class ViewFactory_Tests: XCTestCase {
528528
// When
529529
let view = viewFactory.makeMessageReadIndicatorView(
530530
channel: .mockDMChannel(),
531-
message: .mock(id: .unique, cid: .unique, text: "Test", author: .mock(id: .unique)),
532-
showReadCount: false
531+
message: .mock(id: .unique, cid: .unique, text: "Test", author: .mock(id: .unique))
533532
)
534533

535534
// Then

docusaurus/docs/iOS/swiftui/components/read-indicators.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ In order to implement your own version of the read indicator, you will need to i
1515
```swift
1616
public func makeMessageReadIndicatorView(
1717
channel: ChatChannel,
18-
message: ChatMessage,
19-
showReadCount: Bool
18+
message: ChatMessage
2019
) -> some View {
2120
CustomMessageReadIndicatorView(
2221
channel: ChatChannel,
@@ -25,6 +24,4 @@ public func makeMessageReadIndicatorView(
2524
}
2625
```
2726

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.
27+
In this method, you receive the channel and the message as parameters. You can use the channel to extract the users who have read the message. In order to do this, call the `readUsers(currentUserId:message:)` 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.

0 commit comments

Comments
 (0)