Browse Source

no more navigation()

master
GAM 4 years ago
parent
commit
95b04c2478
4 changed files with 44 additions and 46 deletions
  1. +39
    -39
      src/main/java/com/example/application/navigation/Navigation.java
  2. +1
    -3
      src/main/java/com/example/application/views/results/MatchView.java
  3. +2
    -3
      src/main/java/com/example/application/views/results/MatchdayView.java
  4. +2
    -1
      src/main/java/com/example/application/views/table/TableView.java

+ 39
- 39
src/main/java/com/example/application/navigation/Navigation.java View File

@ -29,7 +29,9 @@ public class Navigation implements HasUrlParameter<String> {
private final String route; private final String route;
private final boolean onlyMatchdaysWithActivity; private final boolean onlyMatchdaysWithActivity;
private final List<Runnable> runnablesToBeRunAfterSelection = new ArrayList<>();
private final List<Runnable> runnablesToBeRunAfterSeasonSelection = new ArrayList<>();
private final List<Runnable> runnablesToBeRunAfterMatchdaySelection = new ArrayList<>();
private final List<Runnable> runnablesToBeRunAfterMatchSelection = new ArrayList<>();
private final SeasonService seasonService; private final SeasonService seasonService;
private final MatchdayService matchdayService; private final MatchdayService matchdayService;
@ -91,23 +93,11 @@ public class Navigation implements HasUrlParameter<String> {
this.autoselectMatch = autoselectMatch; 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() { private void updateUrl() {
String params = NavigationUtils.getWildcardParam(seasonParam, matchdayParam, matchParam); String params = NavigationUtils.getWildcardParam(seasonParam, matchdayParam, matchParam);
UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", route, params)); UI.getCurrent().getPage().getHistory().pushState(null, String.format("%s/%s", route, params));
} }
private HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Select<Matchday>, Matchday>> matchdaySelectValueChangeListener() {
return matchdayChangeEvent -> {
Matchday matchday = matchdayChangeEvent.getValue();
if (matchday != null) {
matchParam = matchday.toString();
updateUrl();
runnablesToBeRunAfterSelection.forEach(Runnable::run);
}
};
}
private HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Select<Season>, Season>> seasonSelectValueChangeListener() { private HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Select<Season>, Season>> seasonSelectValueChangeListener() {
return seasonChangeEvent -> { return seasonChangeEvent -> {
@ -128,19 +118,36 @@ public class Navigation implements HasUrlParameter<String> {
this.matchdayParam = matchdayParam; this.matchdayParam = matchdayParam;
this.matchParam = null; this.matchParam = null;
updateUrl(); updateUrl();
runnablesToBeRunAfterSelection.forEach(Runnable::run);
runnablesToBeRunAfterSeasonSelection.forEach(Runnable::run);
} }
}; };
} }
private HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Select<Matchday>, 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<? super AbstractField.ComponentValueChangeEvent<Select<Match>, Match>> matchSelectValueChangeListener() { private HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Select<Match>, Match>> matchSelectValueChangeListener() {
return matchChangeEvent -> { return matchChangeEvent -> {
Match match = matchChangeEvent.getValue(); Match match = matchChangeEvent.getValue();
if (match != null) {
if (match == null) {
matchParam = null;
} else {
matchParam = match.toString(); matchParam = match.toString();
updateUrl(); 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<String> {
return matchdayList.stream().anyMatch(matchday -> matchdayParam.equals(matchday.toString())); 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 @Override
public void setParameter(BeforeEvent event, @WildcardParameter String param) { public void setParameter(BeforeEvent event, @WildcardParameter String param) {
Map<UrlParameterType, String> map = NavigationUtils.getParameterMap(param); Map<UrlParameterType, String> map = NavigationUtils.getParameterMap(param);
@ -187,10 +190,8 @@ public class Navigation implements HasUrlParameter<String> {
invalidUrlLabel.setText(String.format("No Match found in Matchday %s in Season %s!", matchday.toString(), season.toString())); 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); matchSelect.setValue(match);
navigate(season.toString(), matchday.toString(), match.toString());
} }
private void noMatchdayFound(Season season) { private void noMatchdayFound(Season season) {
@ -200,7 +201,7 @@ public class Navigation implements HasUrlParameter<String> {
private void autoselectMatch(Season season, Matchday matchday) { private void autoselectMatch(Season season, Matchday matchday) {
Optional<Match> firstMatch = matchService.getFirstMatch(matchday); Optional<Match> firstMatch = matchService.getFirstMatch(matchday);
firstMatch.ifPresentOrElse( firstMatch.ifPresentOrElse(
match -> matchFound(season, matchday, match),
this::matchFound,
() -> noMatchFound(season, matchday)); () -> noMatchFound(season, matchday));
} }
@ -210,9 +211,7 @@ public class Navigation implements HasUrlParameter<String> {
if (paramInvalid(matchParam) && autoselectMatch) { if (paramInvalid(matchParam) && autoselectMatch) {
autoselectMatch(season, matchday); autoselectMatch(season, matchday);
return;
} }
navigate(season.toString(), matchday.toString(), matchParam);
} }
private void noSeasonFound() { private void noSeasonFound() {
@ -232,9 +231,7 @@ public class Navigation implements HasUrlParameter<String> {
if (paramInvalid(matchdayParam) && autoselectMatchday) { if (paramInvalid(matchdayParam) && autoselectMatchday) {
autoselectMatchday(season, matchParam); autoselectMatchday(season, matchParam);
return;
} }
navigate(season.toString(), matchdayParam, matchParam);
} }
private void autoselectSeason(String matchdayParam, String matchParam) { private void autoselectSeason(String matchdayParam, String matchParam) {
@ -262,9 +259,6 @@ public class Navigation implements HasUrlParameter<String> {
public void setParameter(String seasonParam, String matchdayParam, String matchParam) { public void setParameter(String seasonParam, String matchdayParam, String matchParam) {
if (autoselectIfNecessary(seasonParam, matchdayParam, matchParam)) { if (autoselectIfNecessary(seasonParam, matchdayParam, matchParam)) {
for (Runnable runnable : runnablesToBeRunAfterSelection) {
runnable.run();
}
return; return;
} }
@ -297,10 +291,6 @@ public class Navigation implements HasUrlParameter<String> {
} else if (autoselectMatch) { } 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)); 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 @Nullable
@ -348,8 +338,16 @@ public class Navigation implements HasUrlParameter<String> {
return null; 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() { private void configureButtons() {
@ -361,7 +359,9 @@ public class Navigation implements HasUrlParameter<String> {
} }
private ComponentEventListener<ClickEvent<Button>> getButtonClickListener(String matchdayParam) { private ComponentEventListener<ClickEvent<Button>> 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() { private String getPrevMatchdayParam() {
@ -417,6 +417,6 @@ public class Navigation implements HasUrlParameter<String> {
} }
public void selectMatch(Match match) { public void selectMatch(Match match) {
navigate(seasonParam, matchdayParam, match.toString());
matchSelect.setValue(match);
} }
} }

+ 1
- 3
src/main/java/com/example/application/views/results/MatchView.java View File

@ -3,8 +3,6 @@ package com.example.application.views.results;
import com.example.application.navigation.Navigation; import com.example.application.navigation.Navigation;
import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label; import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
public class MatchView extends Div { public class MatchView extends Div {
@ -17,7 +15,7 @@ public class MatchView extends Div {
// public ResultsViewRight(@Autowired ChessComService chessComService, @Autowired PlayerService playerService) { // public ResultsViewRight(@Autowired ChessComService chessComService, @Autowired PlayerService playerService) {
public MatchView(Navigation navigation) { public MatchView(Navigation navigation) {
this.navigation = navigation; this.navigation = navigation;
navigation.addRunnableToBeRunAfterSelection(()-> label.setText(navigation.getSelectedMatch().toString()));
navigation.addRunnableToBeRunAfterMatchSelection(()-> label.setText(navigation.getSelectedMatch().toString()));
add(label); add(label);
// this.chessComService = chessComService; // this.chessComService = chessComService;
// this.playerService = playerService; // this.playerService = playerService;


+ 2
- 3
src/main/java/com/example/application/views/results/MatchdayView.java View File

@ -2,7 +2,6 @@ package com.example.application.views.results;
import com.example.application.components.navigation.SeasonAndMatchdayNavigation; import com.example.application.components.navigation.SeasonAndMatchdayNavigation;
import com.example.application.data.bean.CalculatedMatch; 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.entity.Matchday;
import com.example.application.data.service.MatchService; import com.example.application.data.service.MatchService;
import com.example.application.navigation.Navigation; 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.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; 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.data.selection.SelectionListener;
import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.function.ValueProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -38,7 +36,8 @@ public class MatchdayView extends VerticalLayout {
public MatchdayView(Navigation navigation, @Autowired MatchService matchService) { public MatchdayView(Navigation navigation, @Autowired MatchService matchService) {
this.navigation = navigation; this.navigation = navigation;
this.navigation.addRunnableToBeRunAfterSelection(this::configureContent);
this.navigation.addRunnableToBeRunAfterSeasonSelection(this::configureContent);
this.navigation.addRunnableToBeRunAfterMatchdaySelection(this::configureContent);
this.matchService = matchService; this.matchService = matchService;
prevButton = navigation.getPrevMatchdayButton(); prevButton = navigation.getPrevMatchdayButton();


+ 2
- 1
src/main/java/com/example/application/views/table/TableView.java View File

@ -43,7 +43,8 @@ public class TableView extends VerticalLayout implements HasUrlParameter<String>
this.navigation = new Navigation("table", seasonService, matchdayService, matchService, true); this.navigation = new Navigation("table", seasonService, matchdayService, matchService, true);
this.navigation.setAutoselectSeason(true); this.navigation.setAutoselectSeason(true);
this.navigation.setAutoselectMatchday(true); this.navigation.setAutoselectMatchday(true);
this.navigation.addRunnableToBeRunAfterSelection(this::configureContent);
this.navigation.addRunnableToBeRunAfterSeasonSelection(this::configureContent);
this.navigation.addRunnableToBeRunAfterMatchdaySelection(this::configureContent);
this.invalidUrlLabel = navigation.getInvalidUrlLabel(); this.invalidUrlLabel = navigation.getInvalidUrlLabel();
addClassName("table-view"); addClassName("table-view");


Loading…
Cancel
Save