reclaim/app/lib/data/hub_endpoint.dart
flemming-it e6e9c0753d feat(hub): default to live hub on startup + accept partial flow bags
Two small changes that together get Recl∆Im out of demo mode
on the very first launch after a flow.completed event lands:

  1. HubSettings.useHub defaults to true. New installs and
     post-reset launches probe the hub straight away; if the
     probe fails (no hub running, wrong port, auth denied),
     the existing graceful fallback in main.dart drops back to
     MockRepository. A user can still flip useHub off via the
     Hub-Reiter.

  2. HubRepository._parse now only requires the 'norm' key to
     consider an event 'Recl∆Im-shaped'. SKM, benefit and
     frust default to zero if absent; tier_lowest falls back to
     the cohort source's tier. This makes the partial bag
     produced by flows/durchstich-from-file.yaml — which can
     run without econ.skm_score's nested-input gymnastics —
     enough to surface a real heatmap point.

Wired together: launch the app while chain serve runs and at
least one flow.completed event is in the audit log → the
Heatmap-Tab shows that norm live, with its tier and norm-title
parsed from the actual audit-log JSON instead of Fixtures.

Signed-off-by: flemming-it <sf@flemming.it>
2026-06-18 15:43:15 +02:00

83 lines
2.5 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
/// Persisted user settings for the Ch∆In hub connection.
///
/// Stored under four SharedPreferences keys matching the Studio
/// convention (`hub.host`, `hub.port`, `hub.secure`, `hub.token`)
/// so a hub once configured stays configured across app launches.
/// Distinct from the SDK's `chain.HubEndpoint`, which is a
/// transport-level value type — [HubSettings.toEndpoint] adapts
/// in that direction.
class HubSettings {
const HubSettings({
required this.host,
required this.port,
required this.secure,
this.authToken,
this.useHub = false,
});
final String host;
final int port;
final bool secure;
/// Bearer token; read from `~/.chain/hub-auth-token` by the
/// hub-status page in Phase 1. Optional in Phase 0 because the
/// default `chain serve` runs without auth.
final String? authToken;
/// User intent: try to connect to the hub on startup. False
/// keeps the app in MockRepository mode regardless of host/port.
final bool useHub;
static const defaultHost = '127.0.0.1';
static const defaultPort = 50051;
String get scheme => secure ? 'https' : 'http';
String get url => '$scheme://$host:$port';
static const _kHost = 'hub.host';
static const _kPort = 'hub.port';
static const _kSecure = 'hub.secure';
static const _kToken = 'hub.token';
static const _kUseHub = 'hub.use';
static Future<HubSettings> load() async {
final prefs = await SharedPreferences.getInstance();
return HubSettings(
host: prefs.getString(_kHost) ?? defaultHost,
port: prefs.getInt(_kPort) ?? defaultPort,
secure: prefs.getBool(_kSecure) ?? false,
authToken: prefs.getString(_kToken),
useHub: prefs.getBool(_kUseHub) ?? true,
);
}
Future<void> save() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_kHost, host);
await prefs.setInt(_kPort, port);
await prefs.setBool(_kSecure, secure);
await prefs.setBool(_kUseHub, useHub);
if (authToken != null) {
await prefs.setString(_kToken, authToken!);
} else {
await prefs.remove(_kToken);
}
}
HubSettings copyWith({
String? host,
int? port,
bool? secure,
String? authToken,
bool? useHub,
}) =>
HubSettings(
host: host ?? this.host,
port: port ?? this.port,
secure: secure ?? this.secure,
authToken: authToken ?? this.authToken,
useHub: useHub ?? this.useHub,
);
}