Skip to content
Merged
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
23 changes: 23 additions & 0 deletions app/src/main/java/com/pulseloop/service/DrinkInsights.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ object DrinkInsights {
)
}

/** The week at a glance: a headline about the limit, and a line about dry days. */
fun headline(week: Week, limit: Double, dryTarget: Int, today: LocalDate): Pair<String, String> {
val total = week.standardDrinks
val title = when {
limit <= 0 -> if (total <= 0) "No drinks this week" else "${fmt(total)} drinks this week"
total > limit -> "${fmt(total - limit)} over your limit"
total <= 0 -> "No drinks yet this week"
limit - total < 0.05 -> "Right at your limit"
else -> "${fmt(limit - total)} left this week"
}
// Days still to come this week, today included, while today is still dry.
val daysLeft = 7 - (today.dayOfWeek.value - 1)
val canStillBeDry = if (week.drankToday) daysLeft - 1 else daysLeft
val needed = (dryTarget - week.dryDays).coerceAtLeast(0)
val sub = when {
dryTarget <= 0 -> "${week.dryDays} dry day${if (week.dryDays == 1) "" else "s"} so far"
needed == 0 -> "Dry-day goal reached: ${week.dryDays} of $dryTarget"
needed <= canStillBeDry -> "$needed more dry day${if (needed == 1) "" else "s"} to reach $dryTarget"
else -> "${week.dryDays} of $dryTarget dry days; a fresh week starts Monday"
}
return title to sub
}

/** Standard drinks per week, oldest first, for the [weeks] weeks ending with this one. */
fun weeklyTotals(gramsByDay: Map<LocalDate, Double>, today: LocalDate, gramsPerStandard: Double, weeks: Int): List<Pair<LocalDate, Double>> {
val thisWeek = weekStart(today)
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/pulseloop/service/DrinksRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ object DrinksRepository {
val planToday: Int?,
val todayStandard: Double,
val auditDue: Boolean,
/** Monday to Sunday of this week, with the alcohol logged for each evening (grams). */
val weekDays: List<Pair<LocalDate, Double>>,
/** The user's four most-logged drinks lately, for the one-tap row. */
val topTypes: List<DrinkType>,
)

suspend fun load(db: PulseLoopDatabase, now: Long = System.currentTimeMillis(), zone: ZoneId = ZoneId.systemDefault()): Snapshot {
Expand Down Expand Up @@ -197,9 +201,19 @@ object DrinksRepository {
planToday = DrinkPrefs.plan(today),
todayStandard = DrinkInsights.standardDrinks(byDay[today] ?: 0.0, std),
auditDue = DrinkPrefs.auditDate?.let { it.plusDays(AuditC.RETAKE_DAYS) <= today } ?: true,
weekDays = (0L until 7L).map { DrinkInsights.weekStart(today).plusDays(it) }.map { it to (byDay[it] ?: 0.0) },
topTypes = topTypes(all.filter { it.mealId == null }.map { DrinkType.of(it.type) }),
)
}

private val DEFAULT_TOP = listOf(DrinkType.BEER, DrinkType.WINE, DrinkType.CAIPIRINHA, DrinkType.SPIRIT)

/** The four most-logged types, topped up with common ones so the row is always four wide. */
fun topTypes(logged: List<DrinkType>): List<DrinkType> {
val ranked = logged.groupingBy { it }.eachCount().entries.sortedByDescending { it.value }.map { it.key }
return (ranked + DEFAULT_TOP).distinct().take(4)
}

/** One line for a morning-after card or notification, null when there's nothing honest to say. */
fun morningAfterLine(m: DrinkInsights.MorningAfter): String {
val drinks = "${DrinkInsights.fmt(m.standardDrinks)} drink${if (m.standardDrinks == 1.0) "" else "s"}"
Expand Down
Loading
Loading