Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.0 #37

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

<groupId>com.flowingcode.addons</groupId>
<artifactId>simple-timer</artifactId>
<version>2.2.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<name>Simple Timer Addon</name>
<description>Simple Timer for Vaadin Flow</description>

<properties>
<vaadin.version>14.8.20</vaadin.version>
<vaadin.version>14.11.12</vaadin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,35 @@
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);
}

/**
* Sets the start time
* Sets the start time, for countdown mode.
*
* @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();
}

/**
* Changes the behavior to count up or down Default is false for count down
* Sets the end time, for countup mode.
*
* @param countUp
* @param endTime value in seconds for the end time
*/
public void setCountUp(final boolean countUp) {
getElement().setProperty("countUp", countUp);
public void setEndTime(final Number endTime) {
getElement().setProperty("countUp", true);
getElement().setProperty("endTime", endTime.doubleValue());
reset();
}

Expand Down Expand Up @@ -168,7 +159,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
28 changes: 19 additions & 9 deletions src/main/resources/META-INF/frontend/simple-timer/simple-timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ 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
*/
endTime: {
type: Number,
value: 0
},
/**
* Current time of the timer, in seconds
Expand Down Expand Up @@ -121,21 +127,21 @@ 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());
}
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 @@ -153,9 +159,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 All @@ -175,6 +182,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;
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,35 +31,45 @@
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.math.BigDecimal;
paodb marked this conversation as resolved.
Show resolved Hide resolved

@SuppressWarnings("serial")
@PageTitle("Simple Timer Demo")
@DemoSource
@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);
Comment on lines +42 to +45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize countUpMode and time fields in the constructor.

It is better to initialize the countUpMode and time fields in the constructor for clarity and consistency.

- private boolean countUpMode;
- private BigDecimal time = new BigDecimal(60);
+ private boolean countUpMode = false;
+ private BigDecimal time;

Committable suggestion was skipped due to low confidence.


public SimpletimerDemo() {
this.setSizeFull();
final SimpleTimer timer = new SimpleTimer();
setSizeFull();
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())));
new TextField("Start Time", e -> {
time = new BigDecimal(e.getValue());
update();
});
paodb marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -91,7 +100,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 All @@ -110,4 +121,12 @@ public SimpletimerDemo() {

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

private void update() {
if (countUpMode) {
timer.setEndTime(time);
} else {
timer.setStartTime(time);
}
}
paodb marked this conversation as resolved.
Show resolved Hide resolved
}
Loading