addUserToParticipants method

Future<void> addUserToParticipants(
  1. String userId,
  2. ParticipantType type
)

Implementation

Future<void> addUserToParticipants(
  String userId,
  ParticipantType type,
) async {
  final activityData = ref.read(activityProvider).value!.byId[id]!;
  if (type == ParticipantType.going) {
    final hasNotificationPermission = await ref
        .read(localNotificationServiceProvider)
        .checkNotificationPermission();
    if (hasNotificationPermission) {
      // Schedule a notification for the user
      final minutesInAdvance = ref.read(eventReminderPreferenceProvider);
      ref
          .read(localNotificationServiceProvider)
          .scheduleEventNotification(activityData, minutesInAdvance);
    }
  } else {
    // Cancel the notification for the user
    ref
        .read(localNotificationServiceProvider)
        .cancelEventNotification(activityData);
  }
  update((participants) {
    if (participants[type]?.contains(userId) ?? false) {
      return participants;
    }
    participants.containsKey(type)
        ? participants[type]?.add(userId)
        : participants[type] = [userId];

    for (final participantTypeList in participants.entries) {
      if (participantTypeList.key != type &&
          participantTypeList.value.contains(userId)) {
        participantTypeList.value.remove(userId);
      }
    }
    ref
        .read(firestoreServiceProvider)
        .addUserToParticipants(id, userId, type);
    return participants;
  });
}