LifeMate — Personal, Family & Learning Companion
feat/aparat-learning-library
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 | |
| 3 | import 'package:flutter/material.dart'; |
| 4 | |
| 5 | import 'api.dart'; |
| 6 | import 'aparat_player.dart'; |
| 7 | import 'learning_sync.dart'; |
| 8 | import 'learning_feedback_ui.dart'; |
| 9 | import 'learning_quiz_ui.dart'; |
| 10 | import 'offline_store.dart'; |
| 11 | import 'plan_sync.dart'; |
| 12 | import 'study_ui.dart'; |
| 13 | |
| 14 | const learningTracks = <String, String>{ |
| 15 | 'common': 'عمومی', |
| 16 | 'math': 'ریاضی و فیزیک', |
| 17 | 'experimental': 'علوم تجربی', |
| 18 | 'humanities': 'علوم انسانی', |
| 19 | }; |
| 20 | const learningBookVariants = <String, String>{ |
| 21 | 'standard': 'نسخه معمول', |
| 22 | 'experimental': 'نسخه آزمایشی', |
| 23 | 'religious_variant': 'نسخه دینی متفاوت', |
| 24 | 'gifted': 'استعدادهای درخشان', |
| 25 | }; |
| 26 | |
| 27 | bool isLearningAuthFailure(Object error) => |
| 28 | error is ApiException && |
| 29 | (error.statusCode == 401 || error.statusCode == 403); |
| 30 | |
| 31 | String learningError(Object error) { |
| 32 | if (isOfflineFailure(error)) return 'اتصال برقرار نیست؛ دوباره تلاش کن.'; |
| 33 | if (error is ApiException) { |
| 34 | if (error.statusCode == 401 || error.statusCode == 403) { |
| 35 | return 'نشست معتبر لازم است؛ دوباره وارد حساب شو.'; |
| 36 | } |
| 37 | if (error.statusCode == 409) { |
| 38 | return 'این درس یک تغییر معلق یا تعارض دارد؛ ابتدا وضعیت همگامسازی را بررسی کن.'; |
| 39 | } |
| 40 | } |
| 41 | return 'عملیات تأیید نشد؛ داده را بررسی و دوباره تلاش کن.'; |
| 42 | } |
| 43 | |
| 44 | /// School is a selectable life context, never a role or permission grant. |
| 45 | class EducationLibraryPage extends StatefulWidget { |
| 46 | const EducationLibraryPage({super.key, required this.api}); |
| 47 | final IdentityApi api; |
| 48 | @override |
| 49 | State<EducationLibraryPage> createState() => _EducationLibraryPageState(); |
| 50 | } |
| 51 | |
| 52 | class _EducationLibraryPageState extends State<EducationLibraryPage> { |
| 53 | Map<String, dynamic>? library; |
| 54 | String? error; |
| 55 | String search = ''; |
| 56 | bool busy = false, offline = false; |
| 57 | HttpIdentityApi? get api => |
| 58 | widget.api is HttpIdentityApi ? widget.api as HttpIdentityApi : null; |
| 59 | |
| 60 | @override |
| 61 | void initState() { |
| 62 | super.initState(); |
| 63 | load(); |
| 64 | } |
| 65 | |
| 66 | Future<void> load() async { |
| 67 | if (api == null || busy) return; |
| 68 | setState(() => busy = true); |
| 69 | try { |
| 70 | final value = await api!.requestJson('GET', '/v1/education/library'); |
| 71 | await OfflineStore.forApi(api!).writeList('education_library', [value]); |
| 72 | if (mounted) { |
| 73 | setState(() { |
| 74 | library = value; |
| 75 | error = null; |
| 76 | offline = false; |
| 77 | }); |
| 78 | } |
| 79 | } catch (e) { |
| 80 | if (isLearningAuthFailure(e) && mounted) { |
| 81 | setState(() { |
| 82 | library = null; |
| 83 | offline = false; |
| 84 | }); |
| 85 | } |
| 86 | if (isOfflineFailure(e)) { |
| 87 | try { |
| 88 | final cached = |
| 89 | await OfflineStore.forApi(api!).readList('education_library'); |
| 90 | if (mounted && cached.isNotEmpty) { |
| 91 | setState(() { |
| 92 | library = cached.single; |
| 93 | offline = true; |
| 94 | }); |
| 95 | } |
| 96 | } catch (storageError) { |
| 97 | if (mounted) setState(() => error = learningError(storageError)); |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | if (mounted) setState(() => error = learningError(e)); |
| 102 | } finally { |
| 103 | if (mounted) setState(() => busy = false); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | Future<void> editEducation() async { |
| 108 | if (api == null) return; |
| 109 | final education = library?['education']; |
| 110 | final saved = await showDialog<bool>( |
| 111 | context: context, |
| 112 | builder: (_) => _EducationDialog( |
| 113 | api: api!, |
| 114 | education: education is Map |
| 115 | ? Map<String, dynamic>.from(education) |
| 116 | : null)); |
| 117 | if (saved == true) await load(); |
| 118 | } |
| 119 | |
| 120 | @override |
| 121 | Widget build(BuildContext context) { |
| 122 | if (api == null) { |
| 123 | return const Center( |
| 124 | child: Text('کتابخانه آموزشی به حساب و API واقعی نیاز دارد.')); |
| 125 | } |
| 126 | final education = library?['education']; |
| 127 | final courses = (library?['courses'] as List? ?? []) |
| 128 | .whereType<Map>() |
| 129 | .where((course) => |
| 130 | search.isEmpty || |
| 131 | '${course['titleFa']} ${course['bookCode']}'.contains(search)) |
| 132 | .toList(); |
| 133 | return ListView(padding: const EdgeInsets.all(16), children: [ |
| 134 | Card( |
| 135 | child: Padding( |
| 136 | padding: const EdgeInsets.all(16), |
| 137 | child: Column( |
| 138 | crossAxisAlignment: CrossAxisAlignment.start, |
| 139 | children: [ |
| 140 | Text('مدرسه من', |
| 141 | style: Theme.of(context).textTheme.headlineSmall), |
| 142 | const SizedBox(height: 8), |
| 143 | Text(education is Map |
| 144 | ? 'پایه ${education['nationalGrade']} · ${learningTracks[education['trackCode']] ?? 'رشته تعیین نشده'} · ${education['schoolYear'] ?? ''}' |
| 145 | : 'پایه تحصیلی را انتخاب کن تا کتابهای خودت نمایش داده شود.'), |
| 146 | const Text( |
| 147 | 'از هر درس میتوانی شروع کنی؛ تغییر پایه، برنامه و پیشرفت قبلی را پاک نمیکند.'), |
| 148 | TextButton.icon( |
| 149 | onPressed: busy ? null : editEducation, |
| 150 | icon: const Icon(Icons.school_outlined), |
| 151 | label: const Text('انتخاب پایه و رشته')), |
| 152 | ]))), |
| 153 | if (busy) const LinearProgressIndicator(), |
| 154 | if (error != null) |
| 155 | ListTile( |
| 156 | leading: const Icon(Icons.info_outline), |
| 157 | title: Text(error!), |
| 158 | trailing: |
| 159 | TextButton(onPressed: load, child: const Text('تازهسازی'))), |
| 160 | if (offline) |
| 161 | const Text('فهرست ذخیرهشده؛ اتصال و تازگی داده تأیید نشده است.'), |
| 162 | if (library?['fetchedAt'] != null) |
| 163 | Text('آخرین دریافت: ${library!['fetchedAt']}'), |
| 164 | TextField( |
| 165 | decoration: const InputDecoration( |
| 166 | labelText: 'جستجوی کتاب', prefixIcon: Icon(Icons.search)), |
| 167 | onChanged: (value) => setState(() => search = value.trim())), |
| 168 | TextButton.icon( |
| 169 | onPressed: () => Navigator.of(context).push(MaterialPageRoute( |
| 170 | builder: (_) => LearningFeedbackPage(api: api!))), |
| 171 | icon: const Icon(Icons.feedback_outlined), |
| 172 | label: const Text('بازخورد یا پیشنهاد آموزش')), |
| 173 | const SizedBox(height: 12), |
| 174 | if (!busy && courses.isEmpty) |
| 175 | const Padding( |
| 176 | padding: EdgeInsets.all(16), |
| 177 | child: Text('برای این انتخاب، آموزش ثبتشدهای پیدا نشد.')), |
| 178 | ...courses.map((course) => Card( |
| 179 | child: ListTile( |
| 180 | leading: const Icon(Icons.menu_book_outlined), |
| 181 | title: Text(course['titleFa']?.toString() ?? 'کتاب'), |
| 182 | subtitle: Text( |
| 183 | '${course['unitCount'] ?? 0} بخش فهرستشده · ${course['videoUnitCount'] ?? 0} بخش دارای ویدیو\n' |
| 184 | '${course['schoolYear'] ?? 'سال نسخه نامشخص'} · ${learningBookVariants[course['variant']] ?? 'نسخه نیازمند بررسی'}\n' |
| 185 | '${(course['unitCount'] ?? 0) == 0 ? 'آموزش این کتاب هنوز گردآوری نشده است.' : 'این شمارش، کاملبودن دوره یا کیفیت تدریس را تأیید نمیکند.'}'), |
| 186 | isThreeLine: true, |
| 187 | trailing: const Icon(Icons.chevron_left), |
| 188 | onTap: () => Navigator.of(context).push(MaterialPageRoute( |
| 189 | builder: (_) => LearningCoursePage( |
| 190 | api: api!, courseId: course['id'].toString())))))), |
| 191 | const Padding( |
| 192 | padding: EdgeInsets.symmetric(vertical: 16), |
| 193 | child: Text( |
| 194 | 'منابع پژوهش فعلی از آپاراتاند؛ تا بازبینی کامل تدریس و تطبیق با کتاب، پخش آموزشی منتشر نمیشود. پوشش همه کتابها و هنرستانها هنوز تکمیل نشده است.')), |
| 195 | ]); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | class _EducationDialog extends StatefulWidget { |
| 200 | const _EducationDialog({required this.api, this.education}); |
| 201 | final HttpIdentityApi api; |
| 202 | final Map<String, dynamic>? education; |
| 203 | @override |
| 204 | State<_EducationDialog> createState() => _EducationDialogState(); |
| 205 | } |
| 206 | |
| 207 | class _EducationDialogState extends State<_EducationDialog> { |
| 208 | int? grade; |
| 209 | String? track; |
| 210 | late final TextEditingController year; |
| 211 | bool busy = false; |
| 212 | String? error; |
| 213 | @override |
| 214 | void initState() { |
| 215 | super.initState(); |
| 216 | grade = int.tryParse('${widget.education?['nationalGrade']}'); |
| 217 | track = widget.education?['trackCode']?.toString(); |
| 218 | year = TextEditingController( |
| 219 | text: widget.education?['schoolYear']?.toString() ?? '1405-1406'); |
| 220 | } |
| 221 | |
| 222 | @override |
| 223 | void dispose() { |
| 224 | year.dispose(); |
| 225 | super.dispose(); |
| 226 | } |
| 227 | |
| 228 | Future<void> save() async { |
| 229 | if (grade == null || (grade! >= 10 && track == null)) { |
| 230 | setState(() => error = 'پایه و رشته را انتخاب کن.'); |
| 231 | return; |
| 232 | } |
| 233 | setState(() => busy = true); |
| 234 | try { |
| 235 | await widget.api.requestJson('PUT', '/v1/me/education', body: { |
| 236 | 'nationalGrade': grade, |
| 237 | 'trackCode': grade! < 10 ? 'common' : track, |
| 238 | 'schoolYear': year.text.trim() |
| 239 | }); |
| 240 | if (mounted) Navigator.pop(context, true); |
| 241 | } catch (e) { |
| 242 | if (mounted) setState(() => error = learningError(e)); |
| 243 | } finally { |
| 244 | if (mounted) setState(() => busy = false); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @override |
| 249 | Widget build(BuildContext context) => AlertDialog( |
| 250 | title: const Text('پایه تحصیلی من'), |
| 251 | content: SingleChildScrollView( |
| 252 | child: Column(mainAxisSize: MainAxisSize.min, children: [ |
| 253 | DropdownButtonFormField<int>( |
| 254 | key: const ValueKey('education-grade'), |
| 255 | initialValue: grade, |
| 256 | decoration: const InputDecoration(labelText: 'پایه'), |
| 257 | items: List.generate( |
| 258 | 12, |
| 259 | (index) => DropdownMenuItem( |
| 260 | value: index + 1, child: Text('پایه ${index + 1}'))), |
| 261 | onChanged: busy |
| 262 | ? null |
| 263 | : (value) => setState(() { |
| 264 | grade = value; |
| 265 | if (grade! < 10) track = 'common'; |
| 266 | if (grade! >= 10 && track == 'common') track = null; |
| 267 | })), |
| 268 | if ((grade ?? 0) >= 10) |
| 269 | DropdownButtonFormField<String>( |
| 270 | key: const ValueKey('education-track'), |
| 271 | initialValue: track == 'common' ? null : track, |
| 272 | decoration: const InputDecoration(labelText: 'رشته'), |
| 273 | items: learningTracks.entries |
| 274 | .where((entry) => entry.key != 'common') |
| 275 | .map((entry) => DropdownMenuItem( |
| 276 | value: entry.key, child: Text(entry.value))) |
| 277 | .toList(), |
| 278 | onChanged: |
| 279 | busy ? null : (value) => setState(() => track = value)), |
| 280 | TextField( |
| 281 | key: const ValueKey('education-year'), |
| 282 | controller: year, |
| 283 | enabled: !busy, |
| 284 | decoration: const InputDecoration( |
| 285 | labelText: 'سال تحصیلی، نمونه 1405-1406')), |
| 286 | if (error != null) Text(error!), |
| 287 | ])), |
| 288 | actions: [ |
| 289 | TextButton( |
| 290 | onPressed: busy ? null : () => Navigator.pop(context), |
| 291 | child: const Text('انصراف')), |
| 292 | FilledButton( |
| 293 | onPressed: busy ? null : save, child: const Text('ذخیره')) |
| 294 | ]); |
| 295 | } |
| 296 | |
| 297 | class LearningCoursePage extends StatefulWidget { |
| 298 | const LearningCoursePage( |
| 299 | {super.key, required this.api, required this.courseId}); |
| 300 | final HttpIdentityApi api; |
| 301 | final String courseId; |
| 302 | @override |
| 303 | State<LearningCoursePage> createState() => _LearningCoursePageState(); |
| 304 | } |
| 305 | |
| 306 | class _LearningCoursePageState extends State<LearningCoursePage> { |
| 307 | late final LearningSync sync; |
| 308 | Map<String, dynamic>? course; |
| 309 | String? error; |
| 310 | String search = ''; |
| 311 | bool busy = false; |
| 312 | Timer? poll; |
| 313 | @override |
| 314 | void initState() { |
| 315 | super.initState(); |
| 316 | sync = LearningSync(widget.api); |
| 317 | load(); |
| 318 | poll = Timer.periodic( |
| 319 | const Duration(seconds: 30), (_) => load(syncPending: true)); |
| 320 | } |
| 321 | |
| 322 | @override |
| 323 | void dispose() { |
| 324 | poll?.cancel(); |
| 325 | super.dispose(); |
| 326 | } |
| 327 | |
| 328 | Future<void> load({bool syncPending = false}) async { |
| 329 | if (busy) return; |
| 330 | setState(() => busy = true); |
| 331 | try { |
| 332 | Object? syncError; |
| 333 | if (syncPending) { |
| 334 | try { |
| 335 | await sync.flush(); |
| 336 | } catch (e) { |
| 337 | syncError = e; |
| 338 | } |
| 339 | } |
| 340 | final value = await sync.readCourse(widget.courseId); |
| 341 | if (mounted) { |
| 342 | setState(() { |
| 343 | course = value; |
| 344 | error = syncError == null ? null : learningError(syncError); |
| 345 | }); |
| 346 | } |
| 347 | } catch (e) { |
| 348 | if (mounted) { |
| 349 | setState(() { |
| 350 | if (isLearningAuthFailure(e)) { |
| 351 | course = null; |
| 352 | } |
| 353 | error = learningError(e); |
| 354 | }); |
| 355 | } |
| 356 | } finally { |
| 357 | if (mounted) setState(() => busy = false); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | Future<void> openLesson(Map<String, dynamic> unit) async { |
| 362 | await Navigator.of(context).push(MaterialPageRoute( |
| 363 | builder: (_) => LearningLessonPage( |
| 364 | api: widget.api, |
| 365 | courseId: widget.courseId, |
| 366 | unitId: unit['id'].toString()))); |
| 367 | if (mounted) await load(); |
| 368 | } |
| 369 | |
| 370 | @override |
| 371 | Widget build(BuildContext context) { |
| 372 | final units = (course?['units'] as List? ?? []).whereType<Map>().toList(); |
| 373 | return Scaffold( |
| 374 | appBar: AppBar( |
| 375 | title: Text(course?['titleFa']?.toString() ?? 'درسها'), |
| 376 | actions: [ |
| 377 | IconButton(onPressed: load, icon: const Icon(Icons.refresh)) |
| 378 | ]), |
| 379 | body: ListView(padding: const EdgeInsets.all(16), children: [ |
| 380 | if (busy) const LinearProgressIndicator(), |
| 381 | if (error != null) Text(error!), |
| 382 | if (course?['offline'] == true) |
| 383 | const Text( |
| 384 | 'آفلاین؛ فهرست ذخیرهشده و پیشرفت معلق ممکن است قدیمی باشد.'), |
| 385 | Text('نسخه کتاب: ${course?['schoolYear'] ?? 'نامشخص'}'), |
| 386 | if (course != null && course!['editionVerification'] != 'verified') |
| 387 | const Text( |
| 388 | 'نسخه و سال این کتاب هنوز از صفحه معتبر ناشر تأیید نشده است.'), |
| 389 | if ((course?['reviewCandidateCount'] as num? ?? 0) > 0) |
| 390 | Text( |
| 391 | '${course!['reviewCandidateCount']} منبع در انتظار بازبینی کامل؛ این منابع هنوز برای آموزش منتشر نشدهاند.'), |
| 392 | if (course?['alignmentNotesFa'] != null) |
| 393 | Text(course!['alignmentNotesFa'].toString()), |
| 394 | const Text( |
| 395 | 'میتوانی از هر بخش شروع کنی. پیشرفت، گزارش خودت است و اثبات یادگیری نیست.'), |
| 396 | TextField( |
| 397 | decoration: |
| 398 | const InputDecoration(labelText: 'جستجوی درس یا صفحه'), |
| 399 | onChanged: (value) => setState(() => search = value.trim())), |
| 400 | if (!busy && units.isEmpty) |
| 401 | const Text('ویدیوی منطبق برای این کتاب هنوز ثبت نشده است.'), |
| 402 | ...units |
| 403 | .where( |
| 404 | (u) => search.isEmpty || '${u['titleFa']}'.contains(search)) |
| 405 | .map((u) { |
| 406 | final progress = u['progress']; |
| 407 | final pending = progress is Map && progress['pending_sync'] == true; |
| 408 | return Card( |
| 409 | child: ListTile( |
| 410 | leading: Icon( |
| 411 | progress is Map && progress['status'] == 'completed' |
| 412 | ? Icons.check_circle_outline |
| 413 | : Icons.play_lesson_outlined), |
| 414 | title: Text(u['titleFa']?.toString() ?? 'درس'), |
| 415 | subtitle: Text( |
| 416 | '${(u['videos'] as List? ?? []).length} منبع منتشرشده · ${u['reviewCandidateCount'] ?? 0} نامزد بازبینی${pending ? ' · تغییر ارسالنشده' : ''}'), |
| 417 | trailing: const Icon(Icons.chevron_left), |
| 418 | onTap: () => openLesson(Map<String, dynamic>.from(u)))); |
| 419 | }), |
| 420 | ])); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | class LearningLessonPage extends StatefulWidget { |
| 425 | const LearningLessonPage( |
| 426 | {super.key, |
| 427 | required this.api, |
| 428 | required this.courseId, |
| 429 | required this.unitId}); |
| 430 | final HttpIdentityApi api; |
| 431 | final String courseId, unitId; |
| 432 | @override |
| 433 | State<LearningLessonPage> createState() => _LearningLessonPageState(); |
| 434 | } |
| 435 | |
| 436 | class _LearningLessonPageState extends State<LearningLessonPage> { |
| 437 | late final LearningSync sync; |
| 438 | Map<String, dynamic>? course, unit; |
| 439 | String? error; |
| 440 | bool busy = false; |
| 441 | int videoIndex = 0; |
| 442 | @override |
| 443 | void initState() { |
| 444 | super.initState(); |
| 445 | sync = LearningSync(widget.api); |
| 446 | load(); |
| 447 | } |
| 448 | |
| 449 | Future<void> load({bool syncPending = false}) async { |
| 450 | if (busy) return; |
| 451 | setState(() => busy = true); |
| 452 | try { |
| 453 | Object? syncError; |
| 454 | if (syncPending) { |
| 455 | try { |
| 456 | await sync.flush(); |
| 457 | } catch (e) { |
| 458 | syncError = e; |
| 459 | } |
| 460 | } |
| 461 | final value = await sync.readCourse(widget.courseId); |
| 462 | final units = (value['units'] as List).whereType<Map>(); |
| 463 | final found = units.where((u) => u['id'] == widget.unitId).firstOrNull; |
| 464 | if (found == null) { |
| 465 | throw const ApiException(404, 'learning_unit_not_found'); |
| 466 | } |
| 467 | if (mounted) { |
| 468 | setState(() { |
| 469 | course = value; |
| 470 | unit = Map<String, dynamic>.from(found); |
| 471 | error = syncError == null ? null : learningError(syncError); |
| 472 | }); |
| 473 | } |
| 474 | } catch (e) { |
| 475 | if (mounted) { |
| 476 | setState(() { |
| 477 | if (isLearningAuthFailure(e)) { |
| 478 | course = null; |
| 479 | unit = null; |
| 480 | } |
| 481 | error = learningError(e); |
| 482 | }); |
| 483 | } |
| 484 | } finally { |
| 485 | if (mounted) setState(() => busy = false); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | Future<void> progress(String status) async { |
| 490 | if (busy || unit == null) return; |
| 491 | setState(() => busy = true); |
| 492 | Object? mutationError; |
| 493 | try { |
| 494 | final old = unit!['progress']; |
| 495 | await sync.setProgress(widget.unitId, status, |
| 496 | old is Map ? int.tryParse('${old['version']}') ?? 0 : 0); |
| 497 | if (mounted) setState(() => error = null); |
| 498 | } catch (e) { |
| 499 | mutationError = e; |
| 500 | } finally { |
| 501 | if (mounted) setState(() => busy = false); |
| 502 | } |
| 503 | if (mounted && |
| 504 | (mutationError == null || !isLearningAuthFailure(mutationError))) { |
| 505 | await load(); |
| 506 | } |
| 507 | if (mounted && mutationError != null) { |
| 508 | setState(() { |
| 509 | if (isLearningAuthFailure(mutationError!)) { |
| 510 | course = null; |
| 511 | unit = null; |
| 512 | } |
| 513 | error = learningError(mutationError); |
| 514 | }); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | Future<void> resolve() async { |
| 519 | final accept = await showDialog<bool>( |
| 520 | context: context, |
| 521 | builder: (_) => AlertDialog( |
| 522 | title: const Text('استفاده از پیشرفت سرور؟'), |
| 523 | content: const Text( |
| 524 | 'تغییر معلق در سابقه بازیابی نگه داشته میشود و پیشرفت فعلی سرور نمایش داده خواهد شد.'), |
| 525 | actions: [ |
| 526 | TextButton( |
| 527 | onPressed: () => Navigator.pop(context, false), |
| 528 | child: const Text('انصراف')), |
| 529 | FilledButton( |
| 530 | onPressed: () => Navigator.pop(context, true), |
| 531 | child: const Text('استفاده از سرور')) |
| 532 | ])); |
| 533 | if (accept != true || !mounted) return; |
| 534 | try { |
| 535 | await sync.resolveUsingServer(widget.unitId); |
| 536 | if (mounted) await load(); |
| 537 | } catch (e) { |
| 538 | if (mounted) setState(() => error = learningError(e)); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | Future<void> plan() async { |
| 543 | if (busy || unit == null) return; |
| 544 | final date = await showDatePicker( |
| 545 | context: context, |
| 546 | initialDate: DateTime.now(), |
| 547 | firstDate: DateTime.now().subtract(const Duration(days: 1)), |
| 548 | lastDate: DateTime.now().add(const Duration(days: 730))); |
| 549 | if (date == null || !mounted) return; |
| 550 | final minutes = await showDialog<int>( |
| 551 | context: context, |
| 552 | builder: (_) => SimpleDialog( |
| 553 | title: const Text('زمان برنامهریزیشده'), |
| 554 | children: [15, 30, 45, 60] |
| 555 | .map((m) => SimpleDialogOption( |
| 556 | onPressed: () => Navigator.pop(context, m), |
| 557 | child: Text('$m دقیقه'))) |
| 558 | .toList())); |
| 559 | if (minutes == null || !mounted) return; |
| 560 | setState(() => busy = true); |
| 561 | try { |
| 562 | final outcome = await PlanSync(widget.api).create({ |
| 563 | 'kind': 'study_session', |
| 564 | 'title': '${course?['titleFa']} · ${unit!['titleFa']}', |
| 565 | 'learningUnitId': widget.unitId, |
| 566 | 'visibility': 'private', |
| 567 | 'startsAt': DateTime(date.year, date.month, date.day, |
| 568 | DateTime.now().hour, DateTime.now().minute) |
| 569 | .toUtc() |
| 570 | .toIso8601String(), |
| 571 | 'plannedDurationSeconds': minutes * 60, |
| 572 | }); |
| 573 | if (!mounted) return; |
| 574 | ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 575 | content: Text(outcome.queued |
| 576 | ? 'برنامه روی دستگاه ثبت شد؛ تأیید سرور هنوز دریافت نشده است.' |
| 577 | : 'برنامه در سرور ثبت و تأیید شد.'))); |
| 578 | if (!outcome.queued && outcome.item != null) { |
| 579 | await Navigator.of(context).push(MaterialPageRoute( |
| 580 | builder: (_) => |
| 581 | StudyTimerPage(api: widget.api, item: outcome.item!))); |
| 582 | } |
| 583 | } catch (e) { |
| 584 | if (mounted) setState(() => error = learningError(e)); |
| 585 | } finally { |
| 586 | if (mounted) setState(() => busy = false); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | @override |
| 591 | Widget build(BuildContext context) { |
| 592 | final videos = (unit?['videos'] as List? ?? []).whereType<Map>().toList(); |
| 593 | final video = |
| 594 | videos.isEmpty ? null : videos[videoIndex.clamp(0, videos.length - 1)]; |
| 595 | final current = unit?['progress']; |
| 596 | final pending = current is Map && current['pending_sync'] == true; |
| 597 | final conflict = current is Map && current['queueStatus'] == 'conflict'; |
| 598 | final units = (course?['units'] as List? ?? []).whereType<Map>().toList(); |
| 599 | final index = units.indexWhere((u) => u['id'] == widget.unitId); |
| 600 | void move(int to) => |
| 601 | Navigator.of(context).pushReplacement(MaterialPageRoute( |
| 602 | builder: (_) => LearningLessonPage( |
| 603 | api: widget.api, |
| 604 | courseId: widget.courseId, |
| 605 | unitId: units[to]['id'].toString()))); |
| 606 | return Scaffold( |
| 607 | appBar: |
| 608 | AppBar(title: Text(unit?['titleFa']?.toString() ?? 'آموزش درس')), |
| 609 | body: ListView(padding: const EdgeInsets.all(16), children: [ |
| 610 | if (busy) const LinearProgressIndicator(), |
| 611 | if (error != null) Text(error!), |
| 612 | Text(course?['titleFa']?.toString() ?? ''), |
| 613 | if (course?['offline'] == true) |
| 614 | const Text('آفلاین؛ پخش ویدیو نیاز به اینترنت دارد.'), |
| 615 | if (video != null && video['freeAccess'] != false) ...[ |
| 616 | AparatPlayer( |
| 617 | key: ValueKey(video['aparatId']), |
| 618 | videoHash: video['aparatId'].toString(), |
| 619 | title: video['titleFa']?.toString() ?? 'آموزش'), |
| 620 | Text('مدرس: ${video['creatorName'] ?? 'نام تأیید نشده'}'), |
| 621 | SelectableText(video['creatorUrl']?.toString() ?? ''), |
| 622 | Text(video['qualityNotesFa']?.toString() ?? |
| 623 | 'بازبینی کامل کیفیت تدریس انجام نشده است.'), |
| 624 | if (video['freeAccess'] == null) |
| 625 | const Text('رایگانبودن و دسترسی فعلی این منبع تأیید نشده است.'), |
| 626 | const Text( |
| 627 | 'نمایش ویدیو، یادگیری یا تکمیل درس را خودکار ثبت نمیکند.'), |
| 628 | ] else |
| 629 | const Text( |
| 630 | 'برای این بخش، ویدیوی بازبینیشده و منتشرشده هنوز ثبت نشده است.'), |
| 631 | if (videos.length > 1) |
| 632 | Wrap( |
| 633 | spacing: 8, |
| 634 | children: List.generate( |
| 635 | videos.length, |
| 636 | (i) => ChoiceChip( |
| 637 | label: Text('منبع ${i + 1}'), |
| 638 | selected: videoIndex == i, |
| 639 | onSelected: (_) => setState(() => videoIndex = i)))), |
| 640 | const Divider(), |
| 641 | OutlinedButton.icon( |
| 642 | onPressed: unit == null |
| 643 | ? null |
| 644 | : () => Navigator.of(context).push(MaterialPageRoute( |
| 645 | builder: (_) => LearningQuizPage( |
| 646 | api: widget.api, unitId: widget.unitId))), |
| 647 | icon: const Icon(Icons.quiz_outlined), |
| 648 | label: const Text('تمرین فهم درس و مرور')), |
| 649 | TextButton.icon( |
| 650 | onPressed: unit == null |
| 651 | ? null |
| 652 | : () => Navigator.of(context).push(MaterialPageRoute( |
| 653 | builder: (_) => LearningFeedbackPage( |
| 654 | api: widget.api, unitId: widget.unitId))), |
| 655 | icon: const Icon(Icons.feedback_outlined), |
| 656 | label: const Text('گزارش ایراد این درس')), |
| 657 | Text('پیشرفت شخصی: ${current is Map ? { |
| 658 | 'not_started': 'شروع نشده', |
| 659 | 'in_progress': 'در حال یادگیری', |
| 660 | 'completed': 'تکمیل طبق گزارش من' |
| 661 | }[current['status']] ?? 'نامشخص' : 'شروع نشده'}'), |
| 662 | if (pending) |
| 663 | const Text( |
| 664 | 'تغییر روی دستگاه ذخیره شده؛ تأیید سرور دریافت نشده است.'), |
| 665 | if (current is Map && current['updatedAt'] != null) |
| 666 | Text('آخرین ثبت سرور: ${current['updatedAt']}'), |
| 667 | if (conflict) |
| 668 | TextButton( |
| 669 | onPressed: busy ? null : resolve, |
| 670 | child: const Text('حل تعارض با پیشرفت سرور')), |
| 671 | Wrap(spacing: 8, children: [ |
| 672 | TextButton( |
| 673 | onPressed: busy || pending || unit == null |
| 674 | ? null |
| 675 | : () => progress('not_started'), |
| 676 | child: const Text('شروع نشده')), |
| 677 | OutlinedButton( |
| 678 | onPressed: busy || pending || unit == null |
| 679 | ? null |
| 680 | : () => progress('in_progress'), |
| 681 | child: const Text('در حال یادگیری')), |
| 682 | FilledButton( |
| 683 | onPressed: busy || pending || unit == null |
| 684 | ? null |
| 685 | : () => progress('completed'), |
| 686 | child: const Text('طبق گزارش من تکمیل شد')), |
| 687 | ]), |
| 688 | FilledButton.tonalIcon( |
| 689 | onPressed: busy || unit == null ? null : plan, |
| 690 | icon: const Icon(Icons.event_note_outlined), |
| 691 | label: const Text('افزودن درس به برنامه و تایمر')), |
| 692 | const Text( |
| 693 | 'برنامه بهصورت خصوصی ایجاد میشود؛ اشتراک با خانواده از تنظیمات برنامهریز انجام میشود.'), |
| 694 | TextButton( |
| 695 | onPressed: busy ? null : () => load(syncPending: true), |
| 696 | child: const Text('همگامسازی و تازهسازی')), |
| 697 | Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ |
| 698 | TextButton( |
| 699 | onPressed: index > 0 ? () => move(index - 1) : null, |
| 700 | child: const Text('بخش قبلی')), |
| 701 | TextButton( |
| 702 | onPressed: index >= 0 && index + 1 < units.length |
| 703 | ? () => move(index + 1) |
| 704 | : null, |
| 705 | child: const Text('بخش بعدی')), |
| 706 | ]), |
| 707 | ])); |
| 708 | } |
| 709 | } |