Skip to content

Commit 005f700

Browse files
subscription_list [nfc]: Allow onChannelSelect callback, notifying the selected channel
This will be used soon to provide specific behaviour when selecting a channel, where if specified it will replace the default behaviour of routing to the message list page of the selected channel narrow.
1 parent 166d110 commit 005f700

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/widgets/subscription_list.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@ import 'text.dart';
1313
import 'theme.dart';
1414
import 'unread_count_badge.dart';
1515

16+
typedef OnChannelSelectCallback = void Function(ChannelNarrow narrow);
17+
1618
/// Scrollable listing of subscribed streams.
1719
class SubscriptionListPageBody extends StatefulWidget {
1820
const SubscriptionListPageBody({
1921
super.key,
2022
this.showTopicListButtonInActionSheet = true,
2123
this.hideChannelsIfUserCantPost = false,
24+
this.onChannelSelect,
2225
});
2326

2427
final bool showTopicListButtonInActionSheet;
2528
final bool hideChannelsIfUserCantPost;
2629

30+
/// Callback to invoke when the user selects a channel from the list.
31+
///
32+
/// If null, the default behavior is to navigate to the channel feed.
33+
final OnChannelSelectCallback? onChannelSelect;
34+
35+
// TODO(#412) add onTopicSelect
36+
2737
@override
2838
State<SubscriptionListPageBody> createState() => _SubscriptionListPageBodyState();
2939
}
@@ -72,6 +82,12 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
7282
});
7383
}
7484

85+
void _handleChannelSelect(ChannelNarrow narrow) {
86+
Navigator.push(context,
87+
MessageListPage.buildRoute(context: context,
88+
narrow: narrow));
89+
}
90+
7591
@override
7692
Widget build(BuildContext context) {
7793
// Design referenced from:
@@ -114,6 +130,8 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
114130
message: zulipLocalizations.channelsEmptyPlaceholder);
115131
}
116132

133+
final onChannelSelect = widget.onChannelSelect ?? _handleChannelSelect;
134+
117135
return SafeArea( // horizontal insets
118136
child: CustomScrollView(
119137
slivers: [
@@ -122,14 +140,16 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
122140
_SubscriptionList(
123141
unreadsModel: unreadsModel,
124142
subscriptions: pinned,
125-
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet),
143+
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet,
144+
onChannelSelect: onChannelSelect),
126145
],
127146
if (unpinned.isNotEmpty) ...[
128147
_SubscriptionListHeader(label: zulipLocalizations.unpinnedSubscriptionsLabel),
129148
_SubscriptionList(
130149
unreadsModel: unreadsModel,
131150
subscriptions: unpinned,
132-
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet),
151+
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet,
152+
onChannelSelect: onChannelSelect),
133153
],
134154

135155
// TODO(#188): add button leading to "All Streams" page with ability to subscribe
@@ -180,11 +200,13 @@ class _SubscriptionList extends StatelessWidget {
180200
required this.unreadsModel,
181201
required this.subscriptions,
182202
required this.showTopicListButtonInActionSheet,
203+
required this.onChannelSelect,
183204
});
184205

185206
final Unreads? unreadsModel;
186207
final List<Subscription> subscriptions;
187208
final bool showTopicListButtonInActionSheet;
209+
final OnChannelSelectCallback onChannelSelect;
188210

189211
@override
190212
Widget build(BuildContext context) {
@@ -198,7 +220,8 @@ class _SubscriptionList extends StatelessWidget {
198220
return SubscriptionItem(subscription: subscription,
199221
unreadCount: unreadCount,
200222
showMutedUnreadBadge: showMutedUnreadBadge,
201-
showTopicListButtonInActionSheet: showTopicListButtonInActionSheet);
223+
showTopicListButtonInActionSheet: showTopicListButtonInActionSheet,
224+
onChannelSelect: onChannelSelect);
202225
});
203226
}
204227
}
@@ -211,12 +234,14 @@ class SubscriptionItem extends StatelessWidget {
211234
required this.unreadCount,
212235
required this.showMutedUnreadBadge,
213236
required this.showTopicListButtonInActionSheet,
237+
required this.onChannelSelect,
214238
});
215239

216240
final Subscription subscription;
217241
final int unreadCount;
218242
final bool showMutedUnreadBadge;
219243
final bool showTopicListButtonInActionSheet;
244+
final OnChannelSelectCallback onChannelSelect;
220245

221246
@override
222247
Widget build(BuildContext context) {
@@ -229,11 +254,7 @@ class SubscriptionItem extends StatelessWidget {
229254
// TODO(design) check if this is the right variable
230255
color: designVariables.background,
231256
child: InkWell(
232-
onTap: () {
233-
Navigator.push(context,
234-
MessageListPage.buildRoute(context: context,
235-
narrow: ChannelNarrow(subscription.streamId)));
236-
},
257+
onTap: () => onChannelSelect(ChannelNarrow(subscription.streamId)),
237258
onLongPress: () => showChannelActionSheet(context,
238259
channelId: subscription.streamId,
239260
showTopicListButton: showTopicListButtonInActionSheet),

0 commit comments

Comments
 (0)