|
package app.navigation.components.button;
|
|
|
|
import app.data.entity.Matchday;
|
|
import app.navigation.Navigation;
|
|
import com.vaadin.flow.component.button.Button;
|
|
import com.vaadin.flow.component.icon.Icon;
|
|
import com.vaadin.flow.component.icon.VaadinIcon;
|
|
|
|
import java.util.Optional;
|
|
|
|
public class PrevMatchdayButton extends Button {
|
|
|
|
private final Navigation navigation;
|
|
|
|
public PrevMatchdayButton(Navigation navigation) {
|
|
this.navigation = navigation;
|
|
|
|
if (!navigation.matchdayEnabled())
|
|
throw new IllegalStateException("Cannot instantiate PrevMatchdayButton when Matchdays are not enabled!");
|
|
|
|
setIcon(new Icon(VaadinIcon.ARROW_LEFT));
|
|
|
|
navigation.addRunnableToBeRunAfterSelection(this::configure);
|
|
}
|
|
|
|
private void configure() {
|
|
Optional<Matchday> prevMatchday = getPrevMatchday();
|
|
setEnabled(prevMatchday.isPresent());
|
|
addClickListener(event -> prevMatchday.ifPresent(matchday -> navigation.getMatchdaySelect().setValue(matchday)));
|
|
}
|
|
|
|
private Optional<Matchday> getPrevMatchday() {
|
|
int index = ButtonUtils.getMatchdayIndex(navigation);
|
|
if (index > 0) return Optional.ofNullable(navigation.getMatchdayList().get(index - 1));
|
|
return Optional.empty();
|
|
}
|
|
}
|