Skip to content

Commit 2d3d4a9

Browse files
committed
actions [nfc]: Move subscribe-to-channel-with-feedback to ZulipActions
1 parent 03f6c5a commit 2d3d4a9

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import '../api/exception.dart';
77
import '../api/model/model.dart';
88
import '../api/model/narrow.dart';
99
import '../api/route/messages.dart';
10+
import '../api/route/channels.dart' as channels_api;
1011
import '../generated/l10n/zulip_localizations.dart';
1112
import '../model/binding.dart';
1213
import '../model/narrow.dart';
@@ -239,6 +240,32 @@ abstract final class ZulipAction {
239240

240241
return fetchedMessage?.content;
241242
}
243+
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+
}
242269
}
243270

244271
/// Methods that act through platform APIs and show feedback in the UI.

0 commit comments

Comments
 (0)