Skip to content

Commit 58b7aa5

Browse files
javier-godoypaodb
authored andcommitted
refactor!: use endTime as limit for count up mode
1 parent 66d7abf commit 58b7aa5

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public SimpleTimer(final Number startTime) {
6262
}
6363

6464
/**
65-
* Sets the start time
65+
* Sets the start time, for countdown mode.
6666
*
6767
* @param startTime value in seconds for the start time
6868
*/
@@ -72,6 +72,16 @@ public void setStartTime(final Number startTime) {
7272
reset();
7373
}
7474

75+
/**
76+
* Sets the end time, for countup mode.
77+
*
78+
* @param endTime value in seconds for the end time
79+
*/
80+
public void setEndTime(final Number endTime) {
81+
getElement().setProperty("endTime", endTime.doubleValue());
82+
reset();
83+
}
84+
7585
/**
7686
* Changes the behavior to count up or down Default is false for count down
7787
*
@@ -168,7 +178,7 @@ public CompletableFuture<BigDecimal> getCurrentTimeAsync() {
168178

169179
/**
170180
* Adds a property change listener for the {@code currentTime} property
171-
*
181+
*
172182
* @param listener the property change listener
173183
* @param period the minimum period between listener invocations, or 0 to disable throttling
174184
* @param periodUnit time duration of throttling period

src/main/resources/META-INF/frontend/simple-timer/simple-timer.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ Polymer({
4848
value: 60
4949
},
5050
/**
51+
* End time for the timer in seconds
52+
*/
53+
endTime: {
54+
type: Number,
55+
value: 0
56+
},
57+
/**
5158
* Current time of the timer, in seconds
5259
*/
5360
currentTime: {
@@ -124,16 +131,18 @@ Polymer({
124131
} else {
125132
this.set('currentTime', this.startTime);
126133
}
134+
this.set('_formattedTime', this._formatTime(this.currentTime.toString()));
127135
},
128136

129137
start: function() {
130138
if ((this.currentTime <= 0 && !this.countUp)
131-
|| (this.currentTime >= this.startTime && this.countUp) ) {
139+
|| (this.currentTime >= this.endTime && this.countUp) ) {
132140
// timer is over
133-
this.currentTime = this.countUp ? this.startTime : 0;
141+
this.currentTime = this.countUp ? this.endTime : 0;
142+
this._formattedTime = this._formatTime(this.currentTime);
134143
}
135144

136-
if (!this.startTime || this.isRunning) {
145+
if (this.countUp && !this.endTime || !this.countUp && !this.startTime || this.isRunning) {
137146
this.pause();
138147
return;
139148
}
@@ -151,9 +160,10 @@ Polymer({
151160
return;
152161
}
153162
if ((this.currentTime <= 0 && !this.countUp)
154-
|| (this.currentTime >= this.startTime && this.countUp) ) {
163+
|| (this.currentTime >= this.endTime && this.countUp) ) {
155164
// timer is over
156-
this.currentTime = this.countUp ? this.startTime : 0;
165+
this.currentTime = this.countUp ? this.endTime : 0;
166+
this._formattedTime = this._formatTime(this.currentTime);
157167
this.pause();
158168
this.fire('simple-timer-end');
159169
return;

src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.simpletimer;
2121

22-
import java.math.BigDecimal;
2322
import com.flowingcode.vaadin.addons.demo.DemoSource;
2423
import com.vaadin.flow.component.button.Button;
2524
import com.vaadin.flow.component.checkbox.Checkbox;
@@ -32,6 +31,7 @@
3231
import com.vaadin.flow.component.textfield.TextField;
3332
import com.vaadin.flow.router.PageTitle;
3433
import com.vaadin.flow.router.Route;
34+
import java.math.BigDecimal;
3535

3636
@SuppressWarnings("serial")
3737
@PageTitle("Simple Timer Demo")
@@ -40,7 +40,7 @@
4040
public class SimpletimerDemo extends Div {
4141

4242
public SimpletimerDemo() {
43-
this.setSizeFull();
43+
setSizeFull();
4444
final SimpleTimer timer = new SimpleTimer();
4545
timer.setWidth("100px");
4646
timer.setHeight("50px");
@@ -49,7 +49,10 @@ public SimpletimerDemo() {
4949
Span timerTitle = new Span("Simple Count Up Timer");
5050

5151
final TextField startTime =
52-
new TextField("Start Time", e -> timer.setStartTime(new BigDecimal(e.getValue())));
52+
new TextField("Start Time", e -> {
53+
timer.setStartTime(new BigDecimal(e.getValue()));
54+
timer.setEndTime(new BigDecimal(e.getValue()));
55+
});
5356
final Checkbox countUp = new Checkbox("Count Up", false);
5457
countUp.addValueChangeListener(
5558
e -> {
@@ -91,7 +94,9 @@ public SimpletimerDemo() {
9194
new Checkbox(
9295
"Visible",
9396
e -> {
94-
if (e.isFromClient()) timer.setVisible(!timer.isVisible());
97+
if (e.isFromClient()) {
98+
timer.setVisible(!timer.isVisible());
99+
}
95100
});
96101
visible.setValue(true);
97102

0 commit comments

Comments
 (0)