diff --git a/lib/community/bloc/community_bloc.dart b/lib/community/bloc/community_bloc.dart index 037ffd454..32a3c83fb 100644 --- a/lib/community/bloc/community_bloc.dart +++ b/lib/community/bloc/community_bloc.dart @@ -68,9 +68,33 @@ class CommunityBloc extends Bloc { break; case CommunityAction.follow: try { - CommunityView communityView = await followCommunity(event.communityId, event.value); + // Determines the desired subscribed type outcome based on the value + // If [event.value] is true, then the desired outcome is to subscribe. If [event.value] is false, then the desired outcome is to unsubscribe + SubscribedType? subscribedType = switch (event.value) { + true => SubscribedType.subscribed, + false => SubscribedType.notSubscribed, + _ => null, + }; + + if (GlobalContext.context.mounted && subscribedType == SubscribedType.subscribed) { + showSnackbar(AppLocalizations.of(GlobalContext.context)!.subscriptionRequestSent); + } + CommunityView communityView = await followCommunity(event.communityId, event.value); emit(state.copyWith(status: CommunityStatus.success, communityView: communityView)); + + // Return early if the subscription was successful. Otherwise, retry fetching the community information after a small delay + // This generally occurs on communities on the same instance as the current account + if (GlobalContext.context.mounted && communityView.subscribed == subscribedType) { + if (subscribedType == SubscribedType.subscribed) { + showSnackbar(AppLocalizations.of(GlobalContext.context)!.subscribed); + } else { + showSnackbar(AppLocalizations.of(GlobalContext.context)!.unsubscribed); + } + + return; + } + emit(state.copyWith(status: CommunityStatus.fetching)); // Wait for one second before fetching the community information to get any updated information @@ -78,8 +102,8 @@ class CommunityBloc extends Bloc { GetCommunityResponse? getCommunityResponse = await fetchCommunityInformation(id: event.communityId); emit(state.copyWith(status: CommunityStatus.success, communityView: getCommunityResponse.communityView)); - if (GlobalContext.context.mounted) { - if (event.value) { + if (GlobalContext.context.mounted && getCommunityResponse.communityView.subscribed == subscribedType) { + if (subscribedType == SubscribedType.subscribed) { showSnackbar(AppLocalizations.of(GlobalContext.context)!.subscribed); } else { showSnackbar(AppLocalizations.of(GlobalContext.context)!.unsubscribed); @@ -87,6 +111,7 @@ class CommunityBloc extends Bloc { } }); } catch (e) { + showSnackbar(AppLocalizations.of(GlobalContext.context)!.failedToPerformAction); return emit(state.copyWith(status: CommunityStatus.failure)); } break; diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 9ee4cdd55..a0315bc2e 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -859,6 +859,10 @@ "@failedToLoadVideo": { "description": "Error message that displays when we fail to load a video" }, + "failedToPerformAction": "Failed to perform action", + "@failedToPerformAction": { + "description": "Error message when we fail to perform an action" + }, "failedToUnblock": "Could not unblock: {errorMessage}", "@failedToUnblock": {}, "failedToUpdateNotificationSettings": "Failed to update notification settings", @@ -2197,6 +2201,10 @@ }, "subscribed": "Subscribed", "@subscribed": {}, + "subscriptionRequestSent": "Subscription request sent", + "@subscriptionRequestSent": { + "description": "Message for subscription request sent" + }, "subscriptions": "Subscriptions", "@subscriptions": {}, "successfullyBlocked": "Blocked.",