Skip to content

Commit

Permalink
♻️ refactor room to be a record closes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Mar 31, 2024
1 parent 5ca3c18 commit e772496
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
27 changes: 27 additions & 0 deletions src/main/java/swiss/fihlon/apus/conference/Room.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Apus - A social wall for conferences with additional features.
* Copyright (C) Marcus Fihlon and the individual contributors to Apus.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package swiss.fihlon.apus.conference;

import org.jetbrains.annotations.NotNull;

public record Room(@NotNull String name) implements Comparable<Room> {
@Override
public int compareTo(@NotNull Room other) {
return name.compareTo(other.name);
}
}
2 changes: 1 addition & 1 deletion src/main/java/swiss/fihlon/apus/conference/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;

public record Session(@NotNull String id, @NotNull LocalDateTime startDate, @NotNull LocalDateTime endDate,
@NotNull String room, @NotNull String title, @NotNull List<Speaker> speakers,
@NotNull Room room, @NotNull String title, @NotNull List<Speaker> speakers,
@NotNull Language language)
implements Comparable<Session> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import swiss.fihlon.apus.conference.Language;
import swiss.fihlon.apus.conference.Room;
import swiss.fihlon.apus.conference.Session;
import swiss.fihlon.apus.conference.SessionImportException;
import swiss.fihlon.apus.conference.Speaker;
Expand Down Expand Up @@ -75,11 +76,11 @@ public List<Session> getSessions() {
final JSONObject rooms = day.getJSONObject("rooms");
final Iterator<String> roomKeys = rooms.keys();
while (roomKeys.hasNext()) {
final String room = roomKeys.next();
if (room.contains("info°center") || room.contains("ring°kartbahn") || room.contains("ring°boulevard")) {
final String roomName = roomKeys.next();
if (roomName.contains("info°center") || roomName.contains("ring°kartbahn") || roomName.contains("ring°boulevard")) {
continue;
}
final JSONArray slots = rooms.getJSONArray(room);
final JSONArray slots = rooms.getJSONArray(roomName);
for (int slotCounter = 0; slotCounter < slots.length(); slotCounter++) {
final JSONObject slot = slots.getJSONObject(slotCounter);
lastSlotId = slot.getInt("id");
Expand All @@ -102,7 +103,7 @@ public List<Session> getSessions() {
String.format("%s:%d", acronym, slot.getInt("id")),
LocalDateTime.of(date, startTime),
LocalDateTime.of(date, startTime).plus(duration),
room,
new Room(roomName),
title,
speakers.stream().map(Speaker::new).toList(),
language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import swiss.fihlon.apus.conference.Room;
import swiss.fihlon.apus.conference.Session;
import swiss.fihlon.apus.conference.doag.ConferenceAPI;
import swiss.fihlon.apus.configuration.Configuration;
Expand All @@ -41,7 +42,7 @@ public final class ConferenceService {
private final Configuration configuration;
private final ScheduledFuture<?> updateScheduler;
private List<Session> sessions;
private List<String> rooms;
private List<Room> rooms;

public ConferenceService(@NotNull final TaskScheduler taskScheduler,
@NotNull final Configuration configuration) {
Expand Down Expand Up @@ -76,10 +77,10 @@ public List<Session> getAllSessions() {
}
}

public Map<String, List<Session>> getRoomsWithSessions() {
public Map<Room, List<Session>> getRoomsWithSessions() {
final LocalDateTime now = LocalDateTime.now();
final Map<String, List<Session>> roomsWithSessions = new TreeMap<>();
for (final String room : rooms) {
final Map<Room, List<Session>> roomsWithSessions = new TreeMap<>();
for (final Room room : rooms) {
roomsWithSessions.put(room, new ArrayList<>());
}
final List<Session> runningAndNextSessions = sessions.stream()
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/swiss/fihlon/apus/ui/view/ConferenceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.vaadin.flow.component.notification.Notification;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.TaskScheduler;
import swiss.fihlon.apus.conference.Room;
import swiss.fihlon.apus.conference.Session;
import swiss.fihlon.apus.service.ConferenceService;

Expand Down Expand Up @@ -77,7 +78,7 @@ private void updateConferenceSessions() {
final var today = LocalDate.now();
final var roomCounter = new AtomicInteger(0);
final var roomsWithSessions = conferenceService.getRoomsWithSessions().entrySet();
for (final Map.Entry<String, List<Session>> roomWithSession : roomsWithSessions) {
for (final Map.Entry<Room, List<Session>> roomWithSession : roomsWithSessions) {
if (roomCounter.get() >= MAX_ROOMS_IN_VIEW) {
Notification.show(String.format(getTranslation("conference.error.rooms"), roomsWithSessions.size()));
break;
Expand Down Expand Up @@ -107,10 +108,10 @@ private Component createLegend() {
}

@NotNull
private static SessionView createSessionView(@NotNull final Map.Entry<String, List<Session>> roomWithSession,
private static SessionView createSessionView(@NotNull final Map.Entry<Room, List<Session>> roomWithSession,
@NotNull final LocalDate today,
@NotNull final AtomicInteger roomCounter) {
final String room = roomWithSession.getKey();
final Room room = roomWithSession.getKey();
final List<Session> sessions = roomWithSession.getValue();
final Session session = sessions.isEmpty() ? null : sessions.getFirst();
final SessionView sessionView;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/swiss/fihlon/apus/ui/view/SessionView.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import swiss.fihlon.apus.conference.Language;
import swiss.fihlon.apus.conference.Room;
import swiss.fihlon.apus.conference.Session;
import swiss.fihlon.apus.conference.Speaker;

Expand All @@ -39,14 +40,14 @@
@CssImport(value = "./themes/apus/views/session-view.css")
public final class SessionView extends Div {

private final String room;
private final transient Room room;
private final String title;
private final transient List<Speaker> speakers;
private final LocalTime startTime;
private final LocalTime endTime;
private final Language language;

public SessionView(@NotNull final String room) {
public SessionView(@NotNull final Room room) {
this(room, null, List.of(), null, null, null);
}

Expand All @@ -61,7 +62,7 @@ public SessionView(@NotNull final Session session) {
);
}

public SessionView(@NotNull final String room,
public SessionView(@NotNull final Room room,
@Nullable final String title,
@NotNull final List<Speaker> speakers,
@Nullable final LocalTime startTime,
Expand Down Expand Up @@ -119,7 +120,7 @@ private Component createSpeakersComponent() {

@NotNull
private Component createRoomComponent() {
return new Div(new Text(String.format("\uD83D\uDCCD %s", room)));
return new Div(new Text(String.format("\uD83D\uDCCD %s", room.name())));
}

@NotNull
Expand Down

0 comments on commit e772496

Please sign in to comment.