Food: week strip shows protein / carbs / fat; over-target days in red

Each day's bar is stacked by the share of the energy from protein, carbs and
fat (as in the History chart); hovering shows the calories and grams. A day
more than 5 % over its target shows its calories in bold red. Dashboard and
app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 14:24:00 +02:00
parent c54f66c3c2
commit 2869a208f1
5 changed files with 46 additions and 11 deletions

View file

@ -19,6 +19,8 @@ What changed in each published build of the app. `tool/publish.sh` shows the ent
search box, finds that food first.
- Food: a barcode nothing is found for offers "New food with barcode …", which opens the editor with the
barcode filled in. The dashboard does the same.
- Food: the week strip's day pills show the day's protein / carbs / fat in their colours (by their share of the
energy), and a day over its target has its calories in red. The dashboard does the same.
## 1.1.3 — 2026-10-26

View file

@ -122,6 +122,7 @@ class _FoodPageState extends State<FoodPage> {
Widget _week(double target) {
final mon = plus(d, -(d.weekday - 1));
final k = kcalByDay(widget.sync.store, dayIso(mon), dayIso(plus(mon, 6)));
final m = dayTotals(widget.sync.store, dayIso(mon), dayIso(plus(mon, 6)));
return Row(children: [
for (var i = 0; i < 7; i++)
() {
@ -140,12 +141,13 @@ class _FoodPageState extends State<FoodPage> {
const SizedBox(height: 3),
Container(
width: 12, height: 32, alignment: Alignment.bottomCenter,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(color: card2, borderRadius: BorderRadius.circular(3)),
child: FractionallySizedBox(heightFactor: h / 1.15,
child: Container(decoration: BoxDecoration(color: over ? bad : good, borderRadius: BorderRadius.circular(3)))),
child: FractionallySizedBox(heightFactor: h / 1.15, child: _macroFill(m[dayIso(x)], kc)),
),
const SizedBox(height: 2),
Text(kc == null ? '' : '${kc.round()}', style: const TextStyle(fontSize: 10.5, color: muted)),
Text(kc == null ? '' : '${kc.round()}',
style: TextStyle(fontSize: 10.5, color: over ? bad : muted, fontWeight: over ? FontWeight.bold : null)),
]),
),
),
@ -154,6 +156,20 @@ class _FoodPageState extends State<FoodPage> {
]);
}
/// A week pill's fill, bottom up: protein / carbs / fat by their share of the energy, the rest grey
/// (views._macro_segs — as the History chart stacks them).
Widget _macroFill(Map<String, double>? t, double? kcal) {
if (t == null || kcal == null || kcal <= 0) return const SizedBox();
final e = {for (final k in macros) k: (t[k] ?? 0) * kcalPerG[k]!};
final total = max(kcal, e.values.fold<double>(0, (a, b) => a + b));
final rest = total - e.values.fold<double>(0, (a, b) => a + b);
return Column(children: [
if (rest / total > 0.005) Expanded(flex: (rest / total * 1000).round(), child: Container(color: muted.withValues(alpha: 0.5))),
for (final k in macros.reversed)
if (e[k]! > 0) Expanded(flex: (e[k]! / total * 1000).round(), child: Container(color: macroColors[k])),
]);
}
Widget _meal(String m, List<Entry> es) {
final t = totals(es);
// a long-pressed food can be dropped on another meal's card (db.move: only the meal changes)

View file

@ -75,8 +75,10 @@ a.fd-wd:hover { background: #1d222d; }
.fd-wd.future { opacity: .4; }
.fd-wn { font-size: 13px; color: var(--muted); white-space: nowrap; }
.fd-wb { position: relative; width: 14px; height: 40px; border-radius: 4px; background: #1b1f29; overflow: hidden; }
.fd-wb s { position: absolute; left: 0; right: 0; bottom: 0; max-height: 100%; background: var(--good); opacity: .85; }
.fd-wd.over .fd-wb s { background: var(--bad); }
.fd-wb s { position: absolute; left: 0; right: 0; bottom: 0; max-height: 100%; display: flex; flex-direction: column-reverse; opacity: .85; }
.fd-wb i { display: block; flex: none; } /* protein / carbs / fat by their share of the energy, bottom up (views._macro_segs) */
.fd-wb i.protein { background: var(--pr); } .fd-wb i.carbs { background: var(--cb); } .fd-wb i.fat { background: var(--ft); } .fd-wb i.rest { background: var(--rest); }
.fd-wd.over .fd-wk { color: var(--bad); font-weight: 700; }
.fd-wd.sel .fd-wb s { opacity: 1; }
.fd-wk { font-size: 12px; color: var(--muted); min-height: 1.2em; }
.fd-wsum { display: flex; flex-direction: column; justify-content: center; align-items: flex-end; text-align: right; padding-right: 6px; color: var(--muted); font-size: 13px; }

View file

@ -19,9 +19,9 @@
<div class="fd-main">
<nav class="fd-week" aria-label="This week">
{% for c in v.strip %}
{% if c.future %}<span class="fd-wd future">{% else %}<a class="fd-wd{% if c.sel %} sel{% endif %}{% if c.today %} today{% endif %}{% if c.over %} over{% endif %}{% if c.ahead %} ahead{% endif %}" href="/food?d={{ c.d.isoformat() }}"{% if c.sel %} aria-current="date"{% endif %}>{% endif %}
{% if c.future %}<span class="fd-wd future">{% else %}<a class="fd-wd{% if c.sel %} sel{% endif %}{% if c.today %} today{% endif %}{% if c.over %} over{% endif %}{% if c.ahead %} ahead{% endif %}" href="/food?d={{ c.d.isoformat() }}" title="{{ c.tip }}"{% if c.sel %} aria-current="date"{% endif %}>{% endif %}
<span class="fd-wn">{{ c.d.strftime("%a")[:2] }} {{ c.d.day }}</span>
<span class="fd-wb"><s style="height: {{ c.h }}%"></s></span>
<span class="fd-wb"><s style="height: {{ c.h }}%">{% for g in c.segs %}<i class="{{ g.m }}" style="height: {{ g.h }}%"></i>{% endfor %}</s></span>
<span class="fd-wk">{{ n(c.kcal) if c.kcal else "" }}</span>
{% if c.future %}</span>{% else %}</a>{% endif %}
{% endfor %}

View file

@ -179,6 +179,18 @@ def bars(t, tg):
AHEAD = 60 # days into the future the diary goes (planning meals)
def _macro_segs(v, kcal):
"""A week-strip pill's fill, bottom up: protein / carbs / fat by their share of the energy (% of the
fill), the rest (alcohol, labels that don't add up) grey — as the History chart stacks them."""
if not kcal:
return []
e = {m: (v.get(m) or 0) * KCAL_PER_G[m] for m in MACROS}
total = max(kcal, sum(e.values())) # labels' macros can add up past the kcal
segs = [{"m": m, "h": round(100 * e[m] / total, 1)} for m in MACROS if e[m] > 0]
rest = 100 - sum(x["h"] for x in segs)
return segs + ([{"m": "rest", "h": round(rest, 1)}] if rest > 0.5 else [])
def day(entries_of, totals_of, s, d, today, energy=None, activity=None, added_of=None):
"""entries_of(a, z) / totals_of(a, z): db reads (ISO days); energy: Health's (plan());
activity: Health's day ({steps, workouts, energy}) or None; added_of(entry) → its added sugar, g (sugar.py)."""
@ -200,11 +212,14 @@ def day(entries_of, totals_of, s, d, today, energy=None, activity=None, added_of
strip = []
for i in range(7):
x = mon + timedelta(days=i)
k = (wk.get(x.isoformat()) or {}).get("kcal")
v = wk.get(x.isoformat()) or {}
k = v.get("kcal")
strip.append({"d": x, "kcal": round(k) if k else None, "h": round(min(1.15, (k or 0) / tg["kcal"][0]) * 100),
"today": x == today, "sel": x == d, "future": x > today + timedelta(days=AHEAD),
"ahead": x > today,
"over": bool(k and k > tg["kcal"][0] * 1.05)})
"segs": _macro_segs(v, k), "today": x == today, "sel": x == d,
"future": x > today + timedelta(days=AHEAD), "ahead": x > today,
"over": bool(k and k > tg["kcal"][0] * 1.05),
"tip": f"{round(k)} kcal · protein {round(v.get('protein') or 0)} g, carbs {round(v.get('carbs') or 0)} g, "
f"fat {round(v.get('fat') or 0)} g" if k else "nothing logged"})
burned = ((activity or {}).get("energy") or {}).get("total")
return {"day": d, "today": today, "prev": d - timedelta(days=1),
"next": d + timedelta(days=1) if d < today + timedelta(days=AHEAD) else None, "future": d > today,