LifeMate — Personal, Family & Learning Companion
feat/weekly-planner-android
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/material.dart'; |
| 4 | |
| 5 | import 'api.dart'; |
| 6 | import 'offline_store.dart'; |
| 7 | import 'plan_sync.dart' show newMutationId; |
| 8 | import 'education_ui.dart' show learningError; |
| 9 | |
| 10 | const feedbackCategories = { |
| 11 | 'inaccurate': 'اشتباه آموزشی', |
| 12 | 'edition_mismatch': 'تفاوت با کتاب درسی', |
| 13 | 'broken_video': 'اشکال پخش ویدیو', |
| 14 | 'unclear': 'توضیح نامفهوم', |
| 15 | 'topic_request': 'پیشنهاد موضوع آموزشی', |
| 16 | }; |
| 17 | |
| 18 | const feedbackStatusLabels = { |
| 19 | 'received': 'دریافتشده', |
| 20 | 'triaged': 'بررسی اولیه', |
| 21 | 'planned': 'در برنامه بررسی و توسعه', |
| 22 | 'released': 'انتشار ثبتشده', |
| 23 | 'declined': 'پذیرفتهنشده', |
| 24 | }; |
| 25 | |
| 26 | /// Explicitly chosen feedback only; never extracts private conversations. |
| 27 | class LearningFeedbackSync { |
| 28 | LearningFeedbackSync(this.api, {OfflineStore? store}) |
| 29 | : store = store ?? OfflineStore.forApi(api); |
| 30 | final HttpIdentityApi api; |
| 31 | final OfflineStore store; |
| 32 | |
| 33 | Future<Map<String, dynamic>> submit(Map<String, dynamic> body) async { |
| 34 | final frozen = |
| 35 | Map<String, dynamic>.from(jsonDecode(jsonEncode(body)) as Map); |
| 36 | final mutationId = frozen['mutationId'] as String; |
| 37 | await store.enqueue({ |
| 38 | 'id': mutationId, |
| 39 | 'entityId': mutationId, |
| 40 | 'entityType': 'learning_feedback', |
| 41 | 'operation': 'create', |
| 42 | 'payload': frozen, |
| 43 | }); |
| 44 | try { |
| 45 | return await _send(mutationId, frozen); |
| 46 | } catch (e) { |
| 47 | if (isOfflineFailure(e)) return {'queued': true}; |
| 48 | rethrow; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Future<Map<String, dynamic>> _send( |
| 53 | String mutationId, Map<String, dynamic> body) async { |
| 54 | if (api.cacheNamespace != store.scope) { |
| 55 | throw const ApiException(401, 'cache_scope_closed'); |
| 56 | } |
| 57 | await store.readQueue(); |
| 58 | final result = |
| 59 | await api.requestJson('POST', '/v1/education/feedback', body: body); |
| 60 | if (api.cacheNamespace != store.scope) { |
| 61 | throw const ApiException(401, 'cache_scope_closed'); |
| 62 | } |
| 63 | final acknowledgement = result['acknowledgement']; |
| 64 | final feedback = result['feedback']; |
| 65 | final uuid = |
| 66 | RegExp(r'^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$'); |
| 67 | bool date(dynamic value) => |
| 68 | value is String && DateTime.tryParse(value) != null; |
| 69 | if (acknowledgement is! Map || |
| 70 | feedback is! Map || |
| 71 | acknowledgement['mutationId'] != mutationId || |
| 72 | !['accepted', 'already_applied'].contains(acknowledgement['status']) || |
| 73 | feedback['mutationId'] != mutationId || |
| 74 | feedback['id'] is! String || |
| 75 | !uuid.hasMatch(feedback['id'] as String) || |
| 76 | feedback['unitId'] != body['unitId'] || |
| 77 | feedback['category'] != body['category'] || |
| 78 | feedback['version'] is! int || |
| 79 | feedback['version'] < 1 || |
| 80 | !['received', 'triaged', 'planned', 'released', 'declined'] |
| 81 | .contains(feedback['status']) || |
| 82 | (feedback['withdrawnAt'] == null |
| 83 | ? (feedback['summary'] != body['summary'] || |
| 84 | feedback['shareWithContentTeam'] != true) |
| 85 | : (!date(feedback['withdrawnAt']) || |
| 86 | feedback['summary'] != null || |
| 87 | feedback['shareWithContentTeam'] != false)) || |
| 88 | acknowledgement['feedbackId'] != feedback['id'] || |
| 89 | !date(feedback['createdAt']) || |
| 90 | !date(feedback['updatedAt'])) { |
| 91 | throw const ApiException(502, 'invalid_sync_acknowledgement'); |
| 92 | } |
| 93 | // The chosen summary remains in the durable queue until acknowledged; |
| 94 | // do not retain an extra offline copy after server acceptance/withdrawal. |
| 95 | await store.acknowledge(mutationId, |
| 96 | acceptedAt: feedback['createdAt'] as String); |
| 97 | return {'queued': false, 'feedback': feedback}; |
| 98 | } |
| 99 | |
| 100 | Future<void> flush() async { |
| 101 | for (final pending in await store.readQueue()) { |
| 102 | if (pending['entityType'] != 'learning_feedback' || |
| 103 | pending['queueStatus'] != 'queued') { |
| 104 | continue; |
| 105 | } |
| 106 | try { |
| 107 | await _send(pending['id'] as String, |
| 108 | Map<String, dynamic>.from(pending['payload'] as Map)); |
| 109 | } catch (e) { |
| 110 | if (isOfflineFailure(e)) return; |
| 111 | rethrow; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | class LearningFeedbackPage extends StatefulWidget { |
| 118 | const LearningFeedbackPage({super.key, required this.api, this.unitId}); |
| 119 | final HttpIdentityApi api; |
| 120 | final String? unitId; |
| 121 | @override |
| 122 | State<LearningFeedbackPage> createState() => _LearningFeedbackPageState(); |
| 123 | } |
| 124 | |
| 125 | class _LearningFeedbackPageState extends State<LearningFeedbackPage> { |
| 126 | final summary = TextEditingController(); |
| 127 | String category = 'topic_request'; |
| 128 | bool consent = false, busy = false; |
| 129 | String? message; |
| 130 | Map<String, dynamic>? pendingBody; |
| 131 | List<Map<String, dynamic>> history = []; |
| 132 | int historyEpoch = 0; |
| 133 | late final LearningFeedbackSync sync; |
| 134 | @override |
| 135 | void initState() { |
| 136 | super.initState(); |
| 137 | sync = LearningFeedbackSync(widget.api); |
| 138 | restoreDraft(); |
| 139 | loadHistory(); |
| 140 | } |
| 141 | |
| 142 | Future<void> loadHistory() async { |
| 143 | final epoch = ++historyEpoch; |
| 144 | try { |
| 145 | final result = |
| 146 | await widget.api.requestJson('GET', '/v1/me/education-feedback'); |
| 147 | if (result['feedback'] is! List) { |
| 148 | throw const ApiException(502, 'invalid_api_response'); |
| 149 | } |
| 150 | if (mounted && |
| 151 | epoch == historyEpoch && |
| 152 | widget.api.cacheNamespace == sync.store.scope) { |
| 153 | setState(() => history = (result['feedback'] as List) |
| 154 | .map((row) => Map<String, dynamic>.from(row as Map)) |
| 155 | .toList()); |
| 156 | } |
| 157 | } catch (e) { |
| 158 | if (mounted && epoch == historyEpoch) { |
| 159 | setState(() { |
| 160 | if (e is ApiException && |
| 161 | (e.statusCode == 401 || e.statusCode == 403)) { |
| 162 | history = []; |
| 163 | } |
| 164 | message = learningError(e); |
| 165 | }); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Future<void> withdraw(String id) async { |
| 171 | if (busy) return; |
| 172 | ++historyEpoch; // Earlier reads must not resurrect a withdrawn summary. |
| 173 | setState(() => busy = true); |
| 174 | try { |
| 175 | await widget.api.requestJson( |
| 176 | 'DELETE', '/v1/me/education-feedback/${Uri.encodeComponent(id)}'); |
| 177 | if (mounted) { |
| 178 | setState(() => history.removeWhere((row) => row['id'] == id)); |
| 179 | } |
| 180 | // Clear any acknowledged feedback cache from an earlier test build. |
| 181 | await sync.store.writeList('education_feedback', []); |
| 182 | if (mounted) await loadHistory(); |
| 183 | } catch (e) { |
| 184 | if (mounted) setState(() => message = learningError(e)); |
| 185 | } finally { |
| 186 | if (mounted) setState(() => busy = false); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | Future<void> restoreDraft() async { |
| 191 | try { |
| 192 | final pending = (await sync.store.readQueue()) |
| 193 | .where((m) => |
| 194 | m['entityType'] == 'learning_feedback' && |
| 195 | (m['payload'] as Map)['unitId'] == widget.unitId) |
| 196 | .firstOrNull; |
| 197 | if (pending != null && mounted) { |
| 198 | setState(() { |
| 199 | pendingBody = Map<String, dynamic>.from(pending['payload'] as Map); |
| 200 | summary.text = pendingBody!['summary'].toString(); |
| 201 | category = pendingBody!['category'].toString(); |
| 202 | consent = true; |
| 203 | message = 'بازخورد ارسالنشده روی این دستگاه نگه داشته شده است.'; |
| 204 | }); |
| 205 | } |
| 206 | } catch (e) { |
| 207 | if (mounted) setState(() => message = learningError(e)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | @override |
| 212 | void dispose() { |
| 213 | summary.dispose(); |
| 214 | super.dispose(); |
| 215 | } |
| 216 | |
| 217 | Future<void> send() async { |
| 218 | if (!consent || summary.text.trim().isEmpty) { |
| 219 | setState(() => message = 'خلاصه و رضایت ارسال را تکمیل کن.'); |
| 220 | return; |
| 221 | } |
| 222 | setState(() => busy = true); |
| 223 | pendingBody ??= { |
| 224 | 'mutationId': newMutationId(), |
| 225 | 'unitId': widget.unitId, |
| 226 | 'category': category, |
| 227 | 'summary': summary.text.trim(), |
| 228 | 'shareWithContentTeam': true |
| 229 | }; |
| 230 | try { |
| 231 | final result = await sync.submit(pendingBody!); |
| 232 | if (mounted) { |
| 233 | setState(() { |
| 234 | message = result['queued'] == true |
| 235 | ? 'روی دستگاه ذخیره شد؛ هنوز تأیید سرور دریافت نشده است.' |
| 236 | : 'بازخورد در سرور ثبت شد؛ بررسی و انتشار نتیجه هنوز انجام نشده است.'; |
| 237 | if (result['queued'] != true) { |
| 238 | pendingBody = null; |
| 239 | summary.clear(); |
| 240 | consent = false; |
| 241 | } |
| 242 | }); |
| 243 | if (result['queued'] != true && mounted) await loadHistory(); |
| 244 | } |
| 245 | } catch (e) { |
| 246 | if (mounted) setState(() => message = learningError(e)); |
| 247 | } finally { |
| 248 | if (mounted) setState(() => busy = false); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @override |
| 253 | Widget build(BuildContext context) => Scaffold( |
| 254 | appBar: AppBar(title: const Text('بازخورد و پیشنهاد آموزش')), |
| 255 | body: ListView(padding: const EdgeInsets.all(16), children: [ |
| 256 | const Text( |
| 257 | 'فقط ایراد محتوا یا نیاز آموزشی را بنویس. نام افراد، گفتوگوی خصوصی، اطلاعات سلامت یا شرح آزار را در بازخورد توسعه ننویس. این بخش مشاوره یا رسیدگی اضطراری نیست.'), |
| 258 | DropdownButtonFormField<String>( |
| 259 | initialValue: category, |
| 260 | items: feedbackCategories.entries |
| 261 | .map((entry) => DropdownMenuItem( |
| 262 | value: entry.key, child: Text(entry.value))) |
| 263 | .toList(), |
| 264 | onChanged: busy || pendingBody != null |
| 265 | ? null |
| 266 | : (value) => setState(() => category = value!)), |
| 267 | TextField( |
| 268 | controller: summary, |
| 269 | enabled: !busy && pendingBody == null, |
| 270 | maxLength: 1000, |
| 271 | maxLines: 5, |
| 272 | decoration: |
| 273 | const InputDecoration(labelText: 'خلاصه ایراد یا پیشنهاد')), |
| 274 | CheckboxListTile( |
| 275 | value: consent, |
| 276 | onChanged: busy || pendingBody != null |
| 277 | ? null |
| 278 | : (value) => setState(() => consent = value == true), |
| 279 | title: const Text( |
| 280 | 'میخواهم فقط همین خلاصه برای بررسی محتوا در اختیار تیم توسعه قرار گیرد.')), |
| 281 | if (message != null) Text(message!), |
| 282 | FilledButton( |
| 283 | onPressed: busy ? null : send, |
| 284 | child: Text(pendingBody == null |
| 285 | ? 'ارسال بازخورد' |
| 286 | : 'تلاش دوباره با همان بازخورد')), |
| 287 | if (pendingBody != null) |
| 288 | TextButton( |
| 289 | onPressed: busy |
| 290 | ? null |
| 291 | : () async { |
| 292 | await sync.store.discardEntityChanges( |
| 293 | pendingBody!['mutationId'] as String, |
| 294 | cacheKey: 'education_feedback', |
| 295 | entityType: 'learning_feedback'); |
| 296 | if (mounted) { |
| 297 | setState(() { |
| 298 | pendingBody = null; |
| 299 | consent = false; |
| 300 | message = |
| 301 | 'تلاش بعدی ارسال متوقف شد و پیشنویس در سابقه بازیابی ماند. اگر پاسخ شبکه گم شده باشد، رکورد ممکن است در سرور ثبت شده باشد؛ فهرست ثبتشده را بررسی و رضایت را جدا پس بگیر.'; |
| 302 | }); |
| 303 | } |
| 304 | }, |
| 305 | child: const Text('خارجکردن پیشنویس از صف ارسال')), |
| 306 | const Text( |
| 307 | 'این فرم فقط داده انتخابشده تو را ارسال میکند؛ گفتوگو، یادداشت و اطلاعات خصوصی حساب استخراج نمیشود.'), |
| 308 | const Divider(), |
| 309 | const Text('بازخوردهای ثبتشده من'), |
| 310 | ...history.map((row) => Card( |
| 311 | child: ListTile( |
| 312 | title: Text(row['withdrawnAt'] == null |
| 313 | ? row['summary']?.toString() ?? 'بازخورد' |
| 314 | : 'رضایت ارسال پس گرفته شد'), |
| 315 | subtitle: Text(row['withdrawnAt'] == null |
| 316 | ? 'وضعیت بررسی: ${feedbackStatusLabels[row['status']] ?? 'نامشخص'}' |
| 317 | : 'متن بازخورد حذف شده است.'), |
| 318 | trailing: row['withdrawnAt'] != null |
| 319 | ? null |
| 320 | : TextButton( |
| 321 | onPressed: |
| 322 | busy ? null : () => withdraw(row['id'].toString()), |
| 323 | child: const Text('پسگرفتن رضایت'))))), |
| 324 | ])); |
| 325 | } |