From 58b7aa5926d30b909ea462d664a73be8d4909c49 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] 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);