diff --git a/src/main/java/com/example/application/navigation/Navigation.java b/src/main/java/com/example/application/navigation/Navigation.java index caecee8..775fad1 100644 --- a/src/main/java/com/example/application/navigation/Navigation.java +++ b/src/main/java/com/example/application/navigation/Navigation.java @@ -29,7 +29,9 @@ public class Navigation implements HasUrlParameter { private final String route; private final boolean onlyMatchdaysWithActivity; - private final List runnablesToBeRunAfterSelection = new ArrayList<>(); + private final List runnablesToBeRunAfterSeasonSelection = new ArrayList<>(); + private final List runnablesToBeRunAfterMatchdaySelection = new ArrayList<>(); + private final List runnablesToBeRunAfterMatchSelection = new ArrayList<>(); private final SeasonService seasonService; private final MatchdayService matchdayService; @@ -91,23 +93,11 @@ public class Navigation implements HasUrlParameter { this.autoselectMatch = autoselectMatch; } - // TODO: run the runnables after each selection (anyhow), then push history state (UI.getCurrent().getPage().getHistory().pushState(null, "http://host.com/person?action=edit&id=1");). - // Navigate as little as possible. private void updateUrl() { String params = NavigationUtils.getWildcardParam(seasonParam, matchdayParam, matchParam); UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", route, params)); } - private HasValue.ValueChangeListener, Matchday>> matchdaySelectValueChangeListener() { - return matchdayChangeEvent -> { - Matchday matchday = matchdayChangeEvent.getValue(); - if (matchday != null) { - matchParam = matchday.toString(); - updateUrl(); - runnablesToBeRunAfterSelection.forEach(Runnable::run); - } - }; - } private HasValue.ValueChangeListener, Season>> seasonSelectValueChangeListener() { return seasonChangeEvent -> { @@ -128,19 +118,36 @@ public class Navigation implements HasUrlParameter { this.matchdayParam = matchdayParam; this.matchParam = null; updateUrl(); - runnablesToBeRunAfterSelection.forEach(Runnable::run); + runnablesToBeRunAfterSeasonSelection.forEach(Runnable::run); } }; } + private HasValue.ValueChangeListener, Matchday>> matchdaySelectValueChangeListener() { + return matchdayChangeEvent -> { + Matchday matchday = matchdayChangeEvent.getValue(); + if (matchday == null) { + matchParam = null; + } else { + matchdayParam = matchday.toString(); + fillMatchSelectWithData(matchday); + configureButtons(); + matchSelect.setValue(null); + runnablesToBeRunAfterMatchdaySelection.forEach(Runnable::run); + } + updateUrl(); + }; + } private HasValue.ValueChangeListener, Match>> matchSelectValueChangeListener() { return matchChangeEvent -> { Match match = matchChangeEvent.getValue(); - if (match != null) { + if (match == null) { + matchParam = null; + } else { matchParam = match.toString(); updateUrl(); - runnablesToBeRunAfterSelection.forEach(Runnable::run); // TODO: offer different lists for season, matchday, match + runnablesToBeRunAfterMatchSelection.forEach(Runnable::run); } }; } @@ -169,10 +176,6 @@ public class Navigation implements HasUrlParameter { return matchdayList.stream().anyMatch(matchday -> matchdayParam.equals(matchday.toString())); } - private void navigate(String seasonParam, String matchdayParam, String matchParam) { // TODO: change this to String... -> see where you need which parameters - UI.getCurrent().navigate(String.format("%s/%s", route, NavigationUtils.getWildcardParam(seasonParam, matchdayParam, matchParam))); - } - @Override public void setParameter(BeforeEvent event, @WildcardParameter String param) { Map map = NavigationUtils.getParameterMap(param); @@ -187,10 +190,8 @@ public class Navigation implements HasUrlParameter { invalidUrlLabel.setText(String.format("No Match found in Matchday %s in Season %s!", matchday.toString(), season.toString())); } - private void matchFound(Season season, Matchday matchday, Match match) { + private void matchFound(Match match) { matchSelect.setValue(match); - - navigate(season.toString(), matchday.toString(), match.toString()); } private void noMatchdayFound(Season season) { @@ -200,7 +201,7 @@ public class Navigation implements HasUrlParameter { private void autoselectMatch(Season season, Matchday matchday) { Optional firstMatch = matchService.getFirstMatch(matchday); firstMatch.ifPresentOrElse( - match -> matchFound(season, matchday, match), + this::matchFound, () -> noMatchFound(season, matchday)); } @@ -210,9 +211,7 @@ public class Navigation implements HasUrlParameter { if (paramInvalid(matchParam) && autoselectMatch) { autoselectMatch(season, matchday); - return; } - navigate(season.toString(), matchday.toString(), matchParam); } private void noSeasonFound() { @@ -232,9 +231,7 @@ public class Navigation implements HasUrlParameter { if (paramInvalid(matchdayParam) && autoselectMatchday) { autoselectMatchday(season, matchParam); - return; } - navigate(season.toString(), matchdayParam, matchParam); } private void autoselectSeason(String matchdayParam, String matchParam) { @@ -262,9 +259,6 @@ public class Navigation implements HasUrlParameter { public void setParameter(String seasonParam, String matchdayParam, String matchParam) { if (autoselectIfNecessary(seasonParam, matchdayParam, matchParam)) { - for (Runnable runnable : runnablesToBeRunAfterSelection) { - runnable.run(); - } return; } @@ -297,10 +291,6 @@ public class Navigation implements HasUrlParameter { } else if (autoselectMatch) { invalidUrlLabel.setText(String.format("Invalid URL: Match \"%s\" in Matchday \"%s\" in Season \"%s\" does not exist in the database!", matchParam, matchdayParam, seasonParam)); } - - for (Runnable runnable : runnablesToBeRunAfterSelection) { - runnable.run(); - } } @Nullable @@ -348,8 +338,16 @@ public class Navigation implements HasUrlParameter { return null; } - public void addRunnableToBeRunAfterSelection(Runnable runnable) { - runnablesToBeRunAfterSelection.add(runnable); + public void addRunnableToBeRunAfterSeasonSelection(Runnable runnable) { + runnablesToBeRunAfterSeasonSelection.add(runnable); + } + + public void addRunnableToBeRunAfterMatchdaySelection(Runnable runnable) { + runnablesToBeRunAfterMatchdaySelection.add(runnable); + } + + public void addRunnableToBeRunAfterMatchSelection(Runnable runnable) { + runnablesToBeRunAfterMatchSelection.add(runnable); } private void configureButtons() { @@ -361,7 +359,9 @@ public class Navigation implements HasUrlParameter { } private ComponentEventListener> getButtonClickListener(String matchdayParam) { - return buttonClickEvent -> navigate(seasonParam, matchdayParam, matchParam); + return buttonClickEvent -> matchdayList.stream() + .filter(matchday -> matchdayParam.equals(matchday.toString())) + .findFirst().ifPresent(matchdaySelect::setValue); } private String getPrevMatchdayParam() { @@ -417,6 +417,6 @@ public class Navigation implements HasUrlParameter { } public void selectMatch(Match match) { - navigate(seasonParam, matchdayParam, match.toString()); + matchSelect.setValue(match); } } diff --git a/src/main/java/com/example/application/views/results/MatchView.java b/src/main/java/com/example/application/views/results/MatchView.java index 03d41f8..0c8829a 100644 --- a/src/main/java/com/example/application/views/results/MatchView.java +++ b/src/main/java/com/example/application/views/results/MatchView.java @@ -3,8 +3,6 @@ package com.example.application.views.results; import com.example.application.navigation.Navigation; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Label; -import com.vaadin.flow.router.BeforeEvent; -import com.vaadin.flow.router.HasUrlParameter; public class MatchView extends Div { @@ -17,7 +15,7 @@ public class MatchView extends Div { // public ResultsViewRight(@Autowired ChessComService chessComService, @Autowired PlayerService playerService) { public MatchView(Navigation navigation) { this.navigation = navigation; - navigation.addRunnableToBeRunAfterSelection(()-> label.setText(navigation.getSelectedMatch().toString())); + navigation.addRunnableToBeRunAfterMatchSelection(()-> label.setText(navigation.getSelectedMatch().toString())); add(label); // this.chessComService = chessComService; // this.playerService = playerService; diff --git a/src/main/java/com/example/application/views/results/MatchdayView.java b/src/main/java/com/example/application/views/results/MatchdayView.java index b6d8c8c..258bc0a 100644 --- a/src/main/java/com/example/application/views/results/MatchdayView.java +++ b/src/main/java/com/example/application/views/results/MatchdayView.java @@ -2,7 +2,6 @@ package com.example.application.views.results; import com.example.application.components.navigation.SeasonAndMatchdayNavigation; import com.example.application.data.bean.CalculatedMatch; -import com.example.application.data.entity.Match; import com.example.application.data.entity.Matchday; import com.example.application.data.service.MatchService; import com.example.application.navigation.Navigation; @@ -16,7 +15,6 @@ import com.vaadin.flow.component.html.Label; import com.vaadin.flow.component.orderedlayout.FlexComponent; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; -import com.vaadin.flow.data.selection.SelectionEvent; import com.vaadin.flow.data.selection.SelectionListener; import com.vaadin.flow.function.ValueProvider; import org.springframework.beans.factory.annotation.Autowired; @@ -38,7 +36,8 @@ public class MatchdayView extends VerticalLayout { public MatchdayView(Navigation navigation, @Autowired MatchService matchService) { this.navigation = navigation; - this.navigation.addRunnableToBeRunAfterSelection(this::configureContent); + this.navigation.addRunnableToBeRunAfterSeasonSelection(this::configureContent); + this.navigation.addRunnableToBeRunAfterMatchdaySelection(this::configureContent); this.matchService = matchService; prevButton = navigation.getPrevMatchdayButton(); diff --git a/src/main/java/com/example/application/views/table/TableView.java b/src/main/java/com/example/application/views/table/TableView.java index baf1313..88a0287 100644 --- a/src/main/java/com/example/application/views/table/TableView.java +++ b/src/main/java/com/example/application/views/table/TableView.java @@ -43,7 +43,8 @@ public class TableView extends VerticalLayout implements HasUrlParameter this.navigation = new Navigation("table", seasonService, matchdayService, matchService, true); this.navigation.setAutoselectSeason(true); this.navigation.setAutoselectMatchday(true); - this.navigation.addRunnableToBeRunAfterSelection(this::configureContent); + this.navigation.addRunnableToBeRunAfterSeasonSelection(this::configureContent); + this.navigation.addRunnableToBeRunAfterMatchdaySelection(this::configureContent); this.invalidUrlLabel = navigation.getInvalidUrlLabel(); addClassName("table-view");