Skip to content

Commit afbae48

Browse files
committed
WIP
1 parent fb786ec commit afbae48

File tree

4 files changed

+158
-10
lines changed

4 files changed

+158
-10
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Polymer({
113113
/**
114114
* Time the timer has spent running since it was started
115115
*/
116-
_elapsedTime: {
116+
_elapsed: {
117117
type: Number,
118118
value: 0
119119
},
@@ -125,12 +125,10 @@ Polymer({
125125
},
126126

127127
ready: function() {
128-
if (this.countUp) {
129-
this.set('currentTime', 0);
130-
} else {
131-
this.set('currentTime', this.startTime);
132-
}
133-
this.set('_formattedTime', this._formatTime(this.currentTime.toString()));
128+
if (this.currentTime===undefined) {
129+
this.set('currentTime', this.countUp ? 0 : this.startTime);
130+
}
131+
this.set('_formattedTime', this._formatTime(this.currentTime.toString()));
134132
},
135133

136134
start: function() {
@@ -155,7 +153,8 @@ Polymer({
155153
},
156154

157155
_decreaseTimer: function(timestamp) {
158-
if (!this.isRunning) {
156+
console.error("_decreaseTimer "+this.isRunning+" "+this.currentTime);
157+
if (!this.isRunning || !this.isAttached) {
159158
return;
160159
}
161160
if ((this.currentTime <= 0 && !this.countUp)
@@ -199,5 +198,14 @@ Polymer({
199198
hours = hours.toString().padStart(2, '0');
200199
}
201200
return (this.hours ? hours + ':' : '') + (this.minutes || this.hours ? minutes + ':' : '') + seconds + (this.fractions ? ('.' + timeString[1].substring(0,2)) : '')
202-
}
201+
},
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+
},
203211
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public SimpletimerDemo() {
7171
}
7272
update();
7373
});
74-
final Button start = new Button("Start/Stop", e -> timer.start());
74+
final Button start = new Button("Start", e -> timer.start());
7575
final Button stop = new Button("Stop", e -> timer.pause());
7676
final Button reset =
7777
new Button(
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*-
2+
* #%L
3+
* Simple Timer Addon
4+
* %%
5+
* Copyright (C) 2019 - 2020 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.simpletimer;
21+
22+
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.vaadin.flow.component.button.Button;
24+
import com.vaadin.flow.component.checkbox.Checkbox;
25+
import com.vaadin.flow.component.dialog.Dialog;
26+
import com.vaadin.flow.component.html.Div;
27+
import com.vaadin.flow.component.html.Span;
28+
import com.vaadin.flow.component.notification.Notification;
29+
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
30+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
31+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
32+
import com.vaadin.flow.component.textfield.TextField;
33+
import com.vaadin.flow.router.PageTitle;
34+
import com.vaadin.flow.router.Route;
35+
import java.math.BigDecimal;
36+
37+
@SuppressWarnings("serial")
38+
@PageTitle("Simple Timer Demo")
39+
@DemoSource
40+
@Route(value = "simple-timer/simple-timer2", layout = SimpletimerDemoView.class)
41+
public class SimpletimerDemo2 extends Div {
42+
43+
private final SimpleTimer timer = new SimpleTimer();
44+
45+
private boolean countUpMode;
46+
private BigDecimal time = new BigDecimal(60);
47+
48+
public SimpletimerDemo2() {
49+
setSizeFull();
50+
timer.setWidth("100px");
51+
timer.setHeight("50px");
52+
timer.getStyle().set("font-size", "40px");
53+
timer.setStartTime(60);
54+
55+
Span timerTitle = new Span("Simple Count Up Timer");
56+
57+
final TextField startTime =
58+
new TextField("Start Time", e -> {
59+
time = new BigDecimal(e.getValue());
60+
update();
61+
});
62+
final Checkbox countUp = new Checkbox("Count Up", false);
63+
countUp.addValueChangeListener(
64+
e -> {
65+
countUpMode = countUp.getValue();
66+
if (countUpMode) {
67+
startTime.setLabel("End Time");
68+
timerTitle.setText("Simple Count Up Timer");
69+
} else {
70+
startTime.setLabel("Start Time");
71+
timerTitle.setText("Simple Countdown Timer");
72+
}
73+
update();
74+
});
75+
final Button start = new Button("Start/Stop", e -> timer.start());
76+
final Button stop = new Button("Stop", e -> timer.pause());
77+
final Button reset =
78+
new Button(
79+
"Reset",
80+
e -> {
81+
timer.reset();
82+
});
83+
final Button running =
84+
new Button(
85+
"Current Time",
86+
e ->
87+
timer
88+
.getCurrentTimeAsync()
89+
.thenAccept(
90+
time ->
91+
Notification.show(
92+
time.toPlainString()
93+
+ (timer.isRunning() ? "" : " (Not Running)"))));
94+
final Checkbox fractions = new Checkbox("Fractions", true);
95+
fractions.addValueChangeListener(e -> timer.setFractions(e.getValue()));
96+
final Checkbox minutes = new Checkbox("Minutes", e -> timer.setMinutes(e.getValue()));
97+
final Checkbox hours = new Checkbox("Hours", e -> timer.setHours(e.getValue()));
98+
final Checkbox doubleDigitHours =
99+
new Checkbox("Double digit hours", e -> timer.setDoubleDigitHours(e.getValue()));
100+
final Checkbox visible =
101+
new Checkbox(
102+
"Visible",
103+
e -> {
104+
if (e.isFromClient()) {
105+
timer.setVisible(!timer.isVisible());
106+
}
107+
});
108+
visible.setValue(true);
109+
110+
timer.addTimerEndEvent(e -> Notification.show("Timer Ended"));
111+
112+
final HorizontalLayout topLayout = new HorizontalLayout(timerTitle);
113+
topLayout.setAlignItems(Alignment.CENTER);
114+
115+
HorizontalLayout options =
116+
new HorizontalLayout(countUp, fractions, minutes, hours, visible, doubleDigitHours);
117+
options.setAlignItems(Alignment.CENTER);
118+
options.getStyle().set("flex-wrap", "wrap");
119+
120+
final HorizontalLayout bottomLayout = new HorizontalLayout(start, stop, reset, running);
121+
bottomLayout.setAlignItems(Alignment.BASELINE);
122+
123+
add(new VerticalLayout(topLayout, startTime, options, bottomLayout));
124+
125+
Dialog dlg = new Dialog(timer);
126+
dlg.setModal(false);
127+
dlg.setCloseOnOutsideClick(false);
128+
dlg.add(new Button("Close", ev -> dlg.close()));
129+
add(new Button("OPEN", ev -> dlg.open()));
130+
}
131+
132+
private void update() {
133+
if (countUpMode) {
134+
timer.setEndTime(time);
135+
} else {
136+
timer.setStartTime(time);
137+
}
138+
}
139+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SimpletimerDemoView extends TabbedDemo {
3333

3434
public SimpletimerDemoView() {
3535
addDemo(SimpletimerDemo.class);
36+
addDemo(SimpletimerDemo2.class);
3637
setSizeFull();
3738
}
3839
}

0 commit comments

Comments
 (0)