Skip to content

Commit bd3d446

Browse files
chrisbobbegnprice
authored andcommitted
actions [nfc]: Move subscribe-to-channel-with-feedback to ZulipActions
1 parent e522ac4 commit bd3d446

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

lib/widgets/action_sheet.dart

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -490,27 +490,7 @@ class SubscribeButton extends ActionSheetMenuItemButton {
490490

491491
@override
492492
void onPressed() async {
493-
final store = PerAccountStoreWidget.of(pageContext);
494-
final channel = store.streams[channelId];
495-
if (channel == null || channel is Subscription) return; // TODO could give feedback
496-
497-
try {
498-
await subscribeToChannel(store.connection, subscriptions: [channel.name]);
499-
} catch (e) {
500-
if (!pageContext.mounted) return;
501-
502-
String? errorMessage;
503-
switch (e) {
504-
case ZulipApiException():
505-
errorMessage = e.message;
506-
// TODO(#741) specific messages for common errors, like network errors
507-
// (support with reusable code)
508-
default:
509-
}
510-
511-
final title = ZulipLocalizations.of(pageContext).subscribeFailedTitle;
512-
showErrorDialog(context: pageContext, title: title, message: errorMessage);
513-
}
493+
await ZulipAction.subscribeToChannel(pageContext, channelId: channelId);
514494
}
515495
}
516496

lib/widgets/actions.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,32 @@ abstract final class ZulipAction {
241241
return fetchedMessage?.content;
242242
}
243243

244+
static Future<void> subscribeToChannel(BuildContext context, {
245+
required int channelId,
246+
}) async {
247+
final store = PerAccountStoreWidget.of(context);
248+
final channel = store.streams[channelId];
249+
if (channel == null || channel is Subscription) return; // TODO could give feedback
250+
251+
try {
252+
await channels_api.subscribeToChannel(store.connection, subscriptions: [channel.name]);
253+
} catch (e) {
254+
if (!context.mounted) return;
255+
256+
String? errorMessage;
257+
switch (e) {
258+
case ZulipApiException():
259+
errorMessage = e.message;
260+
// TODO(#741) specific messages for common errors, like network errors
261+
// (support with reusable code)
262+
default:
263+
}
264+
265+
final title = ZulipLocalizations.of(context).subscribeFailedTitle;
266+
showErrorDialog(context: context, title: title, message: errorMessage);
267+
}
268+
}
269+
244270
/// Unsubscribe from a channel, possibly after a confirmation dialog,
245271
/// showing an error dialog on failure.
246272
///

0 commit comments

Comments
 (0)