mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
@@ -162,6 +162,7 @@ public class Habit
|
|||||||
public synchronized void setFrequency(@NonNull Frequency frequency)
|
public synchronized void setFrequency(@NonNull Frequency frequency)
|
||||||
{
|
{
|
||||||
data.frequency = frequency;
|
data.frequency = frequency;
|
||||||
|
invalidateNewerThan(Timestamp.ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import java.io.*;
|
|||||||
import java.text.*;
|
import java.text.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||||
|
|
||||||
public abstract class ScoreList implements Iterable<Score>
|
public abstract class ScoreList implements Iterable<Score>
|
||||||
{
|
{
|
||||||
protected final Habit habit;
|
protected final Habit habit;
|
||||||
@@ -268,6 +270,7 @@ public abstract class ScoreList implements Iterable<Score>
|
|||||||
if (from.isNewerThan(to)) return;
|
if (from.isNewerThan(to)) return;
|
||||||
|
|
||||||
double rollingSum = 0.0;
|
double rollingSum = 0.0;
|
||||||
|
int numerator = habit.getFrequency().getNumerator();
|
||||||
int denominator = habit.getFrequency().getDenominator();
|
int denominator = habit.getFrequency().getDenominator();
|
||||||
final double freq = habit.getFrequency().toDouble();
|
final double freq = habit.getFrequency().toDouble();
|
||||||
final int[] checkmarkValues = habit.getCheckmarks().getValues(from, to);
|
final int[] checkmarkValues = habit.getCheckmarks().getValues(from, to);
|
||||||
@@ -288,8 +291,14 @@ public abstract class ScoreList implements Iterable<Score>
|
|||||||
}
|
}
|
||||||
else if (checkmarkValues[offset] != Checkmark.SKIP)
|
else if (checkmarkValues[offset] != Checkmark.SKIP)
|
||||||
{
|
{
|
||||||
double value = Math.min(1, checkmarkValues[offset]);
|
if (checkmarkValues[offset] == YES_MANUAL)
|
||||||
previousValue = Score.compute(freq, previousValue, value);
|
rollingSum += 1.0;
|
||||||
|
if (offset + denominator < checkmarkValues.length)
|
||||||
|
if (checkmarkValues[offset + denominator] == YES_MANUAL)
|
||||||
|
rollingSum -= 1.0;
|
||||||
|
|
||||||
|
double percentageCompleted = Math.min(1, rollingSum / numerator);
|
||||||
|
previousValue = Score.compute(freq, previousValue, percentageCompleted);
|
||||||
}
|
}
|
||||||
scores.add(new Score(from.plus(i), previousValue));
|
scores.add(new Score(from.plus(i), previousValue));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import java.util.*;
|
|||||||
import static org.hamcrest.MatcherAssert.*;
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
import static org.hamcrest.core.IsEqual.*;
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
import static org.hamcrest.number.IsCloseTo.*;
|
import static org.hamcrest.number.IsCloseTo.*;
|
||||||
|
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||||
|
|
||||||
public class ScoreListTest extends BaseUnitTest
|
public class ScoreListTest extends BaseUnitTest
|
||||||
{
|
{
|
||||||
@@ -188,6 +189,28 @@ public class ScoreListTest extends BaseUnitTest
|
|||||||
assertThat(actual[i], closeTo(expected[i], E));
|
assertThat(actual[i], closeTo(expected[i], E));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_imperfectNonDaily()
|
||||||
|
{
|
||||||
|
habit.setFrequency(new Frequency(3, 7));
|
||||||
|
ArrayList<Integer> values = new ArrayList<>();
|
||||||
|
for (int k = 0; k < 100; k++)
|
||||||
|
{
|
||||||
|
values.add(YES_MANUAL);
|
||||||
|
values.add(YES_MANUAL);
|
||||||
|
values.add(NO);
|
||||||
|
values.add(NO);
|
||||||
|
values.add(NO);
|
||||||
|
values.add(NO);
|
||||||
|
values.add(NO);
|
||||||
|
}
|
||||||
|
toggle(values);
|
||||||
|
assertThat(habit.getScores().getTodayValue(), closeTo(2/3.0, E));
|
||||||
|
|
||||||
|
habit.setFrequency(new Frequency(4, 7));
|
||||||
|
assertThat(habit.getScores().getTodayValue(), closeTo(0.5, E));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_groupBy()
|
public void test_groupBy()
|
||||||
{
|
{
|
||||||
@@ -196,9 +219,9 @@ public class ScoreListTest extends BaseUnitTest
|
|||||||
habit.getScores().groupBy(DateUtils.TruncateField.MONTH, Calendar.SATURDAY);
|
habit.getScores().groupBy(DateUtils.TruncateField.MONTH, Calendar.SATURDAY);
|
||||||
|
|
||||||
assertThat(list.size(), equalTo(5));
|
assertThat(list.size(), equalTo(5));
|
||||||
assertThat(list.get(0).getValue(), closeTo(0.687724, E));
|
assertThat(list.get(0).getValue(), closeTo(0.601508, E));
|
||||||
assertThat(list.get(1).getValue(), closeTo(0.636747, E));
|
assertThat(list.get(1).getValue(), closeTo(0.580580, E));
|
||||||
assertThat(list.get(2).getValue(), closeTo(0.533860, E));
|
assertThat(list.get(2).getValue(), closeTo(0.474609, E));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -220,11 +243,17 @@ public class ScoreListTest extends BaseUnitTest
|
|||||||
{
|
{
|
||||||
Habit habit = fixtures.createShortHabit();
|
Habit habit = fixtures.createShortHabit();
|
||||||
|
|
||||||
String expectedCSV = "2015-01-25,0.2654\n2015-01-24,0.2389\n" +
|
String expectedCSV =
|
||||||
"2015-01-23,0.2475\n2015-01-22,0.2203\n" +
|
"2015-01-25,0.2234\n" +
|
||||||
"2015-01-21,0.1921\n2015-01-20,0.1628\n" +
|
"2015-01-24,0.2134\n" +
|
||||||
"2015-01-19,0.1325\n2015-01-18,0.1011\n" +
|
"2015-01-23,0.2031\n" +
|
||||||
"2015-01-17,0.0686\n2015-01-16,0.0349\n";
|
"2015-01-22,0.1742\n" +
|
||||||
|
"2015-01-21,0.1443\n" +
|
||||||
|
"2015-01-20,0.1134\n" +
|
||||||
|
"2015-01-19,0.0994\n" +
|
||||||
|
"2015-01-18,0.0849\n" +
|
||||||
|
"2015-01-17,0.0518\n" +
|
||||||
|
"2015-01-16,0.0175\n";
|
||||||
|
|
||||||
StringWriter writer = new StringWriter();
|
StringWriter writer = new StringWriter();
|
||||||
habit.getScores().writeCSV(writer);
|
habit.getScores().writeCSV(writer);
|
||||||
@@ -241,6 +270,15 @@ public class ScoreListTest extends BaseUnitTest
|
|||||||
reps.toggle(today.minus(i));
|
reps.toggle(today.minus(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void toggle(ArrayList<Integer> values)
|
||||||
|
{
|
||||||
|
RepetitionList reps = habit.getRepetitions();
|
||||||
|
Timestamp today = DateUtils.getToday();
|
||||||
|
for (int i = 0; i < values.size(); i++)
|
||||||
|
if (values.get(i) == YES_MANUAL)
|
||||||
|
reps.toggle(today.minus(i));
|
||||||
|
}
|
||||||
|
|
||||||
private void addSkip(final int day)
|
private void addSkip(final int day)
|
||||||
{
|
{
|
||||||
RepetitionList reps = habit.getRepetitions();
|
RepetitionList reps = habit.getRepetitions();
|
||||||
|
|||||||
Reference in New Issue
Block a user