ChatBloc constructor

ChatBloc()

Implementation

ChatBloc() : super(const ChatState()) {
  on<CheckChatId>((event, emit) async {
    emit(state.copyWith(
      isMessageLoading: true,
    ));
    final res = await chatRepository.getChat(sellerId: event.sellerId);
    res.fold((l) {
      emit(state.copyWith(
        isMessageLoading: false,
      ));
    }, (r) {
      emit(state.copyWith(
        isMessageLoading: false,
      ));
    });
  });

  on<CreateAndSendMessage>((event, emit) async {
    final res = await chatRepository.createChat(id: event.userId);
    res.fold((l) async {
      event.onSuccess();
    }, (r) {
      AppHelper.errorSnackBar(context: event.context, message: r);
    });
  });

  on<SendMessage>((event, emit) {
    chatRepository.sendMessage(
        chatDocId: event.chatId ?? "",
        message: MessageModel(
            message: event.message,
            senderId: LocalStorage.getUser().id ?? 0,
            doc: ""));
  });

  on<SendImage>((event, emit) async {});

  on<EditMessage>((event, emit) {
    chatRepository.editMessage(
        chatDocId: event.chatId ?? "",
        message: event.message,
        docId: event.messageId);
  });

  on<ReplyMessage>((event, emit) {
    chatRepository.replyMessage(
      chatDocId: event.chatId ?? "",
      message: MessageModel(
          message: event.message,
          senderId: LocalStorage.getUser().id ?? 0,
          doc: "",
          replyDocId: event.messageId),
    );
  });

  on<DeleteMessage>((event, emit) {
    chatRepository.deleteMessage(
        chatDocId: event.chatId ?? "", docId: event.messageId);
  });
}