LifeMate — Personal, Family & Learning Companion
docs/owner-guide-and-delivery-plan
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 'package:flutter/foundation.dart'; |
| 2 | import 'api.dart'; |
| 3 | import 'runtime_config.dart'; |
| 4 | import 'session_store.dart'; |
| 5 | import 'web_instance.dart'; |
| 6 | |
| 7 | class RuntimeController extends ChangeNotifier { |
| 8 | RuntimeController( |
| 9 | {RuntimeConfigStore? configStore, |
| 10 | SessionStore? sessionStore, |
| 11 | this.onScopeDiscarded, |
| 12 | this.onScopeQuarantined, |
| 13 | HttpIdentityApi Function(RuntimeConfig)? apiFactory}) |
| 14 | : _configStore = configStore ?? RuntimeConfigStore(), |
| 15 | _sessionStore = sessionStore ?? createSessionStore(), |
| 16 | _apiFactory = apiFactory; |
| 17 | final RuntimeConfigStore _configStore; |
| 18 | final SessionStore _sessionStore; |
| 19 | final Future<void> Function(String)? onScopeDiscarded; |
| 20 | final Future<void> Function(String)? onScopeQuarantined; |
| 21 | final HttpIdentityApi Function(RuntimeConfig)? _apiFactory; |
| 22 | RuntimeConfig config = RuntimeConfig(); |
| 23 | HttpIdentityApi? api; |
| 24 | Object? error; |
| 25 | bool initialized = false; |
| 26 | bool _disposed = false; |
| 27 | int _generation = 0; |
| 28 | String? get cacheNamespace => api?.cacheNamespace; |
| 29 | void _notify() { |
| 30 | if (!_disposed) notifyListeners(); |
| 31 | } |
| 32 | |
| 33 | HttpIdentityApi _createApi(RuntimeConfig config) { |
| 34 | final next = _apiFactory?.call(config) ?? |
| 35 | HttpIdentityApi( |
| 36 | baseUrl: config.apiBaseUrl!, sessionStore: _sessionStore); |
| 37 | next.onSessionChanged = _notify; |
| 38 | next.onScopeDiscarded = onScopeDiscarded; |
| 39 | next.onScopeQuarantined = onScopeQuarantined; |
| 40 | return next; |
| 41 | } |
| 42 | |
| 43 | Future<void> initialize() async { |
| 44 | if (_disposed) return; |
| 45 | final generation = ++_generation; |
| 46 | error = null; |
| 47 | api?.close(); |
| 48 | api = null; |
| 49 | try { |
| 50 | requireWebInstanceOwnership(); |
| 51 | final loaded = await _configStore.load(); |
| 52 | if (_disposed || generation != _generation) return; |
| 53 | config = loaded; |
| 54 | if (config.isConfigured) { |
| 55 | api = _createApi(config); |
| 56 | await api!.restoreSession(); |
| 57 | } |
| 58 | } catch (failure) { |
| 59 | if (_disposed || generation != _generation) return; |
| 60 | error = failure; |
| 61 | } |
| 62 | if (_disposed || generation != _generation) return; |
| 63 | initialized = true; |
| 64 | _notify(); |
| 65 | } |
| 66 | |
| 67 | /// Server switches invalidate the session before any new endpoint is used. |
| 68 | /// Caller discards its previous user/endpoint cache and queued mutations. |
| 69 | Future<void> updateEndpoints(RuntimeConfig next) async { |
| 70 | requireWebInstanceOwnership(); |
| 71 | if (_disposed) return; |
| 72 | _generation++; |
| 73 | try { |
| 74 | await api?.logout(revoke: false); |
| 75 | await _sessionStore.clear(); |
| 76 | api?.close(); |
| 77 | api = null; |
| 78 | await _configStore.saveAndroid(next); |
| 79 | config = next; |
| 80 | error = null; |
| 81 | if (next.isConfigured && !_disposed) { |
| 82 | api = _createApi(next); |
| 83 | } |
| 84 | } catch (failure) { |
| 85 | error = failure; |
| 86 | rethrow; |
| 87 | } finally { |
| 88 | _notify(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | Future<void> signOut() async { |
| 93 | requireWebInstanceOwnership(); |
| 94 | try { |
| 95 | await api?.logout(); |
| 96 | error = null; |
| 97 | } catch (failure) { |
| 98 | error = failure; |
| 99 | rethrow; |
| 100 | } finally { |
| 101 | _notify(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @override |
| 106 | void dispose() { |
| 107 | _disposed = true; |
| 108 | _generation++; |
| 109 | api?.close(); |
| 110 | super.dispose(); |
| 111 | } |
| 112 | } |