Skip to content

Commit 3895749

Browse files
Fix openChannel not working when searching or another chat shown (#975)
1 parent de341d8 commit 3895749

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6-
### 🔄 Changed
6+
### 🐞 Fixed
7+
- Fix openChannel not working when searching or another chat shown [#975](https://github.com/GetStream/stream-chat-swiftui/pull/975)
78

89
# [4.89.1](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.89.1)
910
_September 23, 2025_

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
212212
}
213213
}
214214
}
215+
216+
if isSearching {
217+
searchText = ""
218+
}
219+
220+
if selectedChannel != nil {
221+
selectedChannel = nil
222+
}
215223

216224
loadUntilFound()
217225
}

StreamChatSwiftUITests/Tests/ChatChannelList/ChatChannelListViewModel_Tests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,47 @@ class ChatChannelListViewModel_Tests: StreamChatTestCase {
491491
}
492492
wait(for: [expectation], timeout: 1.0)
493493
}
494+
495+
func test_openChannel_whenSearching_shouldClearSearchState() {
496+
// Given
497+
let existingChannel = ChatChannel.mockDMChannel()
498+
let channelListController = makeChannelListController(channels: [existingChannel])
499+
let viewModel = ChatChannelListViewModel(
500+
channelListController: channelListController,
501+
selectedChannelId: nil,
502+
searchType: .messages
503+
)
504+
viewModel.searchText = "query"
505+
XCTAssertTrue(viewModel.isSearching, "Precondition failed: isSearching should be true before opening a channel")
506+
507+
// When
508+
viewModel.openChannel(with: existingChannel.cid)
509+
510+
// Then
511+
XCTAssertFalse(viewModel.isSearching, "isSearching should be false after opening a channel")
512+
XCTAssertEqual(viewModel.searchText, "", "searchText should be cleared after opening a channel")
513+
XCTAssertNil(viewModel.messageSearchController, "Message search controller should be cleared when search ends")
514+
XCTAssertNil(viewModel.channelListSearchController, "Channel search controller should be cleared when search ends")
515+
}
516+
517+
func test_openChannel_whenSelectedChannelIsSet_shouldClearSelectedChannel() {
518+
// Given
519+
let existingChannel = ChatChannel.mockDMChannel()
520+
let targetChannel = ChatChannel.mockDMChannel() // not in the list
521+
let channelListController = makeChannelListController(channels: [existingChannel])
522+
let viewModel = ChatChannelListViewModel(
523+
channelListController: channelListController,
524+
selectedChannelId: nil
525+
)
526+
viewModel.selectedChannel = existingChannel.channelSelectionInfo
527+
XCTAssertNotNil(viewModel.selectedChannel, "Precondition failed: selectedChannel should be set before opening a channel")
528+
529+
// When
530+
viewModel.openChannel(with: targetChannel.cid)
531+
532+
// Then
533+
XCTAssertNil(viewModel.selectedChannel, "selectedChannel should be cleared immediately when opening another channel")
534+
}
494535

495536
// MARK: - private
496537

0 commit comments

Comments
 (0)