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:
parent
98b110dd56
commit
820aca7d92
25 changed files with 606 additions and 41 deletions
66
app/test/prefs_test.dart
Normal file
66
app/test/prefs_test.dart
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// The dashboard's settings as they sync (prefs.dart = settingsync.py): values read with the dashboard's
|
||||
// defaults until they've come, the food split from its preset or the custom fields, the fields' checks.
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dash_app/app/prefs.dart';
|
||||
import 'package:dash_app/health/cycle_model.dart';
|
||||
import 'package:dash_app/health/food_model.dart';
|
||||
import 'package:dash_app/sync/store.dart';
|
||||
import 'package:dash_app/work/work_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
final store = SyncStore('${Directory.systemTemp.createTempSync('prefs').path}/d.db', 'test00000000');
|
||||
void fromDashboard(String k, Object? v) => store.put(tSetting, k, {'k': k, 'v': jsonEncode(v)});
|
||||
|
||||
test('defaults until the dashboard\'s values come', () {
|
||||
final f = FoodSettings.of(store);
|
||||
expect(f.split, (20, 50, 30));
|
||||
expect(f.goalRate, 0);
|
||||
expect(CycleSettings.of(store).cycleLength, 28);
|
||||
expect(WorkSettings.of(store).dayStart, 7);
|
||||
});
|
||||
|
||||
test('the dashboard\'s values: presets, the custom split, numbers as stored', () {
|
||||
fromDashboard('food.macro_preset', 'high_protein');
|
||||
fromDashboard('food.goal_rate', 0.25);
|
||||
fromDashboard('cycle.cycle_length', 30.0);
|
||||
fromDashboard('workload.currency', 'RON');
|
||||
expect(FoodSettings.of(store).split, (30, 40, 30));
|
||||
expect(FoodSettings.of(store).goalRate, 0.25);
|
||||
expect(CycleSettings.of(store).cycleLength, 30);
|
||||
expect(WorkSettings.of(store).currency, 'RON');
|
||||
fromDashboard('food.macro_preset', 'custom');
|
||||
fromDashboard('food.custom_protein', 35);
|
||||
fromDashboard('food.custom_carbs', 35);
|
||||
fromDashboard('food.custom_fat', 30);
|
||||
expect(FoodSettings.of(store).split, (35, 35, 30));
|
||||
});
|
||||
|
||||
test('set writes only a change', () {
|
||||
final p = Prefs(store);
|
||||
p.set('food.sugar_max', 40);
|
||||
final h = store.rows(tSetting).firstWhere((r) => r['_u'] == 'food.sugar_max')['_h'];
|
||||
p.set('food.sugar_max', 40);
|
||||
expect(store.rows(tSetting).firstWhere((r) => r['_u'] == 'food.sugar_max')['_h'], h);
|
||||
expect(FoodSettings.of(store).sugarMax, 40);
|
||||
});
|
||||
|
||||
test('fields: sections in order, checks as the dashboard', () {
|
||||
void field(String k, int pos, Map<String, Object?> spec) =>
|
||||
store.put(tField, k, {'k': k, 'plugin': k.split('.').first, 'title': k.split('.').first, 'pos': pos, 'spec': jsonEncode(spec)});
|
||||
field('cycle.cycle_length', 0, {'key': 'cycle_length', 'type': 'number', 'min': 15, 'max': 60, 'integer': true});
|
||||
field('food.goal_rate', 1, {'key': 'goal_rate', 'type': 'select', 'options': [[0.0, 'Keep'], [0.25, 'Lose 0.25 %']]});
|
||||
field('workload.currency', 2, {'key': 'currency', 'type': 'text', 'max': 3, 'pattern': '[A-Za-z]{3}'});
|
||||
final secs = Prefs(store).sections();
|
||||
expect([for (final s in secs) s.$1], ['cycle', 'food', 'workload']);
|
||||
final cl = secs[0].$3.single, gr = secs[1].$3.single, cur = secs[2].$3.single;
|
||||
expect(cl.check('29.6'), (29, null));
|
||||
expect(cl.check('80').$2, 'Must be between 15 and 60.');
|
||||
expect(gr.check(0.25), (0.25, null));
|
||||
expect(gr.check('1').$2, 'Pick one of the options.');
|
||||
expect(cur.check('eu1').$2, isNotNull);
|
||||
expect(cur.check('EUR'), ('EUR', null));
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue