Skip to content

Commit

Permalink
🚧 #80 #70 hide dialogs and load initial config correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Nov 22, 2024
1 parent 2157cc6 commit bacfa0f
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 169 deletions.
Binary file modified src/main/bundles/prod.bundle
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/frontend/themes/espflow/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ html {
--lumo-space-xs: 0.1875rem;
--lumo-body-text-color: hsla(214, 0%, 0%, 0.94);
--lumo-font-family: 'Arimo', sans-serif;
scrollbar-width: thin;
}

[theme~="dark"] {
Expand Down Expand Up @@ -280,3 +279,4 @@ html {
.xterm {
height: 100%;
}

3 changes: 2 additions & 1 deletion src/main/java/com/esp/espflow/entity/WizardEspEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

@Data
@Entity
@Table(name = "wizard_flashesp")
@Table(name = "wizards_dialogs")
public class WizardEspEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private boolean isWizardEnabled;

private String name;

}
2 changes: 1 addition & 1 deletion src/main/java/com/esp/espflow/entity/dto/WizardEspDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* @author rub'n
*/
@Builder
public record WizardEspDto(boolean isWizardEnabled, String name) {}
public record WizardEspDto(Long id, boolean isWizardEnabled, String name) {}
11 changes: 8 additions & 3 deletions src/main/java/com/esp/espflow/generator/DataGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@
import java.util.Set;

import static com.esp.espflow.util.EspFlowConstants.FRONTEND_IMAGES_AVATAR_USER;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASHESP_VIEW;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASH_ESP_VIEW;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW;

/**
* initial configuration
*/
@SpringComponent
public class DataGenerator {

@Bean
public CommandLineRunner loadSettings(final WizardEspService service) {
return (arg) -> {
return args -> {

var wizardReadFlashView = WizardEspDto.builder()
.id(1L)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.isWizardEnabled(true)
.build();

var wizardFlashView = WizardEspDto.builder()
.name(WIZARD_FLASHESP_VIEW)
.id(2L)
.name(WIZARD_FLASH_ESP_VIEW)
.isWizardEnabled(true)
.build();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/esp/espflow/mappers/WizardEspMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ public class WizardEspMapper {

public static final WizardEspMapper INSTANCE = new WizardEspMapper();

private WizardEspMapper() {
}
private WizardEspMapper() {}

public WizardEspEntity dtoToEntity(final WizardEspDto dto) {
final WizardEspEntity entity = new WizardEspEntity();
entity.setId(dto.id());
entity.setName(dto.name());
entity.setWizardEnabled(dto.isWizardEnabled());
return entity;
}

public WizardEspDto entityToDto(final WizardEspEntity entity) {
return WizardEspDto.builder()
.id(entity.getId())
.name(entity.getName())
.isWizardEnabled(entity.isWizardEnabled())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ public PasswordEncoder passwordEncoder() {
protected void configure(HttpSecurity http) throws Exception {

http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/images/*.png")).permitAll());
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/images/*.png"))
.permitAll());

http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/images/*.svg"))
.permitAll());

http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.permitAll());

// Icons from the line-awesome addon
http.authorizeHttpRequests(authorize -> authorize
.requestMatchers(new AntPathRequestMatcher("/line-awesome/**/*.svg")).permitAll());
http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/line-awesome/**/*.svg"))
.permitAll());

super.configure(http);
setLoginView(http, LoginView.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.esp.espflow.entity.WizardEspEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -11,4 +12,8 @@ public interface WizardEspRepository extends JpaRepository<WizardEspEntity, Long

Optional<WizardEspEntity> findByName(String name);

@Query("SELECT COUNT(isWizardEnabled) " +
"FROM WizardEspEntity " +
"WHERE isWizardEnabled = true")
Integer areAllWizardsEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
* @author rub'n
Expand All @@ -23,43 +25,53 @@ public class WizardEspService {
private final WizardEspRepository wizardEspRepository;

/**
*
* From the initial configuration
*
* @param wizardEspDto list
*/
public void saveAll(List<WizardEspDto> wizardEspDto) {
wizardEspDto.forEach(this::save);
Stream.of(wizardEspDto)
.flatMap(Collection::stream)
.map(WizardEspMapper.INSTANCE::dtoToEntity)
.peek(entity -> log.info("initial load entity saved {}", entity))
.forEach(this.wizardEspRepository::save);
}

/**
* Saves the wizar entity or updates it if it exists
* Saves the wizard entity or updates it if it exists
*
* @param wizardEspDto
*/
public void save(WizardEspDto wizardEspDto) {
var entity = WizardEspMapper.INSTANCE.dtoToEntity(wizardEspDto);
this.wizardEspRepository.findByName(entity.getName())
.ifPresentOrElse(currentEntity -> {
currentEntity.setWizardEnabled(entity.isWizardEnabled());
wizardEspRepository.save(currentEntity);
log.info("Entity updated {}", currentEntity);
this.wizardEspRepository.findById(entity.getId())
.ifPresentOrElse(entityIfPresent -> {
entityIfPresent.setWizardEnabled(entity.isWizardEnabled());
wizardEspRepository.save(entityIfPresent);
log.info("Entity updated {}", entityIfPresent);
}, () -> {
wizardEspRepository.save(entity);
log.info("Entity saved {}", entity);
});
}

/**
*
* @param name of this Wizard
*
* @return A {@link Optional< WizardEspEntity >}
* @return A {@link WizardEspEntity}
*/
@Transactional
public Optional<WizardEspDto> findWizardFlashEsp(String name) {
public Optional<WizardEspDto> findByName(String name) {
return this.wizardEspRepository.findByName(name)
.map(WizardEspMapper.INSTANCE::entityToDto);
}

/**
* Count the wizards with the isWizardEnabled field to true
*
* @return A {boolean}
*/
public boolean areAllWizardsEnabled() {
return this.wizardEspRepository.areAllWizardsEnabled() == this.wizardEspRepository.count();
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/esp/espflow/util/EspFlowConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class EspFlowConstants {
/*
* for wizard
*/
public static final String WIZARD_FLASHESP_VIEW = "wizardFlashEspView";
public static final String WIZARD_FLASH_ESP_VIEW = "wizardFlashEspView";
public static final String WIZARD_READ_FLASH_ESP_VIEW = "wizardReadFlashEspView";
public static final String INNER_HTML = "innerHTML";
public static final String AVATAR_STEP_ACTIVE = "avatar-step-active";
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
import static com.esp.espflow.util.EspFlowConstants.OVERFLOW_X;
import static com.esp.espflow.util.EspFlowConstants.OVERFLOW_Y;
import static com.esp.espflow.util.EspFlowConstants.PORT;
import static com.esp.espflow.util.EspFlowConstants.SETTINGS;
import static com.esp.espflow.util.EspFlowConstants.SIZE_25_PX;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASHESP_VIEW;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASH_ESP_VIEW;

/**
* @author rubn
Expand Down Expand Up @@ -398,16 +399,16 @@ protected void onAttach(AttachEvent attachEvent) {
"}"
).then(String.class, hash -> {
log.info("Fragmento de URI: {}", hash);
if (Objects.nonNull(hash) && !hash.contains("settings")) {
if (Objects.nonNull(hash) && !hash.contains(SETTINGS)) {
this.add(this.wizardFlashEspDialog);
this.wizardFlashEspDialog.open();
} else {
ui.getPage().fetchCurrentURL(url -> {
final String ref = StringUtils.defaultIfEmpty(url.getRef(), StringUtils.EMPTY);
if (!ref.contains("settings")) {
if (!ref.contains(SETTINGS)) {
this.add(this.wizardFlashEspDialog);
this.wizardFlashEspRepository.findWizardFlashEsp(WIZARD_FLASHESP_VIEW)
.ifPresent((hide) -> {
this.wizardFlashEspRepository.findByName(WIZARD_FLASH_ESP_VIEW)
.ifPresent(hide -> {
if (hide.isWizardEnabled()) {
this.wizardFlashEspDialog.open();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import static com.esp.espflow.util.EspFlowConstants.STEP1;
import static com.esp.espflow.util.EspFlowConstants.STEP2;
import static com.esp.espflow.util.EspFlowConstants.STEP3;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASHESP_VIEW;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASH_ESP_VIEW;

/**
* Wizard to show the steps for flash_id and write_flash process
Expand Down Expand Up @@ -272,11 +272,15 @@ private void configureFooter() {
hideButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY_INLINE);
hideButton.getStyle().set("margin-right", "auto");
hideButton.addClickListener(event -> {
final WizardEspDto wizardEspDto = WizardEspDto.builder()
.isWizardEnabled(false)
.name(WIZARD_FLASHESP_VIEW)
.build();
this.wizardEspService.save(wizardEspDto);
this.wizardEspService.findByName(WIZARD_FLASH_ESP_VIEW)
.ifPresent(entityPresent -> {
final WizardEspDto wizardEspDto = WizardEspDto.builder()
.id(entityPresent.id())
.isWizardEnabled(false)
.name(WIZARD_FLASH_ESP_VIEW)
.build();
this.wizardEspService.save(wizardEspDto);
});
super.close();
});
super.getFooter().add(hideButton);
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@
import java.util.stream.Stream;

import static com.esp.espflow.util.EspFlowConstants.BOX_SHADOW_VAADIN_BUTTON;
import static com.esp.espflow.util.EspFlowConstants.CURSOR_POINTER;
import static com.esp.espflow.util.EspFlowConstants.HIDDEN;
import static com.esp.espflow.util.EspFlowConstants.LOADING;
import static com.esp.espflow.util.EspFlowConstants.NO_DEVICES_SHOWN;
import static com.esp.espflow.util.EspFlowConstants.OVERFLOW_X;
import static com.esp.espflow.util.EspFlowConstants.OVERFLOW_Y;
import static com.esp.espflow.util.EspFlowConstants.PORT_FAILURE;
import static com.esp.espflow.util.EspFlowConstants.SETTINGS;
import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW;

/**
Expand Down Expand Up @@ -322,7 +324,7 @@ private HorizontalLayout getFooter() {
final Div divSpanTotalDevices = new Div(this.spanTotalDevices, this.spanTotalDevicesValue);

this.divWithPortErrors.setVisible(false);
this.divWithPortErrors.getStyle().setCursor("pointer");
this.divWithPortErrors.getStyle().setCursor(CURSOR_POINTER);
Tooltip.forComponent(divWithPortErrors).setText("Change port permissions - SPACE");
this.divWithPortErrors.addClickShortcut(Key.SPACE);
this.divWithPortErrors.addClickListener(event -> this.showErroneousPortsInDialog());
Expand All @@ -338,7 +340,7 @@ private HorizontalLayout getFooter() {
event -> this.showErroneousPortsInDialog()).setCheckable(true);

spanPortFailure.addClassName(Left.SMALL);
spanPortFailure.getStyle().setCursor("pointer");
spanPortFailure.getStyle().setCursor(CURSOR_POINTER);
this.divWithPortErrors.add(spanPortFailure);
divWithPortErrors.getStyle().set("color", "red");

Expand Down Expand Up @@ -687,7 +689,6 @@ protected void onDetach(DetachEvent detachEvent) {

@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
if (attachEvent.isInitialAttach()) {
super.onAttach(attachEvent);
final UI ui = attachEvent.getUI();
Expand All @@ -708,16 +709,16 @@ protected void onAttach(AttachEvent attachEvent) {
"}"
).then(String.class, hash -> {
log.info("Fragmento de URI: {}", hash);
if (Objects.nonNull(hash) && !hash.contains("settings")) {
if (Objects.nonNull(hash) && !hash.contains(SETTINGS)) {
this.add(this.wizardReadFlashView);
this.wizardReadFlashView.open();
} else {
ui.getPage().fetchCurrentURL(url -> {
final String ref = StringUtils.defaultIfEmpty(url.getRef(), StringUtils.EMPTY);
if (!ref.contains("settings")) {
if (!ref.contains(SETTINGS)) {
this.add(this.wizardReadFlashView);
this.wizardEspService.findWizardFlashEsp(WIZARD_READ_FLASH_ESP_VIEW)
.ifPresent((hide) -> {
this.wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW)
.ifPresent(hide -> {
if (hide.isWizardEnabled()) {
this.wizardReadFlashView.open();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,15 @@ private void configureFooter() {
hideButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY_INLINE);
hideButton.getStyle().set("margin-right", "auto");
hideButton.addClickListener(event -> {
final WizardEspDto wizardEspDto = WizardEspDto.builder()
.isWizardEnabled(false)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.build();
this.wizardEspService.save(wizardEspDto);
this.wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW)
.ifPresent(entityPresent -> {
final WizardEspDto wizardEspDto = WizardEspDto.builder()
.id(entityPresent.id())
.isWizardEnabled(false)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.build();
this.wizardEspService.save(wizardEspDto);
});
super.close();
});
super.getFooter().add(hideButton);
Expand Down
Loading

0 comments on commit bacfa0f

Please sign in to comment.