Skip to content

Commit d615438

Browse files
Added WhatsApp style header example
1 parent 6e55f53 commit d615438

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Copyright © 2023 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import StreamChat
6+
import StreamChatSwiftUI
7+
import SwiftUI
8+
9+
import SwiftUI
10+
11+
struct WhatsAppChannelHeaderModifier: ChatChannelHeaderViewModifier {
12+
13+
let channel: ChatChannel
14+
15+
func body(content: Content) -> some View {
16+
content.toolbar {
17+
WhatsAppChannelHeader(channel: channel)
18+
}
19+
}
20+
}
21+
22+
struct WhatsAppChannelHeader: ToolbarContent {
23+
24+
@ObservedObject private var channelHeaderLoader = InjectedValues[\.utils].channelHeaderLoader
25+
26+
@Injected(\.chatClient) var chatClient
27+
@Injected(\.utils) var utils
28+
@Injected(\.fonts) var fonts
29+
@Injected(\.colors) var colors
30+
31+
var channel: ChatChannel
32+
33+
private var currentUserId: String {
34+
chatClient.currentUserId ?? ""
35+
}
36+
37+
private var channelNamer: ChatChannelNamer {
38+
utils.channelNamer
39+
}
40+
41+
private var channelSubtitle: String {
42+
if channel.memberCount <= 2 {
43+
return channel.onlineInfoText(currentUserId: currentUserId)
44+
} else {
45+
return channel
46+
.lastActiveMembers
47+
.map { $0.name ?? $0.id }
48+
.joined(separator: ", ")
49+
}
50+
}
51+
52+
var body: some ToolbarContent {
53+
ToolbarItem(placement: .principal) {
54+
HStack {
55+
ChannelAvatarView(
56+
avatar: channelHeaderLoader.image(for: channel),
57+
showOnlineIndicator: false,
58+
size: CGSize(width: 36, height: 36)
59+
)
60+
VStack(alignment: .leading) {
61+
Text(channelNamer(channel, currentUserId) ?? "")
62+
.font(fonts.bodyBold)
63+
Text(channelSubtitle)
64+
.font(fonts.caption1)
65+
.foregroundColor(Color(colors.textLowEmphasis))
66+
}
67+
}
68+
}
69+
ToolbarItem(placement: .topBarTrailing) {
70+
HStack {
71+
Button(action: {
72+
print("tapped on video")
73+
}, label: {
74+
Image(systemName: "video")
75+
})
76+
Button(action: {
77+
print("tapped on audio")
78+
}, label: {
79+
Image(systemName: "phone")
80+
})
81+
}
82+
}
83+
}
84+
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
84507C98281AC40F0081DDC2 /* AddUsersViewModel_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84507C97281AC40F0081DDC2 /* AddUsersViewModel_Tests.swift */; };
167167
84507C9A281ACCD70081DDC2 /* AddUsersView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84507C99281ACCD70081DDC2 /* AddUsersView_Tests.swift */; };
168168
8451617E2AE7B093000A9230 /* AppleMessageComposerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8451617D2AE7B093000A9230 /* AppleMessageComposerView.swift */; };
169+
845161802AE7C4E2000A9230 /* WhatsAppChannelHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8451617F2AE7C4E2000A9230 /* WhatsAppChannelHeader.swift */; };
169170
8463D9262836617F002B1894 /* ChannelListPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8463D9252836617F002B1894 /* ChannelListPage.swift */; };
170171
8465FBBE2746873A00AF091E /* StreamChatSwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8465FBB52746873A00AF091E /* StreamChatSwiftUI.framework */; };
171172
8465FCBF27468B6900AF091E /* DemoAppSwiftUIApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8465FCBE27468B6900AF091E /* DemoAppSwiftUIApp.swift */; };
@@ -669,6 +670,7 @@
669670
84507C97281AC40F0081DDC2 /* AddUsersViewModel_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddUsersViewModel_Tests.swift; sourceTree = "<group>"; };
670671
84507C99281ACCD70081DDC2 /* AddUsersView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddUsersView_Tests.swift; sourceTree = "<group>"; };
671672
8451617D2AE7B093000A9230 /* AppleMessageComposerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleMessageComposerView.swift; sourceTree = "<group>"; };
673+
8451617F2AE7C4E2000A9230 /* WhatsAppChannelHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WhatsAppChannelHeader.swift; sourceTree = "<group>"; };
672674
8463D9252836617F002B1894 /* ChannelListPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelListPage.swift; sourceTree = "<group>"; };
673675
8465FBB52746873A00AF091E /* StreamChatSwiftUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = StreamChatSwiftUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
674676
8465FBBD2746873A00AF091E /* StreamChatSwiftUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StreamChatSwiftUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -1420,6 +1422,7 @@
14201422
84335013274BAB15007A1B81 /* ViewFactoryExamples.swift */,
14211423
84FF723E2782FB2E006E26C8 /* iMessagePocView.swift */,
14221424
8451617D2AE7B093000A9230 /* AppleMessageComposerView.swift */,
1425+
8451617F2AE7C4E2000A9230 /* WhatsAppChannelHeader.swift */,
14231426
8417AE912ADEDB6400445021 /* UserRepository.swift */,
14241427
84EDBC36274FE5CD0057218D /* Localizable.strings */,
14251428
8465FCCA27468B7500AF091E /* Info.plist */,
@@ -2670,6 +2673,7 @@
26702673
84335016274BABF3007A1B81 /* NewChatView.swift in Sources */,
26712674
84335018274BAD4B007A1B81 /* NewChatViewModel.swift in Sources */,
26722675
84B288CD274C544B00DD090B /* CreateGroupView.swift in Sources */,
2676+
845161802AE7C4E2000A9230 /* WhatsAppChannelHeader.swift in Sources */,
26732677
8465FCDC274694D200AF091E /* SceneDelegate.swift in Sources */,
26742678
8465FCD9274694D200AF091E /* LaunchAnimationState.swift in Sources */,
26752679
84B288D1274CEDD000DD090B /* GroupNameView.swift in Sources */,

0 commit comments

Comments
 (0)