Skip to content

Commit

Permalink
refactor!: use endTime as limit for count up mode
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 66d7abf commit 58b7aa5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SimpleTimer(final Number startTime) {
}

/**
* Sets the start time
* Sets the start time, for countdown mode.
*
* @param startTime value in seconds for the start time
*/
Expand All @@ -72,6 +72,16 @@ public void setStartTime(final Number startTime) {
reset();
}

/**
* Sets the end time, for countup mode.
*
* @param endTime value in seconds for the end time
*/
public void setEndTime(final Number endTime) {
getElement().setProperty("endTime", endTime.doubleValue());
reset();
}

/**
* Changes the behavior to count up or down Default is false for count down
*
Expand Down Expand Up @@ -168,7 +178,7 @@ public CompletableFuture<BigDecimal> getCurrentTimeAsync() {

/**
* Adds a property change listener for the {@code currentTime} property
*
*
* @param listener the property change listener
* @param period the minimum period between listener invocations, or 0 to disable throttling
* @param periodUnit time duration of throttling period
Expand Down
20 changes: 15 additions & 5 deletions src/main/resources/META-INF/frontend/simple-timer/simple-timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ Polymer({
value: 60
},
/**
* End time for the timer in seconds
*/
endTime: {
type: Number,
value: 0
},
/**
* Current time of the timer, in seconds
*/
currentTime: {
Expand Down Expand Up @@ -124,16 +131,18 @@ Polymer({
} else {
this.set('currentTime', this.startTime);
}
this.set('_formattedTime', this._formatTime(this.currentTime.toString()));
},

start: function() {
if ((this.currentTime <= 0 && !this.countUp)
|| (this.currentTime >= this.startTime && this.countUp) ) {
|| (this.currentTime >= this.endTime && this.countUp) ) {
// timer is over
this.currentTime = this.countUp ? this.startTime : 0;
this.currentTime = this.countUp ? this.endTime : 0;
this._formattedTime = this._formatTime(this.currentTime);
}

if (!this.startTime || this.isRunning) {
if (this.countUp && !this.endTime || !this.countUp && !this.startTime || this.isRunning) {
this.pause();
return;
}
Expand All @@ -151,9 +160,10 @@ Polymer({
return;
}
if ((this.currentTime <= 0 && !this.countUp)
|| (this.currentTime >= this.startTime && this.countUp) ) {
|| (this.currentTime >= this.endTime && this.countUp) ) {
// timer is over
this.currentTime = this.countUp ? this.startTime : 0;
this.currentTime = this.countUp ? this.endTime : 0;
this._formattedTime = this._formatTime(this.currentTime);
this.pause();
this.fire('simple-timer-end');
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package com.flowingcode.vaadin.addons.simpletimer;

import java.math.BigDecimal;
import com.flowingcode.vaadin.addons.demo.DemoSource;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.checkbox.Checkbox;
Expand All @@ -32,6 +31,7 @@
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.math.BigDecimal;

@SuppressWarnings("serial")
@PageTitle("Simple Timer Demo")
Expand All @@ -40,7 +40,7 @@
public class SimpletimerDemo extends Div {

public SimpletimerDemo() {
this.setSizeFull();
setSizeFull();
final SimpleTimer timer = new SimpleTimer();
timer.setWidth("100px");
timer.setHeight("50px");
Expand All @@ -49,7 +49,10 @@ public SimpletimerDemo() {
Span timerTitle = new Span("Simple Count Up Timer");

final TextField startTime =
new TextField("Start Time", e -> timer.setStartTime(new BigDecimal(e.getValue())));
new TextField("Start Time", e -> {
timer.setStartTime(new BigDecimal(e.getValue()));
timer.setEndTime(new BigDecimal(e.getValue()));
});
final Checkbox countUp = new Checkbox("Count Up", false);
countUp.addValueChangeListener(
e -> {
Expand Down Expand Up @@ -91,7 +94,9 @@ public SimpletimerDemo() {
new Checkbox(
"Visible",
e -> {
if (e.isFromClient()) timer.setVisible(!timer.isVisible());
if (e.isFromClient()) {
timer.setVisible(!timer.isVisible());
}
});
visible.setValue(true);

Expand Down

0 comments on commit 58b7aa5

Please sign in to comment.