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

@ -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);