dashPi: a self-hosted home dashboard, its sync server and phone app

The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end
encrypted sync mailbox (PHP + SQLite) and the Android companion app
(Flutter), with docs, clean-install checks and screenshots made from
demo data.

Built mostly with an AI coding assistant; not yet reviewed line by line
by a human (see README and SECURITY.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-24 09:35:12 +02:00
commit fb3c01cdd7
333 changed files with 45151 additions and 0 deletions

View file

@ -0,0 +1,208 @@
// Food ▸ History (views.history): over the shared Health period — tiles (the average *logged* day:
// empty days don't count as zero; days logged + within 10 % of the target; protein, fibre; where the
// energy came from), energy per day (per week from 120 days: the average logged day) stacked by macro
// with the target as a dashed line (tap a bar = its numbers), and the foods eaten most.
import 'dart:math';
import 'package:flutter/material.dart';
import '../app/sync_service.dart';
import '../app/theme.dart';
import 'cycle_model.dart' show daysBetween, parseDay, plus, today;
import 'food_model.dart';
import 'food_page.dart' show macroColors;
import '../widgets/line_chart.dart' show RangeChip;
import 'range.dart';
const weeklyFrom = 120;
const _mon = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
class FoodHistoryPage extends StatefulWidget {
const FoodHistoryPage({super.key, required this.sync, required this.range});
final SyncService sync;
final HealthRange range;
@override
State<FoodHistoryPage> createState() => _FoodHistoryPageState();
}
class _FoodHistoryPageState extends State<FoodHistoryPage> {
String? tip;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Food history')),
body: ListenableBuilder(
listenable: Listenable.merge([widget.sync, widget.range]),
builder: (context, _) {
final store = widget.sync.store;
final now = today();
final first = store.db.select("SELECT min(json_extract(data, '\$.day')) AS d FROM rec WHERE t = 'food.entry'").first['d'] as String?;
final a = widget.range.from(first == null ? now : parseDay(first), now);
final days = dayTotals(store, dayIso(a), dayIso(now));
final logged = days.values.where((v) => (v['kcal'] ?? 0) > 0).toList();
double? avg(String k) => logged.isEmpty ? null : logged.map((v) => v[k] ?? 0).reduce((x, y) => x + y) / logged.length;
final target = targets(store, FoodSettings.of(store))['kcal']!.$1;
final onTarget = logged.where((v) => ((v['kcal'] ?? 0) - target).abs() <= target * 0.1).length;
final e = {for (final m in macros) m: (avg(m) ?? 0) * kcalPerG[m]!};
final tot = e.values.fold<double>(0, (x, y) => x + y);
final top = topFoods(store, dayIso(a), dayIso(now));
String r(double? v) => v == null ? '—' : v.round().toString();
return ListView(padding: const EdgeInsets.all(12), children: [
GridView.count(
crossAxisCount: MediaQuery.of(context).size.width >= 700 ? 4 : 2,
shrinkWrap: true, physics: const NeverScrollableScrollPhysics(),
mainAxisSpacing: 10, crossAxisSpacing: 10, childAspectRatio: 1.9,
children: [
_tile('Average day', '${r(avg('kcal'))} kcal', 'target ${target.round()}'),
_tile('Days logged', '${logged.length} of ${daysBetween(a, now) + 1}', '$onTarget within 10 % of target'),
_tile('Protein', '${r(avg('protein'))} g', 'a logged day on average'),
_tile('Fibre', '${r(avg('fiber'))} g', 'at least 25 g'),
],
),
const SizedBox(height: 10),
if (tot > 0)
Card(child: Padding(
padding: const EdgeInsets.all(12),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const Text('Where the energy came from', style: TextStyle(color: muted, fontSize: 13)),
const SizedBox(height: 6),
ClipRRect(borderRadius: BorderRadius.circular(5), child: Row(children: [
for (final m in macros) Expanded(flex: max(1, (100 * e[m]! / tot).round()),
child: Container(height: 12, color: macroColors[m])),
])),
const SizedBox(height: 4),
Text(macros.map((m) => '${m[0].toUpperCase()}${m.substring(1)} ${(100 * e[m]! / tot).round()} %').join(' · '),
style: const TextStyle(fontSize: 13)),
]),
)),
const SizedBox(height: 10),
Card(child: Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 6),
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Row(children: [
const Expanded(child: Text('Energy per day', style: TextStyle(fontWeight: FontWeight.bold))),
RangeChip(widget.range),
]),
const SizedBox(height: 8),
_Bars(days: days, a: a, z: now, target: target, onTip: (t) => setState(() => tip = t)),
SizedBox(height: 20, child: Text(tip ?? 'Tap a bar for its numbers', textAlign: TextAlign.center,
style: const TextStyle(color: muted, fontSize: 12.5))),
]),
)),
const SizedBox(height: 10),
if (top.isNotEmpty)
Card(child: Padding(
padding: const EdgeInsets.all(12),
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
const Text('Eaten most', style: TextStyle(fontWeight: FontWeight.bold)),
for (final (name, n, kcal) in top)
Padding(padding: const EdgeInsets.symmetric(vertical: 4), child: Row(children: [
Expanded(child: Text(name)),
Text('$n× · ${kcal.round()} kcal', style: const TextStyle(color: muted)),
])),
]),
)),
]);
},
),
);
Widget _tile(String label, String value, String sub) => Card(child: Padding(
padding: const EdgeInsets.all(12),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [
Text(label.toUpperCase(), style: const TextStyle(color: muted, fontSize: 11.5)),
Text(value, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
Text(sub, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: muted, fontSize: 12)),
]),
));
}
/// Bars per day (or per week, the average logged day), stacked protein / carbs / fat by energy, the rest grey.
class _Bars extends StatelessWidget {
const _Bars({required this.days, required this.a, required this.z, required this.target, required this.onTip});
final Map<String, Map<String, double>> days;
final DateTime a, z;
final double target;
final void Function(String) onTip;
List<(DateTime, List<Map<String, double>>, String)> _buckets() {
final n = daysBetween(a, z) + 1;
final out = <(DateTime, List<Map<String, double>>, String)>[];
if (n >= weeklyFrom) {
for (var s = plus(a, -(a.weekday - 1)); !s.isAfter(z); s = plus(s, 7)) {
final vs = [for (var i = 0; i < 7; i++) days[dayIso(plus(s, i))]].whereType<Map<String, double>>()
.where((v) => (v['kcal'] ?? 0) > 0).toList();
out.add((s, vs, 'Week of ${s.day} ${_mon[s.month - 1]} ${s.year}'));
}
} else {
for (var i = 0; i < n; i++) {
final x = plus(a, i), v = days[dayIso(x)];
out.add((x, v != null && (v['kcal'] ?? 0) > 0 ? [v] : [], '${x.day} ${_mon[x.month - 1]} ${x.year}'));
}
}
return out;
}
@override
Widget build(BuildContext context) {
final b = _buckets();
return LayoutBuilder(builder: (context, box) => GestureDetector(
onTapDown: (d) {
final i = (d.localPosition.dx / box.maxWidth * b.length).floor().clamp(0, b.length - 1);
final (_, vs, label) = b[i];
if (vs.isEmpty) return onTip('$label · nothing logged');
double av(String k) => vs.map((v) => v[k] ?? 0).reduce((x, y) => x + y) / vs.length;
onTip('$label · ${av('kcal').round()} kcal${vs.length > 1 ? ' (${vs.length} days)' : ''} · '
'P ${av('protein').round()} g, C ${av('carbs').round()} g, F ${av('fat').round()} g');
},
child: CustomPaint(size: Size(box.maxWidth, 170), painter: _BarsPainter(b, target)),
));
}
}
class _BarsPainter extends CustomPainter {
_BarsPainter(this.b, this.target);
final List<(DateTime, List<Map<String, double>>, String)> b;
final double target;
@override
void paint(Canvas c, Size size) {
const mb = 16.0;
final h = size.height - mb;
double avg(List<Map<String, double>> vs, String k) => vs.map((v) => v[k] ?? 0).reduce((x, y) => x + y) / vs.length;
final peak = [target * 1.25, for (final (_, vs, _) in b) if (vs.isNotEmpty) avg(vs, 'kcal')].reduce(max);
double y(double k) => h - h * k / peak;
final bw = size.width / max(1, b.length), pad = bw > 4 ? bw * 0.18 : 0.0;
for (var i = 0; i < b.length; i++) {
final (x, vs, _) = b[i];
if (vs.isNotEmpty) {
final k = avg(vs, 'kcal');
final e = {for (final m in macros) m: avg(vs, m) * kcalPerG[m]!};
final sum = e.values.reduce((p, q) => p + q);
final scale = sum > k ? k / sum : 1.0; // labels' macros can add up past the kcal
var base = 0.0;
for (final m in macros) {
final hh = e[m]! * scale;
if (hh > 0) c.drawRect(Rect.fromLTRB(i * bw + pad, y(base + hh), (i + 1) * bw - pad, y(base)), Paint()..color = macroColors[m]!);
base += hh;
}
if (k - base > 1) c.drawRect(Rect.fromLTRB(i * bw + pad, y(k), (i + 1) * bw - pad, y(base)), Paint()..color = muted);
}
final weekly = b.length > 1 && b[1].$1.difference(b[0].$1).inDays == 7;
final label = weekly ? (x.day <= 7 && (b.length <= 30 || x.month % 3 == 1) ? _mon[x.month - 1] : null)
: b.length > 35 ? (x.day == 1 ? _mon[x.month - 1] : null) : (x.weekday == 1 || b.length <= 8 ? '${x.day} ${_mon[x.month - 1]}' : null);
if (label != null) {
final tp = TextPainter(text: TextSpan(text: label, style: const TextStyle(color: muted, fontSize: 10.5)),
textDirection: TextDirection.ltr)..layout();
tp.paint(c, Offset(i * bw + bw / 2 - tp.width / 2, h + 3));
}
}
final ty = y(target);
for (var x = 0.0; x < size.width; x += 8) {
c.drawLine(Offset(x, ty), Offset(min(x + 4, size.width), ty), Paint()..color = accent..strokeWidth = 1.2);
}
}
@override
bool shouldRepaint(covariant _BarsPainter old) => true;
}