-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented pinning channels in the demo app
- Loading branch information
1 parent
b4b1d4c
commit b908015
Showing
4 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// | ||
// Copyright © 2023 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import StreamChatSwiftUI | ||
import SwiftUI | ||
|
||
extension ChatChannel { | ||
static func isPinnedBy(keyForUserId userId: UserId) -> String { | ||
"is_pinned_by_\(userId)" | ||
} | ||
|
||
var isPinned: Bool { | ||
guard let userId = membership?.id else { return false } | ||
let key = Self.isPinnedBy(keyForUserId: userId) | ||
return extraData[key]?.boolValue ?? false | ||
} | ||
} | ||
|
||
struct DemoAppChatChannelListItem: View { | ||
|
||
@Injected(\.fonts) private var fonts | ||
@Injected(\.colors) private var colors | ||
@Injected(\.utils) private var utils | ||
@Injected(\.images) private var images | ||
@Injected(\.chatClient) private var chatClient | ||
|
||
var channel: ChatChannel | ||
var channelName: String | ||
var injectedChannelInfo: InjectedChannelInfo? | ||
var avatar: UIImage | ||
var onlineIndicatorShown: Bool | ||
var disabled = false | ||
var onItemTap: (ChatChannel) -> Void | ||
|
||
public var body: some View { | ||
Button { | ||
onItemTap(channel) | ||
} label: { | ||
HStack { | ||
ChannelAvatarView( | ||
avatar: avatar, | ||
showOnlineIndicator: onlineIndicatorShown | ||
) | ||
|
||
VStack(alignment: .leading, spacing: 4) { | ||
HStack { | ||
ChatTitleView(name: channelName) | ||
|
||
Spacer() | ||
|
||
if injectedChannelInfo == nil && channel.unreadCount != .noUnread { | ||
UnreadIndicatorView( | ||
unreadCount: channel.unreadCount.messages | ||
) | ||
} | ||
} | ||
|
||
HStack { | ||
subtitleView | ||
|
||
Spacer() | ||
|
||
HStack(spacing: 4) { | ||
if shouldShowReadEvents { | ||
MessageReadIndicatorView( | ||
readUsers: channel.readUsers( | ||
currentUserId: chatClient.currentUserId, | ||
message: channel.latestMessages.first | ||
), | ||
showReadCount: false | ||
) | ||
} | ||
SubtitleText(text: injectedChannelInfo?.timestamp ?? channel.timestampText) | ||
.accessibilityIdentifier("timestampView") | ||
} | ||
} | ||
} | ||
} | ||
.padding(.all, 8) | ||
} | ||
.foregroundColor(.black) | ||
.disabled(disabled) | ||
.background(channel.isPinned ? Color(colors.pinnedBackground) : .clear) | ||
.id("\(channel.id)-\(channel.isPinned ? "pinned" : "not-pinned")-base") | ||
} | ||
|
||
private var subtitleView: some View { | ||
HStack(spacing: 4) { | ||
if let image = image { | ||
Image(uiImage: image) | ||
.customizable() | ||
.frame(maxHeight: 12) | ||
.foregroundColor(Color(colors.subtitleText)) | ||
} else { | ||
if channel.shouldShowTypingIndicator { | ||
TypingIndicatorView() | ||
} | ||
} | ||
SubtitleText(text: injectedChannelInfo?.subtitle ?? channel.subtitleText) | ||
Spacer() | ||
} | ||
.accessibilityIdentifier("subtitleView") | ||
} | ||
|
||
private var shouldShowReadEvents: Bool { | ||
if let message = channel.latestMessages.first, | ||
message.isSentByCurrentUser, | ||
!message.isDeleted { | ||
return channel.config.readEventsEnabled | ||
} | ||
|
||
return false | ||
} | ||
|
||
private var image: UIImage? { | ||
if channel.isMuted { | ||
return images.muted | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
struct DemoAppChatChannelNavigatableListItem<ChannelDestination: View>: View { | ||
private var channel: ChatChannel | ||
private var channelName: String | ||
private var avatar: UIImage | ||
private var disabled: Bool | ||
private var onlineIndicatorShown: Bool | ||
@Binding private var selectedChannel: ChannelSelectionInfo? | ||
private var channelDestination: (ChannelSelectionInfo) -> ChannelDestination | ||
private var onItemTap: (ChatChannel) -> Void | ||
|
||
init( | ||
channel: ChatChannel, | ||
channelName: String, | ||
avatar: UIImage, | ||
onlineIndicatorShown: Bool, | ||
disabled: Bool = false, | ||
selectedChannel: Binding<ChannelSelectionInfo?>, | ||
channelDestination: @escaping (ChannelSelectionInfo) -> ChannelDestination, | ||
onItemTap: @escaping (ChatChannel) -> Void | ||
) { | ||
self.channel = channel | ||
self.channelName = channelName | ||
self.channelDestination = channelDestination | ||
self.onItemTap = onItemTap | ||
self.avatar = avatar | ||
self.onlineIndicatorShown = onlineIndicatorShown | ||
self.disabled = disabled | ||
_selectedChannel = selectedChannel | ||
} | ||
|
||
public var body: some View { | ||
ZStack { | ||
DemoAppChatChannelListItem( | ||
channel: channel, | ||
channelName: channelName, | ||
injectedChannelInfo: injectedChannelInfo, | ||
avatar: avatar, | ||
onlineIndicatorShown: onlineIndicatorShown, | ||
disabled: disabled, | ||
onItemTap: onItemTap | ||
) | ||
|
||
NavigationLink( | ||
tag: channel.channelSelectionInfo, | ||
selection: $selectedChannel | ||
) { | ||
LazyView(channelDestination(channel.channelSelectionInfo)) | ||
} label: { | ||
EmptyView() | ||
} | ||
} | ||
.id("\(channel.id)-\(channel.isPinned ? "pinned" : "not-pinned")-base") | ||
} | ||
|
||
private var injectedChannelInfo: InjectedChannelInfo? { | ||
selectedChannel?.channel.cid.rawValue == channel.cid.rawValue ? selectedChannel?.injectedChannelInfo : nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters