LifeMate — Personal, Family & Learning Companion
feat/standalone-weekly-planner
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:async'; |
| 2 | import 'package:flutter/foundation.dart'; |
| 3 | import 'package:flutter/services.dart'; |
| 4 | import 'package:flutter/widgets.dart' show StringCharacters; |
| 5 | |
| 6 | final androidLocalReminders = AndroidLocalReminderPort(); |
| 7 | |
| 8 | final class LocalReminder { |
| 9 | const LocalReminder( |
| 10 | {required this.id, |
| 11 | required this.planItemId, |
| 12 | required this.scheduledAt, |
| 13 | required this.companionName, |
| 14 | required this.ownerUserId, |
| 15 | required this.serverAcknowledged, |
| 16 | this.pendingMutation = false}); |
| 17 | final String id, planItemId, companionName, ownerUserId; |
| 18 | final DateTime scheduledAt; |
| 19 | final bool serverAcknowledged, pendingMutation; |
| 20 | } |
| 21 | |
| 22 | final class LocalReminderStatus { |
| 23 | const LocalReminderStatus( |
| 24 | {required this.supported, |
| 25 | required this.permissionGranted, |
| 26 | required this.active, |
| 27 | required this.scheduledCount, |
| 28 | this.timing = 'approximate', |
| 29 | this.activeScope, |
| 30 | this.validUntil, |
| 31 | this.exactAlarmSupported = false, |
| 32 | this.canScheduleExactAlarms = false}); |
| 33 | final bool supported, |
| 34 | permissionGranted, |
| 35 | active, |
| 36 | exactAlarmSupported, |
| 37 | canScheduleExactAlarms; |
| 38 | final int scheduledCount; |
| 39 | final String timing; |
| 40 | final String? activeScope; |
| 41 | final DateTime? validUntil; |
| 42 | } |
| 43 | |
| 44 | abstract interface class LocalReminderPort { |
| 45 | Future<void> activate(String scope, {required DateTime validUntil}); |
| 46 | Future<void> replace(String scope, List<LocalReminder> reminders, |
| 47 | {required String currentUserId}); |
| 48 | Future<void> suppressPlanItems(String scope, Set<String> ids); |
| 49 | Future<void> deactivate(); |
| 50 | Future<LocalReminderStatus> status(); |
| 51 | Future<LocalReminderStatus> requestPermission(); |
| 52 | Future<LocalReminderStatus> requestExactAlarmAccess(); |
| 53 | } |
| 54 | |
| 55 | /// One shared lifecycle boundary. Native storage contains no task text or tokens. |
| 56 | /// [LocalReminderStatus.active] means device opt-in, even after lease expiry. |
| 57 | final class AndroidLocalReminderPort implements LocalReminderPort { |
| 58 | AndroidLocalReminderPort({MethodChannel? channel, DateTime Function()? now}) |
| 59 | : _channel = channel ?? const MethodChannel('lifeguide/local_reminders'), |
| 60 | _now = now ?? DateTime.now; |
| 61 | final MethodChannel _channel; |
| 62 | final DateTime Function() _now; |
| 63 | Future<void>? _tail; |
| 64 | int _generation = 0; |
| 65 | String? _scope; |
| 66 | DateTime? _validUntil; |
| 67 | bool get _native => |
| 68 | !kIsWeb && defaultTargetPlatform == TargetPlatform.android; |
| 69 | static final _uuid = RegExp( |
| 70 | r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'); |
| 71 | static final _controls = |
| 72 | RegExp(r'[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]'); |
| 73 | static const _unsupported = LocalReminderStatus( |
| 74 | supported: false, |
| 75 | permissionGranted: false, |
| 76 | active: false, |
| 77 | scheduledCount: 0); |
| 78 | Future<void> _serial(Future<void> Function() work) async { |
| 79 | final previous = _tail; |
| 80 | final release = Completer<void>(); |
| 81 | _tail = release.future; |
| 82 | if (previous != null) await previous; |
| 83 | try { |
| 84 | await work(); |
| 85 | } finally { |
| 86 | if (identical(_tail, release.future)) _tail = null; |
| 87 | release.complete(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Future<void> _write(String method, [Map<String, Object>? arguments]) async { |
| 92 | if (await _channel |
| 93 | .invokeMethod<bool>(method, arguments) |
| 94 | .timeout(const Duration(seconds: 5)) != |
| 95 | true) { |
| 96 | throw StateError('Native reminder operation failed.'); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @override |
| 101 | Future<void> activate(String scope, {required DateTime validUntil}) async { |
| 102 | if (!_native) return; |
| 103 | final now = _now().toUtc(); |
| 104 | if (scope.isEmpty || |
| 105 | scope.length > 1024 || |
| 106 | _controls.hasMatch(scope) || |
| 107 | !validUntil.isUtc || |
| 108 | !validUntil.isAfter(now) || |
| 109 | validUntil.difference(now) > const Duration(hours: 24)) { |
| 110 | throw ArgumentError( |
| 111 | 'A future UTC lease of at most 24 hours is required.'); |
| 112 | } |
| 113 | final generation = ++_generation; |
| 114 | _scope = scope; |
| 115 | _validUntil = validUntil; |
| 116 | await _serial(() async { |
| 117 | if (generation != _generation) return; |
| 118 | try { |
| 119 | await _write('activate', { |
| 120 | 'scope': scope, |
| 121 | 'validUntilEpochMs': validUntil.millisecondsSinceEpoch |
| 122 | }); |
| 123 | } catch (_) { |
| 124 | if (generation == _generation) { |
| 125 | _scope = null; |
| 126 | _validUntil = null; |
| 127 | } |
| 128 | rethrow; |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @override |
| 134 | Future<void> replace(String scope, List<LocalReminder> reminders, |
| 135 | {required String currentUserId}) async { |
| 136 | if (!_native) return; |
| 137 | final until = _validUntil; |
| 138 | if (_scope != scope || until == null || !until.isAfter(_now().toUtc())) { |
| 139 | throw StateError('Reminder scope or lease no longer matches.'); |
| 140 | } |
| 141 | if (!_uuid.hasMatch(currentUserId) || reminders.length > 128) { |
| 142 | throw ArgumentError('Invalid owner or reminder count.'); |
| 143 | } |
| 144 | final ids = <String>{}, payload = <Map<String, Object>>[]; |
| 145 | final now = _now().toUtc(); |
| 146 | for (final row in reminders) { |
| 147 | if (!_uuid.hasMatch(row.id) || |
| 148 | !_uuid.hasMatch(row.planItemId) || |
| 149 | !ids.add(row.id) || |
| 150 | row.ownerUserId != currentUserId || |
| 151 | !row.serverAcknowledged || |
| 152 | row.pendingMutation || |
| 153 | !row.scheduledAt.isUtc || |
| 154 | row.companionName.trim().isEmpty || |
| 155 | row.companionName.characters.length > 40 || |
| 156 | row.companionName.length > 4096 || |
| 157 | _controls.hasMatch(row.companionName)) { |
| 158 | throw ArgumentError( |
| 159 | 'Only acknowledged owner records may schedule reminders.'); |
| 160 | } |
| 161 | if (!row.scheduledAt.isAfter(now) || row.scheduledAt.isAfter(until)) { |
| 162 | continue; |
| 163 | } |
| 164 | payload.add({ |
| 165 | 'id': row.id, |
| 166 | 'planItemId': row.planItemId, |
| 167 | 'scheduledAtEpochMs': row.scheduledAt.millisecondsSinceEpoch, |
| 168 | 'companionName': row.companionName.trim() |
| 169 | }); |
| 170 | } |
| 171 | final generation = _generation; |
| 172 | await _serial(() async { |
| 173 | if (generation != _generation || _scope != scope) return; |
| 174 | await _write('replace', {'scope': scope, 'reminders': payload}); |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | @override |
| 179 | Future<void> suppressPlanItems(String scope, Set<String> ids) async { |
| 180 | if (!_native || ids.isEmpty) return; |
| 181 | if (_scope != null && _scope != scope) { |
| 182 | throw StateError('Reminder scope no longer matches.'); |
| 183 | } |
| 184 | if (ids.length > 128 || ids.any((id) => !_uuid.hasMatch(id))) { |
| 185 | throw ArgumentError('Invalid plan IDs.'); |
| 186 | } |
| 187 | final generation = _generation; |
| 188 | await _serial(() async { |
| 189 | if (generation != _generation) return; |
| 190 | try { |
| 191 | await _write( |
| 192 | 'suppressPlanItems', {'scope': scope, 'planItemIds': ids.toList()}); |
| 193 | } on MissingPluginException {/* Older builds have no native reminders. */} |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | @override |
| 198 | Future<void> deactivate() async { |
| 199 | ++_generation; |
| 200 | _scope = null; |
| 201 | _validUntil = null; |
| 202 | if (!_native) return; |
| 203 | await _serial(() async { |
| 204 | try { |
| 205 | await _write('deactivate'); |
| 206 | } on MissingPluginException {/* Older builds have no native reminders. */} |
| 207 | }); |
| 208 | } |
| 209 | |
| 210 | Future<LocalReminderStatus> _status(String method) async { |
| 211 | if (!_native) return _unsupported; |
| 212 | try { |
| 213 | final raw = await _channel |
| 214 | .invokeMapMethod<String, dynamic>(method) |
| 215 | .timeout(Duration(seconds: method == 'requestPermission' ? 90 : 5)); |
| 216 | if (raw == null || |
| 217 | raw['supported'] != true || |
| 218 | raw['permissionGranted'] is! bool || |
| 219 | raw['active'] is! bool || |
| 220 | raw['scheduledCount'] is! int || |
| 221 | (raw['scheduledCount'] as int) < 0 || |
| 222 | !['exact', 'approximate'].contains(raw['timing'])) { |
| 223 | throw StateError('Invalid native reminder status.'); |
| 224 | } |
| 225 | return LocalReminderStatus( |
| 226 | supported: true, |
| 227 | permissionGranted: raw['permissionGranted'] == true, |
| 228 | active: raw['active'] == true, |
| 229 | scheduledCount: raw['scheduledCount'] as int, |
| 230 | timing: raw['timing'] as String, |
| 231 | activeScope: raw['activeScope'] is String |
| 232 | ? raw['activeScope'] as String |
| 233 | : null, |
| 234 | validUntil: raw['validUntilEpochMs'] is int |
| 235 | ? DateTime.fromMillisecondsSinceEpoch( |
| 236 | raw['validUntilEpochMs'] as int, |
| 237 | isUtc: true) |
| 238 | : null, |
| 239 | exactAlarmSupported: raw['exactAlarmSupported'] == true, |
| 240 | canScheduleExactAlarms: raw['canScheduleExactAlarms'] == true); |
| 241 | } on MissingPluginException { |
| 242 | return _unsupported; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | @override |
| 247 | Future<LocalReminderStatus> status() => _status('status'); |
| 248 | @override |
| 249 | Future<LocalReminderStatus> requestPermission() => |
| 250 | _status('requestPermission'); |
| 251 | @override |
| 252 | Future<LocalReminderStatus> requestExactAlarmAccess() => |
| 253 | _status('requestExactAlarmAccess'); |
| 254 | } |