Settings sync between the dashboard and the app

- Dashboard (settingsync.py): the plugins' settings go into a synced
  settings.db, one row per setting (the row id is the setting's key, so the
  phone and the dashboard edit the same row; the later change wins) plus each
  field as the plugin declares it. Fields marked "local" (addresses, paths on
  one machine) and the plugins' on / off stay out. Values set on another
  device are checked as the Settings page does before they're saved; a
  refused one is written back with the dashboard's value.
- App 1.1.7: a Settings page (the gear on every tab) built from those fields,
  synced both ways. Food targets, the cycle, the work day and the body profile
  read the synced values; with a dashboard that doesn't sync settings yet,
  they still come from its Health summaries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 16:30:00 +02:00
parent 98b110dd56
commit 820aca7d92
25 changed files with 606 additions and 41 deletions

View file

@ -7,6 +7,7 @@
// note, source} — imported ones have id "vital:<t>", ones typed here a random id.
import 'dart:math';
import '../app/prefs.dart';
import '../sync/store.dart';
import 'cycle_model.dart' show iso, parseDay, daysBetween, today;
import 'summaries.dart';
@ -27,9 +28,18 @@ const fields = {
};
const fatDayFields = {'weight', 'waist', 'hip', 'neck', 'fat_scale'};
/// The values set on the dashboard (Settings ▸ Health), from its person summary (profileOf).
/// The dashboard's Settings ▸ Health (synced: prefs.dart); until they've come, its person summary (profileOf).
class Profile {
const Profile({this.sex = female, this.birth, this.height});
static Profile of(SyncStore store) {
final p = Prefs(store), s = profileOf(store);
final b = DateTime.tryParse(p.get<String>('health.birth_date', ''));
final h = p.number('health.height', 0).toDouble();
return Profile(sex: p.get<String>('health.sex', s.sex), birth: b == null ? s.birth : DateTime.utc(b.year, b.month, b.day),
height: h > 0 ? h : s.height);
}
final String sex;
final DateTime? birth;
final double? height;

View file

@ -47,13 +47,13 @@ class _BodyPageState extends State<BodyPage> {
backgroundColor: accent,
foregroundColor: bg,
tooltip: 'Add measurements',
onPressed: () => _edit(BodyView(widget.sync.store, profileOf(widget.sync.store)), null),
onPressed: () => _edit(BodyView(widget.sync.store, Profile.of(widget.sync.store)), null),
child: const Icon(Icons.add),
),
body: ListenableBuilder(
listenable: Listenable.merge([widget.sync, widget.range]),
builder: (context, _) {
final v = BodyView(widget.sync.store, profileOf(widget.sync.store));
final v = BodyView(widget.sync.store, Profile.of(widget.sync.store));
if (v.rows.isEmpty) {
return const Center(child: Text('No measurements yet — + to add the first.', style: TextStyle(color: muted)));
}

View file

@ -5,6 +5,7 @@
// pain_len, tags "a,b", note, source, updated}. Days are UTC dates here (no DST surprises).
import 'dart:math';
import '../app/prefs.dart';
import '../sync/store.dart';
const history = 12; // cycles averaged (at most)
@ -58,10 +59,17 @@ String iso(DateTime d) => d.toIso8601String().substring(0, 10);
int daysBetween(DateTime a, DateTime b) => b.difference(a).inDays; // b - a
DateTime plus(DateTime d, int n) => d.add(Duration(days: n));
/// The dashboard's Settings ▸ Cycle (synced: prefs.dart); its defaults until they've come.
class CycleSettings {
const CycleSettings({this.cycleLength = 28, this.periodLength = 5, this.showFertile = true});
const CycleSettings({this.cycleLength = 28, this.periodLength = 5, this.showFertile = false});
final int cycleLength, periodLength;
final bool showFertile;
static CycleSettings of(SyncStore store) {
final p = Prefs(store);
return CycleSettings(cycleLength: p.number('cycle.cycle_length', 28).round(),
periodLength: p.number('cycle.period_length', 5).round(), showFertile: p.get<bool>('cycle.show_fertile', false));
}
}
class DayLog {

View file

@ -25,7 +25,7 @@ class CyclePage extends StatefulWidget {
class _CyclePageState extends State<CyclePage> {
int offset = 0; // months from this one
final settings = const CycleSettings(); // TODO: from synced settings once those sync (Pi: 28 / 5 / fertile on)
CycleSettings get settings => CycleSettings.of(widget.sync.store); // the dashboard's, synced
void _save(DateTime d, Map<String, dynamic> fields) {
saveDay(widget.sync.store, d, fields);

View file

@ -18,8 +18,9 @@ import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqlite3/sqlite3.dart';
import '../app/prefs.dart';
import '../sync/store.dart';
import 'body_model.dart' show BodyView, bmrMifflin, female, profileOf;
import 'body_model.dart' show BodyView, Profile, bmrMifflin, female;
import 'cycle_model.dart' show iso;
import 'sugar.dart' show fold;
import 'summaries.dart';
@ -33,7 +34,10 @@ const foodCols = ['kcal', 'protein', 'carbs', 'sugar', 'fiber', 'fat', 'sat_fat'
const reuseSources = {'manual', 'parsed'}; // entries worth offering again (imports are too rough)
const generic = {'tbsp (15 ml)': 15.0, 'tsp (5 ml)': 5.0}; // spoons every food can be measured in (ml as g)
/// The dashboard's Settings ▸ Food, as its Health summary brings it (meta:food); its defaults until then.
/// Macro presets: % of the energy from protein / carbs / fat (views.PRESETS; "custom" = the custom fields).
const macroPresets = {'balanced': (20, 50, 30), 'high_protein': (30, 40, 30), 'mediterranean': (20, 45, 35), 'lower_carb': (30, 25, 45)};
/// The dashboard's Settings ▸ Food (synced: prefs.dart); its defaults until they've come.
class FoodSettings {
const FoodSettings({this.goalRate = 0, this.split = (20, 50, 30), this.kcalTarget = 0, this.fiberMin = 25,
this.sugarMax = 50, this.sodiumMax = 2000});
@ -41,7 +45,21 @@ class FoodSettings {
final (int, int, int) split; // protein / carbs / fat % of the energy
final int kcalTarget, fiberMin, sugarMax, sodiumMax;
/// views.split: the preset's split, or the custom one.
static FoodSettings of(SyncStore store) {
if (store.row(tSetting, 'food.macro_preset') == null) return _summary(store); // a dashboard without settings sync
final p = Prefs(store);
int n(String k, int d) => p.number('food.$k', d).round();
final preset = p.get<String>('food.macro_preset', 'balanced');
final split = preset == 'custom'
? (n('custom_protein', 20), n('custom_carbs', 50), n('custom_fat', 30))
: macroPresets[preset] ?? macroPresets['balanced']!;
return FoodSettings(goalRate: p.number('food.goal_rate', 0).toDouble(), split: split, kcalTarget: n('kcal_target', 0),
fiberMin: n('fiber_min', 25), sugarMax: n('sugar_max', 50), sodiumMax: n('sodium_max', 2000));
}
/// Older dashboards: Settings ▸ Food as its Health summary brings it (meta:food).
static FoodSettings _summary(SyncStore store) {
final m = Summaries(store).meta('food');
if (m == null) return const FoodSettings();
final sp = (m['split'] as List?)?.map((v) => (v as num).toInt()).toList();
@ -494,7 +512,7 @@ class Plan {
/// averaged over 28 days with step data — at least 7) minus the weight goal; resting × 1.4 until then.
Plan plan(SyncStore store, FoodSettings s) {
final e = Summaries(store).meta('energy') ?? {};
final b = BodyView(store, profileOf(store));
final b = BodyView(store, Profile.of(store));
final w = (e['weight'] as num?)?.toDouble() ?? b.weight;
final bmr = (e['bmr'] as num?)?.toDouble() ?? bmrMifflin(w, b.p.height, b.p.sex, b.age);
if (s.kcalTarget > 0) return Plan(s.kcalTarget, null, 'set by hand', 0, false);

View file

@ -40,7 +40,7 @@ class FoodPage extends StatefulWidget {
class _FoodPageState extends State<FoodPage> {
DateTime d = today();
FoodDb? fdb;
FoodSettings get s => FoodSettings.of(widget.sync.store); // Settings ▸ Food on the dashboard
FoodSettings get s => FoodSettings.of(widget.sync.store); // the dashboard's, synced
@override
void initState() {

View file

@ -47,7 +47,7 @@ class _OverviewPageState extends State<OverviewPage> {
final last = nights.isNotEmpty && nights.last.day == now;
final steps = sm.day(iso(now))?['steps'] as num?;
final week = [for (var k = 1; k <= 7; k++) sm.day(iso(plus(now, -k)))?['steps'] as num?].whereType<num>().toList();
final cv = CycleView(widget.sync.store, const CycleSettings());
final cv = CycleView(widget.sync.store, CycleSettings.of(widget.sync.store));
var (head, sub) = cycleStatus(cv);
if (cv.status == Status.wait) head = cv.daysToNext == 0 ? 'today' : 'in ${cv.daysToNext} ${cv.daysToNext == 1 ? 'day' : 'days'}';
if (cv.status == Status.period) head = 'day ${cv.cycleDay}';