Skip to content

Commit 06365d6

Browse files
committed
added quicksave Interval of 60sec
1 parent 69a4679 commit 06365d6

File tree

5 files changed

+51
-34
lines changed

5 files changed

+51
-34
lines changed

src/main/java/de/doubleslash/keeptime/view/time/CallBackListener.java renamed to src/main/java/de/doubleslash/keeptime/common/time/CallBackListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package de.doubleslash.keeptime.view.time;
17+
package de.doubleslash.keeptime.common.time;
1818

1919
public interface CallBackListener {
2020
public void call();

src/main/java/de/doubleslash/keeptime/view/time/Interval.java renamed to src/main/java/de/doubleslash/keeptime/common/time/Interval.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package de.doubleslash.keeptime.view.time;
17+
package de.doubleslash.keeptime.common.time;
1818

1919
import java.time.LocalDateTime;
2020
import java.util.List;
@@ -28,16 +28,17 @@
2828

2929
public class Interval {
3030

31-
private static final List<CallBackListener> callBackListeners = new CopyOnWriteArrayList<>();
31+
private final List<CallBackListener> callBackListeners = new CopyOnWriteArrayList<>();
3232

33-
private static Timeline timelineSession;
34-
private static LocalDateTime last = LocalDateTime.now();
33+
private Timeline timelineSession;
34+
private LocalDateTime last = LocalDateTime.now();
35+
private final long seconds;
3536

36-
private Interval() {
37-
throw new IllegalStateException("Utility class");
37+
public Interval(final long seconds) {
38+
this.seconds = seconds;
3839
}
3940

40-
public static void registerCallBack(final CallBackListener cbl) {
41+
public void registerCallBack(final CallBackListener cbl) {
4142
if (timelineSession == null) {
4243
createTimeLine();
4344
}
@@ -47,14 +48,14 @@ public static void registerCallBack(final CallBackListener cbl) {
4748
/**
4849
* only create timeLine if needed
4950
*/
50-
private static void createTimeLine() {
51-
timelineSession = new Timeline(new KeyFrame(Duration.seconds(1), ae -> debounceAndExecuteCallbacks()));
51+
private void createTimeLine() {
52+
timelineSession = new Timeline(new KeyFrame(Duration.seconds(seconds), ae -> debounceAndExecuteCallbacks()));
5253
timelineSession.setCycleCount(Animation.INDEFINITE);
5354
timelineSession.play();
5455

5556
}
5657

57-
private static void debounceAndExecuteCallbacks() {
58+
private void debounceAndExecuteCallbacks() {
5859
final LocalDateTime now = LocalDateTime.now();
5960
final long secondsBewtween = DateFormatter.getSecondsBewtween(last, now);
6061
final int nanoBetween = java.time.Duration.between(last, now).abs().getNano();
@@ -66,7 +67,7 @@ private static void debounceAndExecuteCallbacks() {
6667
}
6768
}
6869

69-
public static void removeCallBack(final CallBackListener cbl) {
70+
public void removeCallBack(final CallBackListener cbl) {
7071
if (timelineSession == null) {
7172
return;
7273
}

src/main/java/de/doubleslash/keeptime/controller/Controller.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import de.doubleslash.keeptime.common.DateFormatter;
3333
import de.doubleslash.keeptime.common.DateProvider;
34+
import de.doubleslash.keeptime.common.time.Interval;
3435
import de.doubleslash.keeptime.model.Model;
3536
import de.doubleslash.keeptime.model.Project;
3637
import de.doubleslash.keeptime.model.Settings;
@@ -40,6 +41,7 @@
4041

4142
@Service
4243
public class Controller {
44+
private final long QUICK_SAVE_INTERVAL = 60;
4345

4446
private static final Logger LOG = LoggerFactory.getLogger(Controller.class);
4547

@@ -51,44 +53,59 @@ public class Controller {
5153
public Controller(final Model model, final DateProvider dateProvider) {
5254
this.model = model;
5355
this.dateProvider = dateProvider;
56+
57+
// initiate quicksaving
58+
new Interval(QUICK_SAVE_INTERVAL).registerCallBack(() -> saveCurrentWork(0));
5459
}
5560

5661
public void changeProject(final Project newProject) {
5762
changeProject(newProject, 0);
5863
}
5964

6065
public void changeProject(final Project newProject, final long minusSeconds) {
61-
final Work currentWork = model.activeWorkItem.get();
6266

6367
final LocalDateTime now = dateProvider.dateTimeNow().minusSeconds(minusSeconds);
6468
final LocalDate dateNow = now.toLocalDate();
65-
if (currentWork != null) {
66-
currentWork.setEndTime(now);
67-
if (currentWork.getNotes().isEmpty()) {
68-
currentWork.setNotes("- No notes -");
69-
}
7069

71-
final String time = DateFormatter
72-
.secondsToHHMMSS(Duration.between(currentWork.getStartTime(), currentWork.getEndTime()).getSeconds());
73-
74-
LOG.info("You worked from '{}' to '{}' ({}) on project '{}' with notes '{}'", currentWork.getStartTime(),
75-
currentWork.getEndTime(), time, currentWork.getProject().getName(), currentWork.getNotes());
76-
77-
// Save in db
78-
model.getWorkRepository().save(currentWork);
79-
}
70+
saveCurrentWork(minusSeconds);
8071

8172
// Start new work
8273
final Work work = new Work(dateNow, now, now.plusSeconds(minusSeconds), newProject, "");
8374

8475
model.getPastWorkItems().add(work);
76+
77+
model.activeWorkItem.set(work);
78+
}
79+
80+
public void saveCurrentWork(final long minusSeconds) {
81+
final Work currentWork = model.activeWorkItem.get();
82+
83+
if (currentWork == null) {
84+
return;
85+
}
86+
final LocalDateTime now = dateProvider.dateTimeNow().minusSeconds(minusSeconds);
87+
final LocalDate dateNow = now.toLocalDate();
88+
89+
currentWork.setEndTime(now);
90+
if (currentWork.getNotes().isEmpty()) {
91+
currentWork.setNotes("- No notes -");
92+
}
93+
94+
final String time = DateFormatter
95+
.secondsToHHMMSS(Duration.between(currentWork.getStartTime(), currentWork.getEndTime()).getSeconds());
96+
97+
LOG.info("Saving Work from '{}' to '{}' ({}) on project '{}' with notes '{}'", currentWork.getStartTime(),
98+
currentWork.getEndTime(), time, currentWork.getProject().getName(), currentWork.getNotes());
99+
100+
// Save in db
101+
model.getWorkRepository().save(currentWork);
102+
85103
if (currentWork != null && !dateNow.isEqual(currentWork.getCreationDate())) {
86104
LOG.info("Removing projects with other creation date than today '{}' from list.", dateNow);
87105
final int sizeBefore = model.getPastWorkItems().size();
88106
model.getPastWorkItems().removeIf(w -> !dateNow.isEqual(w.getCreationDate()));
89107
LOG.debug("Removed '{}' work items from past work items.", sizeBefore - model.getPastWorkItems().size());
90108
}
91-
model.activeWorkItem.set(work);
92109
}
93110

94111
public void addNewProject(final Project project) {
@@ -133,7 +150,7 @@ public void updateSettings(final Color hoverBackgroundColor, final Color hoverFo
133150
@PreDestroy
134151
public void shutdown() {
135152
LOG.info("Controller shutdown");
136-
changeProject(model.getIdleProject(), 0);
153+
saveCurrentWork(0);
137154
}
138155

139156
public void deleteProject(final Project p) {

src/main/java/de/doubleslash/keeptime/view/ViewController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import de.doubleslash.keeptime.common.Resources;
3232
import de.doubleslash.keeptime.common.Resources.RESOURCE;
3333
import de.doubleslash.keeptime.common.StyleUtils;
34+
import de.doubleslash.keeptime.common.time.Interval;
3435
import de.doubleslash.keeptime.controller.Controller;
3536
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
3637
import de.doubleslash.keeptime.model.Model;
3738
import de.doubleslash.keeptime.model.Project;
38-
import de.doubleslash.keeptime.view.time.Interval;
3939
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
4040
import javafx.application.Platform;
4141
import javafx.beans.binding.Bindings;
@@ -284,7 +284,7 @@ private void initialize() {
284284
() -> DateFormatter.secondsToHHMMSS(activeWorkSecondsProperty.get()), activeWorkSecondsProperty));
285285

286286
// update ui each second
287-
Interval.registerCallBack(() -> {
287+
new Interval(1).registerCallBack(() -> {
288288
final LocalDateTime now = LocalDateTime.now();
289289
model.activeWorkItem.get().setEndTime(now); // FIXME not good to change model
290290

@@ -303,6 +303,8 @@ private void initialize() {
303303

304304
mainColorTimeLine.update(model.getSortedPastWorkItems(), controller.calcTodaysSeconds());
305305
updateTaskbarIcon(currentWorkSeconds);
306+
307+
projectsListViewController.tick();
306308
});
307309

308310
mainColorTimeLine = new ColorTimeLine(canvas);

src/main/java/de/doubleslash/keeptime/viewpopup/ViewControllerPopup.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import de.doubleslash.keeptime.model.Model;
2828
import de.doubleslash.keeptime.model.Project;
2929
import de.doubleslash.keeptime.view.ProjectsListViewController;
30-
import de.doubleslash.keeptime.view.time.Interval;
3130
import javafx.fxml.FXML;
3231
import javafx.scene.control.ListView;
3332
import javafx.scene.control.TextField;
@@ -113,8 +112,6 @@ private void initialize() {
113112
}
114113
});
115114

116-
Interval.registerCallBack(() -> projectsListViewController.tick());
117-
118115
}
119116

120117
private void hide() {

0 commit comments

Comments
 (0)