Skip to content
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

Sliding window targets #234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ sealed class ActionHistory {

object Clean : ActionHistory()

data class Streak(val days: Int) : ActionHistory()
data class SlidingWindow(val windowSize: Int, val actionCount: Int)

data class MissedDays(val days: Int) : ActionHistory()
data class Streak(val days: Int, val slidingWindow: SlidingWindow) : ActionHistory()

data class MissedDays(val days: Int, val slidingWindow: SlidingWindow) : ActionHistory()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import java.time.ZoneId
import com.ofalvai.habittracker.core.database.entity.Action as ActionEntity

private const val RECENT_ACTIONS_PER_HABIT = 30 // Max number of days of any dashboard configs
private const val SLIDING_WINDOW_SIZE = 7

fun actionsToRecentDays(actions: List<ActionEntity>): ImmutableList<Action> {
val lastDay = LocalDate.now()
Expand Down Expand Up @@ -76,7 +77,7 @@ fun actionsToHistory(actions: List<ActionEntity>): ActionHistory {
break
}
}
return ActionHistory.Streak(days)
return ActionHistory.Streak(days, actionsToMovingWindow(sortedActions))
} else if (firstActionDate.isBefore(LocalDate.now())) {
// It's a missed day streak
var days = 0
Expand All @@ -87,9 +88,20 @@ fun actionsToHistory(actions: List<ActionEntity>): ActionHistory {
previousDate = previousDate.minusDays(1)
}

return ActionHistory.MissedDays(days)
return ActionHistory.MissedDays(days, actionsToMovingWindow(sortedActions))
} else {
// firstActionDate is in the future. This should never happen
return ActionHistory.Clean
}
}

private fun actionsToMovingWindow(sortedActions: List<ActionEntity>): ActionHistory.SlidingWindow {
val windowStartInclusive = LocalDate.now().minusDays(SLIDING_WINDOW_SIZE.toLong())
return ActionHistory.SlidingWindow(
SLIDING_WINDOW_SIZE,
sortedActions.count {
val instant = windowStartInclusive.atStartOfDay(ZoneId.systemDefault()).toInstant()
it.timestamp.isAfter(instant)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ fun ActionHistoryLabel(totalActionCount: Int, actionHistory: ActionHistory) {
val actionHistoryLabel = when (actionHistory) {
ActionHistory.Clean -> stringResource(R.string.common_action_count_clean)
is ActionHistory.MissedDays -> resources.getQuantityString(
R.plurals.common_action_count_missed_days, actionHistory.days, actionHistory.days
R.plurals.common_action_count_moving_window, actionHistory.slidingWindow.actionCount, actionHistory.slidingWindow.actionCount, actionHistory.slidingWindow.windowSize
)
is ActionHistory.Streak -> resources.getQuantityString(
R.plurals.common_action_count_streak, actionHistory.days, actionHistory.days
R.plurals.common_action_count_moving_window, actionHistory.slidingWindow.actionCount, actionHistory.slidingWindow.actionCount, actionHistory.slidingWindow.windowSize
)
}
val mergedLabel = stringResource(coreR.string.common_interpunct, totalLabel, actionHistoryLabel)
Expand Down Expand Up @@ -269,9 +269,25 @@ fun PreviewHabitCard() {

PreviewTheme {
Column(Modifier.padding(16.dp)) {
HabitCard(habit1, actions1.toImmutableList(), 14, ActionHistory.Clean, { _, _, _ -> }, {}, 0f)
HabitCard(
habit = habit1,
actions = actions1.toImmutableList(),
totalActionCount = 14,
actionHistory = ActionHistory.Clean,
onActionToggle = { _, _, _ -> },
onDetailClick = {},
dragOffset = 0f
)
Spacer(modifier = Modifier.height(16.dp))
HabitCard(habit2, actions2.toImmutableList(), 3, ActionHistory.Streak(3), { _, _, _ -> }, {}, 0f)
HabitCard(
habit = habit2,
actions = actions2.toImmutableList(),
totalActionCount = 3,
actionHistory = ActionHistory.Streak(3, ActionHistory.SlidingWindow(7, 5)),
onActionToggle = { _, _, _ -> },
onDetailClick = {},
dragOffset = 0f
)
}
}
}
5 changes: 3 additions & 2 deletions feature-dashboard/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@

<string name="common_action_count_clean">Start today</string>
<plurals name="common_action_count_total">
<item quantity="one">%d total</item>
<item quantity="other">%d total</item>
</plurals>
<plurals name="common_action_count_streak">
<item quantity="one">%d day streak</item>
<item quantity="other">%d day streak</item>
</plurals>
<plurals name="common_action_count_missed_days">
<item quantity="one">%d missed day</item>
<item quantity="other">%d missed days</item>
</plurals>
<plurals name="common_action_count_moving_window">
<item quantity="other">%1$d in last %2$d days</item>
</plurals>

<string name="habitdetails_singlestat_total">times total</string>
<string name="habitdetails_singlestat_weekly">times this week</string>
Expand Down