-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement daily worked time rounding #248
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
import static org.zephyrsoft.trackworktime.util.DateTimeUtil.truncateEventToMinute; | ||
import static org.zephyrsoft.trackworktime.util.DateTimeUtil.truncateEventsToMinute; | ||
|
||
import android.content.SharedPreferences; | ||
|
||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.pmw.tinylog.Logger; | ||
import org.zephyrsoft.trackworktime.database.DAO; | ||
|
@@ -28,6 +30,7 @@ | |
import org.zephyrsoft.trackworktime.model.TargetEnum; | ||
import org.zephyrsoft.trackworktime.model.TimeInfo; | ||
import org.zephyrsoft.trackworktime.model.TypeEnum; | ||
import org.zephyrsoft.trackworktime.options.Key; | ||
import org.zephyrsoft.trackworktime.util.DateTimeUtil; | ||
|
||
import java.time.DayOfWeek; | ||
|
@@ -114,6 +117,9 @@ public boolean containsEvents() { | |
private final boolean handleFlexiTime; | ||
private final ZoneId zoneId; | ||
|
||
private final boolean roundingEnabled; | ||
private final int smallestTimeUnit; | ||
|
||
private LocalDate startDate; | ||
private Event lastEventBeforeDay; | ||
private LocalDate currentDate; | ||
|
@@ -138,17 +144,24 @@ public boolean containsEvents() { | |
private long currentBalance = 0; | ||
private int futureWorkDays = -1; | ||
|
||
public TimeCalculatorV2(DAO dao, TimerManager timerManager, LocalDate startDate, boolean handleFlexiTime) { | ||
public TimeCalculatorV2(DAO dao, TimerManager timerManager, LocalDate startDate, SharedPreferences preferences) { | ||
this.dao = dao; | ||
this.timerManager = timerManager; | ||
|
||
this.handleFlexiTime = handleFlexiTime; | ||
this.handleFlexiTime = preferences.getBoolean(Key.ENABLE_FLEXI_TIME.getName(), false); | ||
if (handleFlexiTime) { | ||
this.flexiReset = timerManager.getFlexiReset(); | ||
} else { | ||
this.flexiReset = FlexiReset.NONE; | ||
} | ||
|
||
this.roundingEnabled = preferences.getBoolean(Key.ROUNDING_ENABLED.getName(), false); | ||
if (preferences.getBoolean(Key.ROUNDING_ENABLED.getName(), false)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be
instead (we just evaluated the same statement used here). |
||
this.smallestTimeUnit = Integer.parseInt(preferences.getString(Key.SMALLEST_TIME_UNIT.getName(), "1")); | ||
} else { | ||
this.smallestTimeUnit = 1; | ||
Comment on lines
+161
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I am missing something, but I get an error if I do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, my bad - the
it works. |
||
} | ||
|
||
this.zoneId = timerManager.getHomeTimeZone(); | ||
|
||
setStartDate(startDate); | ||
|
@@ -361,7 +374,7 @@ public void calculateNextDay() { | |
|
||
currentDayHasEvents = (events != null && !events.isEmpty()); | ||
|
||
workedTime += calculateWorkTime(events); | ||
workedTime += roundUpToMultiple(calculateWorkTime(events), smallestTimeUnit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be wrapped in an
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While glancing over this again, my previous comment doesn't make sense. The
|
||
|
||
// always show real worked time | ||
currentDayActual = workedTime; | ||
|
@@ -553,4 +566,12 @@ public void calculatePeriod(PeriodEnum period, boolean includeFlexiTime) { | |
this.currentDayActual = currentDayActual; | ||
this.currentDayTarget = currentDayTarget; | ||
} | ||
|
||
public static long roundUpToMultiple(long value, long multiple) { | ||
if (multiple == 1) { | ||
return value; | ||
} else { | ||
return (value + multiple - 1) / multiple * multiple; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work in all cases. For the inputs value = 43, multiple = 15 it should round to 45. But it doesn't, the method would return 57 with these inputs. To be on the safe side, I'd recommend creating a unit test for the calulation method where some "normal" cases and many (if not all) of the edge cases are checked. For a start you can copy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible values of But probably only the values 5, 10, 15, 30 and 60 will be used in real life... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's weird. I'll write some tests for it. Also, I do use 6 as a multiple myself since my work tracks time to an accuracy of 0.1 hours. Therefore I think it makes sense to leave it up to the user to choose any divisor of 60 :) |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.
is superfluous here (above inthis.flexiReset
it's also superfluous).