Skip to content

Commit a851146

Browse files
subscription_list [nfc]: Allow onChannelSelect callback
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 3531217 commit a851146

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

lib/widgets/subscription_list.dart

Lines changed: 31 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,16 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
7282
});
7383
}
7484

85+
void _handleChannelSelect(ChannelNarrow narrow) {
86+
if (widget.onChannelSelect case final onChannelSelect?) {
87+
onChannelSelect(narrow);
88+
} else {
89+
Navigator.push(context,
90+
MessageListPage.buildRoute(context: context,
91+
narrow: narrow));
92+
}
93+
}
94+
7595
@override
7696
Widget build(BuildContext context) {
7797
// Design referenced from:
@@ -123,14 +143,16 @@ class _SubscriptionListPageBodyState extends State<SubscriptionListPageBody> wit
123143
_SubscriptionList(
124144
unreadsModel: unreadsModel,
125145
subscriptions: pinned,
126-
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet),
146+
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet,
147+
onChannelSelect: _handleChannelSelect),
127148
],
128149
if (unpinned.isNotEmpty) ...[
129150
_SubscriptionListHeader(label: zulipLocalizations.unpinnedSubscriptionsLabel),
130151
_SubscriptionList(
131152
unreadsModel: unreadsModel,
132153
subscriptions: unpinned,
133-
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet),
154+
showTopicListButtonInActionSheet: widget.showTopicListButtonInActionSheet,
155+
onChannelSelect: _handleChannelSelect),
134156
],
135157

136158
// TODO(#188): add button leading to "All Streams" page with ability to subscribe
@@ -181,11 +203,13 @@ class _SubscriptionList extends StatelessWidget {
181203
required this.unreadsModel,
182204
required this.subscriptions,
183205
required this.showTopicListButtonInActionSheet,
206+
required this.onChannelSelect,
184207
});
185208

186209
final Unreads? unreadsModel;
187210
final List<Subscription> subscriptions;
188211
final bool showTopicListButtonInActionSheet;
212+
final OnChannelSelectCallback onChannelSelect;
189213

190214
@override
191215
Widget build(BuildContext context) {
@@ -199,7 +223,8 @@ class _SubscriptionList extends StatelessWidget {
199223
return SubscriptionItem(subscription: subscription,
200224
unreadCount: unreadCount,
201225
showMutedUnreadBadge: showMutedUnreadBadge,
202-
showTopicListButtonInActionSheet: showTopicListButtonInActionSheet);
226+
showTopicListButtonInActionSheet: showTopicListButtonInActionSheet,
227+
onChannelSelect: onChannelSelect);
203228
});
204229
}
205230
}
@@ -212,12 +237,14 @@ class SubscriptionItem extends StatelessWidget {
212237
required this.unreadCount,
213238
required this.showMutedUnreadBadge,
214239
required this.showTopicListButtonInActionSheet,
240+
required this.onChannelSelect,
215241
});
216242

217243
final Subscription subscription;
218244
final int unreadCount;
219245
final bool showMutedUnreadBadge;
220246
final bool showTopicListButtonInActionSheet;
247+
final OnChannelSelectCallback onChannelSelect;
221248

222249
@override
223250
Widget build(BuildContext context) {
@@ -230,11 +257,7 @@ class SubscriptionItem extends StatelessWidget {
230257
// TODO(design) check if this is the right variable
231258
color: designVariables.background,
232259
child: InkWell(
233-
onTap: () {
234-
Navigator.push(context,
235-
MessageListPage.buildRoute(context: context,
236-
narrow: ChannelNarrow(subscription.streamId)));
237-
},
260+
onTap: () => onChannelSelect(ChannelNarrow(subscription.streamId)),
238261
onLongPress: () => showChannelActionSheet(context,
239262
channelId: subscription.streamId,
240263
showTopicListButton: showTopicListButtonInActionSheet),

0 commit comments

Comments
 (0)