Skip to content

Commit 136d7ae

Browse files
Improvements to the tab bar display flow
1 parent 7fd95c6 commit 136d7ae

File tree

4 files changed

+31
-60
lines changed

4 files changed

+31
-60
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelHelperViews.swift

-29
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,6 @@ struct TabBarAccessor: UIViewControllerRepresentable {
112112
}
113113
}
114114

115-
struct NavigationControllerAccessor: UIViewControllerRepresentable {
116-
var callback: (UINavigationController) -> Void
117-
private let proxyController = ViewController()
118-
119-
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationControllerAccessor>) ->
120-
UIViewController {
121-
proxyController.callback = callback
122-
return proxyController
123-
}
124-
125-
func updateUIViewController(
126-
_ uiViewController: UIViewController,
127-
context: UIViewControllerRepresentableContext<NavigationControllerAccessor>
128-
) {}
129-
130-
typealias UIViewControllerType = UIViewController
131-
132-
private class ViewController: UIViewController {
133-
var callback: (UINavigationController) -> Void = { _ in }
134-
135-
override func viewWillAppear(_ animated: Bool) {
136-
super.viewWillAppear(animated)
137-
if let navigationController = self.navigationController {
138-
callback(navigationController)
139-
}
140-
}
141-
}
142-
}
143-
144115
var isIphone: Bool {
145116
UIDevice.current.userInterfaceIdiom == .phone
146117
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListView.swift

+1-12
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ public struct ChatChannelListContentView<Factory: ViewFactory>: View {
161161
@ObservedObject private var viewModel: ChatChannelListViewModel
162162
@StateObject private var channelHeaderLoader = ChannelHeaderLoader()
163163
private var onItemTap: (ChatChannel) -> Void
164-
// We have to drop back to UIKit to get the number of pushed controllers.
165-
// This is needed for controlling the tabbar visibility.
166-
@State private var navigationController: UINavigationController?
167164

168165
public init(
169166
viewFactory: Factory,
@@ -211,8 +208,7 @@ public struct ChatChannelListContentView<Factory: ViewFactory>: View {
211208
imageLoader: channelHeaderLoader.image(for:),
212209
onItemTap: onItemTap,
213210
onItemAppear: { index in
214-
let pushedScreens = navigationController?.viewControllers.count ?? 0
215-
viewModel.checkTabBarAppearance(numberOfScreens: pushedScreens)
211+
viewModel.checkTabBarAppearance()
216212
viewModel.checkForChannels(index: index)
217213
},
218214
channelNaming: viewModel.name(forChannel:),
@@ -225,12 +221,5 @@ public struct ChatChannelListContentView<Factory: ViewFactory>: View {
225221

226222
viewFactory.makeChannelListStickyFooterView()
227223
}
228-
.background(
229-
Color.clear.background(
230-
NavigationControllerAccessor { navController in
231-
self.navigationController = navController
232-
}
233-
)
234-
)
235224
}
236225
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

+12-10
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
4848
@Published public var selectedChannel: ChannelSelectionInfo? {
4949
willSet {
5050
hideTabBar = newValue != nil
51-
}
52-
didSet {
53-
if oldValue != nil && selectedChannel == nil {
51+
if selectedChannel != nil && newValue == nil {
5452
// pop happened, apply the queued changes.
5553
if !queuedChannelsChanges.isEmpty {
5654
channels = queuedChannelsChanges
@@ -59,7 +57,12 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
5957
}
6058
}
6159

62-
@Published public var deeplinkChannel: ChannelSelectionInfo?
60+
@Published public var deeplinkChannel: ChannelSelectionInfo? {
61+
willSet {
62+
hideTabBar = newValue != nil
63+
}
64+
}
65+
6366
@Published public var swipedChannelId: String?
6467
@Published public var channelAlertType: ChannelAlertType? {
6568
didSet {
@@ -219,18 +222,17 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
219222
channel.membership != nil
220223
}
221224

222-
func checkTabBarAppearance(numberOfScreens: Int) {
225+
func checkTabBarAppearance() {
223226
guard #available(iOS 15, *) else { return }
224-
let newValue = numberOfScreens > 1
225-
if newValue != hideTabBar {
226-
hideTabBar = newValue
227+
if hideTabBar != false {
228+
hideTabBar = false
227229
}
228230
}
229231

230232
// MARK: - private
231233

232234
private func handleChannelListChanges(_ controller: ChatChannelListController) {
233-
if selectedChannel != nil || !searchText.isEmpty {
235+
if selectedChannel != nil || !searchText.isEmpty || deeplinkChannel != nil {
234236
queuedChannelsChanges = controller.channels
235237
} else {
236238
channels = controller.channels
@@ -350,7 +352,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
350352
}
351353

352354
private func handleChannelAppearance() {
353-
if !queuedChannelsChanges.isEmpty {
355+
if !queuedChannelsChanges.isEmpty && selectedChannel == nil && deeplinkChannel == nil {
354356
channels = queuedChannelsChanges
355357
}
356358
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelNavigatableListItem.swift

+18-9
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,31 @@ public struct ChatChannelNavigatableListItem<ChannelDestination: View>: View {
6363

6464
/// Used for representing selection of an item in the channel list.
6565
/// The optional message is used in case we need to scroll to a particular one in the message list.
66-
public struct ChannelSelectionInfo: Identifiable, Hashable {
66+
public struct ChannelSelectionInfo: Identifiable {
6767

68-
public var id: String {
69-
if let message = message {
70-
return "\(channel.id)-\(message.id)"
71-
} else {
72-
return channel.id
73-
}
74-
}
75-
68+
public let id: String
7669
public let channel: ChatChannel
7770
public let message: ChatMessage?
7871

7972
public init(channel: ChatChannel, message: ChatMessage?) {
8073
self.channel = channel
8174
self.message = message
75+
if let message = message {
76+
id = "\(channel.cid.id)-\(message.id)"
77+
} else {
78+
id = channel.name ?? channel.cid.id
79+
}
80+
}
81+
}
82+
83+
extension ChannelSelectionInfo: Hashable, Equatable {
84+
85+
public static func == (lhs: ChannelSelectionInfo, rhs: ChannelSelectionInfo) -> Bool {
86+
lhs.id == rhs.id
87+
}
88+
89+
public func hash(into hasher: inout Hasher) {
90+
hasher.combine(id)
8291
}
8392
}
8493

0 commit comments

Comments
 (0)