Skip to content

Commit f70197e

Browse files
authored
ChatChannelListView navigation did not trigger when using a custom container and its body reloaded (#609)
1 parent 6a522fc commit f70197e

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
### 🐞 Fixed
77
- Rare crash when accessing frame of the view [#607](https://github.com/GetStream/stream-chat-swiftui/pull/607)
8+
- `ChatChannelListView` navigation did not trigger when using a custom container and its body reloaded [#609](https://github.com/GetStream/stream-chat-swiftui/pull/609)
89

910
# [4.63.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.63.0)
1011
_September 12, 2024_

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListView.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,29 @@ public struct ChatChannelListView<Factory: ViewFactory>: View {
1616

1717
private let viewFactory: Factory
1818
private let title: String
19-
private var onItemTap: (ChatChannel) -> Void
19+
private let customOnItemTap: ((ChatChannel) -> Void)?
2020
private var embedInNavigationView: Bool
2121
private var handleTabBarVisibility: Bool
22-
22+
23+
/// Creates a channel list view.
24+
///
25+
/// - Parameters:
26+
/// - viewFactory: The view factory used for creating views used by the channel list.
27+
/// - viewModel: The view model instance providing the data. Default view model is created if nil.
28+
/// - channelListController: The channel list controller managing the list of channels used as a data souce for the view model. Default controller is created if nil.
29+
/// - title: A title used as the navigation bar title.
30+
/// - onItemTap: A closure for handling a tap on the channel item. Default closure updates the ``ChatChannelListViewModel/selectedChannel`` property in the view model.
31+
/// - selectedChannelId: The id of a channel to be opened after the initial channel list load.
32+
/// - handleTabBarVisibility: True, if TabBar visibility should be automatically updated.
33+
/// - embedInNavigationView: True, if the channel list view should be embedded in a navigation stack.
34+
///
35+
/// Changing the instance of the passed in `viewModel` or `channelListController` does not have an effect without reloading the channel list view by assigning a custom identity. The custom identity should be refreshed when either of the passed in instances have been recreated.
36+
/// ```swift
37+
/// ChatChannelListView(
38+
/// viewModel: viewModel
39+
/// )
40+
/// .id(myCustomViewIdentity)
41+
/// ```
2342
public init(
2443
viewFactory: Factory = DefaultViewFactory.shared,
2544
viewModel: ChatChannelListViewModel? = nil,
@@ -30,23 +49,25 @@ public struct ChatChannelListView<Factory: ViewFactory>: View {
3049
handleTabBarVisibility: Bool = true,
3150
embedInNavigationView: Bool = true
3251
) {
33-
let channelListVM = viewModel ?? ViewModelsFactory.makeChannelListViewModel(
34-
channelListController: channelListController,
35-
selectedChannelId: selectedChannelId
36-
)
3752
_viewModel = StateObject(
38-
wrappedValue: channelListVM
53+
wrappedValue: viewModel ?? ViewModelsFactory.makeChannelListViewModel(
54+
channelListController: channelListController,
55+
selectedChannelId: selectedChannelId
56+
)
3957
)
4058
self.viewFactory = viewFactory
4159
self.title = title
4260
self.handleTabBarVisibility = handleTabBarVisibility
4361
self.embedInNavigationView = embedInNavigationView
44-
if let onItemTap = onItemTap {
45-
self.onItemTap = onItemTap
46-
} else {
47-
self.onItemTap = { channel in
48-
channelListVM.selectedChannel = channel.channelSelectionInfo
49-
}
62+
customOnItemTap = onItemTap
63+
}
64+
65+
var onItemTap: (ChatChannel) -> Void {
66+
if let customOnItemTap {
67+
return customOnItemTap
68+
}
69+
return { [weak viewModel] channel in
70+
viewModel?.selectedChannel = channel.channelSelectionInfo
5071
}
5172
}
5273

0 commit comments

Comments
 (0)