Skip to content

Commit adc9c61

Browse files
committed
Add ChatThreadListHeaderViewModifier implementation
1 parent d9c0676 commit adc9c61

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Copyright © 2024 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import SwiftUI
6+
7+
struct ChatThreadListHeaderViewModifier: ViewModifier {
8+
@Injected(\.fonts) private var fonts
9+
10+
let title: String
11+
12+
func body(content: Content) -> some View {
13+
content
14+
.navigationBarTitleDisplayMode(.inline)
15+
.toolbar {
16+
ToolbarItem(placement: .principal) {
17+
Text(title)
18+
.font(fonts.bodyBold)
19+
}
20+
}
21+
}
22+
}

Sources/StreamChatSwiftUI/ChatThreadList/ChatThreadListView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct ChatThreadListView<Factory: ViewFactory>: View {
2525
/// - viewFactory: The view factory used for creating views used by the thread list.
2626
/// - viewModel: The view model instance providing the data. Default view model is created if nil.
2727
/// - threadListController: The thread list controller managing the list of threads used as a data source for the view model. Default controller is created if nil.
28-
/// - title: A title used as the navigation bar title.
28+
/// - title: A custom title used as the navigation bar title.
2929
/// - onItemTap: A closure for handling a tap on the thread item. Default closure updates the ``ChatThreadListViewModel/selectedThread`` property in the view model.
3030
/// - handleTabBarVisibility: True, if TabBar visibility should be automatically updated.
3131
/// - embedInNavigationView: True, if the thread list view should be embedded in a navigation stack.
@@ -41,7 +41,7 @@ public struct ChatThreadListView<Factory: ViewFactory>: View {
4141
viewFactory: Factory = DefaultViewFactory.shared,
4242
viewModel: ChatThreadListViewModel? = nil,
4343
threadListController: ChatThreadListController? = nil,
44-
title: String = "Threads",
44+
title: String? = nil,
4545
onItemTap: ((ChatThread) -> Void)? = nil,
4646
handleTabBarVisibility: Bool = true,
4747
embedInNavigationView: Bool = true
@@ -52,7 +52,7 @@ public struct ChatThreadListView<Factory: ViewFactory>: View {
5252
)
5353
)
5454
self.viewFactory = viewFactory
55-
self.title = title
55+
self.title = title ?? L10n.Thread.title
5656
self.handleTabBarVisibility = handleTabBarVisibility
5757
self.embedInNavigationView = embedInNavigationView
5858
customOnItemTap = onItemTap
@@ -72,6 +72,7 @@ public struct ChatThreadListView<Factory: ViewFactory>: View {
7272
)
7373
}
7474
}
75+
.modifier(viewFactory.makeThreadListHeaderViewModifier(title: title))
7576
.onAppear {
7677
if !viewModel.hasLoadedThreads {
7778
viewModel.loadThreads()

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ extension ViewFactory {
999999
ChatThreadListLoadingView()
10001000
}
10011001

1002+
public func makeThreadListHeaderViewModifier(title: String) -> some ViewModifier {
1003+
ChatThreadListHeaderViewModifier(title: title)
1004+
}
1005+
10021006
public func makeThreadListModifier() -> some ViewModifier {
10031007
EmptyViewModifier()
10041008
}

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,11 @@ public protocol ViewFactory: AnyObject {
10101010
/// Creates a loading view for the thread list.
10111011
func makeThreadListLoadingView() -> ThreadListLoadingView
10121012

1013+
associatedtype ThreadListHeaderViewModifier: ViewModifier
1014+
/// Creates the thread list header view modifier.
1015+
/// - Parameter title: the title displayed in the header.
1016+
func makeThreadListHeaderViewModifier(title: String) -> ThreadListHeaderViewModifier
1017+
10131018
associatedtype ThreadListModifier: ViewModifier
10141019
/// Returns a view modifier applied to the thread list.
10151020
func makeThreadListModifier() -> ThreadListModifier

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@
514514
AD3AB65A2CB59A660014D4D7 /* MessagePreviewFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB6592CB59A660014D4D7 /* MessagePreviewFormatter.swift */; };
515515
AD3AB65C2CB730090014D4D7 /* Shimmer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65B2CB730090014D4D7 /* Shimmer.swift */; };
516516
AD3AB65E2CB731360014D4D7 /* ChatThreadListLoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65D2CB731360014D4D7 /* ChatThreadListLoadingView.swift */; };
517+
AD3AB6602CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65F2CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift */; };
517518
C14A465B284665B100EF498E /* SDKIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = C14A465A284665B100EF498E /* SDKIdentifier.swift */; };
518519
E3A1C01C282BAC66002D1E26 /* Sentry in Frameworks */ = {isa = PBXBuildFile; productRef = E3A1C01B282BAC66002D1E26 /* Sentry */; };
519520
/* End PBXBuildFile section */
@@ -1097,6 +1098,7 @@
10971098
AD3AB6592CB59A660014D4D7 /* MessagePreviewFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagePreviewFormatter.swift; sourceTree = "<group>"; };
10981099
AD3AB65B2CB730090014D4D7 /* Shimmer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shimmer.swift; sourceTree = "<group>"; };
10991100
AD3AB65D2CB731360014D4D7 /* ChatThreadListLoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListLoadingView.swift; sourceTree = "<group>"; };
1101+
AD3AB65F2CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListHeaderViewModifier.swift; sourceTree = "<group>"; };
11001102
C14A465A284665B100EF498E /* SDKIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SDKIdentifier.swift; sourceTree = "<group>"; };
11011103
/* End PBXFileReference section */
11021104

@@ -2237,6 +2239,7 @@
22372239
AD3AB6552CB54F720014D4D7 /* ChatThreadListNavigatableItem.swift */,
22382240
AD3AB6532CB54F310014D4D7 /* ChatThreadListItem.swift */,
22392241
AD3AB65D2CB731360014D4D7 /* ChatThreadListLoadingView.swift */,
2242+
AD3AB65F2CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift */,
22402243
AD2DDA602CB040EA0040B8D4 /* NoThreadsView.swift */,
22412244
);
22422245
path = ChatThreadList;
@@ -2602,6 +2605,7 @@
26022605
84289BEB2807239B00282ABE /* MediaAttachmentsViewModel.swift in Sources */,
26032606
8465FD902746A95700AF091E /* ComposerHelperViews.swift in Sources */,
26042607
82D64C142AD7E5B700C5C79E /* ImageDecoderRegistry.swift in Sources */,
2608+
AD3AB6602CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift in Sources */,
26052609
82D64C182AD7E5B700C5C79E /* ImageCache.swift in Sources */,
26062610
849CDD942768E0E1003C7A51 /* MessageActionsResolver.swift in Sources */,
26072611
84EADEB72B28A17B0046B50C /* RecordingConstants.swift in Sources */,

0 commit comments

Comments
 (0)