MessagesView.java

  1. package com.privacydashboard.application.views.messages;

  2. import com.privacydashboard.application.data.entity.User;
  3. import com.privacydashboard.application.data.service.CommunicationService;
  4. import com.privacydashboard.application.data.service.DataBaseService;
  5. import com.privacydashboard.application.security.AuthenticatedUser;
  6. import com.privacydashboard.application.views.MainLayout;
  7. import com.privacydashboard.application.views.usefulComponents.MyDialog;
  8. import com.vaadin.flow.component.UI;
  9. import com.vaadin.flow.component.avatar.Avatar;
  10. import com.vaadin.flow.component.button.Button;
  11. import com.vaadin.flow.component.combobox.ComboBox;
  12. import com.vaadin.flow.component.grid.Grid;
  13. import com.vaadin.flow.component.grid.GridVariant;
  14. import com.vaadin.flow.component.html.Div;
  15. import com.vaadin.flow.component.html.Span;
  16. import com.vaadin.flow.component.orderedlayout.FlexComponent;
  17. import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
  18. import com.vaadin.flow.component.orderedlayout.VerticalLayout;
  19. import com.vaadin.flow.component.textfield.TextField;
  20. import com.vaadin.flow.data.value.ValueChangeMode;
  21. import com.vaadin.flow.router.*;

  22. import javax.annotation.security.PermitAll;
  23. import java.util.List;

  24. @PageTitle("Messages")
  25. @Route(value="messages", layout = MainLayout.class)
  26. @PermitAll
  27. public class MessagesView extends Div implements AfterNavigationObserver{
  28.     private final DataBaseService dataBaseService;
  29.     private final AuthenticatedUser authenticatedUser;
  30.     private final CommunicationService communicationService;

  31.     private final Grid<User> grid=new Grid<>();
  32.     private final TextField searchText=new TextField();
  33.     private final Button newMessageButton=new Button("New Message");
  34.     private final HorizontalLayout headerLayout=new HorizontalLayout(searchText, newMessageButton);

  35.     private final MyDialog newMessageDialog=new MyDialog();
  36.     private final ComboBox<User> contactComboBox= new ComboBox<>("Contacts");

  37.     public MessagesView(DataBaseService dataBaseService, AuthenticatedUser authenticatedUser, CommunicationService communicationService) {
  38.         this.dataBaseService= dataBaseService;
  39.         this.authenticatedUser= authenticatedUser;
  40.         this.communicationService= communicationService;

  41.         addClassName("grid-view");
  42.         initializeHeaderLayout();
  43.         initializeGrid();
  44.         initializeNewMessageDialog();
  45.         add(headerLayout, grid);
  46.     }

  47.     private void initializeGrid(){
  48.         grid.addThemeVariants(GridVariant.LUMO_NO_BORDER, GridVariant.LUMO_NO_ROW_BORDERS);
  49.         grid.addComponentColumn(this::showContact);
  50.     }

  51.     private void initializeHeaderLayout(){
  52.         newMessageButton.addClickListener(e-> newMessageDialog.open());
  53.         searchText.setPlaceholder("Search...");
  54.         searchText.setValueChangeMode(ValueChangeMode.LAZY);
  55.         searchText.addValueChangeListener(e-> updateGrid());
  56.         searchText.addClassName("search");
  57.         headerLayout.setAlignItems(FlexComponent.Alignment.CENTER);
  58.     }

  59.     private void initializeNewMessageDialog(){
  60.         contactComboBox.setItems(dataBaseService.getAllContactsFromUser(authenticatedUser.getUser()));
  61.         contactComboBox.setItemLabelGenerator(User::getName);

  62.         Button newMessageButton=new Button("Continue", e-> confirmNewMessage());

  63.         newMessageDialog.setTitle("Select a contact");
  64.         newMessageDialog.setContent(new VerticalLayout(contactComboBox));
  65.         newMessageDialog.setContinueButton(newMessageButton);
  66.     }

  67.     private void confirmNewMessage(){
  68.         if(contactComboBox.getValue()==null){
  69.             return;
  70.         }
  71.         newMessageDialog.close();
  72.         goToConversation(contactComboBox.getValue());
  73.     }

  74.     private HorizontalLayout showContact(User contact){
  75.         Avatar avatar = new Avatar(contact.getName(), contact.getProfilePictureUrl());
  76.         Span name= new Span(contact.getName());
  77.         name.addClassName("name");
  78.         HorizontalLayout card = new HorizontalLayout(avatar, name);
  79.         card.addClassName("card");
  80.         card.addClassName("canOpen");
  81.         card.addClickListener(e-> goToConversation(contact));
  82.         return card;
  83.     }

  84.     private void goToConversation(User contact){
  85.         communicationService.setContact(contact);
  86.         UI.getCurrent().navigate(SingleConversationView.class);
  87.     }

  88.     private void updateGrid(){
  89.         List<User> usersConversation;
  90.         if(searchText.getValue()==null || searchText.getValue().length()==0){
  91.             usersConversation=dataBaseService.getUserConversationFromUser(authenticatedUser.getUser());
  92.         }
  93.         else{
  94.             usersConversation=dataBaseService.getUserConversationFromUserFilterByName(authenticatedUser.getUser(), searchText.getValue());
  95.         }
  96.         grid.setItems(usersConversation);
  97.     }

  98.     @Override
  99.     public void afterNavigation(AfterNavigationEvent event) {
  100.         updateGrid();
  101.     }

  102. }