mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
CheckmarkList: Implement getThisIntervalValue
This commit is contained in:
@@ -222,6 +222,35 @@ public abstract class CheckmarkList
|
||||
else return UNCHECKED;
|
||||
}
|
||||
|
||||
public synchronized int getThisWeekValue(int firstWeekday)
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.WEEK_NUMBER, firstWeekday);
|
||||
}
|
||||
|
||||
public synchronized int getThisMonthValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.MONTH, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
|
||||
public synchronized int getThisQuarterValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.QUARTER, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
|
||||
public synchronized int getThisYearValue()
|
||||
{
|
||||
return getThisIntervalValue(DateUtils.TruncateField.YEAR, Calendar.SATURDAY);
|
||||
}
|
||||
|
||||
private int getThisIntervalValue(DateUtils.TruncateField truncateField, int firstWeekday)
|
||||
{
|
||||
List<Checkmark> groups = habit.getCheckmarks().groupBy(truncateField, firstWeekday, 1);
|
||||
if (groups.isEmpty()) return 0;
|
||||
return groups.get(0).getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values of the checkmarks that fall inside a certain interval
|
||||
* of time.
|
||||
@@ -328,7 +357,7 @@ public abstract class CheckmarkList
|
||||
|
||||
private void computeNumerical(Repetition[] reps)
|
||||
{
|
||||
if (reps.length == 0) throw new IllegalArgumentException();
|
||||
if (reps.length == 0) return;
|
||||
|
||||
Timestamp today = DateUtils.getToday();
|
||||
Timestamp begin = reps[0].getTimestamp();
|
||||
@@ -419,23 +448,36 @@ public abstract class CheckmarkList
|
||||
|
||||
@NonNull
|
||||
public List<Checkmark> groupBy(DateUtils.TruncateField field, int firstWeekday)
|
||||
{
|
||||
return groupBy(field, firstWeekday, 0);
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
public List<Checkmark> groupBy(DateUtils.TruncateField field,
|
||||
int firstWeekday,
|
||||
int maxGroups)
|
||||
{
|
||||
List<Checkmark> checks = getAll();
|
||||
|
||||
int count = 0;
|
||||
Timestamp truncatedTimestamps[] = new Timestamp[checks.size()];
|
||||
int values[] = new int[checks.size()];
|
||||
Timestamp[] truncatedTimestamps = new Timestamp[checks.size()];
|
||||
int[] values = new int[checks.size()];
|
||||
|
||||
for (Checkmark rep : checks)
|
||||
{
|
||||
Timestamp tt = rep.getTimestamp().truncate(field, firstWeekday);
|
||||
if (count == 0 || !truncatedTimestamps[count - 1].equals(tt))
|
||||
{
|
||||
if (maxGroups > 0 && count >= maxGroups) break;
|
||||
truncatedTimestamps[count++] = tt;
|
||||
}
|
||||
|
||||
if(habit.isNumerical())
|
||||
values[count - 1] += rep.getValue();
|
||||
else if(rep.getValue() == Checkmark.CHECKED_EXPLICITLY)
|
||||
values[count - 1] += 1000;
|
||||
|
||||
}
|
||||
|
||||
ArrayList<Checkmark> groupedCheckmarks = new ArrayList<>();
|
||||
|
||||
@@ -28,9 +28,8 @@ import java.util.*;
|
||||
|
||||
import nl.jqno.equalsverifier.*;
|
||||
|
||||
import static java.util.Calendar.JANUARY;
|
||||
import static java.util.Calendar.JULY;
|
||||
import static java.util.Calendar.JUNE;
|
||||
import static java.util.Calendar.*;
|
||||
import static junit.framework.TestCase.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
@@ -422,4 +421,44 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
assertThat(byYear.get(1), equalTo(new Checkmark(timestamp(2014, JANUARY, 1), 8227)));
|
||||
assertThat(byYear.get(2), equalTo(new Checkmark(timestamp(2013, JANUARY, 1), 16172)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTodayValue() throws Exception
|
||||
{
|
||||
Habit habit = fixtures.createLongNumericalHabit(timestamp(2014, JUNE, 1));
|
||||
CheckmarkList checkmarks = habit.getCheckmarks();
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2050, MAY, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisQuarterValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisYearValue(), equalTo(0));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, JUNE, 6));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SUNDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(MONDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(230));
|
||||
assertThat(checkmarks.getThisQuarterValue(), equalTo(3263));
|
||||
assertThat(checkmarks.getThisYearValue(), equalTo(8227));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, JUNE, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisWeekValue(SUNDAY), equalTo(230));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(230));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2014, MAY, 16));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(419));
|
||||
assertThat(checkmarks.getThisWeekValue(THURSDAY), equalTo(134));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(1006));
|
||||
|
||||
DateUtils.setFixedLocalTime(unixTime(2000, MAY, 1));
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(0));
|
||||
assertThat(checkmarks.getThisWeekValue(SATURDAY), equalTo(0));
|
||||
assertThat(checkmarks.getThisMonthValue(), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user