onRemoveReaction method

Future<void> onRemoveReaction({
  1. required String kind,
  2. required GenericEnrichedActivity<A, Ob, T, Or> activity,
  3. required Reaction reaction,
  4. required String feedGroup,
})

Remove reaction from the feed in a reactive way

For example to delete a comment under a tweet

FeedProvider.of(context).bloc.onRemoveReaction()

Implementation

Future<void> onRemoveReaction({
  required String kind,
  required GenericEnrichedActivity<A, Ob, T, Or> activity,
  required Reaction reaction,
  required String feedGroup,
}) async {
  await client.reactions.delete(reaction.id!);
  await trackAnalytics(
      label: 'un$kind', foreignId: activity.foreignId, feedGroup: feedGroup);
  final _activities = getActivities(feedGroup) ?? [activity];
  final activityPath = _activities.getEnrichedActivityPath(activity);

  final indexPath = _activities
      .indexWhere((a) => a.id! == activity.id); //TODO: handle null safety

  final reactionCounts =
      activityPath.reactionCounts.unshiftByKind(kind, ShiftType.decrement);

  final latestReactions = activityPath.latestReactions
      .unshiftByKind(kind, reaction, ShiftType.decrement);

  final ownReactions = activityPath.ownReactions
      .unshiftByKind(kind, reaction, ShiftType.decrement);

  final updatedActivity = activityPath.copyWith(
    ownReactions: ownReactions,
    latestReactions: latestReactions,
    reactionCounts: reactionCounts,
  );

  // remove reaction from the stream
  reactionsManager.unshiftById(activity.id!, reaction, ShiftType.decrement);

  activitiesManager.update(
      feedGroup, _activities.updateIn(updatedActivity, indexPath));
}