createContact method

Future<void> createContact(
  1. BuildContext context
)

Implementation

Future<void> createContact(BuildContext context) async {
  final isValid = stepOneFormKey.currentState?.validate();
  if (isValid != true || selectedContactNotifier.value == null) {
    currentStep.value = 0;
    return;
  }
  final result = await ZenRtcHelper.socketIOEmitFuture(
    ZenRtcSocket.instance.socketio,
    "rtc:users:create-contact",
    {
      "contactUserId": selectedContactNotifier.value!.userId,
      "nickName": nickNameController.text.trim(),
      "description": descriptionController.text.trim(),
      "emails": emailControllers
          .map((e) => e.text.trim())
          .where((e) => e.isNotEmpty)
          .toList(),
      "phones": phoneControllers
          .map((e) => e.text.trim())
          .where((e) => e.isNotEmpty)
          .toList(),
    },
  );
  if (result?.data == null || result!.data is! Map) {
    currentStep.value = 0;
    ZenRtcHelper.showCommonSnackbar(
      title: "Unable to Add Contact",
      subtitle: "Please try again.",
      icon: Icons.error_outline,
      backgroundColor: Colors.red,
    );
    return;
  }
  final data = result.data as Map<String, dynamic>;
  if (data["success"] != true) {
    currentStep.value = 0;
    switch (data["error"]) {
      case "contact_already_present":
        ZenRtcHelper.showCommonSnackbar(
          title: "Contact Already Added",
          subtitle: "This contact already exists in your contacts.",
          icon: Icons.person_remove_alt_1,
          backgroundColor: Colors.orange,
        );
        break;
      case "cannot_add_self":
        ZenRtcHelper.showCommonSnackbar(
          title: "Invalid Contact",
          subtitle: "You cannot add yourself as a contact.",
          icon: Icons.person_off,
          backgroundColor: Colors.red,
        );
        break;
      default:
        ZenRtcHelper.showCommonSnackbar(
          title: "Unable to Add Contact",
          subtitle: "Please try again later.",
          icon: Icons.error_outline,
          backgroundColor: Colors.red,
        );
    }
    return;
  }
  ZenRtcSocket.instance.contacts[selectedContactNotifier.value!.userId] =
      RTCContactModel(
        contactUserId: selectedContactNotifier.value!.userId,
        displayName: selectedContactNotifier.value!.name,
        nickName: nickNameController.text.trim(),
        imageUrl: selectedContactNotifier.value!.imageUrl,
        createdOn: DateTime.now().millisecondsSinceEpoch,
        updatedOn: DateTime.now().millisecondsSinceEpoch,
        description: descriptionController.text.trim(),
        emails: emailControllers
            .map((e) => e.text.trim())
            .where((e) => e.isNotEmpty)
            .toList(),
        phones: phoneControllers
            .map((e) => e.text.trim())
            .where((e) => e.isNotEmpty)
            .toList(),
      );
  ZenRtcHelper.showCommonSnackbar(
    title: "Contact Added",
    subtitle: "The contact has been added successfully.",
    icon: Icons.check_circle_outline,
    backgroundColor: Colors.green,
  );
  Navigator.of(context).pop();
  // currentStep.value = 0;
}