|
|
@ -7,13 +7,9 @@ import app.data.service.ChessComService; |
|
|
|
import app.data.service.MatchService; |
|
|
|
import app.gameimage.GameImageService; |
|
|
|
import app.navigation.Navigation; |
|
|
|
import app.utils.ChessComUtils; |
|
|
|
import app.utils.EntityComponentUtils; |
|
|
|
import app.utils.StringUtils; |
|
|
|
import app.views.navigation.interfaces.ContentConfigurable; |
|
|
|
import com.vaadin.flow.component.ClickEvent; |
|
|
|
import com.vaadin.flow.component.ComponentEventListener; |
|
|
|
import com.vaadin.flow.component.UI; |
|
|
|
import com.vaadin.flow.component.button.Button; |
|
|
|
import com.vaadin.flow.component.button.ButtonVariant; |
|
|
|
import com.vaadin.flow.component.html.Div; |
|
|
@ -24,20 +20,13 @@ import com.vaadin.flow.component.orderedlayout.FlexComponent; |
|
|
|
import com.vaadin.flow.component.orderedlayout.FlexLayout; |
|
|
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
|
|
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
|
|
|
import com.vaadin.flow.component.progressbar.ProgressBar; |
|
|
|
import com.vaadin.flow.component.textfield.TextField; |
|
|
|
import com.vaadin.flow.shared.Registration; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.NoSuchElementException; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
|
|
|
|
private final Navigation navigation; |
|
|
|
private final ChessComService chessComService; |
|
|
|
private final MatchService matchService; |
|
|
|
private final GameImageService gameImageService; |
|
|
|
|
|
|
@ -48,18 +37,9 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
private final FlexLayout gamesLayout = new FlexLayout(); |
|
|
|
private final VerticalLayout noGamesLayout = new VerticalLayout(); |
|
|
|
|
|
|
|
private final FlexLayout editLayoutWrapper = new FlexLayout(); |
|
|
|
private final VerticalLayout editLayout = new VerticalLayout(); |
|
|
|
private final ArrayList<TextField> editTextFields = new ArrayList<>(); |
|
|
|
// TODO: autocorrect "/live/game" to "/game/live/" |
|
|
|
private final Button editSubmitButton = new Button("Submit", new Icon(VaadinIcon.CHECK)); |
|
|
|
// TODO: disable when no 6 values in form, and make sure you don't freeze forever when there are wrong entries |
|
|
|
// TODO: use overlapping progress here |
|
|
|
private Registration editSubmitButtonRegistration; |
|
|
|
private final Button editCancelButton = new Button("Cancel", new Icon(VaadinIcon.CLOSE)); |
|
|
|
private Registration editCancelButtonRegistration; |
|
|
|
private final Button chessComButton = new Button("Autofill with the latest 6 games between the players", new Icon(VaadinIcon.MAGIC)); |
|
|
|
private Registration chessComButtonButtonRegistration; |
|
|
|
private final FlexLayout editMatchLayout = new FlexLayout(); |
|
|
|
|
|
|
|
private final EditMatchCard editMatchCard; |
|
|
|
|
|
|
|
private Match match; |
|
|
|
private CalculatedMatch calculatedMatch; |
|
|
@ -68,18 +48,33 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
@Autowired ChessComService chessComService, |
|
|
|
@Autowired GameImageService gameImageService) { |
|
|
|
this.navigation = navigation; |
|
|
|
this.chessComService = chessComService; |
|
|
|
this.matchService = navigation.getMatchService(); |
|
|
|
this.gameImageService = gameImageService; |
|
|
|
|
|
|
|
this.editMatchCard = new EditMatchCard(navigation, chessComService, gameImageService); |
|
|
|
|
|
|
|
defineLayout(); |
|
|
|
} |
|
|
|
|
|
|
|
//////////// |
|
|
|
// LAYOUT // |
|
|
|
//////////// |
|
|
|
|
|
|
|
private void defineLayout() { |
|
|
|
add(headerLayout); |
|
|
|
|
|
|
|
defineEditMatchLayout(); |
|
|
|
defineHeaderLayout(); |
|
|
|
defineGamesLayout(); |
|
|
|
defineNoGamesLayout(); |
|
|
|
defineEditLayout(); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditMatchLayout() { |
|
|
|
editMatchLayout.add(editMatchCard); |
|
|
|
editMatchLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineNoGamesLayout() { |
|
|
|
noGamesLayout.add(createNoGamesLabel(), createAddGamesButton()); |
|
|
|
noGamesLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
@ -114,49 +109,11 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditLayout() { |
|
|
|
defineEditLayoutWrapper(); |
|
|
|
defineEditLayoutItself(); |
|
|
|
defineEditLayoutTextFields(); |
|
|
|
defineEditSubmitButton(); |
|
|
|
addButtonsToEditLayout(); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditLayoutWrapper() { |
|
|
|
editLayoutWrapper.add(editLayout); |
|
|
|
editLayoutWrapper.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditLayoutItself() { |
|
|
|
editLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
editLayout.setWidth(""); |
|
|
|
|
|
|
|
editLayout.addClassName("wrapper"); |
|
|
|
|
|
|
|
editLayout.add(chessComButton); |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditLayoutTextFields() { |
|
|
|
for (int i = 0; i < 6; i++) { |
|
|
|
TextField textField = new TextField(); |
|
|
|
textField.setWidth("23em"); |
|
|
|
HorizontalLayout horizontalLayout = new HorizontalLayout(new Label(String.format("Game %d:", i + 1)), textField); |
|
|
|
horizontalLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
horizontalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
editLayout.add(horizontalLayout); |
|
|
|
editTextFields.add(textField); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void defineEditSubmitButton() { |
|
|
|
editSubmitButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); |
|
|
|
} |
|
|
|
private void addButtonsToEditLayout() { |
|
|
|
HorizontalLayout buttonLayout = new HorizontalLayout(); |
|
|
|
buttonLayout.setAlignItems(FlexComponent.Alignment.CENTER); |
|
|
|
buttonLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER); |
|
|
|
buttonLayout.add(editSubmitButton, editCancelButton); |
|
|
|
editLayout.add(buttonLayout); |
|
|
|
} |
|
|
|
///////////// |
|
|
|
// CONTENT // |
|
|
|
///////////// |
|
|
|
|
|
|
|
@Override |
|
|
|
public void configureContent() { |
|
|
@ -198,78 +155,14 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
private void configureMainContent() { |
|
|
|
if (navigation.editFlag()) { |
|
|
|
remove(gamesLayout); |
|
|
|
add(editLayoutWrapper); |
|
|
|
configureEditContent(); |
|
|
|
add(editMatchCard); |
|
|
|
editMatchCard.configureContent(match); |
|
|
|
return; |
|
|
|
} |
|
|
|
remove(editLayoutWrapper); |
|
|
|
remove(editMatchCard); |
|
|
|
configureGamesContent(); |
|
|
|
} |
|
|
|
|
|
|
|
private void configureEditContent() { |
|
|
|
List<Game> games = new ArrayList<>(match.getGames()); |
|
|
|
|
|
|
|
clearTextFields(); |
|
|
|
fillTextFieldsWithURLs(games); |
|
|
|
|
|
|
|
configureChessComButton(); |
|
|
|
configureEditSubmitButton(); |
|
|
|
configureEditCancelButton(); |
|
|
|
} |
|
|
|
|
|
|
|
private void clearTextFields() { |
|
|
|
editTextFields.forEach(textField -> textField.setValue("")); |
|
|
|
} |
|
|
|
|
|
|
|
private void fillTextFieldsWithURLs(List<Game> games) { |
|
|
|
for (int i = 0; i < Math.min(games.size(), 6); i++) |
|
|
|
editTextFields.get(i).setValue(ChessComUtils.getGameURL(games.get(i))); |
|
|
|
} |
|
|
|
|
|
|
|
private void configureChessComButton() { |
|
|
|
if (chessComButtonButtonRegistration != null) chessComButtonButtonRegistration.remove(); |
|
|
|
chessComButtonButtonRegistration = chessComButton.addClickListener(createChessComButtonClickListener()); |
|
|
|
} |
|
|
|
|
|
|
|
private ComponentEventListener<ClickEvent<Button>> createChessComButtonClickListener() { |
|
|
|
return event -> { |
|
|
|
List<Game> gamesBetweenPlayers = chessComService.getLatestGamesBetweenPlayers(match, 6, 2); |
|
|
|
fillTextFieldsWithURLs(gamesBetweenPlayers); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private void configureEditSubmitButton() { |
|
|
|
if (editSubmitButtonRegistration != null) editSubmitButtonRegistration.remove(); |
|
|
|
editSubmitButtonRegistration = editSubmitButton.addClickListener(createEditSubmitButtonClickListener()); |
|
|
|
} |
|
|
|
|
|
|
|
private ComponentEventListener<ClickEvent<Button>> createEditSubmitButtonClickListener() { |
|
|
|
return event -> { |
|
|
|
match.getGames().clear(); |
|
|
|
addGamesToMatchFromFieldValues(); |
|
|
|
matchService.update(match); |
|
|
|
match.getGames().forEach(gameImageService::createImageIfNotPresent); |
|
|
|
|
|
|
|
navigation.setEditFlag(false); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private void addGamesToMatchFromFieldValues() { |
|
|
|
for (TextField textField : editTextFields) { |
|
|
|
Optional<Game> game = chessComService.getGame(textField.getValue(), match); // TODO: handle this when Optional is empty! |
|
|
|
game.ifPresent(value -> match.getGames().add(value)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void configureEditCancelButton() { |
|
|
|
if (editCancelButtonRegistration != null) editCancelButtonRegistration.remove(); |
|
|
|
editCancelButtonRegistration = editCancelButton.addClickListener(createEditCancelButtonListener()); |
|
|
|
} |
|
|
|
|
|
|
|
private ComponentEventListener<ClickEvent<Button>> createEditCancelButtonListener() { |
|
|
|
return event -> UI.getCurrent().navigate(String.format("matchday/%s", navigation.getWildcardParam().replace("edit/", ""))); |
|
|
|
} |
|
|
|
|
|
|
|
private void configureGamesContent() { |
|
|
|
if (match.getGames().isEmpty()) { |
|
|
|
remove(gamesLayout); |
|
|
@ -282,7 +175,7 @@ public class MatchComponent extends Div implements ContentConfigurable { |
|
|
|
|
|
|
|
gamesLayout.removeAll(); |
|
|
|
for (Game game : match.getGames()) { |
|
|
|
gamesLayout.add(new GameComponent(game, gameImageService)); |
|
|
|
gamesLayout.add(new GameCard(game, gameImageService)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |