Skip to content

Commit

Permalink
Improved feedback when subscribing/unsubscribing to community (thunde…
Browse files Browse the repository at this point in the history
…r-app#1499)

* added snackbar to community subscription/unsubscription action

* improved subscription logic

* removed unused localization
  • Loading branch information
hjiangsu authored Jul 17, 2024
1 parent 762f130 commit e02bf30
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/community/bloc/community_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,50 @@ class CommunityBloc extends Bloc<CommunityEvent, CommunityState> {
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
await Future.delayed(const Duration(seconds: 1)).then((value) async {
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);
}
}
});
} catch (e) {
showSnackbar(AppLocalizations.of(GlobalContext.context)!.failedToPerformAction);
return emit(state.copyWith(status: CommunityStatus.failure));
}
break;
Expand Down
8 changes: 8 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -2197,6 +2201,10 @@
},
"subscribed": "Subscribed",
"@subscribed": {},
"subscriptionRequestSent": "Subscription request sent",
"@subscriptionRequestSent": {
"description": "Message for subscription request sent"
},
"subscriptions": "Subscriptions",
"@subscriptions": {},
"successfullyBlocked": "Blocked.",
Expand Down

0 comments on commit e02bf30

Please sign in to comment.