Skip to content

Commit ca75e5c

Browse files
Config for left alignment of messages
1 parent d615438 commit ca75e5c

12 files changed

+53
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
- Add message preview with attachments in channel list
1212
- Add support for pre-built XCFramework
1313
- Config for composer text input paddings
14+
- Config for left alignment of messages
1415

1516
### 🐞 Fixed
1617
- Made some `ChannelList` and `MessageListView` parameters optional

Sources/StreamChatSwiftUI/ChatChannel/MessageList/DeletedMessageView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct DeletedMessageView: View {
2626

2727
public var body: some View {
2828
VStack(
29-
alignment: message.isSentByCurrentUser ? .trailing : .leading,
29+
alignment: message.isRightAligned ? .trailing : .leading,
3030
spacing: 4
3131
) {
3232
Text(L10n.Message.deletedMessagePlaceholder)
@@ -38,7 +38,9 @@ public struct DeletedMessageView: View {
3838

3939
if message.isSentByCurrentUser {
4040
HStack {
41-
Spacer()
41+
if message.isRightAligned {
42+
Spacer()
43+
}
4244

4345
if deletedMessageVisibility == .visibleForCurrentUser {
4446
Image(uiImage: images.eye)

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageBubble.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public struct MessageModifierInfo {
3232
/// Modifier that enables message bubble container.
3333
public struct MessageBubbleModifier: ViewModifier {
3434
@Injected(\.colors) private var colors
35+
@Injected(\.utils) private var utils
3536

3637
public var message: ChatMessage
3738
public var isFirst: Bool
@@ -54,7 +55,11 @@ public struct MessageBubbleModifier: ViewModifier {
5455
self.isFirst = isFirst
5556
self.injectedBackgroundColor = injectedBackgroundColor
5657
self.cornerRadius = cornerRadius
57-
self.forceLeftToRight = forceLeftToRight
58+
if utils.messageListConfig.messageListAlignment == .leftAligned {
59+
self.forceLeftToRight = true
60+
} else {
61+
self.forceLeftToRight = forceLeftToRight
62+
}
5863
self.topPadding = topPadding
5964
self.bottomPadding = bottomPadding
6065
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
6262
if message.type == .system || message.type == .error {
6363
factory.makeSystemMessageView(message: message)
6464
} else {
65-
if message.isSentByCurrentUser {
65+
if message.isRightAligned {
6666
MessageSpacer(spacerWidth: spacerWidth)
6767
} else {
6868
if messageListConfig.messageDisplayOptions.showAvatars(for: channel) {
@@ -73,7 +73,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
7373
}
7474
}
7575

76-
VStack(alignment: message.isSentByCurrentUser ? .trailing : .leading) {
76+
VStack(alignment: message.isRightAligned ? .trailing : .leading) {
7777
if isMessagePinned {
7878
MessagePinDetailsView(
7979
message: message,
@@ -185,7 +185,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
185185
factory.makeMessageDateView(for: message)
186186
}
187187
}
188-
} else if !message.isSentByCurrentUser
188+
} else if !message.isRightAligned
189189
&& !channel.isDirectMessageChannel
190190
&& messageListConfig.messageDisplayOptions.showAuthorName {
191191
factory.makeMessageAuthorAndDateView(for: message)
@@ -203,7 +203,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
203203
: nil
204204
)
205205

206-
if !message.isSentByCurrentUser {
206+
if !message.isRightAligned {
207207
MessageSpacer(spacerWidth: spacerWidth)
208208
}
209209
}
@@ -239,7 +239,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
239239
let minimumWidth: CGFloat = 240
240240
let available = max(minimumWidth, (width ?? 0) - spacerWidth) - 2 * padding
241241
let avatarSize: CGFloat = CGSize.messageAvatarSize.width + padding
242-
let totalWidth = message.isSentByCurrentUser ? available : available - avatarSize
242+
let totalWidth = message.isRightAligned ? available : available - avatarSize
243243
return totalWidth
244244
}
245245

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public struct MessageListConfig {
2525
iPadSplitViewEnabled: Bool = true,
2626
scrollingAnchor: UnitPoint = .bottom,
2727
showNewMessagesSeparator: Bool = false,
28-
handleTabBarVisibility: Bool = true
28+
handleTabBarVisibility: Bool = true,
29+
messageListAlignment: MessageListAlignment = .standard
2930
) {
3031
self.messageListType = messageListType
3132
self.typingIndicatorPlacement = typingIndicatorPlacement
@@ -44,6 +45,7 @@ public struct MessageListConfig {
4445
self.scrollingAnchor = scrollingAnchor
4546
self.showNewMessagesSeparator = showNewMessagesSeparator
4647
self.handleTabBarVisibility = handleTabBarVisibility
48+
self.messageListAlignment = messageListAlignment
4749
}
4850

4951
public let messageListType: MessageListType
@@ -63,6 +65,7 @@ public struct MessageListConfig {
6365
public let scrollingAnchor: UnitPoint
6466
public let showNewMessagesSeparator: Bool
6567
public let handleTabBarVisibility: Bool
68+
public let messageListAlignment: MessageListAlignment
6669
}
6770

6871
/// Contains information about the message paddings.
@@ -187,3 +190,13 @@ public enum MessageListType {
187190
case livestream
188191
case commerce
189192
}
193+
194+
/// The alignment of the messages in the message list.
195+
public enum MessageListAlignment {
196+
/// Standard message alignment.
197+
/// The current user's messages are on the right.
198+
/// The other users' messages are on the left.
199+
case standard
200+
/// Everything is left aligned.
201+
case leftAligned
202+
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageRepliesView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public struct MessageRepliesView<Factory: ViewFactory>: View {
4141
)
4242
} label: {
4343
HStack {
44-
if !message.isSentByCurrentUser {
44+
if !message.isRightAligned {
4545
MessageAvatarView(
4646
avatarURL: message.threadParticipants.first?.imageURL,
4747
size: .init(width: 16, height: 16)
4848
)
4949
}
5050
Text("\(replyCount) \(repliesText)")
5151
.font(fonts.footnoteBold)
52-
if message.isSentByCurrentUser {
52+
if message.isRightAligned {
5353
MessageAvatarView(
5454
avatarURL: message.threadParticipants.first?.imageURL,
5555
size: .init(width: 16, height: 16)
@@ -81,7 +81,7 @@ public struct MessageRepliesView<Factory: ViewFactory>: View {
8181
)
8282
.offset(y: -24)
8383
.rotation3DEffect(
84-
.degrees(message.isSentByCurrentUser ? 180 : 0),
84+
.degrees(message.isRightAligned ? 180 : 0),
8585
axis: (x: 0, y: 1, z: 0)
8686
)
8787
)

Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsHelperViews.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public struct ReactionsHStack<Content: View>: View {
1616

1717
public var body: some View {
1818
HStack {
19-
if !message.isSentByCurrentUser {
19+
if !message.isRightAligned {
2020
Spacer()
2121
}
2222

2323
content()
2424

25-
if message.isSentByCurrentUser {
25+
if message.isRightAligned {
2626
Spacer()
2727
}
2828
}

Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsOverlayContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public extension ChatMessage {
6565
availableWidth: CGFloat = UIScreen.main.bounds.width,
6666
reactionsSize: CGFloat
6767
) -> CGFloat {
68-
if isSentByCurrentUser {
68+
if isRightAligned {
6969
var originX = contentRect.origin.x - reactionsSize / 2
7070
let total = originX + reactionsSize
7171
if total > availableWidth {

Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsOverlayView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public struct ReactionsOverlayView<Factory: ViewFactory>: View {
8585
Alert.defaultErrorAlert
8686
}
8787

88-
if !messageDisplayInfo.message.isSentByCurrentUser &&
88+
if !messageDisplayInfo.message.isRightAligned &&
8989
utils.messageListConfig.messageDisplayOptions.showAvatars(for: channel) {
9090
factory.makeMessageAvatarView(
9191
for: utils.messageCachingUtils.authorInfo(from: messageDisplayInfo.message)
@@ -238,13 +238,12 @@ public struct ReactionsOverlayView<Factory: ViewFactory>: View {
238238

239239
private func messageActionsOffsetX(reader: GeometryProxy) -> CGFloat {
240240
let originX = messageActionsOriginX(availableWidth: reader.size.width)
241-
let sentByCurrentUser = messageDisplayInfo.message.isSentByCurrentUser
242241
if popIn {
243242
return originX
244243
} else if willPopOut {
245244
return messageOriginX(proxy: reader)
246245
} else {
247-
return sentByCurrentUser ? messageActionsWidth : 0
246+
return messageDisplayInfo.message.isRightAligned ? messageActionsWidth : 0
248247
}
249248
}
250249

@@ -323,15 +322,15 @@ public struct ReactionsOverlayView<Factory: ViewFactory>: View {
323322
}
324323

325324
private func messageActionsOriginX(availableWidth: CGFloat) -> CGFloat {
326-
if messageDisplayInfo.message.isSentByCurrentUser {
325+
if messageDisplayInfo.message.isRightAligned {
327326
return availableWidth - messageActionsWidth - paddingValue / 2
328327
} else {
329328
return CGSize.messageAvatarSize.width + paddingValue
330329
}
331330
}
332331

333332
private func userReactionsOriginX(availableWidth: CGFloat) -> CGFloat {
334-
if messageDisplayInfo.message.isSentByCurrentUser {
333+
if messageDisplayInfo.message.isRightAligned {
335334
return availableWidth - maxUserReactionsWidth(availableWidth: availableWidth) - paddingValue / 2
336335
} else {
337336
return paddingValue
@@ -340,7 +339,7 @@ public struct ReactionsOverlayView<Factory: ViewFactory>: View {
340339

341340
private var messageActionsWidth: CGFloat {
342341
var width = messageDisplayInfo.contentWidth + 2 * paddingValue
343-
if messageDisplayInfo.message.isSentByCurrentUser {
342+
if messageDisplayInfo.message.isRightAligned {
344343
width -= 2 * paddingValue
345344
}
346345

Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsUsersView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct ReactionsUsersView: View {
3232

3333
var body: some View {
3434
HStack {
35-
if message.isSentByCurrentUser {
35+
if message.isRightAligned {
3636
Spacer()
3737
}
3838

@@ -70,7 +70,7 @@ struct ReactionsUsersView: View {
7070
.background(Color(colors.background))
7171
.cornerRadius(16)
7272

73-
if !message.isSentByCurrentUser {
73+
if !message.isRightAligned {
7474
Spacer()
7575
}
7676
}

0 commit comments

Comments
 (0)