start score from 1.0 for at most and reflect the same in history

This commit is contained in:
KristianTashkov
2021-09-22 15:49:51 +03:00
parent 65d237254c
commit 4355fb4d68
12 changed files with 25 additions and 10 deletions

View File

@@ -31,5 +31,6 @@ data class CreateHabitCommand(
val habit = modelFactory.buildHabit()
habit.copyFrom(model)
habitList.add(habit)
habit.recompute()
}
}

View File

@@ -90,9 +90,10 @@ data class Habit(
isNumerical = isNumerical,
)
val to = DateUtils.getTodayWithOffset().plus(30)
val today = DateUtils.getTodayWithOffset()
val to = today.plus(30)
val entries = computedEntries.getKnown()
var from = entries.lastOrNull()?.timestamp ?: to
var from = entries.lastOrNull()?.timestamp ?: today
if (from.isNewerThan(to)) from = to
scores.recompute(

View File

@@ -75,13 +75,12 @@ class ScoreList {
to: Timestamp,
) {
map.clear()
if (computedEntries.getKnown().isEmpty()) return
if (from.isNewerThan(to)) return
var rollingSum = 0.0
var numerator = frequency.numerator
var denominator = frequency.denominator
val freq = frequency.toDouble()
val values = computedEntries.getByInterval(from, to).map { it.value }.toIntArray()
val isAtMost = numericalHabitType == NumericalHabitType.AT_MOST
// 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
@@ -91,7 +90,7 @@ class ScoreList {
denominator *= 2
}
var previousValue = 0.0
var previousValue = if (isNumerical && isAtMost) 1.0 else 0.0
for (i in values.indices) {
val offset = values.size - i - 1
if (isNumerical) {
@@ -101,7 +100,7 @@ class ScoreList {
}
val normalizedRollingSum = rollingSum / 1000
val percentageCompleted = if (numericalHabitType == NumericalHabitType.AT_LEAST) {
val percentageCompleted = if (!isAtMost) {
if (targetValue > 0)
min(1.0, normalizedRollingSum / targetValue)
else

View File

@@ -45,6 +45,7 @@ data class HistoryCardState(
val color: PaletteColor,
val firstWeekday: DayOfWeek,
val series: List<HistoryChart.Square>,
val defaultSquare: HistoryChart.Square,
val theme: Theme,
val today: LocalDate,
)
@@ -131,6 +132,10 @@ class HistoryCardPresenter(
}
}
}
val defaultSquare = if (habit.isNumerical && habit.targetType == NumericalHabitType.AT_MOST)
HistoryChart.Square.ON
else
HistoryChart.Square.OFF
return HistoryCardState(
color = habit.color,
@@ -138,6 +143,7 @@ class HistoryCardPresenter(
today = today.toLocalDate(),
theme = theme,
series = series,
defaultSquare = defaultSquare
)
}
}

View File

@@ -41,6 +41,7 @@ class HistoryChart(
var firstWeekday: DayOfWeek,
var paletteColor: PaletteColor,
var series: List<Square>,
var defaultSquare: Square,
var theme: Theme,
var today: LocalDate,
var onDateClickedListener: OnDateClickedListener = OnDateClickedListener { },
@@ -189,7 +190,7 @@ class HistoryChart(
offset: Int,
) {
val value = if (offset >= series.size) Square.OFF else series[offset]
val value = if (offset >= series.size) defaultSquare else series[offset]
val squareColor: Color
val color = theme.color(paletteColor.paletteIndex)
squareColor = when (value) {

View File

@@ -49,6 +49,7 @@ class HistoryChartTest {
dateFormatter = JavaLocalDateFormatter(Locale.US),
firstWeekday = SUNDAY,
onDateClickedListener = dateClickedListener,
defaultSquare = OFF,
series = listOf(
2, // today
2, 1, 2, 1, 2, 1, 2,