LifeMate — Personal, Family & Learning Companion
fix/study-duration-display
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:flutter/foundation.dart'; |
| 4 | import 'package:http/http.dart' as http; |
| 5 | import 'package:shared_preferences/shared_preferences.dart'; |
| 6 | |
| 7 | String normalizeApiUrl(String value, {Uri? webOrigin}) { |
| 8 | var uri = Uri.tryParse(value.trim()); |
| 9 | if (uri != null && |
| 10 | !uri.hasScheme && |
| 11 | value.trim().startsWith('/') && |
| 12 | !value.trim().startsWith('//') && |
| 13 | webOrigin != null) { |
| 14 | uri = webOrigin.resolve(value.trim()); |
| 15 | } |
| 16 | if (uri == null || |
| 17 | uri.scheme != 'https' || |
| 18 | uri.host.isEmpty || |
| 19 | uri.userInfo.isNotEmpty || |
| 20 | uri.hasQuery || |
| 21 | uri.hasFragment) { |
| 22 | throw ArgumentError( |
| 23 | 'API address must be HTTPS without credentials, query or fragment.'); |
| 24 | } |
| 25 | uri = uri.normalizePath(); |
| 26 | return uri.toString().replaceFirst(RegExp(r'/+$'), ''); |
| 27 | } |
| 28 | |
| 29 | class RuntimeConfig { |
| 30 | RuntimeConfig( |
| 31 | {String? apiBaseUrl, |
| 32 | List<String> apiFallbackUrls = const [], |
| 33 | Uri? webOrigin}) |
| 34 | : apiBaseUrl = apiBaseUrl == null || apiBaseUrl.trim().isEmpty |
| 35 | ? null |
| 36 | : normalizeApiUrl(apiBaseUrl, webOrigin: webOrigin), |
| 37 | apiFallbackUrls = List.unmodifiable(apiFallbackUrls |
| 38 | .map((url) => normalizeApiUrl(url, webOrigin: webOrigin)) |
| 39 | .toSet()) { |
| 40 | if (this.apiBaseUrl == null && this.apiFallbackUrls.isNotEmpty || |
| 41 | endpoints.length > 6) { |
| 42 | throw ArgumentError( |
| 43 | 'Choose a primary API and at most five alternatives.'); |
| 44 | } |
| 45 | } |
| 46 | final String? apiBaseUrl; |
| 47 | final List<String> apiFallbackUrls; |
| 48 | bool get isConfigured => apiBaseUrl != null; |
| 49 | List<String> get endpoints => [ |
| 50 | if (apiBaseUrl != null) apiBaseUrl!, |
| 51 | ...apiFallbackUrls.where((url) => url != apiBaseUrl) |
| 52 | ]; |
| 53 | Map<String, dynamic> toJson() => |
| 54 | {'apiBaseUrl': apiBaseUrl, 'apiFallbackUrls': apiFallbackUrls}; |
| 55 | factory RuntimeConfig.fromJson(Map<String, dynamic> json, {Uri? webOrigin}) { |
| 56 | final base = json['apiBaseUrl']; |
| 57 | final fallback = json['apiFallbackUrls'] ?? const <String>[]; |
| 58 | if ((base != null && base is! String) || |
| 59 | fallback is! List || |
| 60 | fallback.any((item) => item is! String)) { |
| 61 | throw const FormatException('Invalid runtime config.'); |
| 62 | } |
| 63 | return RuntimeConfig( |
| 64 | apiBaseUrl: base as String?, |
| 65 | apiFallbackUrls: fallback.cast<String>(), |
| 66 | webOrigin: webOrigin); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | class RuntimeConfigStore { |
| 71 | RuntimeConfigStore( |
| 72 | {http.Client? client, |
| 73 | bool? web, |
| 74 | Uri? webOrigin, |
| 75 | SharedPreferencesAsync? preferences}) |
| 76 | : _client = client ?? http.Client(), |
| 77 | _web = web ?? kIsWeb, |
| 78 | _origin = webOrigin ?? Uri.base, |
| 79 | _preferences = preferences; |
| 80 | static const key = 'lifeguide.api_endpoints.v1'; |
| 81 | final http.Client _client; |
| 82 | final bool _web; |
| 83 | final Uri _origin; |
| 84 | final SharedPreferencesAsync? _preferences; |
| 85 | SharedPreferencesAsync get preferences => |
| 86 | _preferences ?? SharedPreferencesAsync(); |
| 87 | |
| 88 | Future<RuntimeConfig> load() async { |
| 89 | if (!_web) { |
| 90 | final raw = await preferences.getString(key); |
| 91 | return raw == null |
| 92 | ? RuntimeConfig() |
| 93 | : RuntimeConfig.fromJson(jsonDecode(raw) as Map<String, dynamic>); |
| 94 | } |
| 95 | if (_origin.scheme != 'https') { |
| 96 | throw StateError('LifeGuide web startup requires HTTPS.'); |
| 97 | } |
| 98 | final uri = _origin.resolve('/config.json').replace(queryParameters: { |
| 99 | 'fresh': DateTime.now().microsecondsSinceEpoch.toString() |
| 100 | }); |
| 101 | final response = await _client.get(uri, headers: { |
| 102 | 'Cache-Control': 'no-store' |
| 103 | }).timeout(const Duration(seconds: 15)); |
| 104 | if (response.statusCode != 200 || response.body.length > 16384) { |
| 105 | throw const FormatException('Runtime configuration is unavailable.'); |
| 106 | } |
| 107 | return RuntimeConfig.fromJson( |
| 108 | jsonDecode(response.body) as Map<String, dynamic>, |
| 109 | webOrigin: _origin); |
| 110 | } |
| 111 | |
| 112 | Future<void> saveAndroid(RuntimeConfig config) async { |
| 113 | if (_web) throw StateError('Web configuration is managed by config.json.'); |
| 114 | await preferences.setString(key, jsonEncode(config.toJson())); |
| 115 | } |
| 116 | } |