ScoreList: Remove reference to Habit

pull/699/head
Alinson S. Xavier 5 years ago
parent 11863cd7b0
commit d0c37fef67

@ -76,7 +76,12 @@ data class Habit(
frequency = frequency, frequency = frequency,
isNumerical = isNumerical, isNumerical = isNumerical,
) )
scores.recompute() scores.recompute(
this.frequency,
this.isNumerical,
this.targetValue,
this.computedEntries
)
} }
fun copyFrom(other: Habit) { fun copyFrom(other: Habit) {

@ -36,7 +36,6 @@ interface ModelFactory {
originalEntries = buildOriginalEntries(), originalEntries = buildOriginalEntries(),
computedEntries = buildComputedEntries(), computedEntries = buildComputedEntries(),
) )
scores.setHabit(habit)
streaks.setHabit(habit) streaks.setHabit(habit)
return habit return habit
} }

@ -31,13 +31,6 @@ public class ScoreList
{ {
private final HashMap<Timestamp, Score> list = new HashMap<>(); private final HashMap<Timestamp, Score> list = new HashMap<>();
private Habit habit;
public void setHabit(Habit habit)
{
this.habit = habit;
}
/** /**
* Returns the score for a given day. If the timestamp given happens before the first * Returns the score for a given day. If the timestamp given happens before the first
* repetition of the habit or after the last computed score, returns a score with value zero. * repetition of the habit or after the last computed score, returns a score with value zero.
@ -83,11 +76,16 @@ public class ScoreList
return scores; return scores;
} }
public void recompute() public void recompute(
Frequency frequency,
boolean isNumerical,
double targetValue,
EntryList computedEntries
)
{ {
list.clear(); list.clear();
List<Entry> entries = habit.getOriginalEntries().getKnown(); List<Entry> entries = computedEntries.getKnown();
if (entries.isEmpty()) return; if (entries.isEmpty()) return;
Entry oldest = entries.get(entries.size() - 1); Entry oldest = entries.get(entries.size() - 1);
@ -96,10 +94,10 @@ public class ScoreList
if (from.isNewerThan(today)) return; if (from.isNewerThan(today)) return;
double rollingSum = 0.0; double rollingSum = 0.0;
int numerator = habit.getFrequency().getNumerator(); int numerator = frequency.getNumerator();
int denominator = habit.getFrequency().getDenominator(); int denominator = frequency.getDenominator();
final double freq = habit.getFrequency().toDouble(); final double freq = frequency.toDouble();
final Integer[] values = habit.getComputedEntries() final Integer[] values = computedEntries
.getByInterval(from, today) .getByInterval(from, today)
.stream() .stream()
.map(Entry::getValue) .map(Entry::getValue)
@ -108,7 +106,7 @@ public class ScoreList
// For non-daily boolean habits, we double the numerator and the denominator to smooth // For non-daily boolean habits, we double the numerator and the denominator to smooth
// out irregular repetition schedules (for example, weekly habits performed on different // out irregular repetition schedules (for example, weekly habits performed on different
// days of the week) // days of the week)
if (!habit.isNumerical() && freq < 1.0) if (!isNumerical && freq < 1.0)
{ {
numerator *= 2; numerator *= 2;
denominator *= 2; denominator *= 2;
@ -118,14 +116,14 @@ public class ScoreList
for (int i = 0; i < values.length; i++) for (int i = 0; i < values.length; i++)
{ {
int offset = values.length - i - 1; int offset = values.length - i - 1;
if (habit.isNumerical()) if (isNumerical)
{ {
rollingSum += values[offset]; rollingSum += values[offset];
if (offset + denominator < values.length) if (offset + denominator < values.length)
{ {
rollingSum -= values[offset + denominator]; rollingSum -= values[offset + denominator];
} }
double percentageCompleted = Math.min(1, rollingSum / 1000 / habit.getTargetValue()); double percentageCompleted = Math.min(1, rollingSum / 1000 / targetValue);
previousValue = Score.compute(freq, previousValue, percentageCompleted); previousValue = Score.compute(freq, previousValue, percentageCompleted);
} }
else else

Loading…
Cancel
Save