From aed92311c2de113de21234f7cf1d5d0919db9086 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:28:06 -0300 Subject: [PATCH 1/6] build: set version to 3.0.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2aa5258..59a4b2d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.flowingcode.addons simple-timer - 2.2.1-SNAPSHOT + 3.0.0-SNAPSHOT Simple Timer Addon Simple Timer for Vaadin Flow From 7cd334108507d981c50f2b5594c5362ba17c739b Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:30:03 -0300 Subject: [PATCH 2/6] build: upgrade Vaadin version to 14.11.12 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 59a4b2d..8da6b5e 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ Simple Timer for Vaadin Flow - 14.8.20 + 14.11.12 1.8 1.8 UTF-8 From 1b1a9fef946dc24a3fc3afead93f38133910506f Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:40:42 -0300 Subject: [PATCH 3/6] fix: format time when timer starts/stops --- .../resources/META-INF/frontend/simple-timer/simple-timer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js index f801941..0e8ae52 100644 --- a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js +++ b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js @@ -121,10 +121,8 @@ Polymer({ ready: function() { if (this.countUp) { this.set('currentTime', 0); - this.set('_formattedTime', '0'); } else { this.set('currentTime', this.startTime); - this.set('_formattedTime', this.startTime.toString()); } }, @@ -175,6 +173,9 @@ Polymer({ if (timeString[0].indexOf('-') === 0) { return 0; } + if (timeString.length==1) { + timeString.push("00"); + } var seconds = timeString[0]; var minutes = seconds / 60 | 0; var hours = minutes / 60 | 0; From d7cb75d5a3a86c1044b07adec9a404fc91ba70d0 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:33:26 -0300 Subject: [PATCH 4/6] refactor!: use endTime as limit for count up mode --- .../addons/simpletimer/SimpleTimer.java | 14 +++++++++++-- .../frontend/simple-timer/simple-timer.js | 20 ++++++++++++++----- .../addons/simpletimer/SimpletimerDemo.java | 13 ++++++++---- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java index 033d1b3..75c55a2 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java @@ -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 */ @@ -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 * @@ -168,7 +178,7 @@ public CompletableFuture 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 diff --git a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js index 0e8ae52..409b8ad 100644 --- a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js +++ b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js @@ -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: { @@ -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; } @@ -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; diff --git a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java index c93e395..86ff35b 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java @@ -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; @@ -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") @@ -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"); @@ -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 -> { @@ -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); From e47e9ea851b70069c21aa4ff347abd50f6ecb324 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:09:12 -0300 Subject: [PATCH 5/6] refactor!: remove default value for startTime --- .../vaadin/addons/simpletimer/SimpleTimer.java | 13 +------------ .../META-INF/frontend/simple-timer/simple-timer.js | 3 +-- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java index 75c55a2..26cc41c 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java @@ -41,24 +41,13 @@ public class SimpleTimer extends Component implements HasSize, HasStyle, Serializable { private static final long serialVersionUID = 1L; - private static final int START_TIME_S = 60; private static final String DISPLAY = "display"; private static final String INLINE = "inline"; private static final String CURRENT_TIME = "currentTime"; - /** Creates a timer with a start time of 60 */ + /** Creates a timer */ public SimpleTimer() { - this(START_TIME_S); - } - - /** - * Creates a timer using the start time passed in the constructor - * - * @param startTime value in seconds for the start time - */ - public SimpleTimer(final Number startTime) { getElement().getStyle().set(DISPLAY, INLINE); - setStartTime(startTime); } /** diff --git a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js index 409b8ad..4965118 100644 --- a/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js +++ b/src/main/resources/META-INF/frontend/simple-timer/simple-timer.js @@ -41,11 +41,10 @@ Polymer({ properties: { /** * Start time for the timer in seconds - * @default 60 */ startTime: { type: Number, - value: 60 + value: 0 }, /** * End time for the timer in seconds From d15be7f7d82d2cb74ebb9a2e24bb6fe1b294fb13 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:10:07 -0300 Subject: [PATCH 6/6] refactor!: configure count mode when startTime or endTime are called --- .../addons/simpletimer/SimpleTimer.java | 12 ++-------- .../addons/simpletimer/SimpletimerDemo.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java index 26cc41c..f1bb167 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/simpletimer/SimpleTimer.java @@ -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(); @@ -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 * diff --git a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java index 86ff35b..6ca9a14 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/simpletimer/SimpletimerDemo.java @@ -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()); @@ -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); + } + } }