Skip to content

Commit 05e474d

Browse files
committed
fix: add target used to display the timer value.
1 parent d5a11b6 commit 05e474d

File tree

5 files changed

+53
-43
lines changed

5 files changed

+53
-43
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public void setDoubleDigitHours(final boolean doubleDigitHours) {
109109
getElement().setProperty("doubleDigitHours", doubleDigitHours);
110110
}
111111

112+
public void setTargetId(final String targetId) {
113+
getElement().setProperty("targetId", targetId);
114+
}
115+
112116
/** Starts or stops the timer if it is already started */
113117
public void start() {
114118
getElement().callJsFunction("start");
@@ -167,8 +171,8 @@ public CompletableFuture<BigDecimal> getCurrentTimeAsync() {
167171
* @return this registration, for chaining
168172
*/
169173
public Registration addCurrentTimeChangeListener(
170-
PropertyChangeListener listener, long period, TimeUnit periodUnit) {
171-
int millis = (int) Math.min(periodUnit.toMillis(period), Integer.MAX_VALUE);
174+
PropertyChangeListener listener, final long period, final TimeUnit periodUnit) {
175+
final int millis = (int) Math.min(periodUnit.toMillis(period), Integer.MAX_VALUE);
172176
if (listener == null) {
173177
listener = ev -> {};
174178
}
@@ -202,7 +206,7 @@ public boolean isVisible() {
202206
}
203207

204208
@Override
205-
public void setVisible(boolean visible) {
209+
public void setVisible(final boolean visible) {
206210
getStyle().set(DISPLAY, visible ? INLINE : "none");
207211
}
208212
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ Polymer({
110110
type: Boolean,
111111
value: false
112112
},
113+
targetId: {
114+
type: String,
115+
value: null,
116+
},
113117
/**
114118
* Time the timer has spent running since it was started
115119
*/
@@ -120,7 +124,8 @@ Polymer({
120124

121125
_formattedTime: {
122126
type: String,
123-
value: '0'
127+
value: '0',
128+
observer: "_updateTarget",
124129
}
125130
},
126131

@@ -153,8 +158,7 @@ Polymer({
153158
},
154159

155160
_decreaseTimer: function(timestamp) {
156-
console.error("_decreaseTimer "+this.isRunning+" "+this.currentTime);
157-
if (!this.isRunning || !this.isAttached) {
161+
if (!this.isRunning) {
158162
return;
159163
}
160164
if ((this.currentTime <= 0 && !this.countUp)
@@ -199,13 +203,9 @@ Polymer({
199203
}
200204
return (this.hours ? hours + ':' : '') + (this.minutes || this.hours ? minutes + ':' : '') + seconds + (this.fractions ? ('.' + timeString[1].substring(0,2)) : '')
201205
},
202-
203-
detached: function() {
204-
this.isAttached=false;
205-
},
206-
attached: function() {
207-
this.isAttached=true;
208-
debugger;
209-
if (this.isRunning) window.requestAnimationFrame(this._decreaseTimer.bind(this));
210-
},
206+
_updateTarget: function(newValue, oldValue){
207+
if (document.getElementById(this.targetId)) {
208+
document.getElementById(this.targetId).innerText = newValue;
209+
}
210+
}
211211
});

src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationView.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@
44
import com.vaadin.flow.component.ClientCallable;
55
import com.vaadin.flow.component.dialog.Dialog;
66
import com.vaadin.flow.component.html.Div;
7+
import com.vaadin.flow.component.html.Label;
78
import com.vaadin.flow.router.Route;
89

910
@Route("/it")
1011
public class IntegrationView extends Div implements IntegrationCallables {
1112

12-
private SimpleTimer timer = new SimpleTimer();
13+
private final SimpleTimer timer = new SimpleTimer();
1314

1415
private Dialog dialog;
16+
17+
private final Label timerTarget;
18+
1519
public IntegrationView() {
1620
add(timer);
21+
timerTarget = new Label();
22+
timerTarget.setId("timerTarget");
23+
timer.setTargetId("timerTarget");
1724
}
1825

1926
@Override
2027
@ClientCallable
2128
public void openDialog() {
2229
if (dialog == null) {
23-
dialog = new Dialog(timer);
30+
dialog = new Dialog(timerTarget);
2431
}
2532
dialog.open();
2633
}
@@ -33,13 +40,13 @@ public void closeDialog() {
3340

3441
@Override
3542
@ClientCallable
36-
public void setStartTime(Integer startTime) {
43+
public void setStartTime(final Integer startTime) {
3744
timer.setStartTime(startTime);
3845
}
3946

4047
@Override
4148
@ClientCallable
42-
public void setEndTime(Integer endTime) {
49+
public void setEndTime(final Integer endTime) {
4350
timer.setEndTime(endTime);
4451
}
4552

src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleIT.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package com.flowingcode.addons.simpletimer.integration;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.equalTo;
5-
import static org.hamcrest.Matchers.greaterThan;
6-
import static org.hamcrest.Matchers.lessThan;
7-
import static org.hamcrest.Matchers.nullValue;
8-
import static org.junit.Assert.assertFalse;
9-
import static org.junit.Assert.assertTrue;
4+
import static org.hamcrest.Matchers.*;
5+
import static org.junit.Assert.*;
106
import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport;
117
import java.util.concurrent.TimeUnit;
12-
import org.junit.Ignore;
138
import org.junit.Test;
149

1510
public class SimpleIT extends AbstractViewTest implements HasRpcSupport {
@@ -20,10 +15,10 @@ public SimpleIT() {
2015
super("it");
2116
}
2217

23-
private static void sleep(long millis) {
18+
private static void sleep(final long millis) {
2419
try {
2520
Thread.sleep(millis);
26-
} catch (InterruptedException e) {
21+
} catch (final InterruptedException e) {
2722
Thread.currentThread().interrupt();
2823
}
2924
}
@@ -42,8 +37,8 @@ public void countDown() {
4237

4338
$server.start();
4439
assertTrue($server.isRunning());
45-
double t0 = currentTime();
46-
double t1 = currentTime();
40+
final double t0 = currentTime();
41+
final double t1 = currentTime();
4742
assertThat(t0, lessThan(1.0));
4843
assertThat(t1, lessThan(t0));
4944
}
@@ -58,33 +53,32 @@ public void countUp() {
5853

5954
$server.start();
6055
assertTrue($server.isRunning());
61-
double t0 = currentTime();
62-
double t1 = currentTime();
56+
final double t0 = currentTime();
57+
final double t1 = currentTime();
6358
assertThat(t0, greaterThan(0.0));
6459
assertThat(t1, greaterThan(t0));
6560
}
6661

6762
@Test
68-
@Ignore
6963
public void countUpInDialog() {
7064
$server.openDialog();
7165
$server.setEndTime(100);
7266
assertThat(currentTime(), equalTo(0.0));
7367

74-
long w0 = System.nanoTime();
68+
final long w0 = System.nanoTime();
7569
$server.start();
76-
double t0 = currentTime();
70+
final double t0 = currentTime();
7771
assertThat(t0, greaterThan(0.0));
78-
long w1 = System.nanoTime();
72+
final long w1 = System.nanoTime();
7973

8074
$server.closeDialog();
8175
sleep(0);
8276
$server.openDialog();
8377

84-
long w2 = System.nanoTime();
78+
final long w2 = System.nanoTime();
8579
// double delta = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - now) / 1000.0;
86-
double t1 = currentTime();
87-
long w3 = System.nanoTime();
80+
final double t1 = currentTime();
81+
final long w3 = System.nanoTime();
8882
// assertThat(t1, greaterThan(t0 + delta));
8983
System.out.println(TimeUnit.NANOSECONDS.toMillis(w3 - w0) / 1000.0);
9084
System.out.println(TimeUnit.NANOSECONDS.toMillis(w2 - w1) / 1000.0);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.vaadin.flow.component.checkbox.Checkbox;
2525
import com.vaadin.flow.component.dialog.Dialog;
2626
import com.vaadin.flow.component.html.Div;
27+
import com.vaadin.flow.component.html.Label;
2728
import com.vaadin.flow.component.html.Span;
2829
import com.vaadin.flow.component.notification.Notification;
2930
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
@@ -52,7 +53,7 @@ public SimpletimerDemo2() {
5253
timer.getStyle().set("font-size", "40px");
5354
timer.setStartTime(60);
5455

55-
Span timerTitle = new Span("Simple Count Up Timer");
56+
final Span timerTitle = new Span("Simple Count Up Timer");
5657

5758
final TextField startTime =
5859
new TextField("Start Time", e -> {
@@ -109,10 +110,11 @@ public SimpletimerDemo2() {
109110

110111
timer.addTimerEndEvent(e -> Notification.show("Timer Ended"));
111112

112-
final HorizontalLayout topLayout = new HorizontalLayout(timerTitle);
113+
timer.setVisible(false);
114+
final HorizontalLayout topLayout = new HorizontalLayout(timerTitle, timer);
113115
topLayout.setAlignItems(Alignment.CENTER);
114116

115-
HorizontalLayout options =
117+
final HorizontalLayout options =
116118
new HorizontalLayout(countUp, fractions, minutes, hours, visible, doubleDigitHours);
117119
options.setAlignItems(Alignment.CENTER);
118120
options.getStyle().set("flex-wrap", "wrap");
@@ -122,7 +124,10 @@ public SimpletimerDemo2() {
122124

123125
add(new VerticalLayout(topLayout, startTime, options, bottomLayout));
124126

125-
Dialog dlg = new Dialog(timer);
127+
final Label timerTarget = new Label();
128+
timerTarget.setId("timerTarget");
129+
130+
final Dialog dlg = new Dialog(timerTarget);
126131
dlg.setModal(false);
127132
dlg.setCloseOnOutsideClick(false);
128133
dlg.add(new Button("Close", ev -> dlg.close()));

0 commit comments

Comments
 (0)