Skip to content

Commit 3814e89

Browse files
performance improvements
1 parent b905e30 commit 3814e89

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public struct ChatChannelView<Factory: ViewFactory>: View {
3636
currentDateString: viewModel.currentDateString,
3737
isGroup: !viewModel.channel.isDirectMessageChannel,
3838
unreadCount: viewModel.channel.unreadCount.messages,
39+
listId: viewModel.listId,
3940
onMessageAppear: viewModel.handleMessageAppear(index:),
4041
onScrollToBottom: viewModel.scrollToLastMessage,
4142
onLongPress: { displayInfo in

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ChatChannelViewModel: ObservableObject, ChatChannelControllerDelega
1212
@Injected(\.utils) var utils
1313

1414
private var cancellables = Set<AnyCancellable>()
15+
private var lastRefreshThreshold = 200
16+
private let refreshThreshold = 200
1517
private var timer: Timer?
1618
private var currentDate: Date? {
1719
didSet {
@@ -38,6 +40,7 @@ public class ChatChannelViewModel: ObservableObject, ChatChannelControllerDelega
3840
var channelController: ChatChannelController
3941

4042
@Published var scrolledId: String?
43+
@Published var listId = UUID().uuidString
4144

4245
@Published var showScrollToLatestButton = false
4346
@Published var currentDateString: String?
@@ -120,6 +123,12 @@ public class ChatChannelViewModel: ObservableObject, ChatChannelControllerDelega
120123
) {
121124
messages = channelController.messages
122125

126+
let count = channelController.messages.count
127+
if count > lastRefreshThreshold {
128+
lastRefreshThreshold = lastRefreshThreshold + refreshThreshold
129+
listId = UUID().uuidString
130+
}
131+
123132
if !showScrollToLatestButton {
124133
scrollToLastMessage()
125134
}
@@ -168,10 +177,13 @@ public class ChatChannelViewModel: ObservableObject, ChatChannelControllerDelega
168177
}
169178

170179
if _loadingPreviousMessages.compareAndSwap(old: false, new: true) {
171-
channelController.loadPreviousMessages(limit: 250, completion: { [weak self] _ in
172-
guard let self = self else { return }
173-
self.loadingPreviousMessages = false
174-
})
180+
channelController.loadPreviousMessages(
181+
limit: refreshThreshold,
182+
completion: { [weak self] _ in
183+
guard let self = self else { return }
184+
self.loadingPreviousMessages = false
185+
}
186+
)
175187
}
176188
}
177189

Sources/StreamChatSwiftUI/ChatChannel/MessageList/LinkAttachmentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public struct LinkAttachmentView: View {
6363
if !imageHidden {
6464
ZStack {
6565
LazyImage(source: linkAttachment.previewURL!)
66+
.onDisappear(.reset)
6667
.processors([ImageProcessors.Resize(width: width)])
6768
.priority(.high)
6869
.frame(width: width - 2 * padding, height: (width - 2 * padding) / 2)

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageAvatarView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public struct MessageAvatarView: View {
2323
)
2424

2525
LazyImage(source: adjustedURL)
26+
.onDisappear(.reset)
2627
.clipShape(Circle())
2728
.frame(
2829
width: CGSize.messageAvatarSize.width,
2930
height: CGSize.messageAvatarSize.height
3031
)
31-
.id(url)
32+
3233
} else {
3334
Image(systemName: "person.circle")
3435
.resizable()
3536
.frame(
3637
width: CGSize.messageAvatarSize.width,
3738
height: CGSize.messageAvatarSize.height
3839
)
39-
.id("placeholder")
4040
}
4141
}
4242
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
1616
var currentDateString: String?
1717
var isGroup: Bool
1818
var unreadCount: Int
19+
var listId: String
1920

2021
var onMessageAppear: (Int) -> Void
2122
var onScrollToBottom: () -> Void
@@ -89,6 +90,7 @@ struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
8990
}
9091
}
9192
}
93+
.id(listId)
9294
}
9395
}
9496
.coordinateSpace(name: scrollAreaId)

0 commit comments

Comments
 (0)