Skip to content

Commit 5c40df6

Browse files
author
Martin Plieske
committed
refactored Controller and ColorTimeLine
1 parent 6f5453c commit 5c40df6

File tree

4 files changed

+32
-63
lines changed

4 files changed

+32
-63
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.time.LocalDateTime;
66
import java.util.ArrayList;
77
import java.util.List;
8-
import java.util.Optional;
98

109
import javax.annotation.PreDestroy;
1110

@@ -237,29 +236,35 @@ public void setComment(final String notes) {
237236
* Calculate todays seconds counted as work
238237
*/
239238
public long calcTodaysWorkSeconds() {
239+
final List<Work> workItems = new ArrayList<>();
240240

241-
return model.getPastWorkItems().stream().filter(work -> {
242-
final Project project = work.getProject();
243-
// find up to date reference to project
244-
final Optional<Project> optionalProject = model.getAllProjects().stream()
245-
.filter(p -> p.getId() == project.getId()).findAny();
246-
if (optionalProject.isPresent()) {
247-
return optionalProject.get().isWork();
241+
for (final Work w : model.getPastWorkItems()) {
242+
if (w.getProject().isWork()) {
243+
workItems.add(w);
248244
}
249-
// TODO should not happen
250-
return false;
251-
}).mapToLong(work -> Duration.between(work.getStartTime(), work.getEndTime()).getSeconds()).sum();
245+
}
246+
247+
return calcSeconds(workItems);
252248
}
253249

254250
/**
255251
* Calculate todays present seconds (work+nonWork)
256252
*/
257253
public long calcTodaysSeconds() {
258-
return model.getPastWorkItems().stream()
259-
.mapToLong(work -> Duration.between(work.getStartTime(), work.getEndTime()).getSeconds()).sum();
254+
return calcSeconds(model.getPastWorkItems());
260255
}
261256

262257
public ObservableList<Project> getAvailableProjects() {
263258
return model.getAvailableProjects();
264259
}
260+
261+
public long calcSeconds(final List<Work> workItems) {
262+
long seconds = 0;
263+
264+
for (final Work w : workItems) {
265+
seconds += Duration.between(w.getStartTime(), w.getEndTime()).getSeconds();
266+
}
267+
268+
return seconds;
269+
}
265270
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package de.doubleslash.keeptime.view;
22

3+
import java.time.Duration;
34
import java.util.List;
45

6+
import de.doubleslash.keeptime.model.Work;
57
import javafx.scene.canvas.Canvas;
68
import javafx.scene.canvas.GraphicsContext;
79
import javafx.scene.paint.Color;
8-
import javafx.scene.shape.Rectangle;
910

1011
public class ColorTimeLine {
1112

@@ -15,16 +16,22 @@ public ColorTimeLine(final Canvas canvas) {
1516
this.canvas = canvas;
1617
}
1718

18-
public void update(final List<Rectangle> rects) {
19+
public void update(final List<Work> workItems, final long seconds) {
1920
final GraphicsContext gc = canvas.getGraphicsContext2D();
2021

2122
gc.setFill(new Color(.3, .3, .3, .3));
2223
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
2324
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
25+
double currentX = 0;
26+
for (final Work w : workItems) {
27+
final long workedSeconds = Duration.between(w.getStartTime(), w.getEndTime()).getSeconds();
28+
final double width = (double) workedSeconds / seconds * canvas.getWidth();
29+
final Color fill = w.getProject().getColor();
2430

25-
for (final Rectangle rect : rects) {
26-
gc.setFill(rect.getFill());
27-
gc.fillRect(rect.getX(), 0, rect.getWidth(), canvas.getHeight());
31+
gc.setFill(fill);
32+
gc.fillRect(currentX, 0, width, canvas.getHeight());
33+
34+
currentX += width;
2835
}
2936

3037
}

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package de.doubleslash.keeptime.view;
22

3-
import java.time.Duration;
43
import java.time.LocalDate;
5-
import java.util.ArrayList;
64
import java.util.Comparator;
75
import java.util.List;
86
import java.util.SortedSet;
@@ -34,8 +32,6 @@
3432
import javafx.scene.input.ClipboardContent;
3533
import javafx.scene.layout.BorderPane;
3634
import javafx.scene.layout.GridPane;
37-
import javafx.scene.paint.Color;
38-
import javafx.scene.shape.Rectangle;
3935
import javafx.scene.text.Font;
4036
import javafx.scene.text.FontWeight;
4137
import javafx.util.Callback;
@@ -95,27 +91,7 @@ private void updateReport(final LocalDate newvalue) {
9591
this.currentDayLabel.setText(DateFormatter.toDayDateString(newvalue));
9692
final List<Work> currentWorkItems = model.getWorkRepository().findByCreationDate(newvalue);
9793

98-
final List<Rectangle> rects = new ArrayList<>();
99-
long maxSeconds = 0;
100-
for (final Work w : currentWorkItems) {
101-
maxSeconds += Duration.between(w.getStartTime(), w.getEndTime()).getSeconds();
102-
}
103-
double currentX = 0;
104-
105-
for (final Work w : currentWorkItems) {
106-
final long workedSeconds = Duration.between(w.getStartTime(), w.getEndTime()).getSeconds();
107-
final double width = (double) workedSeconds / maxSeconds * canvas.getWidth();
108-
final Color fill = w.getProject().getColor();
109-
110-
final Rectangle rect = new Rectangle(currentX, 0, width, 0);
111-
rect.setFill(fill);
112-
113-
rects.add(rect);
114-
115-
currentX += width;
116-
}
117-
118-
colorTimeLine.update(rects);
94+
colorTimeLine.update(currentWorkItems, controller.calcSeconds(currentWorkItems));
11995

12096
final SortedSet<Project> workedProjectsSet = currentWorkItems.stream().map(Work::getProject)
12197
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Project::getName))));

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.io.IOException;
55
import java.time.Duration;
66
import java.time.LocalDateTime;
7-
import java.util.ArrayList;
87
import java.util.Comparator;
98
import java.util.HashMap;
109
import java.util.List;
@@ -24,7 +23,6 @@
2423
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
2524
import de.doubleslash.keeptime.model.Model;
2625
import de.doubleslash.keeptime.model.Project;
27-
import de.doubleslash.keeptime.model.Work;
2826
import de.doubleslash.keeptime.view.time.Interval;
2927
import javafx.application.Platform;
3028
import javafx.beans.binding.Bindings;
@@ -76,7 +74,6 @@
7674
import javafx.scene.layout.VBox;
7775
import javafx.scene.paint.Color;
7876
import javafx.scene.shape.Circle;
79-
import javafx.scene.shape.Rectangle;
8077
import javafx.scene.text.TextAlignment;
8178
import javafx.stage.Modality;
8279
import javafx.stage.Stage;
@@ -320,23 +317,7 @@ private void initialize() {
320317
label.setText(DateFormatter.secondsToHHMMSS(seconds));
321318
}
322319

323-
final long maxSeconds = controller.calcTodaysSeconds();
324-
double currentX = 0;
325-
final List<Rectangle> rects = new ArrayList<>();
326-
for (final Work w : model.getPastWorkItems()) {
327-
final long workedSeconds = Duration.between(w.getStartTime(), w.getEndTime()).getSeconds();
328-
final double width = (double) workedSeconds / maxSeconds * canvas.getWidth();
329-
final Color fill = w.getProject().getColor();
330-
331-
final Rectangle rect = new Rectangle(currentX, 0, width, 0);
332-
rect.setFill(fill);
333-
334-
rects.add(rect);
335-
336-
currentX += width;
337-
}
338-
339-
mainColorTimeLine.update(rects);
320+
mainColorTimeLine.update(model.getPastWorkItems(), controller.calcTodaysSeconds());
340321
updateTaskbarIcon(currentWorkSeconds);
341322
});
342323

0 commit comments

Comments
 (0)