diff --git a/app/src/main/java/com/pulseloop/service/MealJournal.kt b/app/src/main/java/com/pulseloop/service/MealJournal.kt index 91d21089..0b4d7657 100644 --- a/app/src/main/java/com/pulseloop/service/MealJournal.kt +++ b/app/src/main/java/com/pulseloop/service/MealJournal.kt @@ -10,9 +10,9 @@ import java.time.ZoneId /** * What a logged meal means for the rest of the app. The food log pays off in a ring app when it - * meets the night after it, so a logged meal tags the journal itself ("Late meal", "Alcohol") - * instead of waiting for the user to tick the same thing by hand, and the coach sees the day's - * intake. + * meets the night after it, so a logged meal tags the journal itself ("Late meal", "Alcohol", + * "Caffeine after 2 pm") instead of waiting for the user to tick the same thing by hand, and the + * coach sees the day's intake. * * Tags are only ever added. A user who unticks one keeps it unticked until another meal of the * same kind is logged. @@ -36,6 +36,25 @@ object MealJournal { RegexOption.IGNORE_CASE, ) + /** From this hour, caffeine counts as "Caffeine after 2 pm". */ + const val CAFFEINE_CUTOFF_HOUR = 14 + + // Coffee, caffeinated teas, mate, colas, guaraná and energy drinks, in English and Portuguese. + // Plain "tea"/"chá" is left out: chamomile and most herbal teas have no caffeine. + private val CAFFEINE = Regex( + "\\b(coffee|caf[eé]|cafezinho|pingado|espresso|expresso|cappuccino|latte|macchiato|mocha|" + + "americano|cold brew|frappuccino|ristretto|cortado|flat white|black tea|green tea|matcha|" + + "chai|earl grey|ch[aá] (preto|verde|mate)|mate|chimarr[aã]o|terer[eé]|yerba|" + + "cola|coke|pepsi|guaran[aá]|energy drink|energ[eé]tico|red bull|monster)\\b", + RegexOption.IGNORE_CASE, + ) + private val CAFFEINE_FREE = Regex( + "\\b(decaf|decaffeinated|descafeinado|caffeine[- ]free|sem cafe[ií]na|zero cafe[ií]na)\\b", + RegexOption.IGNORE_CASE, + ) + /** "Café da manhã" is breakfast, not coffee; drop it before looking for caffeine. */ + private val BREAKFAST_PT = Regex("caf[eé] da manh[aã]", RegexOption.IGNORE_CASE) + /** The evening a meal belongs to: its own date, or the day before when eaten before [EVENING_ENDS_HOUR]. */ fun eveningOf(timestamp: Long, zone: ZoneId = ZoneId.systemDefault()): LocalDate { val local = Instant.ofEpochMilli(timestamp).atZone(zone) @@ -52,16 +71,29 @@ object MealJournal { return ALCOHOL.containsMatchIn(text) && !ALCOHOL_FREE.containsMatchIn(text) } + fun isCaffeinated(name: String, notes: String? = null): Boolean { + val text = BREAKFAST_PT.replace(listOfNotNull(name, notes).joinToString(" "), " ") + return CAFFEINE.containsMatchIn(text) && !CAFFEINE_FREE.containsMatchIn(text) + } + + /** 14:00 onwards, through the small hours that still belong to the evening. */ + fun isAfterCaffeineCutoff(timestamp: Long, zone: ZoneId = ZoneId.systemDefault()): Boolean { + val hour = Instant.ofEpochMilli(timestamp).atZone(zone).hour + return hour >= CAFFEINE_CUTOFF_HOUR || hour < EVENING_ENDS_HOUR + } + /** The journal tags one meal implies, keyed by the day they belong to. */ fun tagsFor(meal: MealEntryEntity, zone: ZoneId = ZoneId.systemDefault()): Map> { val tags = buildSet { if (isLate(meal.timestamp, zone)) add(JournalInsights.Tag.LATE_MEAL.key) if (isAlcohol(meal.name, meal.notes)) add(JournalInsights.Tag.ALCOHOL.key) + if (isAfterCaffeineCutoff(meal.timestamp, zone) && isCaffeinated(meal.name, meal.notes)) { + add(JournalInsights.Tag.LATE_CAFFEINE.key) + } } if (tags.isEmpty()) return emptyMap() - // A daytime drink belongs to its own day; anything in the late window to its evening. - val day = if (isLate(meal.timestamp, zone)) eveningOf(meal.timestamp, zone) - else Instant.ofEpochMilli(meal.timestamp).atZone(zone).toLocalDate() + // A meal belongs to its own day; one in the small hours to the evening before. + val day = eveningOf(meal.timestamp, zone) return mapOf(day to tags) } diff --git a/app/src/main/java/com/pulseloop/ui/screens/JournalUi.kt b/app/src/main/java/com/pulseloop/ui/screens/JournalUi.kt index e2db01f0..d1aa9203 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/JournalUi.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/JournalUi.kt @@ -46,9 +46,13 @@ fun JournalTagsCard( date: LocalDate, title: String, tags: List = JournalInsights.Tag.entries, + /** Offer a Today / Yesterday switch, so a forgotten day can still be tagged the morning after. */ + allowYesterday: Boolean = false, footer: (@Composable () -> Unit)? = null, ) { - val day = remember(date) { dayKey(date) } + var onYesterday by remember(date) { mutableStateOf(false) } + val shown = if (allowYesterday && onYesterday) date.minusDays(1) else date + val day = remember(shown) { dayKey(shown) } val entries by remember(day) { db.journalDao().forDayFlow(day) }.collectAsState(initial = emptyList()) val selected = entries.map { it.tag }.toSet() val scope = rememberCoroutineScope() @@ -57,7 +61,16 @@ fun JournalTagsCard( Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).background(PulseColors.card).padding(16.dp), verticalArrangement = Arrangement.spacedBy(10.dp), ) { - Text(title, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.textPrimary) + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Text( + if (allowYesterday && onYesterday) "Tag yesterday" else title, + fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.textPrimary, + modifier = Modifier.weight(1f), + ) + if (allowYesterday) { + DaySwitch(onYesterday) { onYesterday = it } + } + } // Chips size to their label and wrap as a row, one line each: a fixed two-column grid // broke the longer labels over two lines in a monospace face. FlowRow( @@ -94,6 +107,27 @@ fun JournalTagsCard( } } +/** Today / Yesterday, as two small segments. */ +@Composable +private fun DaySwitch(yesterday: Boolean, onChange: (Boolean) -> Unit) { + val shape = RoundedCornerShape(16.dp) + Row(Modifier.clip(shape).border(1.dp, PulseColors.borderSubtle, shape)) { + listOf(false to "Today", true to "Yesterday").forEach { (isYesterday, label) -> + val on = yesterday == isYesterday + Text( + label, + fontSize = 12.sp, + fontWeight = if (on) FontWeight.Bold else FontWeight.Normal, + color = if (on) PulseColors.onAccent else PulseColors.textSecondary, + modifier = Modifier + .background(if (on) PulseColors.accent else PulseColors.cardSoft) + .clickable { onChange(isYesterday) } + .padding(horizontal = 10.dp, vertical = 6.dp), + ) + } + } +} + /** Journal screen: tag today and yesterday, then see which tags line up with your nights. */ @Composable fun JournalScreen(db: PulseLoopDatabase, onBack: () -> Unit) { diff --git a/app/src/main/java/com/pulseloop/ui/screens/TodayGlance.kt b/app/src/main/java/com/pulseloop/ui/screens/TodayGlance.kt index e516bacd..7aaeb4a9 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/TodayGlance.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/TodayGlance.kt @@ -190,7 +190,7 @@ fun MealSheet(onDismiss: () -> Unit, onPick: (start: String) -> Unit) { fun TagDaySheet(db: PulseLoopDatabase, onDismiss: () -> Unit, onSeeEffects: () -> Unit) { ModalBottomSheet(onDismissRequest = onDismiss, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true), containerColor = PulseColors.secondaryBackground) { Column(Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(bottom = 32.dp)) { - JournalTagsCard(db, java.time.LocalDate.now(), "Tag your day") { + JournalTagsCard(db, java.time.LocalDate.now(), "Tag your day", allowYesterday = true) { Text( "See what affects you", fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = PulseColors.accent, diff --git a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt index fb1cb3e7..abec23c5 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/TodayScreen.kt @@ -250,6 +250,7 @@ fun TodayScreen( JournalTagsCard( db, java.time.LocalDate.now(), "Tag your day", tags = com.pulseloop.service.JournalInsights.Tag.entries.take(6), + allowYesterday = true, ) { Text( "See what affects you", diff --git a/app/src/test/java/com/pulseloop/service/MealJournalTest.kt b/app/src/test/java/com/pulseloop/service/MealJournalTest.kt index 3d95af1b..3c9bf03c 100644 --- a/app/src/test/java/com/pulseloop/service/MealJournalTest.kt +++ b/app/src/test/java/com/pulseloop/service/MealJournalTest.kt @@ -54,6 +54,34 @@ class MealJournalTest { assertFalse(MealJournal.isAlcohol("Rumbled eggs")) } + @Test + fun `a snack with coffee after 2 pm tags late caffeine on its day`() { + assertEquals(mapOf(day to setOf("late_caffeine")), MealJournal.tagsFor(meal("Pão de queijo e café", day.atTime(15, 30)), zone)) + assertEquals(mapOf(day to setOf("late_caffeine")), MealJournal.tagsFor(meal("Cappuccino", day.atTime(14, 0)), zone)) + } + + @Test + fun `coffee before 2 pm tags nothing and a midnight espresso belongs to the evening before`() { + assertTrue(MealJournal.tagsFor(meal("Black coffee", day.atTime(13, 59)), zone).isEmpty()) + assertEquals( + mapOf(day to setOf("late_meal", "late_caffeine")), + MealJournal.tagsFor(meal("Espresso", day.plusDays(1).atTime(0, 30)), zone), + ) + } + + @Test + fun `caffeine is recognised in drinks and notes, decaf and herbal tea are not`() { + assertTrue(MealJournal.isCaffeinated("Coca-Cola zero")) + assertTrue(MealJournal.isCaffeinated("Guaraná Antarctica")) + assertTrue(MealJournal.isCaffeinated("Chimarrão")) + assertTrue(MealJournal.isCaffeinated("Toast", notes = "with a latte")) + assertFalse(MealJournal.isCaffeinated("Decaf coffee")) + assertFalse(MealJournal.isCaffeinated("Café descafeinado")) + assertFalse(MealJournal.isCaffeinated("Chamomile tea")) + assertFalse(MealJournal.isCaffeinated("Café da manhã: pão com ovo")) + assertFalse(MealJournal.isCaffeinated("Chocolate cake")) + } + @Test fun `the lunch reminder waits for 13h30 and stays quiet once lunch is logged`() { assertNull(MealReminder.due(LocalTime.of(13, 0), emptyList()))