-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from penguineer/app-frame
Create the application frame
- Loading branch information
Showing
11 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/penguineering/gartenplus/auth/CurrentUserSupplierFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/penguineering/gartenplus/ui/appframe/AppFrameLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/penguineering/gartenplus/ui/appframe/GartenplusHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/penguineering/gartenplus/ui/appframe/GartenplusLogo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/penguineering/gartenplus/ui/appframe/GartenplusPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/penguineering/gartenplus/ui/appframe/LoggedUserView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/com/penguineering/gartenplus/views/HomeView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Binary file added
BIN
+145 KB
src/main/resources/META-INF/resources/assets/GartenPlusLogoRounded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.