From 7d86c9508579d853c78c9631d7566b1d058f375d Mon Sep 17 00:00:00 2001 From: Slawek84PL Date: Sun, 8 Dec 2024 18:37:46 +0100 Subject: [PATCH 1/4] Added ToastMessageFacade and implementation for building toast messages --- .../disasteralerts/message/ToastMessage.java | 18 +++++++++ .../message/ToastMessageFacade.java | 11 +++++ .../message/ToastMessageImpl.java | 40 +++++++++++++++++++ .../message/ToastMessageMapper.java | 9 +++++ .../message/ToastMessageType.java | 5 +++ .../message/dto/ToastMessageDTO.java | 7 ++++ 6 files changed, 90 insertions(+) create mode 100644 src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java create mode 100644 src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java create mode 100644 src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java create mode 100644 src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java create mode 100644 src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java create mode 100644 src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java new file mode 100644 index 0000000..b5b6288 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessage.java @@ -0,0 +1,18 @@ +package pl.ateam.disasteralerts.message; + +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +class ToastMessage { + + @NotNull + private ToastMessageType type; + private String icon; + @NotNull + private String header; + @NotNull + private String message; +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java new file mode 100644 index 0000000..83b74a4 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageFacade.java @@ -0,0 +1,11 @@ +package pl.ateam.disasteralerts.message; + +import org.springframework.stereotype.Component; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Component +public interface ToastMessageFacade { + + ToastMessageDTO buildMessage(ToastMessageType type, String header, String message); + ToastMessageDTO buildMessage(ToastMessageType type, String icon, String header, String message); +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java new file mode 100644 index 0000000..6059411 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageImpl.java @@ -0,0 +1,40 @@ +package pl.ateam.disasteralerts.message; + +import org.springframework.stereotype.Service; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Service +class ToastMessageImpl implements ToastMessageFacade { + + private final ToastMessageMapper toastMessageMapper; + + public ToastMessageImpl(ToastMessageMapper toastMessageMapper) { + this.toastMessageMapper = toastMessageMapper; + } + + @Override + public ToastMessageDTO buildMessage(ToastMessageType type, String header, String message) { + String icon = getDefaultIcon(type); + return buildMessage(type, icon, header, message); + } + + @Override + public ToastMessageDTO buildMessage(ToastMessageType type, String icon, String header, String message) { + ToastMessage built = ToastMessage.builder() + .type(type) + .icon(icon) + .header(header) + .message(message) + .build(); + return toastMessageMapper.mapToastMessageToToastMessageDTO(built); + } + + private String getDefaultIcon(ToastMessageType type) { + String icon = "bi bi-balloon-heart-fill"; + switch (type) { + case DANGER -> icon = "bi bi-exclamation-circle-fill"; + case SUCCESS -> icon = "bi bi-check2-square"; + } + return icon; + } +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java new file mode 100644 index 0000000..db4ac51 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageMapper.java @@ -0,0 +1,9 @@ +package pl.ateam.disasteralerts.message; + +import org.mapstruct.Mapper; +import pl.ateam.disasteralerts.message.dto.ToastMessageDTO; + +@Mapper(componentModel = "spring") +interface ToastMessageMapper { + ToastMessageDTO mapToastMessageToToastMessageDTO(ToastMessage toastMessage); +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java new file mode 100644 index 0000000..713470f --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/ToastMessageType.java @@ -0,0 +1,5 @@ +package pl.ateam.disasteralerts.message; + +public enum ToastMessageType { + DANGER, INFO, SUCCESS +} diff --git a/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java b/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java new file mode 100644 index 0000000..039b5c0 --- /dev/null +++ b/src/main/java/pl/ateam/disasteralerts/message/dto/ToastMessageDTO.java @@ -0,0 +1,7 @@ +package pl.ateam.disasteralerts.message.dto; + +public record ToastMessageDTO(String type, + String icon, + String header, + String message) { +} From 10d2287956180347f5ff36260222bb44792970d8 Mon Sep 17 00:00:00 2001 From: Slawek84PL Date: Sun, 8 Dec 2024 19:00:54 +0100 Subject: [PATCH 2/4] Dynamically assign text color class and icon based on toast message type --- src/main/resources/templates/fragments.html | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index 6c96890..0c00074 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -67,14 +67,13 @@