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 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) ?? false, ); } Future 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, ); }