fi.evolver.ai.vaadin.view.ProfileView Maven / Gradle / Ivy
package fi.evolver.ai.vaadin.view;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.html.Hr;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
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.component.select.Select;
import com.vaadin.flow.i18n.I18NProvider;
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.theme.lumo.LumoUtility;
import fi.evolver.ai.vaadin.UserProfileRepository;
import fi.evolver.ai.vaadin.component.ComponentSource;
import fi.evolver.ai.vaadin.entity.UserProfile;
import fi.evolver.ai.vaadin.translations.PageTitleTranslationKey;
import fi.evolver.utils.string.StringUtils;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.security.PermitAll;
@Uses(Icon.class)
@Uses(Select.class)
@PermitAll
@PageTitleTranslationKey("nav.profile")
public class ProfileView extends VerticalLayout {
@Serial
private static final long serialVersionUID = 1L;
@Autowired(required = false)
private List> additionalComponents = new ArrayList<>();
private final UserProfileRepository userProfileRepository;
private final I18NProvider I18nProvider;
private final Select languageSelect = new Select();
private final HorizontalLayout endSection = new HorizontalLayout();
@Autowired
public ProfileView(
UserProfileRepository userProfileRepository,
I18NProvider I18nProvider) {
this.userProfileRepository = userProfileRepository;
this.I18nProvider = I18nProvider;
setAlignItems(FlexComponent.Alignment.START);
setSpacing(true);
}
@PostConstruct
private void setup() {
additionalComponents.stream()
.sorted()
.map(ComponentSource::getComponent)
.forEach(this::add);
if (I18nProvider != null)
createLanguageSelect();
createLogoutSection();
createEndSection();
}
private void createEndSection() {
endSection.setPadding(false);
endSection.setWidthFull();
endSection.addClassNames(LumoUtility.AlignItems.END, LumoUtility.JustifyContent.BETWEEN);
add(new Hr());
add(endSection);
}
private void createLogoutSection() {
Button logoutButton = new Button(getTranslation("common.logout"), VaadinIcon.SIGN_OUT.create(), click -> {
UI.getCurrent().getPage().setLocation("/login");
SecurityContextLogoutHandler logoutHandler = new SecurityContextLogoutHandler();
logoutHandler.logout(VaadinServletRequest.getCurrent().getHttpServletRequest(), null, null);
});
endSection.add(logoutButton);
}
private void createLanguageSelect() {
List availableLocales = I18nProvider.getProvidedLocales();
final List cleanedLocaled = availableLocales.stream().map(l -> {
if (l.equals(Locale.ROOT))
return Locale.ENGLISH;
return l;
}).toList();
availableLocales = cleanedLocaled.stream().filter(l -> {
if (StringUtils.isNullOrEmpty(l.getCountry()))
return cleanedLocaled.stream().noneMatch(
x -> x.getLanguage().equals(l.getLanguage()) && StringUtils.hasText(x.getCountry()));
return true;
}).distinct().toList();
languageSelect.setLabel(getTranslation("profile.languageSelect"));
languageSelect.setItemLabelGenerator(l -> l.getDisplayName(Locale.ROOT));
languageSelect.setItems(availableLocales);
languageSelect.setValue(availableLocales.stream()
.filter(l -> l.getLanguage().equals(getLocale().getLanguage()))
.findFirst().orElse(getLocale()));
languageSelect.addValueChangeListener(valueChangeEvent -> {
Locale value = valueChangeEvent.getValue();
if (value == null)
return;
UserProfile user = userProfileRepository.findOrCreateUserProfile();
user.setLanguage(value.toLanguageTag());
userProfileRepository.save(user);
UI.getCurrent().getPage().reload();
});
endSection.add(languageSelect);
}
}