diff --git a/src/main/java/app/navigation/Navigation.java b/src/main/java/app/navigation/Navigation.java index fb97661..6b23466 100644 --- a/src/main/java/app/navigation/Navigation.java +++ b/src/main/java/app/navigation/Navigation.java @@ -7,6 +7,7 @@ import app.data.entity.Season; import app.data.service.MatchService; import app.data.service.MatchdayService; import app.data.service.SeasonService; +import app.navigation.service.NavigationLevel; import com.vaadin.flow.component.AbstractField; import com.vaadin.flow.component.HasValue; import com.vaadin.flow.component.UI; @@ -15,6 +16,7 @@ import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.WildcardParameter; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.lang.NonNull; import java.util.ArrayList; import java.util.List; @@ -41,9 +43,7 @@ public class Navigation implements HasUrlParameter { private final Select matchdaySelect = new Select<>(); private final Select matchSelect = new Select<>(); - private boolean seasonEnabled = false; - private boolean matchdayEnabled = false; - private boolean matchEnabled = false; + private NavigationLevel navigationLevel = NavigationLevel.SEASON; private final ValidationLabel validationLabel = new ValidationLabel(); @@ -69,32 +69,24 @@ public class Navigation implements HasUrlParameter { this.onlyMatchdaysWithActivity = onlyMatchdaysWithActivity; } - public void setSeasonEnabled(boolean seasonEnabled) { - this.seasonEnabled = seasonEnabled; - } - - public void setMatchdayEnabled(boolean matchdayEnabled) { - this.matchdayEnabled = matchdayEnabled; - } - - public void setMatchEnabled(boolean matchEnabled) { - this.matchEnabled = matchEnabled; + public void setNavigationLevel(@NonNull NavigationLevel navigationLevel) { + this.navigationLevel = navigationLevel; } public void addRunnableToBeRunAfterSelection(Runnable runnable) { runnablesToBeRunAfterSelection.add(runnable); } - public boolean isSeasonEnabled() { - return seasonEnabled; + public boolean seasonEnabled() { + return this.navigationLevel.compareTo(NavigationLevel.SEASON) >= 0; } - public boolean isMatchdayEnabled() { - return matchdayEnabled; + public boolean matchdayEnabled() { + return this.navigationLevel.compareTo(NavigationLevel.MATCHDAY) >= 0; } - public boolean isMatchEnabled() { - return matchEnabled; + public boolean matchEnabled() { + return this.navigationLevel.compareTo(NavigationLevel.MATCH) >= 0; } private String getRoute() { @@ -106,9 +98,9 @@ public class Navigation implements HasUrlParameter { String seasonParam = null; String matchdayParam = null; String matchParam = null; - if (seasonEnabled && seasonSelect.getOptionalValue().isPresent()) seasonParam = seasonSelect.getValue().toString(); - if (matchdayEnabled && matchdaySelect.getOptionalValue().isPresent()) matchdayParam = matchdaySelect.getValue().toString(); - if (matchEnabled && matchSelect.getOptionalValue().isPresent()) matchParam = matchSelect.getValue().toString(); + if (seasonEnabled() && seasonSelect.getOptionalValue().isPresent()) seasonParam = seasonSelect.getValue().toString(); + if (matchdayEnabled() && matchdaySelect.getOptionalValue().isPresent()) matchdayParam = matchdaySelect.getValue().toString(); + if (matchEnabled() && matchSelect.getOptionalValue().isPresent()) matchParam = matchSelect.getValue().toString(); String params = NavigationUtils.getWildcardParam(seasonParam, matchdayParam, matchParam); UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", getRoute(), params)); @@ -116,8 +108,8 @@ public class Navigation implements HasUrlParameter { private HasValue.ValueChangeListener, Season>> seasonSelectValueChangeListener() { return event -> { - if (!seasonEnabled) throw new IllegalStateException("Cannot select season when it is not enabled!"); - if (matchdayEnabled) { + if (!seasonEnabled()) throw new IllegalStateException("Cannot select season when it is not enabled!"); + if (matchdayEnabled()) { fillMatchdaySelectWithData(event.getValue()); autoselectMatchday(); return; @@ -128,8 +120,8 @@ public class Navigation implements HasUrlParameter { private HasValue.ValueChangeListener, Matchday>> matchdaySelectValueChangeListener() { return event -> { - if (!matchdayEnabled) throw new IllegalStateException("Cannot select matchday when it is not enabled!"); - if (matchEnabled) { + if (!matchdayEnabled()) throw new IllegalStateException("Cannot select matchday when it is not enabled!"); + if (matchEnabled()) { fillMatchSelectWithData(event.getValue()); autoselectMatch(); return; @@ -140,7 +132,7 @@ public class Navigation implements HasUrlParameter { private HasValue.ValueChangeListener, Match>> matchSelectValueChangeListener() { return event -> { - if (!matchEnabled) throw new IllegalStateException("Cannot select match when it is not enabled!"); + if (!matchEnabled()) throw new IllegalStateException("Cannot select match when it is not enabled!"); doPostSelectionStuff(); }; } @@ -152,7 +144,7 @@ public class Navigation implements HasUrlParameter { } private void autoselectSeason() { - if (!seasonEnabled) throw new IllegalStateException("This method should not be called when season is not enabled!"); + if (!seasonEnabled()) throw new IllegalStateException("This method should not be called when season is not enabled!"); if (seasonList.isEmpty()) { validationLabel.setText("No Seasons in List!"); validationLabel.setValid(false); @@ -162,7 +154,7 @@ public class Navigation implements HasUrlParameter { } private void autoselectMatchday() { // TODO: add date stuff and choose depending on date instead! - if (!matchdayEnabled) throw new IllegalStateException("This method should not be called when matchday is not enabled!"); + if (!matchdayEnabled()) throw new IllegalStateException("This method should not be called when matchday is not enabled!"); if (matchdayList.isEmpty()) { validationLabel.setText("No Matchdays in List!"); validationLabel.setValid(false); @@ -174,7 +166,7 @@ public class Navigation implements HasUrlParameter { } private void autoselectMatch() { - if (!matchEnabled) throw new IllegalStateException("This method should not be called when match is not enabled!"); + if (!matchEnabled()) throw new IllegalStateException("This method should not be called when match is not enabled!"); if (matchList.isEmpty()) { validationLabel.setText("No Matches in List!"); validationLabel.setValid(false); @@ -210,17 +202,17 @@ public class Navigation implements HasUrlParameter { } private void navigate(Optional seasonParam, Optional matchdayParam, Optional matchParam) { - if (!seasonEnabled) return; + if (!seasonEnabled()) return; Optional season = getSeasonFromParam(seasonParam); if (season.isPresent()) seasonSelect.setValue(season.get()); else autoselectSeason(); - if (!matchdayEnabled) return; + if (!matchdayEnabled()) return; Optional matchday = getMatchdayFromParam(matchdayParam); if (matchday.isPresent()) matchdaySelect.setValue(matchday.get()); else autoselectMatchday(); - if (!matchEnabled) return; + if (!matchEnabled()) return; Optional match = getMatchFromParam(matchParam); if (match.isPresent()) matchSelect.setValue(match.get()); else autoselectMatch(); diff --git a/src/main/java/app/navigation/components/NavigationHeader.java b/src/main/java/app/navigation/components/NavigationHeader.java index 083971e..c62db5c 100644 --- a/src/main/java/app/navigation/components/NavigationHeader.java +++ b/src/main/java/app/navigation/components/NavigationHeader.java @@ -27,13 +27,13 @@ public class NavigationHeader extends HorizontalLayout { private void configureChildren() { removeAll(); - if (navigation.isSeasonEnabled()) { + if (navigation.seasonEnabled()) { add(seasonLabel, navigation.getSeasonSelect()); } - if (navigation.isMatchdayEnabled()) { + if (navigation.matchdayEnabled()) { add(matchdayLabel, navigation.getMatchdaySelect()); } - if (navigation.isMatchEnabled()) { + if (navigation.matchEnabled()) { add(matchLabel, navigation.getMatchSelect()); } } diff --git a/src/main/java/app/navigation/components/button/NextMatchdayButton.java b/src/main/java/app/navigation/components/button/NextMatchdayButton.java index 6e753b6..a1e57d5 100644 --- a/src/main/java/app/navigation/components/button/NextMatchdayButton.java +++ b/src/main/java/app/navigation/components/button/NextMatchdayButton.java @@ -15,7 +15,7 @@ public class NextMatchdayButton extends Button { public NextMatchdayButton(Navigation navigation) { this.navigation = navigation; - if (!navigation.isMatchdayEnabled()) + if (!navigation.matchdayEnabled()) throw new IllegalStateException("Cannot instantiate NextMatchdayButton when Matchdays are not enabled!"); setIcon(new Icon(VaadinIcon.ARROW_RIGHT)); diff --git a/src/main/java/app/navigation/components/button/PrevMatchdayButton.java b/src/main/java/app/navigation/components/button/PrevMatchdayButton.java index c0c0a2a..cd3dd95 100644 --- a/src/main/java/app/navigation/components/button/PrevMatchdayButton.java +++ b/src/main/java/app/navigation/components/button/PrevMatchdayButton.java @@ -15,7 +15,7 @@ public class PrevMatchdayButton extends Button { public PrevMatchdayButton(Navigation navigation) { this.navigation = navigation; - if (!navigation.isMatchdayEnabled()) + if (!navigation.matchdayEnabled()) throw new IllegalStateException("Cannot instantiate PrevMatchdayButton when Matchdays are not enabled!"); setIcon(new Icon(VaadinIcon.ARROW_LEFT)); diff --git a/src/main/java/app/navigation/service/NavigationLevel.java b/src/main/java/app/navigation/service/NavigationLevel.java new file mode 100644 index 0000000..8f714b7 --- /dev/null +++ b/src/main/java/app/navigation/service/NavigationLevel.java @@ -0,0 +1,8 @@ +package app.navigation.service; + +public enum NavigationLevel { + NONE, + SEASON, + MATCHDAY, + MATCH +} diff --git a/src/main/java/app/views/match/MatchView.java b/src/main/java/app/views/match/MatchView.java index 564abc7..45b17b3 100644 --- a/src/main/java/app/views/match/MatchView.java +++ b/src/main/java/app/views/match/MatchView.java @@ -3,6 +3,7 @@ package app.views.match; import app.data.service.MatchService; import app.data.service.MatchdayService; import app.data.service.SeasonService; +import app.navigation.service.NavigationLevel; import app.navigation.service.NavigationService; import app.views.main.MainView; import app.views.navigation.NavigationViewBase; @@ -20,7 +21,7 @@ public class MatchView extends NavigationViewBase { public MatchView(@Autowired NavigationService navigationService, @Autowired MatchService matchService) { - super(navigationService, true, true, true); + super(navigationService, "match", NavigationLevel.MATCH); this.matchService = matchService; addClassName("match-view"); @@ -28,15 +29,6 @@ public class MatchView extends NavigationViewBase { configureLayout(); } - /////////// - // ROUTE // - /////////// - - @Override - public String getRoute() { - return "match"; - } - //////////// // LAYOUT // //////////// diff --git a/src/main/java/app/views/matchday/MatchdayView.java b/src/main/java/app/views/matchday/MatchdayView.java index 23d773b..144146f 100644 --- a/src/main/java/app/views/matchday/MatchdayView.java +++ b/src/main/java/app/views/matchday/MatchdayView.java @@ -1,6 +1,7 @@ package app.views.matchday; import app.data.service.MatchService; +import app.navigation.service.NavigationLevel; import app.navigation.service.NavigationService; import app.views.main.MainView; import app.views.matchday.components.MatchdayComponent; @@ -15,29 +16,16 @@ import org.springframework.beans.factory.annotation.Autowired; @PageTitle("Schachliga DACH - Results - Matchdays") public class MatchdayView extends NavigationViewBase { - private final MatchService matchService; - private MatchdayComponent matchdayComponent; - public MatchdayView(@Autowired NavigationService navigationService, - @Autowired MatchService matchService) { - super(navigationService, true, true, false); - this.matchService = matchService; + public MatchdayView(@Autowired NavigationService navigationService) { + super(navigationService, "matchday", NavigationLevel.MATCHDAY); addClassName("matchday-view"); configureLayout(); } - /////////// - // ROUTE // - /////////// - - @Override - public String getRoute() { - return "matchday"; - } - //////////// // LAYOUT // //////////// diff --git a/src/main/java/app/views/navigation/NavigationViewBase.java b/src/main/java/app/views/navigation/NavigationViewBase.java index f81e642..410f0e8 100644 --- a/src/main/java/app/views/navigation/NavigationViewBase.java +++ b/src/main/java/app/views/navigation/NavigationViewBase.java @@ -2,6 +2,7 @@ package app.views.navigation; import app.navigation.Navigation; import app.navigation.components.NavigationHeader; +import app.navigation.service.NavigationLevel; import app.navigation.service.NavigationService; import com.vaadin.flow.component.orderedlayout.FlexComponent; import com.vaadin.flow.component.orderedlayout.VerticalLayout; @@ -16,14 +17,11 @@ public abstract class NavigationViewBase extends VerticalLayout implements HasUr protected final NavigationHeader navigationHeader; protected NavigationViewBase(@Autowired NavigationService navigationService, - boolean seasonEnabled, - boolean matchdayEnabled, - boolean matchEnabled) { + String route, + NavigationLevel navigationLevel) { this.navigation = navigationService.getNewNavigation(); - navigation.setRoute(getRoute()); - navigation.setSeasonEnabled(seasonEnabled); - navigation.setMatchdayEnabled(matchdayEnabled); - navigation.setMatchEnabled(matchEnabled); + navigation.setRoute(route); + navigation.setNavigationLevel(navigationLevel); this.navigationHeader = new NavigationHeader(navigation); @@ -38,12 +36,6 @@ public abstract class NavigationViewBase extends VerticalLayout implements HasUr navigation.addRunnableToBeRunAfterSelection(this::configureContent); } - /////////// - // ROUTE // - /////////// - - protected abstract String getRoute(); - ///////////// // CONTENT // ///////////// diff --git a/src/main/java/app/views/table/TableView.java b/src/main/java/app/views/table/TableView.java index d60cceb..a44344d 100644 --- a/src/main/java/app/views/table/TableView.java +++ b/src/main/java/app/views/table/TableView.java @@ -1,6 +1,7 @@ package app.views.table; import app.data.service.PlayerService; +import app.navigation.service.NavigationLevel; import app.navigation.service.NavigationService; import app.views.main.MainView; import app.views.navigation.NavigationViewBase; @@ -21,7 +22,7 @@ public class TableView extends NavigationViewBase { public TableView(@Autowired NavigationService navigationService, @Autowired PlayerService playerService) { - super(navigationService, true, true, false); + super(navigationService, "table", NavigationLevel.MATCHDAY); this.playerService = playerService; this.navigation.setOnlyMatchdaysWithActivity(true); @@ -31,15 +32,6 @@ public class TableView extends NavigationViewBase { configureLayout(); } - /////////// - // ROUTE // - /////////// - - @Override - public String getRoute() { - return "table"; - } - //////////// // LAYOUT // ////////////