Skip to content

Commit 7ad5ba6

Browse files
Fixed PR remarks and bugs
1 parent 8a66431 commit 7ad5ba6

File tree

9 files changed

+75
-68
lines changed

9 files changed

+75
-68
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 {
141+
if message.isSentByCurrentUser {
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: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,55 +33,37 @@ 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 !channel.currentlyTypingUsersFiltered(
52-
currentUserId: chatClient.currentUserId
53-
).isEmpty {
54-
TypingIndicatorView()
55-
}
56-
SubtitleText(text: subtitleText)
57-
Spacer()
58-
}
59-
}
60-
}
61-
62-
Spacer()
63-
64-
VStack(alignment: .trailing, spacing: 4) {
65-
if channel.unreadCount == .noUnread {
36+
HStack {
37+
titleView
38+
6639
Spacer()
67-
} else {
68-
UnreadIndicatorView(
69-
unreadCount: channel.unreadCount.messages
70-
)
40+
41+
if channel.unreadCount != .noUnread {
42+
UnreadIndicatorView(
43+
unreadCount: channel.unreadCount.messages
44+
)
45+
}
7146
}
7247

73-
HStack(spacing: 4) {
74-
if let message = channel.latestMessages.first,
75-
message.isSentByCurrentUser,
76-
!message.isDeleted {
77-
MessageReadIndicatorView(
78-
readUsers: channel.readUsers(
79-
currentUserId: chatClient.currentUserId
80-
),
81-
showReadCount: false
82-
)
48+
HStack {
49+
subtitleView
50+
51+
Spacer()
52+
53+
HStack(spacing: 4) {
54+
if let message = channel.latestMessages.first,
55+
message.isSentByCurrentUser,
56+
!message.isDeleted {
57+
MessageReadIndicatorView(
58+
readUsers: channel.readUsers(
59+
currentUserId: chatClient.currentUserId,
60+
message: channel.latestMessages.first
61+
),
62+
showReadCount: false
63+
)
64+
}
65+
SubtitleText(text: timestampText)
8366
}
84-
SubtitleText(text: timestampText)
8567
}
8668
}
8769
}
@@ -93,6 +75,32 @@ public struct ChatChannelListItem: View {
9375
.id("\(channel.id)-base")
9476
}
9577

78+
private var titleView: some View {
79+
Text(channelName)
80+
.lineLimit(1)
81+
.font(fonts.bodyBold)
82+
.foregroundColor(Color(colors.text))
83+
}
84+
85+
private var subtitleView: some View {
86+
HStack(spacing: 4) {
87+
if let image = image {
88+
Image(uiImage: image)
89+
.customizable()
90+
.frame(maxHeight: 12)
91+
.foregroundColor(Color(colors.subtitleText))
92+
} else {
93+
if !channel.currentlyTypingUsersFiltered(
94+
currentUserId: chatClient.currentUserId
95+
).isEmpty {
96+
TypingIndicatorView()
97+
}
98+
}
99+
SubtitleText(text: subtitleText)
100+
Spacer()
101+
}
102+
}
103+
96104
private var subtitleText: String {
97105
if channel.isMuted {
98106
return L10n.Channel.Item.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)