Skip to content

Commit 8869ebd

Browse files
committed
fixed error where application would hang for n seconds after pc was in standby. callbacks do not pause while in standby mode
1 parent 2271d3b commit 8869ebd

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

src/main/java/de/doubleslash/keeptime/common/DateFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static String secondsToHHMMSS(final long currentWorkSeconds) {
2121
}
2222

2323
public static long getSecondsBewtween(final LocalDateTime startDate, final LocalDateTime endDate) {
24-
return Duration.between(startDate, endDate).getSeconds();
24+
return Math.abs(Duration.between(startDate, endDate).getSeconds());
2525
}
2626

2727
public static String toDayDateString(final LocalDate newvalue) {
Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,70 @@
11
package de.doubleslash.keeptime.view.time;
22

3+
import java.time.LocalDateTime;
34
import java.util.List;
45
import java.util.concurrent.CopyOnWriteArrayList;
56

7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import de.doubleslash.keeptime.common.DateFormatter;
611
import javafx.animation.Animation;
712
import javafx.animation.KeyFrame;
813
import javafx.animation.Timeline;
914
import javafx.util.Duration;
1015

1116
public class Interval {
1217

13-
public static List<CallBackListener> callBackListeners = new CopyOnWriteArrayList<>();
18+
private final static Logger Log = LoggerFactory.getLogger(Interval.class);
19+
20+
public static List<CallBackListener> callBackListeners = new CopyOnWriteArrayList<>();
21+
22+
private static Timeline timelineSession;
23+
private static LocalDateTime last = LocalDateTime.now();
24+
25+
public static void registerCallBack(final CallBackListener cbl) {
26+
if (timelineSession == null) {
27+
createTimeLine();
28+
}
29+
callBackListeners.add(cbl);
30+
}
1431

15-
private static Timeline timelineSession;
32+
/**
33+
* only create timeLine if needed
34+
*/
35+
private static void createTimeLine() {
36+
timelineSession = new Timeline(new KeyFrame(Duration.seconds(1), (ae) -> {
37+
debounceAndExecuteCallbacks();
38+
}));
39+
timelineSession.setCycleCount(Animation.INDEFINITE);
40+
timelineSession.play();
1641

17-
public static void registerCallBack(CallBackListener cbl) {
18-
if (timelineSession == null)
19-
createTimeLine();
20-
callBackListeners.add(cbl);
21-
}
42+
}
2243

23-
/**
24-
* only create timeLine if needed
25-
*/
26-
private static void createTimeLine() {
27-
timelineSession = new Timeline(new KeyFrame(Duration.seconds(1), (ae) -> {
28-
callBackListeners.forEach(CallBackListener::call);
29-
}));
30-
timelineSession.setCycleCount(Animation.INDEFINITE);
31-
timelineSession.play();
44+
private static void debounceAndExecuteCallbacks() {
45+
final LocalDateTime now = LocalDateTime.now();
46+
final long secondsBewtween = DateFormatter.getSecondsBewtween(last, now);
47+
final int nanoBetween = java.time.Duration.between(last, now).abs().getNano();
3248

33-
}
49+
// ignore callbacks faster than ~1 second (can happen if we were in standby)
50+
// Log.debug("{} - {} = {}.{}", now, last, secondsBewtween, nanoBetween);
51+
if (secondsBewtween >= 1 || nanoBetween > 900000000) {
52+
callBackListeners.forEach(CallBackListener::call);
53+
last = now;
54+
}
55+
}
3456

35-
public static void removeCallBack(CallBackListener cbl) {
36-
if (timelineSession == null)
37-
return;
57+
public static void removeCallBack(final CallBackListener cbl) {
58+
if (timelineSession == null) {
59+
return;
60+
}
3861

39-
callBackListeners.remove(cbl);
62+
callBackListeners.remove(cbl);
4063

41-
if (callBackListeners.isEmpty()) {
42-
timelineSession.stop();
43-
timelineSession = null;
44-
}
45-
}
64+
if (callBackListeners.isEmpty()) {
65+
timelineSession.stop();
66+
timelineSession = null;
67+
}
68+
}
4669

4770
}

0 commit comments

Comments
 (0)