001package edu.pdx.cs410J.grader.poa;
002
003import com.google.common.annotations.VisibleForTesting;
004import com.google.common.eventbus.EventBus;
005import com.google.common.eventbus.Subscribe;
006import com.google.inject.Inject;
007import com.google.inject.Singleton;
008
009@Singleton
010public class EmailCredentialsPresenter {
011  @VisibleForTesting
012  static final String ENTER_CREDENTIALS_MESSAGE = "Enter email address and password";
013
014  private final EventBus bus;
015  private final EmailCredentialsView view;
016  private String emailAddress;
017  private String password;
018
019  @Inject
020  public EmailCredentialsPresenter(EventBus bus, EmailCredentialsView view) {
021    this.bus = bus;
022    this.view = view;
023
024    this.bus.register(this);
025
026    this.view.addEmailAddressValueListener(this::setEmailAddress);
027    this.view.addPasswordValueListener(this::setPassword);
028    this.view.addSubmitCredentialsListener(this::fireEmailCredentialsEvent);
029  }
030
031  private void fireEmailCredentialsEvent() {
032    if (this.emailAddress != null && this.password != null) {
033      this.bus.post(new EmailCredentials(this.emailAddress, this.password));
034    }
035  }
036
037  @Subscribe
038  public void displayEmailCredentialsView(DownloadPOASubmissionsRequest event) {
039    if (event.getEmailAddress() != null) {
040      this.view.setEmailAddress(event.getEmailAddress());
041    }
042    if (event.getPassword() != null) {
043      this.view.setPassword(event.getPassword());
044    }
045    fireStatusMessageEvent(ENTER_CREDENTIALS_MESSAGE);
046    this.view.setIsVisible(true);
047  }
048
049  private void fireStatusMessageEvent(String message) {
050    this.bus.post(new StatusMessage(message));
051  }
052
053  public String getEmailAddress() {
054    return emailAddress;
055  }
056
057  @VisibleForTesting
058  void setEmailAddress(String emailAddress) {
059    this.emailAddress = emailAddress;
060  }
061
062  public String getPassword() {
063    return password;
064  }
065
066  @VisibleForTesting
067  void setPassword(String password) {
068    this.password = password;
069  }
070}