Skip to content

Commit

Permalink
Merge pull request #2 from penguineer/app-frame
Browse files Browse the repository at this point in the history
Create the application frame
  • Loading branch information
penguineer authored Jul 18, 2024
2 parents a2eda7e + 890094a commit 16be19a
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.component.page.Push;
import com.vaadin.flow.server.AppShellSettings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
Expand All @@ -16,4 +17,10 @@ public class GartenplusApplication implements AppShellConfigurator {
public static void main(String[] args) {
SpringApplication.run(GartenplusApplication.class, args);
}

@Override
public void configurePage(AppShellSettings settings) {
settings.addFavIcon("icon", "favicon.ico", "256x256");
settings.addLink("shortcut icon", "favicon.ico");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.penguineering.gartenplus.auth;

import com.penguineering.gartenplus.auth.user.UserDTO;
import com.vaadin.flow.server.VaadinSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.web.context.WebApplicationContext;

import java.util.Optional;
import java.util.function.Supplier;

@Configuration
public class CurrentUserSupplierFactory {
@Bean(name = "user")
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public Supplier<UserDTO> getCurrentUserSupplier() {
return () -> retrieveUserFromSession().orElse(null);
}

private Optional<UserDTO> retrieveUserFromSession() {
return retrieveSecurityContext()
.map(SecurityContext::getAuthentication)
.map(Authentication::getPrincipal)
.map(GartenplusUser.class::cast)
.map(GartenplusUser::getUser);
}

private Optional<SecurityContext> retrieveSecurityContext() {
return Optional.ofNullable(VaadinSession.getCurrent())
.map(VaadinSession::getSession)
.map(s -> s.getAttribute("SPRING_SECURITY_CONTEXT"))
.map(SecurityContext.class::cast);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.penguineering.gartenplus.ui.appframe;

import com.penguineering.gartenplus.auth.user.UserDTO;
import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.RouterLayout;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.function.Supplier;

public class AppFrameLayout extends VerticalLayout implements RouterLayout {
private final Div content;

public AppFrameLayout(@Qualifier("user") Supplier<UserDTO> currentUser) {
setId("gartenplus-app");


GartenplusHeader header = new GartenplusHeader(currentUser);
add(header);

content = new Div();
content.setId("gartenplus-content");
content.setSizeFull();
add(content);
}

private HorizontalLayout createHeader(Supplier<UserDTO> currentUser) {
HorizontalLayout header = new HorizontalLayout();
header.setId("gartenplus-header");
header.setSizeFull();

header.add(new LoggedUserView(currentUser));

return header;
}

@Override
public void showRouterLayoutContent(HasElement newContent) {
// Previous content is automatically removed

newContent.getElement()
.getComponent()
.ifPresent(content::add);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.penguineering.gartenplus.ui.appframe;

import com.penguineering.gartenplus.auth.user.UserDTO;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;

import java.util.function.Supplier;

public class GartenplusHeader extends Div {
public GartenplusHeader(Supplier<UserDTO> currentUser) {
setWidthFull();
getStyle().set("display", "contents");

HorizontalLayout headerLayout = new HorizontalLayout();
headerLayout.setId("gartenplus-header");
headerLayout.setWidthFull();

headerLayout.add(new GartenplusLogo());

headerLayout.add(new H1("GartenPlus"));

Div spacer = new Div();
spacer.setWidthFull();
headerLayout.add(spacer);

headerLayout.add(new LoggedUserView(currentUser));

this.add(headerLayout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.penguineering.gartenplus.ui.appframe;

import com.vaadin.flow.component.Unit;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;

public class GartenplusLogo extends Div {
private static final String PATH = "assets/GartenPlusLogoRounded.png";
private static final String ALT = "GartenPlus Logo";
private static final int DIMENSIONS = 48;

public GartenplusLogo() {
getStyle().set("display", "contents");

Image logo = new Image(PATH, ALT);
logo.setWidth(DIMENSIONS, Unit.PIXELS);
logo.setHeight(DIMENSIONS, Unit.PIXELS);

add(logo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.penguineering.gartenplus.ui.appframe;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.RouterLayout;

public class GartenplusPage extends VerticalLayout implements RouterLayout {
public GartenplusPage() {
setId("gartenplus-app");
setPadding(false);
setMargin(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.penguineering.gartenplus.ui.appframe;

import com.penguineering.gartenplus.auth.user.UserDTO;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.avatar.AvatarVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Optional;
import java.util.function.Supplier;

public class LoggedUserView extends Div {
Logger logger = LoggerFactory.getLogger(LoggedUserView.class);

final Avatar avatar;

public LoggedUserView(Supplier<UserDTO> currentUser) {
Optional<UserDTO> optUser = Optional.ofNullable(currentUser.get());

HorizontalLayout avatarLayout = new HorizontalLayout();
avatarLayout.setAlignItems(FlexComponent.Alignment.CENTER);

avatar = new Avatar();
avatar.setId("logged-user-avatar");
avatar.addThemeVariants(AvatarVariant.LUMO_LARGE);
avatar.setTooltipEnabled(true);

avatarLayout.add(avatar);
add(avatarLayout);

// Set the user's name
optUser.
map(UserDTO::displayName)
.ifPresent(avatar::setName);

// load the avatar image
optUser
.map(UserDTO::avatarUrl)
.map(URI::toASCIIString)
.ifPresent(avatar::setImage);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/penguineering/gartenplus/views/HomeView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.penguineering.gartenplus.views;

import com.penguineering.gartenplus.ui.appframe.AppFrameLayout;
import com.penguineering.gartenplus.ui.appframe.GartenplusPage;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLayout;
import jakarta.annotation.security.PermitAll;

@Route(value = "", layout = AppFrameLayout.class)
@PermitAll
public class HomeView extends GartenplusPage {

public HomeView() {
add(new Paragraph("Der Garten wächst noch …"));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/favicon.ico
Binary file not shown.

0 comments on commit 16be19a

Please sign in to comment.