Skip to content

Instantly share code, notes, and snippets.

@alperefesahin
Created January 8, 2023 13:07
Show Gist options
  • Save alperefesahin/797c15e663e7f0a6cba35eb979bc8e7c to your computer and use it in GitHub Desktop.
Save alperefesahin/797c15e663e7f0a6cba35eb979bc8e7c to your computer and use it in GitHub Desktop.
Future<void> createNewChannel({
required bool isCreateNewChatPageForCreatingGroup,
}) async {
if (state.isInProgress) {
return;
}
String channelImageUrl = "";
String channelName = state.channelName;
final listOfMemberIDs = {...state.listOfSelectedUserIDs};
final currentUserId = _authCubit.state.authUser.id;
listOfMemberIDs.add(currentUserId);
if (isCreateNewChatPageForCreatingGroup) {
// If page opened for creating group case:
// We can directly enter the group name and upload the image.
channelName = state.channelName;
channelImageUrl = randomGroupProfilePhoto;
} else if (!isCreateNewChatPageForCreatingGroup) {
// If page opened for creating [1-1 chat] case:
// Channel name will be selected user's name, and the image of the channel
// will be image of the selected user.
if (listOfMemberIDs.length == 2) {
final String selectedUserId =
listOfMemberIDs.where((memberIDs) => memberIDs != currentUserId).toList().first;
final selectedUserFromFirestore =
await _firebaseFirestore.userDocument(userId: selectedUserId);
final getSelectedUserDataFromFirestore = await selectedUserFromFirestore.get();
final selectedUserData = getSelectedUserDataFromFirestore.data() as Map<String, dynamic>?;
channelName = selectedUserData?["displayName"];
channelImageUrl = selectedUserData?["photoUrl"];
}
}
final isChannelNameValid =
!isCreateNewChatPageForCreatingGroup ? true : state.isChannelNameValid;
if (listOfMemberIDs.length >= 2 && isChannelNameValid) {
emit(state.copyWith(isInProgress: true, isChannelCreated: false));
await _chatService.createNewChannel(
listOfMemberIDs: listOfMemberIDs.toList(),
channelName: channelName,
channelImageUrl: channelImageUrl,
);
emit(state.copyWith(isInProgress: false, isChannelCreated: true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment