Add a Meals log to Habits, with a bulk label for unlabelled meals
A meal's amount and its kind are both optional by design, which is what makes them easy to lose track of: a meal with no kind sits in the grey band on the food chart, one with no grams is dropped from it silently, and there was nowhere to go and see which meals those were. The panel lists every meal across every day, ignoring the day picker as the Notes log does — the ones worth finding are spread through history rather than sitting on the day being viewed. Each filter chip carries its own count, so the size of the gap reads off the panel without selecting anything. Rows go through attachRowHandlers, so tapping one opens the usual edit dialog and long-press still measures. On the "no kind" filter it also offers to label the whole list at once, which is what history needs after kinds arrived months into logging. backfillFoodKind does it in one pass — one save, one sync, one render, following setDefaultFoodKind rather than calling updateEvent hundreds of times; sync already posts the whole event list, so it costs nothing extra on the wire. The optional cut-off date filters the list as well as the edit, so the number on the button is the rows on screen: labelling all of history as one kind is wrong if the food was switched partway, and once labelled the early meals cannot be told apart. There is no undo, so the guards are the live count, the number on the button and a confirm. Owner-only; the server's guest rules would refuse it anyway. Meals are the only event type that gets this — the only one with optional fields worth filling in afterwards. Nothing appears until there is a meal to list, and the kind filter and bulk block wait for a kind to exist, so an account not using kinds is unchanged.
This commit is contained in:
1 parent
41827664de
commit
fab0ae1420
6 files changed
+407
-4
No files matched your search
+213
-4
@@ -499,12 +499,14 @@
|
||||
}
|
||||
|
||||
// The chips both dialogs use. Built from the live kinds plus "No kind",
|
||||
// which always comes last and is always offered — a meal is allowed to have
|
||||
// no kind, permanently, and the picker should never imply otherwise.
|
||||
// which always comes last — a meal is allowed to have no kind, permanently,
|
||||
// and the picker should never imply otherwise. The bulk label in the meals
|
||||
// log is the one caller that drops it, labelling unlabelled meals as
|
||||
// unlabelled being the one thing it cannot mean.
|
||||
//
|
||||
// Hidden entirely when no kinds are defined, so the app looks exactly as it
|
||||
// did to anyone who never wants them. onPick receives the chosen id.
|
||||
function renderKindPicker(pickerEl, fieldEl, selectedId, onPick) {
|
||||
function renderKindPicker(pickerEl, fieldEl, selectedId, onPick, { includeNoKind = true } = {}) {
|
||||
const kinds = liveFoodKinds();
|
||||
fieldEl.hidden = kinds.length === 0;
|
||||
if (kinds.length === 0) return;
|
||||
@@ -522,7 +524,7 @@
|
||||
pickerEl.appendChild(b);
|
||||
};
|
||||
for (const k of kinds) chip(k.id, k.name, k.colorIndex ?? 0);
|
||||
chip(NO_KIND, "No kind", 0);
|
||||
if (includeNoKind) chip(NO_KIND, "No kind", 0);
|
||||
}
|
||||
|
||||
// ---------- food kinds ----------
|
||||
@@ -623,6 +625,61 @@
|
||||
return new Map(loadFoodKinds().map(k => [k.id, k.name]));
|
||||
}
|
||||
|
||||
// ---------- labelling meals after the fact ----------
|
||||
// Kinds arrived after months of logging, so history is full of meals that
|
||||
// carry none. The Meals log in Habits lists them; these are the rules it
|
||||
// filters by, and the one bulk edit it offers.
|
||||
//
|
||||
// What "missing" means, given both fields are legitimately optional: a meal
|
||||
// has no kind when foodKindId is "", and no amount when grams is absent or
|
||||
// zero — the same test the charts use to decide there is nothing to plot.
|
||||
const MEAL_GAPS = {
|
||||
all: () => true,
|
||||
kind: (e) => !e.foodKindId,
|
||||
amount: (e) => !(Number.isFinite(e.grams) && e.grams > 0),
|
||||
};
|
||||
|
||||
// The meals a filter selects, newest first. Pure, so the rules above can be
|
||||
// checked directly. beforeYmd is optional and takes meals strictly before the
|
||||
// start of that day, which is what makes a bulk label safe when the food was
|
||||
// switched partway: everything on or after the cut-off keeps what it has.
|
||||
//
|
||||
// Deliberately not filtered on: days marked "not counted". That mark means
|
||||
// "leave this out of the averages", not "this did not happen" — the meal is
|
||||
// still a record of what was fed, and labelling it keeps the record whole.
|
||||
function mealsMissing(events, gap, beforeYmd) {
|
||||
const test = MEAL_GAPS[gap] || MEAL_GAPS.all;
|
||||
let cut = Infinity;
|
||||
if (beforeYmd) {
|
||||
const t = new Date(`${beforeYmd}T00:00:00`).getTime();
|
||||
if (Number.isFinite(t)) cut = t;
|
||||
}
|
||||
return events
|
||||
.filter(e => e.type === "eat" && !e.deleted && e.at < cut && test(e))
|
||||
.sort((a, b) => b.at - a.at);
|
||||
}
|
||||
|
||||
// Give every unlabelled meal the same kind in one pass: one save, one sync,
|
||||
// one render, as setDefaultFoodKind does for the kinds themselves. Calling
|
||||
// updateEvent per meal would re-render and re-queue a sync hundreds of times
|
||||
// over. It costs nothing extra on the wire either — sync already posts the
|
||||
// whole event list, so the server simply sees more rows with newer stamps.
|
||||
//
|
||||
// loggedBy / loggedByShare are left alone, so a sitter's meals keep their
|
||||
// attribution while gaining a kind. Returns how many it changed.
|
||||
function backfillFoodKind(kindId, beforeYmd) {
|
||||
if (!kindId) return 0;
|
||||
const ids = new Set(mealsMissing(loadAll(), "kind", beforeYmd).map(e => e.id));
|
||||
if (ids.size === 0) return 0;
|
||||
const now = Date.now();
|
||||
saveAll(loadAll().map(e =>
|
||||
ids.has(e.id) ? { ...e, foodKindId: kindId, updatedAt: now } : e
|
||||
));
|
||||
scheduleSync();
|
||||
render();
|
||||
return ids.size;
|
||||
}
|
||||
|
||||
// ---------- helpers ----------
|
||||
function ymd(date) {
|
||||
const y = date.getFullYear();
|
||||
@@ -1598,6 +1655,157 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- the meals log ----------
|
||||
// A cross-day list of every meal, filterable by what a meal is missing. Both
|
||||
// of a meal's extras are optional by design, which is exactly what makes them
|
||||
// easy to lose track of: a meal with no kind sits in a grey band on the chart
|
||||
// and a meal with no amount is dropped from it silently, and until this panel
|
||||
// there was nowhere to go and see which meals those were.
|
||||
//
|
||||
// Meals only. They are the one event type with optional fields worth filling
|
||||
// in afterwards — a pee is a pee, a walk is a span, and a weigh-in is refused
|
||||
// without a number — so this stays a meals log rather than a general one.
|
||||
const MEALS_LOG_PAGE = 50;
|
||||
const MEAL_GAP_LABELS = { all: "All", kind: "No kind", amount: "No amount" };
|
||||
|
||||
let mealsLogGap = "all";
|
||||
let mealsLogCut = ""; // "" = no cut-off
|
||||
let mealsLogShown = MEALS_LOG_PAGE;
|
||||
let mealsLogKind = NO_KIND; // what the bulk label would apply; none until picked
|
||||
|
||||
function renderMealsLog(events) {
|
||||
const wrap = document.getElementById("meals-log");
|
||||
wrap.hidden = !events.some(e => e.type === "eat" && !e.deleted);
|
||||
if (wrap.hidden) return;
|
||||
|
||||
const kinds = liveFoodKinds();
|
||||
const kindNames = foodKindNames();
|
||||
// Filtering on a missing kind says nothing before any kind exists: every
|
||||
// meal would match. The chip stays hidden, so an account that never uses
|
||||
// kinds sees a plain list.
|
||||
const usingKinds = kinds.length > 0;
|
||||
if (!usingKinds && mealsLogGap === "kind") mealsLogGap = "all";
|
||||
|
||||
const rows = mealsMissing(events, mealsLogGap, mealsLogCut);
|
||||
const counts = {
|
||||
all: mealsMissing(events, "all", mealsLogCut).length,
|
||||
kind: mealsMissing(events, "kind", mealsLogCut).length,
|
||||
amount: mealsMissing(events, "amount", mealsLogCut).length,
|
||||
};
|
||||
|
||||
// The counts ride on the chips, so how much is unlabelled can be read
|
||||
// without selecting anything — which is the main thing this panel is for.
|
||||
for (const btn of document.querySelectorAll("#meals-log-filter .kind-chip")) {
|
||||
const gap = btn.dataset.gap;
|
||||
const on = gap === mealsLogGap;
|
||||
btn.hidden = gap === "kind" && !usingKinds;
|
||||
btn.classList.toggle("active", on);
|
||||
btn.setAttribute("aria-checked", String(on));
|
||||
btn.textContent = `${MEAL_GAP_LABELS[gap]} ${counts[gap]}`;
|
||||
}
|
||||
|
||||
const list = document.getElementById("meals-log-list");
|
||||
const empty = document.getElementById("meals-log-empty");
|
||||
list.innerHTML = "";
|
||||
empty.hidden = rows.length > 0;
|
||||
empty.textContent =
|
||||
mealsLogGap === "kind" ? "Every meal here has a kind." :
|
||||
mealsLogGap === "amount" ? "Every meal here has an amount." :
|
||||
"No meals before that date.";
|
||||
|
||||
const shown = rows.slice(0, mealsLogShown);
|
||||
for (const ev of shown) {
|
||||
const li = document.createElement("li");
|
||||
li.className = "event";
|
||||
li.dataset.type = "eat";
|
||||
li.dataset.id = ev.id;
|
||||
li.innerHTML = `
|
||||
<span class="dot"></span>
|
||||
<span class="note-date"></span>
|
||||
<span class="note-text"></span>
|
||||
`;
|
||||
const when = new Date(ev.at);
|
||||
li.querySelector(".note-date").textContent =
|
||||
`${when.toLocaleDateString(undefined, { month: "short", day: "numeric" })} ${formatTime(ev.at)}`;
|
||||
// What is missing is named rather than left blank — the gaps are the
|
||||
// point here, unlike the History row, which just omits what it hasn't got.
|
||||
const bits = [];
|
||||
bits.push(Number.isFinite(ev.grams) && ev.grams > 0 ? `${Math.round(ev.grams)} g` : "no amount");
|
||||
if (usingKinds) {
|
||||
bits.push(kindNames.get(ev.foodKindId) || (ev.foodKindId ? "Deleted kind" : "no kind"));
|
||||
}
|
||||
if (ev.note) bits.push(ev.note);
|
||||
li.querySelector(".note-text").textContent = bits.join(" · ");
|
||||
// Same handlers as every other row: tap to edit (where a kind or an
|
||||
// amount can be filled in one at a time), long-press to measure.
|
||||
attachRowHandlers(li, ev);
|
||||
list.appendChild(li);
|
||||
}
|
||||
|
||||
// Capped rather than paged: a year of logging is a thousand rows, and
|
||||
// building them all on every render would cost more than it shows.
|
||||
const more = document.getElementById("meals-log-more");
|
||||
const rest = rows.length - shown.length;
|
||||
more.hidden = rest <= 0;
|
||||
more.textContent = `Show ${Math.min(rest, MEALS_LOG_PAGE)} more`;
|
||||
|
||||
// The bulk label sits on the list rather than in Settings, so the number on
|
||||
// the button is the rows being looked at. Owner-only: a guest may not touch
|
||||
// the owner's meals, and the server would refuse it anyway.
|
||||
const bulk = document.getElementById("meals-log-bulk");
|
||||
const canBulk = usingKinds && !isGuest() && mealsLogGap === "kind" && rows.length > 0;
|
||||
bulk.hidden = !canBulk;
|
||||
if (canBulk) {
|
||||
if (mealsLogKind && !kinds.some(k => k.id === mealsLogKind)) mealsLogKind = NO_KIND;
|
||||
// No "No kind" chip: labelling unlabelled meals as unlabelled is the one
|
||||
// thing this cannot mean.
|
||||
renderKindPicker(document.getElementById("meals-log-kind-picker"), bulk, mealsLogKind,
|
||||
(id) => { mealsLogKind = id; renderMealsLog(live()); }, { includeNoKind: false });
|
||||
const apply = document.getElementById("meals-log-apply");
|
||||
// Nothing is preselected, so labelling hundreds of meals always takes a
|
||||
// deliberate pick first.
|
||||
apply.disabled = !mealsLogKind;
|
||||
apply.textContent = `Label ${rows.length} ${rows.length === 1 ? "meal" : "meals"}`;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById("meals-log-filter").addEventListener("click", (e) => {
|
||||
const btn = e.target.closest(".kind-chip");
|
||||
if (!btn) return;
|
||||
mealsLogGap = btn.dataset.gap;
|
||||
mealsLogShown = MEALS_LOG_PAGE;
|
||||
renderMealsLog(live());
|
||||
});
|
||||
|
||||
document.getElementById("meals-log-more").addEventListener("click", () => {
|
||||
mealsLogShown += MEALS_LOG_PAGE;
|
||||
renderMealsLog(live());
|
||||
});
|
||||
|
||||
const mealsCutOn = document.getElementById("meals-log-before-on");
|
||||
const mealsCutDate = document.getElementById("meals-log-before");
|
||||
function mealsCutChanged() {
|
||||
// An empty date with the box ticked is not a cut-off of nothing, it is no
|
||||
// cut-off — otherwise ticking the box would empty the list.
|
||||
if (mealsCutOn.checked && !mealsCutDate.value) mealsCutDate.value = ymd(new Date());
|
||||
mealsLogCut = mealsCutOn.checked ? mealsCutDate.value : "";
|
||||
mealsLogShown = MEALS_LOG_PAGE;
|
||||
renderMealsLog(live());
|
||||
}
|
||||
mealsCutOn.addEventListener("change", mealsCutChanged);
|
||||
mealsCutDate.addEventListener("change", mealsCutChanged);
|
||||
|
||||
document.getElementById("meals-log-apply").addEventListener("click", () => {
|
||||
if (!mealsLogKind) return;
|
||||
const n = mealsMissing(live(), "kind", mealsLogCut).length;
|
||||
if (n === 0) return;
|
||||
const name = foodKindNames().get(mealsLogKind) || "this kind";
|
||||
// The only guard, so it names both numbers the decision turns on.
|
||||
if (!confirm(`Label ${n} ${n === 1 ? "meal" : "meals"} as ${name}?\n\nThis cannot be undone.`)) return;
|
||||
backfillFoodKind(mealsLogKind, mealsLogCut);
|
||||
mealsLogKind = NO_KIND;
|
||||
});
|
||||
|
||||
// ---------- lightbox ----------
|
||||
const lightbox = document.getElementById("lightbox");
|
||||
const lightboxImg = document.getElementById("lightbox-img");
|
||||
@@ -3447,6 +3655,7 @@
|
||||
// logger distorts, so they count everywhere regardless.
|
||||
renderWeight(events);
|
||||
renderNotes(events);
|
||||
renderMealsLog(events);
|
||||
// After the lists, so a pick whose event has gone is dropped in the same
|
||||
// pass that stops drawing it as picked.
|
||||
renderMeasureBar(events);
|
||||
|
||||
Reference in new issue
Block a user