Skip to content

Commit e3fc906

Browse files
Fixed animation glitch
1 parent 8641bb9 commit e3fc906

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
155155
if let message = messageController?.message {
156156
var array = Array(messages)
157157
array.append(message)
158-
withAnimation {
159-
self.messages = LazyCachedMapCollection(source: array, map: { $0 })
160-
}
158+
self.messages = LazyCachedMapCollection(source: array, map: { $0 })
161159
} else {
162-
withAnimation {
160+
if shouldAnimate(changes: changes) {
161+
withAnimation {
162+
self.messages = messages
163+
}
164+
} else {
163165
self.messages = messages
164166
}
165167
}
@@ -317,6 +319,20 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
317319
}
318320
}
319321

322+
private func shouldAnimate(changes: [ListChange<ChatMessage>]) -> Bool {
323+
for change in changes {
324+
switch change {
325+
case .insert(_, index: _),
326+
.remove(_, index: _):
327+
return true
328+
default:
329+
log.debug("detected non-animatable change")
330+
}
331+
}
332+
333+
return false
334+
}
335+
320336
deinit {
321337
messageCachingUtils.clearCache()
322338
}
@@ -355,7 +371,7 @@ extension ChatMessage: Identifiable {
355371
states += fileAttachments.compactMap { $0.uploadingState?.state }
356372

357373
if states.isEmpty {
358-
return ""
374+
return "empty"
359375
}
360376

361377
let strings = states.map { "\($0)" }

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct MessageContainerView<Factory: ViewFactory>: View {
7777
isFirst: showsAllInfo,
7878
scrolledId: $scrolledId
7979
)
80+
.animation(nil)
8081
.overlay(
8182
reactionsShown ?
8283
factory.makeMessageReactionView(
@@ -174,14 +175,10 @@ struct MessageContainerView<Factory: ViewFactory>: View {
174175
.padding(.horizontal, messageListConfig.messagePaddings.horizontal)
175176
.padding(.bottom, showsAllInfo || isMessagePinned ? paddingValue : 2)
176177
.padding(.top, isLast ? paddingValue : 0)
177-
.background(isMessagePinned || shouldAnimateBackground ? Color(colors.pinnedBackground) : nil)
178+
.background(isMessagePinned ? Color(colors.pinnedBackground) : nil)
178179
.padding(.bottom, isMessagePinned ? paddingValue / 2 : 0)
179180
}
180-
181-
private var shouldAnimateBackground: Bool {
182-
scrolledId == message.messageId
183-
}
184-
181+
185182
private var isMessagePinned: Bool {
186183
message.pinDetails != nil
187184
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListHelperViews.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct MessageDateView: View {
4343
Text(dateFormatter.string(from: message.createdAt))
4444
.font(fonts.footnote)
4545
.foregroundColor(Color(colors.textLowEmphasis))
46+
.animation(nil)
4647
}
4748
}
4849

0 commit comments

Comments
 (0)