Skip to content

Commit

Permalink
refactor!: configure count mode when startTime or endTime are called
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Jul 11, 2024
1 parent fd489d0 commit 6e52645
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public SimpleTimer() {
* @param startTime value in seconds for the start time
*/
public void setStartTime(final Number startTime) {
getElement().setProperty("countUp", false);
getElement().setProperty("startTime", startTime.doubleValue());
getElement().setProperty(CURRENT_TIME, startTime.doubleValue());
reset();
Expand All @@ -67,20 +68,11 @@ public void setStartTime(final Number startTime) {
* @param endTime value in seconds for the end time
*/
public void setEndTime(final Number endTime) {
getElement().setProperty("countUp", true);
getElement().setProperty("endTime", endTime.doubleValue());
reset();
}

/**
* Changes the behavior to count up or down Default is false for count down
*
* @param countUp
*/
public void setCountUp(final boolean countUp) {
getElement().setProperty("countUp", countUp);
reset();
}

/**
* Enables showing fractions of a second
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,37 @@
@Route(value = "simple-timer/simple-timer", layout = SimpletimerDemoView.class)
public class SimpletimerDemo extends Div {

private final SimpleTimer timer = new SimpleTimer();

private boolean countUpMode;
private BigDecimal time = new BigDecimal(60);

public SimpletimerDemo() {
setSizeFull();
final SimpleTimer timer = new SimpleTimer();
timer.setWidth("100px");
timer.setHeight("50px");
timer.getStyle().set("font-size", "40px");
timer.setStartTime(60);

Span timerTitle = new Span("Simple Count Up Timer");

final TextField startTime =
new TextField("Start Time", e -> {
timer.setStartTime(new BigDecimal(e.getValue()));
timer.setEndTime(new BigDecimal(e.getValue()));
time = new BigDecimal(e.getValue());
update();
});
final Checkbox countUp = new Checkbox("Count Up", false);
countUp.addValueChangeListener(
e -> {
timer.setCountUp(countUp.getValue());
if (e.getValue()) {
countUpMode = countUp.getValue();
if (countUpMode) {
startTime.setLabel("End Time");
timerTitle.setText("Simple Count Up Timer");
} else {
startTime.setLabel("Start Time");
timerTitle.setText("Simple Countdown Timer");
}
update();
});
final Button start = new Button("Start/Stop", e -> timer.start());
final Button stop = new Button("Stop", e -> timer.pause());
Expand Down Expand Up @@ -115,4 +121,12 @@ public SimpletimerDemo() {

add(new VerticalLayout(topLayout, startTime, options, bottomLayout));
}

private void update() {
if (countUpMode) {
timer.setEndTime(time);
} else {
timer.setStartTime(time);
}
}
}

0 comments on commit 6e52645

Please sign in to comment.