Skip to content

Commit b326ef4

Browse files
Implemented message list customizations
1 parent 6dde8f1 commit b326ef4

23 files changed

+126
-27
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
2929
}
3030
}
3131

32-
private var isActive = true
32+
private var isActive = true {
33+
didSet {
34+
if oldValue == false && isActive == true {
35+
messages = channelDataSource.messages
36+
}
37+
}
38+
}
3339

3440
private let messageListDateOverlay: DateFormatter = {
3541
let df = DateFormatter()
@@ -49,7 +55,12 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
4955
@Published public var scrolledId: String?
5056
@Published public var listId = UUID().uuidString
5157

52-
@Published public var showScrollToLatestButton = false
58+
@Published public var showScrollToLatestButton = false {
59+
didSet {
60+
isActive = !showScrollToLatestButton
61+
}
62+
}
63+
5364
@Published public var currentDateString: String?
5465
@Published public var messages = LazyCachedMapCollection<ChatMessage>() {
5566
didSet {
@@ -176,11 +187,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
176187
self.messages = messages
177188
}
178189

179-
let count = messages.count
180-
if count > lastRefreshThreshold {
181-
lastRefreshThreshold = lastRefreshThreshold + refreshThreshold
182-
listId = UUID().uuidString
183-
}
190+
maybeRefreshMessageList()
184191

185192
if !showScrollToLatestButton && scrolledId == nil {
186193
scrollToLastMessage()
@@ -192,7 +199,9 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
192199
didUpdateChannel channel: EntityChange<ChatChannel>,
193200
channelController: ChatChannelController
194201
) {
195-
messages = channelController.messages
202+
if isActive {
203+
messages = channelController.messages
204+
}
196205
checkHeaderType()
197206
}
198207

@@ -217,7 +226,6 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
217226
public func onViewAppear() {
218227
reactionsShown = false
219228
isActive = true
220-
messages = channelDataSource.messages
221229
}
222230

223231
public func onViewDissappear() {
@@ -227,7 +235,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
227235
// MARK: - private
228236

229237
private func checkForNewMessages(index: Int) {
230-
if index < channelDataSource.messages.count - 20 {
238+
if index < messages.count - 20 {
231239
return
232240
}
233241

@@ -238,6 +246,8 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
238246
completion: { [weak self] _ in
239247
guard let self = self else { return }
240248
self.loadingPreviousMessages = false
249+
self.messages = self.channelDataSource.messages
250+
self.maybeRefreshMessageList()
241251
}
242252
)
243253
}
@@ -262,6 +272,14 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
262272
}
263273
}
264274

275+
private func maybeRefreshMessageList() {
276+
let count = messages.count
277+
if count > lastRefreshThreshold {
278+
lastRefreshThreshold = lastRefreshThreshold + refreshThreshold
279+
listId = UUID().uuidString
280+
}
281+
}
282+
265283
private func checkHeaderType() {
266284
let type: ChannelHeaderType
267285
let typingUsers = channel.currentlyTypingUsersFiltered(

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ open class MessageComposerViewModel: ObservableObject {
6767
didSet {
6868
switch pickerTypeState {
6969
case let .expanded(attachmentPickerType):
70-
overlayShown = attachmentPickerType == .media
70+
overlayShown = attachmentPickerType == .media || attachmentPickerType == .custom
7171
if attachmentPickerType == .instantCommands {
7272
composerCommand = ComposerCommand(
7373
id: "instantCommands",

Sources/StreamChatSwiftUI/ChatChannel/Composer/SlowModeView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public struct SlowModeView: View {
2020
.padding(.horizontal, 8)
2121
.font(fonts.bodyBold)
2222
.frame(width: cooldownDuration < 10 ? size : nil, height: size)
23-
2423
.background(
2524
Color(
2625
colors.disabledColorForColor(colors.highlightedAccentBackground)

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct MessageContainerView<Factory: ViewFactory>: View {
1313
@Injected(\.colors) private var colors
1414
@Injected(\.images) private var images
1515
@Injected(\.chatClient) private var chatClient
16+
@Injected(\.utils) private var utils
1617

1718
var factory: Factory
1819
let channel: ChatChannel
@@ -41,11 +42,13 @@ struct MessageContainerView<Factory: ViewFactory>: View {
4142
if message.isSentByCurrentUser {
4243
MessageSpacer(spacerWidth: spacerWidth)
4344
} else {
44-
if showsAllInfo {
45-
factory.makeMessageAvatarView(for: message.author)
46-
} else {
47-
Color.clear
48-
.frame(width: CGSize.messageAvatarSize.width)
45+
if messageListConfig.messageDisplayOptions.showAvatars {
46+
if showsAllInfo {
47+
factory.makeMessageAvatarView(for: message.author)
48+
} else {
49+
Color.clear
50+
.frame(width: CGSize.messageAvatarSize.width)
51+
}
4952
}
5053
}
5154

@@ -147,11 +150,14 @@ struct MessageContainerView<Factory: ViewFactory>: View {
147150
channel: channel,
148151
message: message
149152
)
150-
MessageDateView(message: message)
153+
154+
if messageListConfig.messageDisplayOptions.showMessageDate {
155+
MessageDateView(message: message)
156+
}
151157
}
152158
} else if !message.isSentByCurrentUser && !channel.isDirectMessageChannel {
153159
MessageAuthorAndDateView(message: message)
154-
} else {
160+
} else if messageListConfig.messageDisplayOptions.showMessageDate {
155161
MessageDateView(message: message)
156162
}
157163
}
@@ -163,7 +169,7 @@ struct MessageContainerView<Factory: ViewFactory>: View {
163169
}
164170
}
165171
.padding(.top, reactionsShown && !isMessagePinned ? 3 * paddingValue : 0)
166-
.padding(.horizontal, paddingValue)
172+
.padding(.horizontal, messageListConfig.messagePaddings.horizontal)
167173
.padding(.bottom, showsAllInfo || isMessagePinned ? paddingValue : 2)
168174
.padding(.top, isLast ? paddingValue : 0)
169175
.background(isMessagePinned || shouldAnimateBackground ? Color(colors.pinnedBackground) : nil)
@@ -197,6 +203,10 @@ struct MessageContainerView<Factory: ViewFactory>: View {
197203
&& channel.config.reactionsEnabled
198204
}
199205

206+
private var messageListConfig: MessageListConfig {
207+
utils.messageListConfig
208+
}
209+
200210
private func dragChanged(to value: CGFloat) {
201211
let horizontalTranslation = value
202212

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,53 @@
22
// Copyright © 2022 Stream.io Inc. All rights reserved.
33
//
44

5+
import CoreGraphics
56
import StreamChat
67

78
/// Configuration for the message list.
89
public struct MessageListConfig {
910
public init(
1011
messageListType: MessageListType = .messaging,
1112
typingIndicatorPlacement: TypingIndicatorPlacement = .bottomOverlay,
12-
groupMessages: Bool = true
13+
groupMessages: Bool = true,
14+
messageDisplayOptions: MessageDisplayOptions = MessageDisplayOptions(),
15+
messagePaddings: MessagePaddings = MessagePaddings()
1316
) {
1417
self.messageListType = messageListType
1518
self.typingIndicatorPlacement = typingIndicatorPlacement
1619
self.groupMessages = groupMessages
20+
self.messageDisplayOptions = messageDisplayOptions
21+
self.messagePaddings = messagePaddings
1722
}
1823

1924
let messageListType: MessageListType
2025
let typingIndicatorPlacement: TypingIndicatorPlacement
2126
let groupMessages: Bool
27+
let messageDisplayOptions: MessageDisplayOptions
28+
let messagePaddings: MessagePaddings
29+
}
30+
31+
/// Contains information about the message paddings.
32+
public struct MessagePaddings {
33+
34+
/// Horizontal padding for messages.
35+
let horizontal: CGFloat
36+
37+
public init(horizontal: CGFloat = 8) {
38+
self.horizontal = horizontal
39+
}
40+
}
41+
42+
/// Used to show and hide different helper views around the message.
43+
public struct MessageDisplayOptions {
44+
45+
let showAvatars: Bool
46+
let showMessageDate: Bool
47+
48+
public init(showAvatars: Bool = true, showMessageDate: Bool = true) {
49+
self.showAvatars = showAvatars
50+
self.showMessageDate = showMessageDate
51+
}
2252
}
2353

2454
/// Type of message list. Currently only `messaging` is supported.

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListHelperViews.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import SwiftUI
77

88
/// View that displays the message author and the date of sending.
99
struct MessageAuthorAndDateView: View {
10+
11+
@Injected(\.utils) private var utils
1012
@Injected(\.fonts) private var fonts
1113
@Injected(\.colors) private var colors
1214

@@ -17,7 +19,9 @@ struct MessageAuthorAndDateView: View {
1719
Text(message.author.name ?? "")
1820
.font(fonts.footnoteBold)
1921
.foregroundColor(Color(colors.textLowEmphasis))
20-
MessageDateView(message: message)
22+
if utils.messageListConfig.messageDisplayOptions.showMessageDate {
23+
MessageDateView(message: message)
24+
}
2125
Spacer()
2226
}
2327
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
99

1010
@Injected(\.utils) private var utils
1111
@Injected(\.chatClient) private var chatClient
12+
@Injected(\.colors) private var colors
1213

1314
var factory: Factory
1415
var channel: ChatChannel
@@ -117,6 +118,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
117118
.id(listId)
118119
}
119120
}
121+
.background(Color(colors.background))
120122
.coordinateSpace(name: scrollAreaId)
121123
.onPreferenceChange(WidthPreferenceKey.self) { value in
122124
if let value = value, value != width {
@@ -160,14 +162,14 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
160162
}
161163

162164
if showScrollToLatestButton {
163-
ScrollToBottomButton(
165+
factory.makeScrollToBottomButton(
164166
unreadCount: channel.unreadCount.messages,
165167
onScrollToBottom: onScrollToBottom
166168
)
167169
}
168170

169171
if let date = currentDateString {
170-
DateIndicatorView(date: date)
172+
factory.makeDateIndicatorView(dateString: date)
171173
}
172174

173175
if shouldShowTypingIndicator {

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import SwiftUI
88
/// Stateless component for the channel list.
99
/// If used directly, you should provide the channel list.
1010
public struct ChannelList<Factory: ViewFactory>: View {
11+
12+
@Injected(\.colors) private var colors
13+
1114
private var factory: Factory
1215
var channels: LazyCachedMapCollection<ChatChannel>
1316
@Binding var selectedChannel: ChannelSelectionInfo?
@@ -65,6 +68,7 @@ public struct ChannelList<Factory: ViewFactory>: View {
6568
channelsVStack
6669
}
6770
}
71+
.background(Color(colors.background))
6872
}
6973

7074
private var channelsVStack: some View {

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public struct ChatChannelListView<Factory: ViewFactory>: View {
9696
)
9797
}
9898
}
99+
.background(Color(colors.background))
99100
}
100101
}
101102
}

Sources/StreamChatSwiftUI/ChatChannelList/SearchResultsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct SearchResultsView<Factory: ViewFactory>: View {
2828
.standardPadding()
2929
Spacer()
3030
}
31-
.background(Color(colors.background1))
3231

3332
ScrollView {
3433
LazyVStack(spacing: 0) {
@@ -57,6 +56,7 @@ struct SearchResultsView<Factory: ViewFactory>: View {
5756
.overlay(
5857
loadingSearchResults ? ProgressView() : nil
5958
)
59+
.background(Color(colors.background))
6060
}
6161
}
6262

0 commit comments

Comments
 (0)