Skip to content

Commit a45cefe

Browse files
Added config to update channels from messages list
1 parent 06c8774 commit a45cefe

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
130130
.offset(x: self.offsetX)
131131
.simultaneousGesture(
132132
DragGesture(
133-
minimumDistance: 15,
133+
minimumDistance: 10,
134134
coordinateSpace: .local
135135
)
136136
.updating($offset) { (value, gestureState, _) in

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public struct MessageListConfig {
1818
pageSize: Int = 50,
1919
messagePopoverEnabled: Bool = true,
2020
doubleTapOverlayEnabled: Bool = false,
21-
becomesFirstResponderOnOpen: Bool = false
21+
becomesFirstResponderOnOpen: Bool = false,
22+
updateChannelsFromMessageList: Bool = false
2223
) {
2324
self.messageListType = messageListType
2425
self.typingIndicatorPlacement = typingIndicatorPlacement
@@ -30,6 +31,7 @@ public struct MessageListConfig {
3031
self.messagePopoverEnabled = messagePopoverEnabled
3132
self.doubleTapOverlayEnabled = doubleTapOverlayEnabled
3233
self.becomesFirstResponderOnOpen = becomesFirstResponderOnOpen
34+
self.updateChannelsFromMessageList = updateChannelsFromMessageList
3335
}
3436

3537
let messageListType: MessageListType
@@ -42,6 +44,7 @@ public struct MessageListConfig {
4244
let messagePopoverEnabled: Bool
4345
let doubleTapOverlayEnabled: Bool
4446
let becomesFirstResponderOnOpen: Bool
47+
let updateChannelsFromMessageList: Bool
4548
}
4649

4750
/// Contains information about the message paddings.

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
242242
private func handleChannelListChanges(_ controller: ChatChannelListController) {
243243
if selectedChannel != nil || !searchText.isEmpty || deeplinkChannel != nil {
244244
queuedChannelsChanges = controller.channels
245+
updateChannelsIfNeeded()
245246
} else {
246247
channels = controller.channels
247248
}
@@ -363,35 +364,47 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
363364
if !queuedChannelsChanges.isEmpty && selectedChannel == nil && deeplinkChannel == nil {
364365
channels = queuedChannelsChanges
365366
} else if !queuedChannelsChanges.isEmpty {
366-
let selected = selectedChannel != nil ? selectedChannel?.channel : deeplinkChannel?.channel
367-
var index: Int?
368-
var temp = Array(queuedChannelsChanges)
369-
for i in 0..<temp.count {
370-
let current = temp[i]
371-
if current.cid == selected?.cid {
372-
index = i
373-
selectedChannel?.injectedChannelInfo = InjectedChannelInfo(
374-
subtitle: current.subtitleText,
375-
unreadCount: 0,
376-
timestamp: current.timestampText,
377-
lastMessageAt: current.lastMessageAt,
378-
latestMessages: current.latestMessages
379-
)
380-
break
381-
}
382-
}
383-
if let index = index, let selected = selected {
384-
temp[index] = selected
385-
}
386-
markDirty = true
387-
channels = LazyCachedMapCollection(source: temp, map: { $0 })
367+
handleQueuedChanges()
388368
} else if queuedChannelsChanges.isEmpty && (selectedChannel != nil || deeplinkChannel != nil) {
389369
if selectedChannel?.injectedChannelInfo == nil {
390370
selectedChannel?.injectedChannelInfo = InjectedChannelInfo(unreadCount: 0)
391371
}
392372
}
393373
}
394374

375+
private func updateChannelsIfNeeded() {
376+
if utils.messageListConfig.updateChannelsFromMessageList {
377+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
378+
self?.handleChannelAppearance()
379+
}
380+
}
381+
}
382+
383+
private func handleQueuedChanges() {
384+
let selected = selectedChannel != nil ? selectedChannel?.channel : deeplinkChannel?.channel
385+
var index: Int?
386+
var temp = Array(queuedChannelsChanges)
387+
for i in 0..<temp.count {
388+
let current = temp[i]
389+
if current.cid == selected?.cid {
390+
index = i
391+
selectedChannel?.injectedChannelInfo = InjectedChannelInfo(
392+
subtitle: current.subtitleText,
393+
unreadCount: 0,
394+
timestamp: current.timestampText,
395+
lastMessageAt: current.lastMessageAt,
396+
latestMessages: current.latestMessages
397+
)
398+
break
399+
}
400+
}
401+
if let index = index, let selected = selected {
402+
temp[index] = selected
403+
}
404+
markDirty = true
405+
channels = LazyCachedMapCollection(source: temp, map: { $0 })
406+
}
407+
395408
private func observeChannelDismiss() {
396409
NotificationCenter.default.addObserver(
397410
self,

StreamChatSwiftUITests/Tests/ChatChannelList/ChatChannelListViewModel_Tests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import XCTest
88

99
class ChatChannelListViewModel_Tests: StreamChatTestCase {
1010

11+
override open func setUp() {
12+
super.setUp()
13+
let utils = Utils(
14+
messageListConfig: MessageListConfig(updateChannelsFromMessageList: true)
15+
)
16+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
17+
}
18+
1119
func test_channelListVMCreation_channelsLoaded() {
1220
// Given
1321
let channelListController = makeChannelListController()

0 commit comments

Comments
 (0)