Skip to content

Commit 47868eb

Browse files
Added possibility to apply custom modifiers to the lists
1 parent b01ed7d commit 47868eb

File tree

9 files changed

+86
-1
lines changed

9 files changed

+86
-1
lines changed

DemoAppSwiftUI/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
1313

1414
var chatClient: ChatClient = {
1515
var config = ChatClientConfig(apiKey: .init(apiKeyString))
16-
// config.isLocalStorageEnabled = true
16+
config.isLocalStorageEnabled = true
1717
config.applicationGroupIdentifier = applicationGroupIdentifier
1818

1919
let client = ChatClient(config: config)

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
133133
}
134134
.id(listId)
135135
}
136+
.modifier(factory.makeMessageListModifier())
136137
}
137138
.background(
138139
factory.makeMessageListBackground(

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelHelperViews.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ struct ChatTitleView: View {
7070
}
7171
}
7272

73+
struct EmptyViewModifier: ViewModifier {
74+
75+
public func body(content: Content) -> some View {
76+
content
77+
}
78+
}
79+
7380
extension CGSize {
7481
/// Default size of the avatar used in the channel list.
7582
public static var defaultAvatarSize: CGSize = CGSize(width: 48, height: 48)

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct ChannelsLazyVStack<Factory: ViewFactory>: View {
166166

167167
factory.makeChannelListFooterView()
168168
}
169+
.modifier(factory.makeChannelListModifier())
169170
}
170171
}
171172

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ extension ViewFactory {
162162
)
163163
}
164164

165+
public func makeChannelListModifier() -> some ViewModifier {
166+
EmptyViewModifier()
167+
}
168+
165169
// MARK: messages
166170

167171
public func makeChannelDestination() -> (ChannelSelectionInfo) -> ChatChannelView<Self> {
@@ -196,6 +200,10 @@ extension ViewFactory {
196200
}
197201
}
198202

203+
public func makeMessageListModifier() -> some ViewModifier {
204+
EmptyViewModifier()
205+
}
206+
199207
public func makeMessageAvatarView(for userDisplayInfo: UserDisplayInfo) -> some View {
200208
MessageAvatarView(avatarURL: userDisplayInfo.imageURL)
201209
}

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ public protocol ViewFactory: AnyObject {
168168
channelDestination: @escaping (ChannelSelectionInfo) -> ChannelDestination
169169
) -> ChannelListSearchResultItem
170170

171+
associatedtype ChannelListModifier: ViewModifier
172+
/// Returns a view modifier applied to the channel list.
173+
func makeChannelListModifier() -> ChannelListModifier
174+
171175
// MARK: - messages
172176

173177
associatedtype ChannelDestination: View
@@ -178,6 +182,10 @@ public protocol ViewFactory: AnyObject {
178182
/// Returns a function that creats the message thread destination.
179183
func makeMessageThreadDestination() -> (ChatChannel, ChatMessage) -> MessageThreadDestination
180184

185+
associatedtype MessageListModifier: ViewModifier
186+
/// Returns a view modifier applied to the message list.
187+
func makeMessageListModifier() -> MessageListModifier
188+
181189
associatedtype UserAvatar: View
182190
/// Creates the message avatar view.
183191
/// - Parameter userDisplayInfo: the author's display info.

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,28 @@ class ViewFactory_Tests: StreamChatTestCase {
593593
// Then
594594
XCTAssert(view is EmptyView)
595595
}
596+
597+
func test_viewFactory_makeChannelListModifier() {
598+
// Given
599+
let viewFactory = DefaultViewFactory.shared
600+
601+
// When
602+
let modifier = viewFactory.makeChannelListModifier()
603+
604+
// Then
605+
XCTAssert(modifier is EmptyViewModifier)
606+
}
607+
608+
func test_viewFactory_makeMessageListModifier() {
609+
// Given
610+
let viewFactory = DefaultViewFactory.shared
611+
612+
// When
613+
let modifier = viewFactory.makeMessageListModifier()
614+
615+
// Then
616+
XCTAssert(modifier is EmptyViewModifier)
617+
}
596618
}
597619

598620
extension ChannelAction: Equatable {

docusaurus/docs/iOS/swiftui/components/helper-views.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ var body: some Scene {
179179
}
180180
```
181181

182+
## Applying Custom Modifier
183+
184+
You can customize the channel list further, by applying your own custom view modifier. In order to do this, you need to implement the method `makeChannelListModifier`, which by default doesn't apply anything additional to the view. Here's an example how to add vertical padding to the channel list:
185+
186+
```swift
187+
func makeChannelListModifier() -> some ViewModifier {
188+
VerticalPaddingViewModifier()
189+
}
190+
191+
struct VerticalPaddingViewModifier: ViewModifier {
192+
193+
public func body(content: Content) -> some View {
194+
content
195+
.padding(.vertical, 8)
196+
}
197+
198+
}
199+
```
200+
182201
## Customizing the Leading and Trailing Areas
183202

184203
When the user swipes to the right, the SDK by default shows two buttons - one for deleting the channel and one for showing more options. You can customize this view by implementing the `makeTrailingSwipeActionsView` in the `ViewFactory`. Please note that the width of this view is fixed. It's up to the integrating code how the available width will be filled.

docusaurus/docs/iOS/swiftui/components/message-list.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ let appearance = Appearance(colors: colors)
5555
streamChat = StreamChat(chatClient: chatClient, appearance: appearance)
5656
```
5757

58+
## Applying Custom Modifier
59+
60+
You can customize the message list further, by applying your own custom view modifier. In order to do this, you need to implement the method `makeMessageListModifier`, which by default doesn't apply anything additional to the view. Here's an example how to add vertical padding to the message list:
61+
62+
```swift
63+
func makeMessageListModifier() -> some ViewModifier {
64+
VerticalPaddingViewModifier()
65+
}
66+
67+
struct VerticalPaddingViewModifier: ViewModifier {
68+
69+
public func body(content: Content) -> some View {
70+
content
71+
.padding(.vertical, 8)
72+
}
73+
74+
}
75+
```
76+
5877
## Custom Message Container View
5978

6079
However, if you are building a livestream app similar to Twitch, you will need a different type of user interface for the message views. The SwiftUI SDK allows swapping the message container view with your own implementation, without needing to implement the whole message list, the composer or the reactions. In order to do this, you need to implement the method `makeMessageContainerView` in the `ViewFactory` protocol.

0 commit comments

Comments
 (0)