Skip to content

Commit b8fb1b1

Browse files
Added support for gradient background in the message bubbles
1 parent 6e43654 commit b8fb1b1

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
172172
if let message = messageController?.message {
173173
var array = Array(messages)
174174
array.append(message)
175-
self.messages = LazyCachedMapCollection(source: array, map: { $0 })
175+
withAnimation {
176+
self.messages = LazyCachedMapCollection(source: array, map: { $0 })
177+
}
176178
} else {
177-
self.messages = messages
179+
withAnimation {
180+
self.messages = messages
181+
}
178182
}
179183

180184
maybeRefreshMessageList()

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageBubble.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct MessageBubbleModifier: ViewModifier {
2020
.modifier(
2121
BubbleModifier(
2222
corners: corners,
23-
backgroundColor: Color(backgroundColor),
23+
backgroundColors: background,
2424
cornerRadius: cornerRadius
2525
)
2626
)
@@ -38,19 +38,19 @@ public struct MessageBubbleModifier: ViewModifier {
3838
}
3939
}
4040

41-
private var backgroundColor: UIColor {
41+
private var background: [Color] {
4242
if let injectedBackgroundColor = injectedBackgroundColor {
43-
return injectedBackgroundColor
43+
return [Color(injectedBackgroundColor)]
4444
}
45-
45+
var colors = colors
4646
if message.isSentByCurrentUser {
4747
if message.type == .ephemeral {
48-
return colors.background8
48+
return colors.messageCurrentUserEmphemeralBackground.map { Color($0) }
4949
} else {
50-
return colors.background6
50+
return colors.messageCurrentUserBackground.map { Color($0) }
5151
}
5252
} else {
53-
return colors.background8
53+
return colors.messageOtherUserBackground.map { Color($0) }
5454
}
5555
}
5656
}
@@ -60,13 +60,13 @@ public struct BubbleModifier: ViewModifier {
6060
@Injected(\.colors) private var colors
6161

6262
var corners: UIRectCorner
63-
var backgroundColor: Color
63+
var backgroundColors: [Color]
6464
var borderColor: Color? = nil
6565
var cornerRadius: CGFloat = 18
6666

6767
public func body(content: Content) -> some View {
6868
content
69-
.background(backgroundColor)
69+
.background(background)
7070
.overlay(
7171
BubbleBackgroundShape(
7272
cornerRadius: cornerRadius, corners: corners
@@ -83,6 +83,21 @@ public struct BubbleModifier: ViewModifier {
8383
)
8484
)
8585
}
86+
87+
@ViewBuilder
88+
private var background: some View {
89+
if backgroundColors.count == 1 {
90+
backgroundColors[0]
91+
} else if backgroundColors.count > 1 {
92+
LinearGradient(
93+
gradient: Gradient(colors: backgroundColors),
94+
startPoint: .top,
95+
endPoint: .bottom
96+
)
97+
} else {
98+
Color.clear
99+
}
100+
}
86101
}
87102

88103
/// Shape that allows rounding of arbitrary corners.
@@ -138,7 +153,7 @@ extension View {
138153
modifier(
139154
BubbleModifier(
140155
corners: corners,
141-
backgroundColor: background,
156+
backgroundColors: [background],
142157
borderColor: borderColor
143158
)
144159
)

Sources/StreamChatSwiftUI/ColorPalette.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public struct ColorPalette {
6464
public var alternativeActiveTint: UIColor = .streamAccentGreen
6565
public var inactiveTint: UIColor = .streamGray
6666
public var alternativeInactiveTint: UIColor = .streamGrayGainsboro
67+
68+
// MARK: - Messages
69+
70+
public lazy var messageCurrentUserBackground: [UIColor] = [background6]
71+
public lazy var messageCurrentUserEmphemeralBackground: [UIColor] = [background8]
72+
public lazy var messageOtherUserBackground: [UIColor] = [background8]
6773
}
6874

6975
// Those colors are default defined stream constants, which are fallback values if you don't implement your color theme.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The message list view in the SwiftUI SDK allows several customization options. T
88

99
If you are developing an app with this use-case, you can customize the [message avatars](../custom-avatar), [reactions](../message-reactions), [theming and presentation logic](../../getting-started) and the different types of [attachments](../attachments).
1010

11+
## Message List Configuration
12+
1113
Additionally, you can control the display of the helper views around the message (date indicators, avatars) and paddings, via the `MessageListConfig`'s properties `MessageDisplayOptions` and `MessagePaddings`. The `MessageListConfig` is part of the `Utils` class in `StreamChat`. Here's an example on how to hide the date indicators and avatars, while also increasing the horizontal padding.
1214

1315
```swift
@@ -41,6 +43,20 @@ func makeMessageListBackground(
4143

4244
In this method, you receive the `colors` used in the SDK, but you can also use your own colors like in the example above. If you want to have a different background for message threads, use the `isInThread` value to distinguish between a regular message list and a thread.
4345

46+
If you want to change the background of the message bubbles, you can update the `messageCurrentUserBackground` and `messageOtherUserBackground` in the `ColorPalette` on `StreamChat` setup. These values are arrays of `UIColor` - if you want to have a gradient background just provide all the colors that the gradient should be consisted of.
47+
48+
```swift
49+
var colors = ColorPalette()
50+
colors.messageCurrentUserBackground = [UIColor.red, UIColor.white]
51+
colors.messageOtherUserBackground = [UIColor.white, UIColor.red]
52+
53+
let appearance = Appearance(colors: colors)
54+
55+
streamChat = StreamChat(chatClient: chatClient, appearance: appearance)
56+
```
57+
58+
## Custom Message Container View
59+
4460
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.
4561

4662
For example, if you need a simple text message view, alligned on the left, you can do it like this:

0 commit comments

Comments
 (0)