Skip to content

Commit fdeed04

Browse files
Going back to channel list when leaving a channel
1 parent 71725d3 commit fdeed04

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChannelHeader/ChatChannelHeaderViewModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct DefaultChatChannelHeader: ToolbarContent {
5252

5353
ToolbarItem(placement: .navigationBarTrailing) {
5454
ZStack {
55-
NavigationLink(destination: LazyView(ChatChannelInfoView(channel: channel))) {
55+
NavigationLink(destination: LazyView(ChatChannelInfoView(channel: channel, shownFromMessageList: true))) {
5656
Rectangle()
5757
.fill(Color(colors.background))
5858
.contentShape(Rectangle())

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/ChatChannelInfoView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@ public struct ChatChannelInfoView: View, KeyboardReadable {
1313
@Injected(\.fonts) private var fonts
1414

1515
@StateObject private var viewModel: ChatChannelInfoViewModel
16+
private var shownFromMessageList: Bool
1617

1718
@Environment(\.presentationMode) var presentationMode
1819

19-
public init(channel: ChatChannel) {
20+
public init(
21+
channel: ChatChannel,
22+
shownFromMessageList: Bool = false
23+
) {
2024
_viewModel = StateObject(
2125
wrappedValue: ChatChannelInfoViewModel(channel: channel)
2226
)
27+
self.shownFromMessageList = shownFromMessageList
2328
}
2429

2530
init(viewModel: ChatChannelInfoViewModel) {
2631
_viewModel = StateObject(wrappedValue: viewModel)
32+
shownFromMessageList = false
2733
}
2834

2935
public var body: some View {
@@ -77,7 +83,11 @@ public struct ChatChannelInfoView: View, KeyboardReadable {
7783
message: Text(message),
7884
primaryButton: .destructive(Text(buttonTitle)) {
7985
viewModel.leaveConversationTapped {
80-
presentationMode.wrappedValue.dismiss()
86+
if shownFromMessageList {
87+
notifyChannelDismiss()
88+
} else {
89+
presentationMode.wrappedValue.dismiss()
90+
}
8191
}
8292
},
8393
secondaryButton: .cancel()

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,21 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
420420
private func observeChannelDismiss() {
421421
NotificationCenter.default.addObserver(
422422
self,
423-
selector: #selector(updateQueuedChannels),
424-
name: NSNotification.Name(channelDismissed),
423+
selector: #selector(dismissPresentedChannel),
424+
name: NSNotification.Name(dismissChannel),
425425
object: nil
426426
)
427427
}
428428

429-
@objc private func updateQueuedChannels() {
430-
if !queuedChannelsChanges.isEmpty {
431-
withAnimation {
432-
channels = queuedChannelsChanges
433-
}
434-
}
429+
@objc private func dismissPresentedChannel() {
430+
selectedChannel = nil
435431
}
436432
}
437433

438-
private let channelDismissed = "io.getstream.channelDismissed"
434+
private let dismissChannel = "io.getstream.dismissChannel"
439435

440-
public func notifyChannelDismiss() {
441-
NotificationCenter.default.post(name: NSNotification.Name(channelDismissed), object: nil)
436+
func notifyChannelDismiss() {
437+
NotificationCenter.default.post(name: NSNotification.Name(dismissChannel), object: nil)
442438
}
443439

444440
/// Enum for the type of alert presented in the channel list view.

StreamChatSwiftUITests/Tests/ChatChannelList/ChatChannelListViewModel_Tests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,24 @@ class ChatChannelListViewModel_Tests: StreamChatTestCase {
242242
XCTAssert(unreadCount == 0)
243243
}
244244

245+
func test_channelListVM_channelDismiss() {
246+
// Given
247+
let channelId = ChannelId.unique
248+
let channel = ChatChannel.mock(cid: channelId, unreadCount: .mock(messages: 1))
249+
let channelListController = makeChannelListController(channels: [channel])
250+
let viewModel = ChatChannelListViewModel(
251+
channelListController: channelListController,
252+
selectedChannelId: nil
253+
)
254+
viewModel.selectedChannel = ChannelSelectionInfo(channel: channel, message: nil)
255+
256+
// When
257+
notifyChannelDismiss()
258+
259+
// Then
260+
XCTAssert(viewModel.selectedChannel == nil)
261+
}
262+
245263
// MARK: - private
246264

247265
private func makeChannelListController(

0 commit comments

Comments
 (0)