|
1 | 1 | package de.doubleslash.keeptime.view.time; |
2 | 2 |
|
| 3 | +import java.time.LocalDateTime; |
3 | 4 | import java.util.List; |
4 | 5 | import java.util.concurrent.CopyOnWriteArrayList; |
5 | 6 |
|
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import de.doubleslash.keeptime.common.DateFormatter; |
6 | 11 | import javafx.animation.Animation; |
7 | 12 | import javafx.animation.KeyFrame; |
8 | 13 | import javafx.animation.Timeline; |
9 | 14 | import javafx.util.Duration; |
10 | 15 |
|
11 | 16 | public class Interval { |
12 | 17 |
|
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 | + } |
14 | 31 |
|
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(); |
16 | 41 |
|
17 | | - public static void registerCallBack(CallBackListener cbl) { |
18 | | - if (timelineSession == null) |
19 | | - createTimeLine(); |
20 | | - callBackListeners.add(cbl); |
21 | | - } |
| 42 | + } |
22 | 43 |
|
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(); |
32 | 48 |
|
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 | + } |
34 | 56 |
|
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 | + } |
38 | 61 |
|
39 | | - callBackListeners.remove(cbl); |
| 62 | + callBackListeners.remove(cbl); |
40 | 63 |
|
41 | | - if (callBackListeners.isEmpty()) { |
42 | | - timelineSession.stop(); |
43 | | - timelineSession = null; |
44 | | - } |
45 | | - } |
| 64 | + if (callBackListeners.isEmpty()) { |
| 65 | + timelineSession.stop(); |
| 66 | + timelineSession = null; |
| 67 | + } |
| 68 | + } |
46 | 69 |
|
47 | 70 | } |
0 commit comments