AllChatsBloc constructor

AllChatsBloc({
  1. required LoggedIn? loggedIn,
  2. required String thisMemberId,
  3. required String appId,
  4. required ChatBloc chatBloc,
  5. bool? paged,
  6. String? orderBy,
  7. bool? descending,
  8. EliudQuery? eliudQuery,
  9. required RoomRepository roomRepository,
  10. int roomLimit = 5,
})

Implementation

AllChatsBloc(
    {required this.loggedIn,
    required this.thisMemberId,
    required this.appId,
    required this.chatBloc,
    this.paged,
    this.orderBy,
    this.descending,
    this.eliudQuery,
    required RoomRepository roomRepository,
    this.roomLimit = 5})
    : _roomRepository = roomRepository,
      super(AllChatsLoading()) {
  on<LoadAllChats>((event, emit) {
    _mapLoadAllChatsWithDetailsToState();
  });

  on<NewPage>((event, emit) {
    pages = pages +
        1; // it doesn't matter so much if we increase pages beyond the end
    _mapLoadAllChatsWithDetailsToState();
  });

  on<AddAllChats>((event, emit) {
    _mapAddAllChatsToState(event);
  });

  on<UpdateAllChats>((event, emit) async {
    await _mapUpdateAllChatsToState(event);
  });

  on<DeleteAllChats>((event, emit) async {
    await _mapDeleteAllChatsToState(event);
  });

  on<AllChatsUpdated>((event, emit) async {
    var theState = state;
    if (theState is AllChatsLoaded) {
      var currentRoom = theState.currentRoom;
      if (currentRoom != null) {
        for (var selectedEnhancedRoom in theState.enhancedRoomModels) {
          if (selectedEnhancedRoom.roomModel.documentID ==
              currentRoom.documentID) {
            chatBloc.add(SelectChatEvent(selectedEnhancedRoom));
          }
        }
      }
      emit(_mapAllChatsUpdatedToState(event, currentRoom));
    } else {
      RoomModel? currentRoom;
      emit(_mapAllChatsUpdatedToState(event, currentRoom));
    }
  });

  on<SelectChat>((event, emit) async {
    var theState = state;
    if (theState is AllChatsLoaded) {
      for (var selectedEnhancedRoom in theState.enhancedRoomModels) {
        if (selectedEnhancedRoom.roomModel.documentID ==
            event.selected.documentID) {
//            chatListBloc.add(SelectChatEvent(selectedEnhancedRoom)); <<<< RECOVER THIS LINE
          chatBloc.add(OpenChatWithMembersEvent(event.selected.members!));
        }
      }
      emit(AllChatsLoaded(
          enhancedRoomModels: theState.enhancedRoomModels,
          mightHaveMore: theState.mightHaveMore,
          currentRoom: event.selected));
    } else {
      throw Exception("Unexpected state");
    }
  });

  on<BlockMember>((event, emit) async {
    var theState = state;
    if (loggedIn != null) {
      loggedIn!.registerBlockedMember(event.memberId);
      // reload all chats
      add(LoadAllChats());
      if (theState is AllChatsLoaded) {
        emit(theState.copyWith(newCurrentRoom: null));
      }
    }
  });

  on<NewLastReadEvent>((event, emit) async {
    var theState = state;
    if (theState is AllChatsLoaded) {
      List<EnhancedRoomModel> newEnhancedRoomModels = [];
      for (var enhancedRoomModel in theState.enhancedRoomModels) {
        if (enhancedRoomModel.roomModel.documentID == event.roomId) {
          EnhancedRoomModel newEnhancedModel;
          if (event.memberId == thisMemberId) {
            newEnhancedModel = enhancedRoomModel.copyWith(
                timeStampThisMemberRead: event.lastRead);
          } else {
            newEnhancedModel = enhancedRoomModel.copyWith(
                otherMemberLastRead: event.lastRead);
          }
          newEnhancedRoomModels.add(newEnhancedModel);
          chatBloc.add(UpdateEnhancedRoomModel(newEnhancedModel));
        } else {
          newEnhancedRoomModels.add(enhancedRoomModel);
        }
      }
      emit(AllChatsLoaded(
          mightHaveMore: theState.mightHaveMore,
          currentRoom: theState.currentRoom,
          enhancedRoomModels: newEnhancedRoomModels));
    }
  });
}