Skip to content

Commit 25fd8d3

Browse files
authored
Merge pull request #53 from doubleSlashde/feature/save_active_work_item_periodically
added quicksave Interval of 60sec
2 parents b8b0af4 + a8cdb2b commit 25fd8d3

File tree

12 files changed

+99
-99
lines changed

12 files changed

+99
-99
lines changed

src/main/java/de/doubleslash/keeptime/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void initialiseApplication(final Stage primaryStage) throws Exception {
138138
readSettings();
139139

140140
final List<Work> todaysWorkItems = model.getWorkRepository()
141-
.findByCreationDateOrderByStartTimeAsc(LocalDate.now());
141+
.findByStartDateOrderByStartTimeAsc(LocalDate.now());
142142
LOG.info("Found {} past work items", todaysWorkItems.size());
143143
model.getPastWorkItems().addAll(todaysWorkItems);
144144

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: 41 additions & 25 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;
@@ -39,6 +40,7 @@
3940

4041
@Service
4142
public class Controller {
43+
private final long QUICK_SAVE_INTERVAL = 60;
4244

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

@@ -50,44 +52,59 @@ public class Controller {
5052
public Controller(final Model model, final DateProvider dateProvider) {
5153
this.model = model;
5254
this.dateProvider = dateProvider;
55+
56+
// initiate quicksaving
57+
new Interval(QUICK_SAVE_INTERVAL).registerCallBack(() -> saveCurrentWork(dateProvider.dateTimeNow()));
5358
}
5459

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

5964
public void changeProject(final Project newProject, final long minusSeconds) {
60-
final Work currentWork = model.activeWorkItem.get();
6165

62-
final LocalDateTime now = dateProvider.dateTimeNow().minusSeconds(minusSeconds);
63-
final LocalDate dateNow = now.toLocalDate();
64-
if (currentWork != null) {
65-
currentWork.setEndTime(now);
66-
if (currentWork.getNotes().isEmpty()) {
67-
currentWork.setNotes("- No notes -");
68-
}
66+
final LocalDateTime workEnd = dateProvider.dateTimeNow().minusSeconds(minusSeconds);
67+
final LocalDate today = dateProvider.dateTimeNow().toLocalDate();
6968

70-
final String time = DateFormatter
71-
.secondsToHHMMSS(Duration.between(currentWork.getStartTime(), currentWork.getEndTime()).getSeconds());
69+
final Work oldWork = saveCurrentWork(workEnd);
7270

73-
LOG.info("You worked from '{}' to '{}' ({}) on project '{}' with notes '{}'", currentWork.getStartTime(),
74-
currentWork.getEndTime(), time, currentWork.getProject().getName(), currentWork.getNotes());
75-
76-
// Save in db
77-
model.getWorkRepository().save(currentWork);
71+
if (oldWork != null && !today.isEqual(oldWork.getStartTime().toLocalDate())) {
72+
LOG.info("Removing projects with other creation date than today '{}' from list.", today);
73+
final int sizeBefore = model.getPastWorkItems().size();
74+
model.getPastWorkItems().removeIf(w -> !today.isEqual(w.getStartTime().toLocalDate()));
75+
LOG.debug("Removed '{}' work items from past work items.", sizeBefore - model.getPastWorkItems().size());
7876
}
7977

8078
// Start new work
81-
final Work work = new Work(dateNow, now, now.plusSeconds(minusSeconds), newProject, "");
79+
final Work newWork = new Work(workEnd, workEnd.plusSeconds(minusSeconds), newProject, "");
8280

83-
model.getPastWorkItems().add(work);
84-
if (currentWork != null && !dateNow.isEqual(currentWork.getCreationDate())) {
85-
LOG.info("Removing projects with other creation date than today '{}' from list.", dateNow);
86-
final int sizeBefore = model.getPastWorkItems().size();
87-
model.getPastWorkItems().removeIf(w -> !dateNow.isEqual(w.getCreationDate()));
88-
LOG.debug("Removed '{}' work items from past work items.", sizeBefore - model.getPastWorkItems().size());
81+
model.getPastWorkItems().add(newWork);
82+
83+
model.activeWorkItem.set(newWork);
84+
85+
}
86+
87+
public Work saveCurrentWork(final LocalDateTime workEnd) {
88+
final Work currentWork = model.activeWorkItem.get();
89+
90+
if (currentWork == null) {
91+
return null;
92+
}
93+
94+
currentWork.setEndTime(workEnd);
95+
if (currentWork.getNotes().isEmpty()) {
96+
currentWork.setNotes("- No notes -");
8997
}
90-
model.activeWorkItem.set(work);
98+
99+
final String time = DateFormatter
100+
.secondsToHHMMSS(Duration.between(currentWork.getStartTime(), currentWork.getEndTime()).getSeconds());
101+
102+
LOG.info("Saving Work from '{}' to '{}' ({}) on project '{}' with notes '{}'", currentWork.getStartTime(),
103+
currentWork.getEndTime(), time, currentWork.getProject().getName(), currentWork.getNotes());
104+
105+
// Save in db
106+
return model.getWorkRepository().save(currentWork);
107+
91108
}
92109

93110
public void addNewProject(final Project project) {
@@ -203,7 +220,6 @@ public void editProject(final Project projectToBeUpdated, final Project newValue
203220
public void editWork(final Work workToBeEdited, final Work newValuedWork) {
204221
LOG.info("Changing work '{}' to '{}'.", workToBeEdited, newValuedWork);
205222

206-
workToBeEdited.setCreationDate(newValuedWork.getCreationDate());
207223
workToBeEdited.setStartTime(newValuedWork.getStartTime());
208224
workToBeEdited.setEndTime(newValuedWork.getEndTime());
209225
workToBeEdited.setNotes(newValuedWork.getNotes());
@@ -215,7 +231,7 @@ public void editWork(final Work workToBeEdited, final Work newValuedWork) {
215231
model.getPastWorkItems().removeIf(w -> (w.getId() == workToBeEdited.getId()));
216232
// add if started today
217233
final LocalDate dateNow = dateProvider.dateTimeNow().toLocalDate();
218-
if (dateNow.equals(editedWork.getCreationDate())) {
234+
if (dateNow.equals(editedWork.getStartTime().toLocalDate())) {
219235
model.getPastWorkItems().add(editedWork);
220236
}
221237

src/main/java/de/doubleslash/keeptime/model/Work.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package de.doubleslash.keeptime.model;
1818

19-
import java.time.LocalDate;
2019
import java.time.LocalDateTime;
2120

2221
import javax.persistence.Column;
@@ -36,8 +35,6 @@ public class Work {
3635
@Column(name = "id", updatable = false, nullable = false)
3736
private long id;
3837

39-
// TODO revise CreationDate Logic to use Date of StartTime
40-
private LocalDate creationDate;
4138
private LocalDateTime startTime;
4239
private LocalDateTime endTime;
4340

@@ -49,10 +46,8 @@ public class Work {
4946
public Work() {
5047
}
5148

52-
public Work(final LocalDate creationDate, final LocalDateTime startTime, final LocalDateTime endTime,
53-
final Project project, final String notes) {
49+
public Work(final LocalDateTime startTime, final LocalDateTime endTime, final Project project, final String notes) {
5450
super();
55-
this.creationDate = creationDate;
5651
this.startTime = startTime;
5752
this.endTime = endTime;
5853
this.project = project;
@@ -63,14 +58,6 @@ public long getId() {
6358
return id;
6459
}
6560

66-
public LocalDate getCreationDate() {
67-
return creationDate;
68-
}
69-
70-
public void setCreationDate(final LocalDate creationDate) {
71-
this.creationDate = creationDate;
72-
}
73-
7461
public LocalDateTime getStartTime() {
7562
return startTime;
7663
}
@@ -105,8 +92,8 @@ public void setNotes(final String notes) {
10592

10693
@Override
10794
public String toString() {
108-
return "Work [id=" + id + ", creationDate=" + creationDate + ", startTime=" + startTime + ", endTime=" + endTime
109-
+ ", projectName=" + project.getName() + ", notes=" + notes + "]";
95+
return "Work [id=" + id + ", startTime=" + startTime + ", endTime=" + endTime + ", projectName="
96+
+ project.getName() + ", notes=" + notes + "]";
11097
}
11198

11299
}

src/main/java/de/doubleslash/keeptime/model/repos/WorkRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import java.util.List;
2121

2222
import org.springframework.data.jpa.repository.JpaRepository;
23+
import org.springframework.data.jpa.repository.Query;
2324
import org.springframework.stereotype.Repository;
2425

2526
import de.doubleslash.keeptime.model.Work;
2627

2728
@Repository
2829
public interface WorkRepository extends JpaRepository<Work, Long> {
2930

30-
List<Work> findByCreationDateOrderByStartTimeAsc(LocalDate creationDate);
31+
@Query(value = "SELECT * FROM work WHERE CAST(start_time AS DATE) = ?1 ORDER BY start_time ASC", nativeQuery = true)
32+
List<Work> findByStartDateOrderByStartTimeAsc(LocalDate creationDate);
3133
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,6 @@ public void initializeWith(final Work work) {
292292
setTextColor(projectComboBox.getEditor(), model.hoverFontColor.get());
293293
}
294294

295-
public Work getWorkFromUserInput() {
296-
return new Work(startDatePicker.getValue(),
297-
LocalDateTime.of(startDatePicker.getValue(), startTimeSpinner.getValue()),
298-
LocalDateTime.of(endDatePicker.getValue(), endTimeSpinner.getValue()), selectedProject,
299-
noteTextArea.getText());
300-
}
301-
302295
private void enableStrgA_combo() {
303296
// strg+a Behaviour bug hack
304297
// https://stackoverflow.com/questions/51943654/javafx-combobox-make-control-a-select-all-in-text-box-while-dropdown-is-visi
@@ -321,6 +314,12 @@ private void setColor(final Node object, final Color color) {
321314
object.setStyle(style);
322315
}
323316

317+
public Work getWorkFromUserInput() {
318+
return new Work(LocalDateTime.of(startDatePicker.getValue(), startTimeSpinner.getValue()),
319+
LocalDateTime.of(endDatePicker.getValue(), endTimeSpinner.getValue()), selectedProject,
320+
noteTextArea.getText());
321+
}
322+
324323
private void setTextColor(final Node object, final Color color) {
325324
final String style = StyleUtils.changeStyleAttribute(object.getStyle(), "fx-text-fill",
326325
"rgba(" + ColorHelper.colorToCssRgba(color) + ")");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void updateReport(final LocalDate dateToShow) {
197197

198198
this.currentDayLabel.setText(DateFormatter.toDayDateString(this.currentReportDate));
199199
final List<Work> currentWorkItems = model.getWorkRepository()
200-
.findByCreationDateOrderByStartTimeAsc(this.currentReportDate);
200+
.findByStartDateOrderByStartTimeAsc(this.currentReportDate);
201201

202202
colorTimeLine.update(currentWorkItems, controller.calcSeconds(currentWorkItems));
203203

@@ -260,7 +260,7 @@ private void loadCalenderWidget() {
260260
@Override
261261
public void updateItem(final LocalDate item, final boolean empty) {
262262
super.updateItem(item, empty);
263-
if (model.getWorkRepository().findByCreationDateOrderByStartTimeAsc(item).isEmpty()) {
263+
if (model.getWorkRepository().findByStartDateOrderByStartTimeAsc(item).isEmpty()) {
264264
setDisable(true);
265265
setStyle(FX_BACKGROUND_COLOR_NOT_WORKED);
266266
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
import de.doubleslash.keeptime.common.Resources.RESOURCE;
3434
import de.doubleslash.keeptime.common.ScreenPosHelper;
3535
import de.doubleslash.keeptime.common.StyleUtils;
36+
import de.doubleslash.keeptime.common.time.Interval;
3637
import de.doubleslash.keeptime.controller.Controller;
3738
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
3839
import de.doubleslash.keeptime.model.Model;
3940
import de.doubleslash.keeptime.model.Project;
40-
import de.doubleslash.keeptime.view.time.Interval;
4141
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
4242
import javafx.application.Platform;
4343
import javafx.beans.binding.Bindings;
@@ -287,7 +287,7 @@ private void initialize() {
287287
() -> DateFormatter.secondsToHHMMSS(activeWorkSecondsProperty.get()), activeWorkSecondsProperty));
288288

289289
// update ui each second
290-
Interval.registerCallBack(() -> {
290+
new Interval(1).registerCallBack(() -> {
291291
final LocalDateTime now = LocalDateTime.now();
292292
model.activeWorkItem.get().setEndTime(now); // FIXME not good to change model
293293

@@ -306,6 +306,7 @@ private void initialize() {
306306

307307
mainColorTimeLine.update(model.getSortedPastWorkItems(), controller.calcTodaysSeconds());
308308
updateTaskbarIcon(currentWorkSeconds);
309+
309310
});
310311

311312
mainColorTimeLine = new ColorTimeLine(canvas);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
import de.doubleslash.keeptime.common.ColorHelper;
2727
import de.doubleslash.keeptime.common.StyleUtils;
28+
import de.doubleslash.keeptime.common.time.Interval;
2829
import de.doubleslash.keeptime.controller.Controller;
2930
import de.doubleslash.keeptime.model.Model;
3031
import de.doubleslash.keeptime.model.Project;
3132
import de.doubleslash.keeptime.view.ProjectsListViewController;
32-
import de.doubleslash.keeptime.view.time.Interval;
3333
import javafx.fxml.FXML;
3434
import javafx.scene.control.ListView;
3535
import javafx.scene.control.TextField;
@@ -79,7 +79,7 @@ public void setStage(final Stage primaryStage) {
7979
projectsListViewController = new ProjectsListViewController(model, controller, stage, projectListView,
8080
searchTextField, true);
8181

82-
Interval.registerCallBack(() -> projectsListViewController.tick());
82+
new Interval(1).registerCallBack(() -> projectsListViewController.tick());
8383

8484
}
8585

0 commit comments

Comments
 (0)