Skip to content

Commit

Permalink
Merge pull request #86 from CSC207-2022F-UofT/feature-3-updates-fixes
Browse files Browse the repository at this point in the history
Calendar Screen Clean Architecture bug fix
  • Loading branch information
celinelumqr authored Dec 7, 2022
2 parents 71404fd + 088f9c0 commit c7d2c9f
Show file tree
Hide file tree
Showing 20 changed files with 501 additions and 111 deletions.
3 changes: 0 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
InstructorChooseTaskCreateScreen chooseInstructortask = new InstructorChooseTaskCreateScreen(screens, cardLayout);
screens.add("instructorTaskCreate", chooseInstructortask);

CalendarScreen calendarScreen = new CalendarScreen(screens, cardLayout);
screens.add("calendar", calendarScreen);

ScheduleCTScreen scheduleCTScreen = new ScheduleCTScreen(scheduleCTController, screens, cardLayout);
screens.add("scheduleCT", scheduleCTScreen);
screens.add("scheduleCTView", (Component) scheduleCTOutputView);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/screens/StudentMainScreen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package screens;

import screens.calendar_scheduler.WorkingHoursController;
import screens.calendar_scheduler.CalendarPresenter;
import screens.calendar_scheduler.CalendarScreen;
import screens.calendar_scheduler.WorkingHoursPresenter;
import screens.login_registration.LogoutController;
import screens.task_management.todolist_screens.ToDoListPresenter;
import screens.task_management.todolist_screens.ToDoListScreen;
Expand Down Expand Up @@ -99,6 +103,17 @@ public void actionPerformed(ActionEvent evt) {
cardLayout.show(screens, "toDoList");
}
if (evt.getSource() == calendar) {
// Refresh calendar screen
CalendarPresenter calendarPresenter = new CalendarPresenter();
WorkingHoursPresenter workingHoursPresenter = new WorkingHoursPresenter();
WorkingHoursController workingHoursController = new WorkingHoursController();
ToDoListInteractor toDoListInteractor = new ToDoListInteractor(calendarPresenter);
calendarPresenter.setToDoListInput(toDoListInteractor);

CalendarScreen calendarScreen = new CalendarScreen(screens, cardLayout, calendarPresenter,
workingHoursController, workingHoursPresenter);
screens.add("calendar", calendarScreen);

cardLayout.show(screens, "calendar");
}
if (evt.getSource() == progressTracker) {
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/screens/calendar_scheduler/CalendarPresenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package screens.calendar_scheduler;

import screens.task_management.todolist_screens.ToDoListFail;
import use_cases.task_management.todolist_use_case.*;

import java.util.ArrayList;

public class CalendarPresenter implements ToDoListOutputBoundary {

private ToDoListInputBoundary toDoListInput;

public void setToDoListInput(ToDoListInputBoundary toDoListInput) {
this.toDoListInput = toDoListInput;
}

public ArrayList<ArrayList<String>> getToDoList() {
ToDoListResponseModel responseModel = toDoListInput.getToDoList();
return responseModel.getToDoListView();
}

@Override
public ToDoListResponseModel display(ToDoListResponseModel responseModel) {

// Initialise task list array
ArrayList<ArrayList<String>> taskList = new ArrayList<>();

// Iterate through each task in to-do list
for (ToDoListItem item : responseModel.getToDoList()) {

// Intialise task item array
ArrayList<String> taskItem = new ArrayList<>();

// Get title, task type, start time, end time, recurrence and frequency of task item
if (item.getTaskType().equals(TaskType.TEST)) {
taskItem.add(item.getTaskTitle());
taskItem.add("Test");
taskItem.add(item.getStartTime().toString());
taskItem.add(item.getEndTime().toString());
taskItem.add(null);
taskItem.add(null);
} else if (item.getTaskType().equals(TaskType.EVENT)) {
taskItem.add(item.getTaskTitle());
taskItem.add("Event");
taskItem.add(item.getStartTime().toString());
taskItem.add(item.getEndTime().toString());
taskItem.add(item.isRecurring());
taskItem.add(item.getFrequency());
} else {
taskItem.add(item.getTaskTitle());
taskItem.add("Assignment");
taskItem.add(null);
taskItem.add(null);
taskItem.add(null);
taskItem.add(null);
}

// Add task item to task list array
taskList.add(taskItem);
}

responseModel.setToDoListView(taskList);

return responseModel;

}

@Override
public ToDoListResponseModel failView(String error) {
throw new ToDoListFail(error);
}
}
44 changes: 17 additions & 27 deletions src/main/java/screens/calendar_scheduler/CalendarScreen.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package screens.calendar_scheduler;

import entities.CurrentUser;
import entities.StudentUser;
import entities.Task;
import entities.TaskMap;
import use_cases.task_management.todolist_use_case.ToDoListResponseModel;

import javax.swing.*;
import java.awt.*;
Expand All @@ -12,8 +9,6 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Objects;

public class CalendarScreen extends JPanel implements ActionListener {
Expand All @@ -33,30 +28,23 @@ public class CalendarScreen extends JPanel implements ActionListener {
JPanel screens;
CardLayout screenLayout;

/**
* The current entities
*/
StudentUser user;
ArrayList<Task> allTasks;
CalendarPresenter presenter;
WorkingHoursController workingHoursController;
WorkingHoursPresenter workingHoursPresenter;

/**
* The window of the screen for the Calendar view
*/
public CalendarScreen(JPanel screens, CardLayout screenLayout) {
public CalendarScreen(JPanel screens, CardLayout screenLayout, CalendarPresenter presenter,
WorkingHoursController workingHoursController, WorkingHoursPresenter workingHoursPresenter) {

this.user = (StudentUser) CurrentUser.getCurrentUser();
this.presenter = presenter;
this.workingHoursController = workingHoursController;
this.workingHoursPresenter = workingHoursPresenter;
this.screens = screens;
this.screenLayout = screenLayout;

// Get the user's existing tasks
ArrayList<String> taskIDs = user.getToDoList();
ArrayList<Task> allTasks = new ArrayList<>();
for (String taskID : taskIDs) {
//get the Task object for this task from the entity TaskMap static variable
Task existingTask = TaskMap.getTaskMap().get(taskID);
allTasks.add(existingTask);
}
this.allTasks = allTasks;
ArrayList<ArrayList<String>> taskList = presenter.getToDoList();

// Create label for title of screen
JLabel title = new JLabel("Calendar");
Expand All @@ -76,11 +64,11 @@ public CalendarScreen(JPanel screens, CardLayout screenLayout) {
LocalDate currDate = LocalDate.now();
cardLayout = new CardLayout();
viewPanel = new JPanel(cardLayout);
DayViewPanel dayViewPanel = new DayViewPanel(currDate, this.user, this.allTasks);
DayViewPanel dayViewPanel = new DayViewPanel(currDate, taskList, this.workingHoursPresenter);
viewPanel.add("day", dayViewPanel);
WeekViewPanel weekViewPanel = new WeekViewPanel(currDate, this.user, this.allTasks);
WeekViewPanel weekViewPanel = new WeekViewPanel(currDate, taskList, this.workingHoursPresenter);
viewPanel.add("week", weekViewPanel);
MonthViewPanel monthViewPanel = new MonthViewPanel(currDate, this.allTasks);
MonthViewPanel monthViewPanel = new MonthViewPanel(currDate, taskList);
viewPanel.add("month", monthViewPanel);


Expand Down Expand Up @@ -118,7 +106,8 @@ public void actionPerformed(ActionEvent e) {
// Get current working hours
LocalTime currStartTime;
LocalTime currEndTime;
ArrayList<LocalTime> currWorkingHours = user.getWorkingHours();
ArrayList<LocalTime> currWorkingHours = workingHoursPresenter.getWorkingHours();

if (currWorkingHours.size() == 0) {
currStartTime = LocalTime.now();
currEndTime = LocalTime.now();
Expand Down Expand Up @@ -181,7 +170,8 @@ public void actionPerformed(ActionEvent e) {
ArrayList<LocalTime> workingHours = new ArrayList<>();
workingHours.add(LocalTime.of(startHour, startMinute));
workingHours.add(LocalTime.of(endHour, endMinute));
this.user.setWorkingHours(workingHours);

this.workingHoursController.setWorkingHours(workingHours);
}

// Trigger button to return to main dashboard
Expand Down
99 changes: 67 additions & 32 deletions src/main/java/screens/calendar_scheduler/DayViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

public class DayViewPanel extends JPanel {

public DayViewPanel(LocalDate date, StudentUser user, ArrayList<Task> allTasks) {
public DayViewPanel(LocalDate date, ArrayList<ArrayList<String>> allTasks, WorkingHoursPresenter workingHoursPresenter) {

// Get date details
int day = date.getDayOfMonth();
int year = date.getYear();

// Get working hours
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime startTime = LocalTime.parse("07:00", formatter);
LocalTime endTime = LocalTime.parse("23:00", formatter);
if (user != null) {
ArrayList<LocalTime> workingHours = user.getWorkingHours();
if (workingHours.size() != 0) {
startTime = workingHours.get(0);
endTime = workingHours.get(1);
}
LocalTime workingStartTime = LocalTime.parse("07:00", formatter);
LocalTime workingEndTime = LocalTime.parse("23:00", formatter);
ArrayList<LocalTime> workingHours = workingHoursPresenter.getWorkingHours();
if (workingHours != null) {
workingStartTime = workingHours.get(0);
workingEndTime = workingHours.get(1);
}

// Create title panel
Expand Down Expand Up @@ -84,14 +84,14 @@ public DayViewPanel(LocalDate date, StudentUser user, ArrayList<Task> allTasks)
datePanel.add(dayPanel, dayConstraint);

// Create hour panels
int startTimeHour = startTime.getHour();
int endTimeHour = endTime.getHour();
int workingStartTimeHour = workingStartTime.getHour();
int workingEndTimeHour = workingEndTime.getHour();
ArrayList<String> hours = new ArrayList<>();
hours.add("");
for (int i = startTimeHour; i <= endTimeHour; i++) {
for (int i = workingStartTimeHour; i <= workingEndTimeHour; i++) {
hours.add(Integer.toString(i));
}
for (int i = 0; i <= endTimeHour - startTimeHour; i++) {
for (int i = 0; i <= workingEndTimeHour - workingStartTimeHour; i++) {
GridBagConstraints c = new GridBagConstraints();
JPanel hourPanel = new JPanel();
hourPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
Expand All @@ -103,33 +103,68 @@ public DayViewPanel(LocalDate date, StudentUser user, ArrayList<Task> allTasks)
}

// Create task panels
for (Task task : allTasks) {
if (task instanceof Timeblockable) {
for (ArrayList<String> task : allTasks) {
if (!Objects.equals(task.get(1), "Assignment")) {

LocalDateTime taskStartTime = ((Timeblockable) task).getTimeBlock()[0];
LocalDateTime taskEndTime = ((Timeblockable) task).getTimeBlock()[1];
ArrayList<LocalDateTime> startTimes = new ArrayList<>();
ArrayList<LocalDateTime> endTimes = new ArrayList<>();
LocalDateTime taskStartTime = LocalDateTime.parse(task.get(2));
LocalDateTime taskEndTime = LocalDateTime.parse(task.get(3));
long taskDuration = ChronoUnit.MINUTES.between(taskStartTime, taskEndTime);

TemporalField field = WeekFields.of(Locale.US).dayOfWeek();
LocalDate startOfWeek = date.with(field, 1);

if (taskStartTime.compareTo(startOfWeek.atStartOfDay()) <= 0
&& startOfWeek.atStartOfDay().compareTo(taskStartTime.plusWeeks(1)) < 0) {
GridBagConstraints c = new GridBagConstraints();
JPanel taskPanel = new JPanel();
taskPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
taskPanel.setBackground(Color.PINK);
JLabel taskLabel = new JLabel(task.getTitle());
taskPanel.add(taskLabel);
// Convert recurring tasks into multiple start/end times
if (Objects.equals(task.get(4), "true")) {
if (Objects.equals(task.get(5), "daily")) {
LocalDateTime dateIterator = taskStartTime;
while (dateIterator.isBefore(taskStartTime.plusYears(1))) {
startTimes.add(dateIterator);
endTimes.add(dateIterator.plusMinutes(taskDuration));
dateIterator = dateIterator.plusDays(1);
}
} else if (Objects.equals(task.get(5), "weekly")) {
LocalDateTime dateIterator = taskStartTime;
while (dateIterator.isBefore(taskStartTime.plusYears(1))) {
startTimes.add(dateIterator);
endTimes.add(dateIterator.plusMinutes(taskDuration));
dateIterator = dateIterator.plusWeeks(1);
}
} else {
LocalDateTime dateIterator = taskStartTime;
while (dateIterator.isBefore(taskStartTime.plusYears(1))) {
startTimes.add(dateIterator);
endTimes.add(dateIterator.plusMinutes(taskDuration));
dateIterator = dateIterator.plusMonths(1);
}
}
} else {
startTimes.add(taskStartTime);
endTimes.add(taskEndTime);
}

for (int i = 0; i < startTimes.size(); i++) {
if (startTimes.get(i).isAfter(date.atStartOfDay())
&& startTimes.get(i).isBefore(date.plusDays(1).atStartOfDay())) {
GridBagConstraints c = new GridBagConstraints();
JPanel taskPanel = new JPanel();
taskPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
taskPanel.setBackground(Color.PINK);
JLabel taskLabel = new JLabel(task.get(0));
taskPanel.add(taskLabel);

int taskStartHour = startTimes.get(i).getHour();
int taskEndHour = endTimes.get(i).getHour();

int taskStartHour = taskStartTime.getHour();
int taskEndHour = taskEndTime.getHour();
c.gridy = taskStartHour - startTimeHour;
c.gridheight = taskEndHour - taskStartTime.getHour();
c.gridy = taskStartHour - workingStartTimeHour +2;
c.gridheight = taskEndHour - taskStartHour;

int taskDayOfWeek = taskStartTime.getDayOfWeek().getValue();
c.gridx = taskDayOfWeek + 1;
int taskDayOfWeek = startTimes.get(i).getDayOfWeek().getValue();
c.gridx = taskDayOfWeek + 1;

datePanel.add(taskPanel, c);
datePanel.add(taskPanel, c);
}
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/screens/calendar_scheduler/MonthViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Objects;

public class MonthViewPanel extends JPanel {

public MonthViewPanel(LocalDate date, ArrayList<Task> allTasks) {
public MonthViewPanel(LocalDate date, ArrayList<ArrayList<String>> allTasks) {

// Get date details
int month = date.getMonthValue() - 1;
Expand Down Expand Up @@ -78,14 +80,14 @@ public MonthViewPanel(LocalDate date, ArrayList<Task> allTasks) {
int testCount = 0;
int eventCount = 0;

for (Task task : allTasks) {
if (task instanceof Assignment) {
for (ArrayList<String> task : allTasks) {
if (Objects.equals(task.get(1), "Assignment")) {
assignmentCount += 1;
}
if (task instanceof Test) {
if (Objects.equals(task.get(1), "Test")) {
testCount += 1;
}
if (task instanceof Event) {
if (Objects.equals(task.get(1), "Event")) {
eventCount += 1;
}
}
Expand Down
Loading

0 comments on commit c7d2c9f

Please sign in to comment.