Skip to content

Commit d6bfa2d

Browse files
committed
actions [nfc]: Move subscribe-to-channel-with-feedback to ZulipActions
1 parent ede818a commit d6bfa2d

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
@@ -486,27 +486,7 @@ class SubscribeButton extends ActionSheetMenuItemButton {
486486

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

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)