Skip to content

Commit d58573b

Browse files
Added possibility to disable scrolling in the channel list
1 parent 30b27f1 commit d58573b

File tree

1 file changed

+104
-25
lines changed

1 file changed

+104
-25
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct ChannelList<Factory: ViewFactory>: View {
1212
var channels: LazyCachedMapCollection<ChatChannel>
1313
@Binding var selectedChannel: ChatChannel?
1414
@Binding var currentChannelId: String?
15+
private var scrollable: Bool
1516
private var onlineIndicatorShown: (ChatChannel) -> Bool
1617
private var imageLoader: (ChatChannel) -> UIImage
1718
private var onItemTap: (ChatChannel) -> Void
@@ -27,6 +28,7 @@ public struct ChannelList<Factory: ViewFactory>: View {
2728
channels: LazyCachedMapCollection<ChatChannel>,
2829
selectedChannel: Binding<ChatChannel?>,
2930
currentChannelId: Binding<String?>,
31+
scrollable: Bool = true,
3032
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
3133
imageLoader: @escaping (ChatChannel) -> UIImage,
3234
onItemTap: @escaping (ChatChannel) -> Void,
@@ -48,38 +50,115 @@ public struct ChannelList<Factory: ViewFactory>: View {
4850
self.trailingSwipeRightButtonTapped = trailingSwipeRightButtonTapped
4951
self.trailingSwipeLeftButtonTapped = trailingSwipeLeftButtonTapped
5052
self.leadingSwipeButtonTapped = leadingSwipeButtonTapped
53+
self.scrollable = scrollable
5154
_selectedChannel = selectedChannel
5255
_currentChannelId = currentChannelId
5356
}
5457

5558
public var body: some View {
56-
ScrollView {
57-
LazyVStack(spacing: 0) {
58-
ForEach(channels) { channel in
59-
factory.makeChannelListItem(
60-
channel: channel,
61-
channelName: channelNaming(channel),
62-
avatar: imageLoader(channel),
63-
onlineIndicatorShown: onlineIndicatorShown(channel),
64-
disabled: currentChannelId == channel.id,
65-
selectedChannel: $selectedChannel,
66-
swipedChannelId: $currentChannelId,
67-
channelDestination: channelDestination,
68-
onItemTap: onItemTap,
69-
trailingSwipeRightButtonTapped: trailingSwipeRightButtonTapped,
70-
trailingSwipeLeftButtonTapped: trailingSwipeLeftButtonTapped,
71-
leadingSwipeButtonTapped: leadingSwipeButtonTapped
72-
)
73-
.onAppear {
74-
if let index = channels.firstIndex(where: { chatChannel in
75-
chatChannel.id == channel.id
76-
}) {
77-
onItemAppear(index)
78-
}
59+
Group {
60+
if scrollable {
61+
ScrollView {
62+
channelsVStack
63+
}
64+
} else {
65+
channelsVStack
66+
}
67+
}
68+
}
69+
70+
private var channelsVStack: some View {
71+
ChannelsLazyVStack(
72+
factory: factory,
73+
channels: channels,
74+
selectedChannel: $selectedChannel,
75+
currentChannelId: $currentChannelId,
76+
onlineIndicatorShown: onlineIndicatorShown,
77+
imageLoader: imageLoader,
78+
onItemTap: onItemTap,
79+
onItemAppear: onItemAppear,
80+
channelNaming: channelNaming,
81+
channelDestination: channelDestination,
82+
trailingSwipeRightButtonTapped: trailingSwipeRightButtonTapped,
83+
trailingSwipeLeftButtonTapped: trailingSwipeLeftButtonTapped,
84+
leadingSwipeButtonTapped: leadingSwipeButtonTapped
85+
)
86+
}
87+
}
88+
89+
/// LazyVStack displaying list of channels.
90+
struct ChannelsLazyVStack<Factory: ViewFactory>: View {
91+
92+
private var factory: Factory
93+
var channels: LazyCachedMapCollection<ChatChannel>
94+
@Binding var selectedChannel: ChatChannel?
95+
@Binding var currentChannelId: String?
96+
private var onlineIndicatorShown: (ChatChannel) -> Bool
97+
private var imageLoader: (ChatChannel) -> UIImage
98+
private var onItemTap: (ChatChannel) -> Void
99+
private var onItemAppear: (Int) -> Void
100+
private var channelNaming: (ChatChannel) -> String
101+
private var channelDestination: (ChatChannel) -> Factory.ChannelDestination
102+
private var trailingSwipeRightButtonTapped: (ChatChannel) -> Void
103+
private var trailingSwipeLeftButtonTapped: (ChatChannel) -> Void
104+
private var leadingSwipeButtonTapped: (ChatChannel) -> Void
105+
106+
init(
107+
factory: Factory,
108+
channels: LazyCachedMapCollection<ChatChannel>,
109+
selectedChannel: Binding<ChatChannel?>,
110+
currentChannelId: Binding<String?>,
111+
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
112+
imageLoader: @escaping (ChatChannel) -> UIImage,
113+
onItemTap: @escaping (ChatChannel) -> Void,
114+
onItemAppear: @escaping (Int) -> Void,
115+
channelNaming: @escaping (ChatChannel) -> String,
116+
channelDestination: @escaping (ChatChannel) -> Factory.ChannelDestination,
117+
trailingSwipeRightButtonTapped: @escaping (ChatChannel) -> Void,
118+
trailingSwipeLeftButtonTapped: @escaping (ChatChannel) -> Void,
119+
leadingSwipeButtonTapped: @escaping (ChatChannel) -> Void
120+
) {
121+
self.factory = factory
122+
self.channels = channels
123+
self.onItemTap = onItemTap
124+
self.onItemAppear = onItemAppear
125+
self.channelNaming = channelNaming
126+
self.channelDestination = channelDestination
127+
self.imageLoader = imageLoader
128+
self.onlineIndicatorShown = onlineIndicatorShown
129+
self.trailingSwipeRightButtonTapped = trailingSwipeRightButtonTapped
130+
self.trailingSwipeLeftButtonTapped = trailingSwipeLeftButtonTapped
131+
self.leadingSwipeButtonTapped = leadingSwipeButtonTapped
132+
_selectedChannel = selectedChannel
133+
_currentChannelId = currentChannelId
134+
}
135+
136+
public var body: some View {
137+
LazyVStack(spacing: 0) {
138+
ForEach(channels) { channel in
139+
factory.makeChannelListItem(
140+
channel: channel,
141+
channelName: channelNaming(channel),
142+
avatar: imageLoader(channel),
143+
onlineIndicatorShown: onlineIndicatorShown(channel),
144+
disabled: currentChannelId == channel.id,
145+
selectedChannel: $selectedChannel,
146+
swipedChannelId: $currentChannelId,
147+
channelDestination: channelDestination,
148+
onItemTap: onItemTap,
149+
trailingSwipeRightButtonTapped: trailingSwipeRightButtonTapped,
150+
trailingSwipeLeftButtonTapped: trailingSwipeLeftButtonTapped,
151+
leadingSwipeButtonTapped: leadingSwipeButtonTapped
152+
)
153+
.onAppear {
154+
if let index = channels.firstIndex(where: { chatChannel in
155+
chatChannel.id == channel.id
156+
}) {
157+
onItemAppear(index)
79158
}
80-
81-
Divider()
82159
}
160+
161+
Divider()
83162
}
84163
}
85164
}

0 commit comments

Comments
 (0)