Skip to content

Commit 903f5bf

Browse files
implemented typing indicators for channels
1 parent 0ab1865 commit 903f5bf

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

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

9595
public var id: String {
96-
"\(cid.id)-\(lastMessageAt ?? createdAt)-\(lastActiveMembersCount)-\(mutedString)-\(unreadCount.messages)"
96+
"\(cid.id)-\(lastMessageAt ?? createdAt)-\(lastActiveMembersCount)-\(mutedString)-\(unreadCount.messages)-\(typingUsersString)"
9797
}
9898

9999
public var lastActiveMembersCount: Int {
@@ -102,4 +102,11 @@ extension ChatChannel: Identifiable {
102102
}
103103
.count
104104
}
105+
106+
public var typingUsersString: String {
107+
currentlyTypingUsers.map { user in
108+
user.id
109+
}
110+
.joined(separator: "-")
111+
}
105112
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListItem.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public struct ChatChannelListItem: View {
7575
private var subtitleText: String {
7676
if channel.isMuted {
7777
return L10n.Channel.Item.muted
78+
} else if !channel.currentlyTypingUsers.isEmpty {
79+
return typingIndicatorString(for: Array(channel.currentlyTypingUsers))
7880
} else if let latestMessage = channel.latestMessages.first {
7981
return "\(latestMessage.author.name ?? latestMessage.author.id): \(latestMessage.textContent ?? latestMessage.text)"
8082
} else {
@@ -96,6 +98,15 @@ public struct ChatChannelListItem: View {
9698
return ""
9799
}
98100
}
101+
102+
private func typingIndicatorString(for typingUsers: [ChatUser]) -> String {
103+
if let user = typingUsers.first(where: { user in user.name != nil }), let name = user.name {
104+
return L10n.MessageList.TypingIndicator.users(name, typingUsers.count - 1)
105+
} else {
106+
// If we somehow cannot fetch any user name, we simply show that `Someone is typing`
107+
return L10n.MessageList.TypingIndicator.typingUnknown
108+
}
109+
}
99110
}
100111

101112
/// View for the avatar used in channels (includes online indicator overlay).

0 commit comments

Comments
 (0)