001package edu.pdx.cs410J.grader.poa;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import com.google.inject.Inject;
006import edu.pdx.cs410J.grader.gradebook.Assignment;
007import edu.pdx.cs410J.grader.gradebook.GradeBook;
008import edu.pdx.cs410J.grader.gradebook.Student;
009
010import java.io.File;
011
012public class GradeBookPresenter {
013  private final GradeBookView view;
014  private final EventBus bus;
015  private GradeBook gradeBook;
016
017  @Inject
018  public GradeBookPresenter(EventBus bus, GradeBookView view) {
019    this.bus = bus;
020    this.view = view;
021
022    bus.register(this);
023
024    view.addGradeBookFileListener(this::publishLoadGradeBookEvent);
025    view.addSaveGradeBookListener(this::publishSaveGradeBookEvent);
026  }
027
028  private void publishSaveGradeBookEvent() {
029    this.bus.post(new SaveGradeBook(this.gradeBook));
030  }
031
032  private void publishLoadGradeBookEvent(File file) {
033    this.bus.post(new LoadGradeBook(file));
034  }
035
036  @Subscribe
037  public void noteGradeBook(GradeBookLoaded event) {
038    gradeBook = event.getGradeBook();
039    this.view.setGradeBookName(gradeBook.getClassName());
040    this.view.canSaveGradeBook(false);
041  }
042
043  @Subscribe
044  public void recordGradeInGradeBook(RecordGradeEvent event) {
045    Student student = event.getStudent();
046    Assignment assignment = event.getAssignment();
047    student.setGrade(assignment, event.getScore());
048
049    if (event.isLate()) {
050      student.addLate(assignment);
051    }
052
053    this.view.canSaveGradeBook(true);
054  }
055}