LifeMate — Personal, Family & Learning Companion
fix/local-task-persistence
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 | // ignore_for_file: prefer_interpolation_to_compose_strings, curly_braces_in_flow_control_structures, use_build_context_synchronously |
| 2 | |
| 3 | import 'dart:math'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'api.dart'; |
| 6 | import 'offline_store.dart'; |
| 7 | |
| 8 | String faKind(String kind) { |
| 9 | switch (kind) { |
| 10 | case 'assignment': |
| 11 | return 'تکلیف'; |
| 12 | case 'exam': |
| 13 | return 'امتحان'; |
| 14 | case 'study_session': |
| 15 | return 'مطالعه'; |
| 16 | case 'event': |
| 17 | return 'رویداد'; |
| 18 | case 'routine': |
| 19 | return 'روتین'; |
| 20 | case 'goal': |
| 21 | return 'هدف'; |
| 22 | default: |
| 23 | return 'کار'; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | DateTime? parseDate(dynamic value) { |
| 28 | if (value == null) return null; |
| 29 | return DateTime.tryParse(value.toString())?.toLocal(); |
| 30 | } |
| 31 | |
| 32 | String shortDate(dynamic value) { |
| 33 | final d = parseDate(value); |
| 34 | if (d == null) return ''; |
| 35 | final minute = d.minute.toString().padLeft(2, '0'); |
| 36 | return d.month.toString() + |
| 37 | '/' + |
| 38 | d.day.toString() + |
| 39 | ' · ' + |
| 40 | d.hour.toString() + |
| 41 | ':' + |
| 42 | minute; |
| 43 | } |
| 44 | |
| 45 | class Phase3HomeContent extends StatelessWidget { |
| 46 | const Phase3HomeContent({ |
| 47 | super.key, |
| 48 | required this.api, |
| 49 | required this.page, |
| 50 | required this.adultShell, |
| 51 | this.localOnly = false, |
| 52 | }); |
| 53 | final IdentityApi api; |
| 54 | final String page; |
| 55 | final bool adultShell; |
| 56 | final bool localOnly; |
| 57 | |
| 58 | @override |
| 59 | Widget build(BuildContext context) { |
| 60 | if (page == 'امروز') return TodayPage(api: api, localOnly: localOnly); |
| 61 | if (page == 'برنامهریز' || |
| 62 | page == 'برنامه هفتگی' || |
| 63 | page == 'تقویم' || |
| 64 | page == 'تکالیف و کارها') { |
| 65 | return PlannerPage(api: api, localOnly: localOnly); |
| 66 | } |
| 67 | if (localOnly) return const LocalFeaturesNotice(); |
| 68 | if (page == 'خانواده') return ParentFamilyDashboard(api: api); |
| 69 | if (page == 'امتحانها و نمرات') return SchoolPage(api: api); |
| 70 | if (page == 'تمرکز') { |
| 71 | return const CoreNotice( |
| 72 | title: 'تمرکز', |
| 73 | subtitle: |
| 74 | 'جلسه مطالعه در برنامهریز فعال است؛ تایمر پیشرفته در فاز بعد اضافه میشود.', |
| 75 | ); |
| 76 | } |
| 77 | if (page == 'همراه هوشمند' || page == 'راهنما') { |
| 78 | return const CoreNotice( |
| 79 | title: 'راهنمای هوشمند', |
| 80 | subtitle: |
| 81 | 'هسته برنامهریزی این فاز مستقل از AI است و راهنمای هوشمند در فاز ۴ فعال میشود.', |
| 82 | ); |
| 83 | } |
| 84 | return SchoolPage(api: api); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | class CoreNotice extends StatelessWidget { |
| 89 | const CoreNotice({super.key, required this.title, required this.subtitle}); |
| 90 | final String title; |
| 91 | final String subtitle; |
| 92 | @override |
| 93 | Widget build(BuildContext context) => Center( |
| 94 | child: Card( |
| 95 | margin: const EdgeInsets.all(20), |
| 96 | child: Padding( |
| 97 | padding: const EdgeInsets.all(24), |
| 98 | child: Column(mainAxisSize: MainAxisSize.min, children: [ |
| 99 | Text(title, style: Theme.of(context).textTheme.headlineSmall), |
| 100 | const SizedBox(height: 8), |
| 101 | Text(subtitle, textAlign: TextAlign.center), |
| 102 | ]), |
| 103 | ), |
| 104 | ), |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | String localDataErrorText(Object error) { |
| 109 | if (error is ApiException) { |
| 110 | switch (error.code) { |
| 111 | case 'local_storage_restart_required': |
| 112 | return 'وضعیت ذخیره نامشخص است. برنامه را از تنظیمات دستگاه «توقف اجباری» کن و دوباره باز کن.'; |
| 113 | case 'local_storage_failed': |
| 114 | return 'ذخیره روی دستگاه انجام نشد. دوباره تلاش کن.'; |
| 115 | case 'local_data_corrupt': |
| 116 | return 'اطلاعات ذخیرهشده روی دستگاه خراب است؛ داده قبلی حفظ شده است.'; |
| 117 | case 'local_invalid_item': |
| 118 | return 'عنوان، نوع یا زمان برنامه معتبر نیست.'; |
| 119 | case 'local_invalid_profile': |
| 120 | return 'نام و تم پروفایل را بررسی کن.'; |
| 121 | case 'local_item_not_found': |
| 122 | return 'این مورد در اطلاعات دستگاه پیدا نشد.'; |
| 123 | case 'local_feature_unavailable': |
| 124 | return 'این قابلیت در حالت محلی فعال نیست.'; |
| 125 | } |
| 126 | } |
| 127 | return 'دسترسی به اطلاعات روی دستگاه انجام نشد. دوباره تلاش کن.'; |
| 128 | } |
| 129 | |
| 130 | String _planErrorText(Object error, bool localOnly) { |
| 131 | if (localOnly || (error is ApiException && error.code.startsWith('local_'))) { |
| 132 | return localDataErrorText(error); |
| 133 | } |
| 134 | return 'عملیات انجام نشد. دوباره تلاش کن.'; |
| 135 | } |
| 136 | |
| 137 | class LocalDataNotice extends StatelessWidget { |
| 138 | const LocalDataNotice({super.key}); |
| 139 | |
| 140 | @override |
| 141 | Widget build(BuildContext context) => const Card( |
| 142 | child: ListTile( |
| 143 | leading: Icon(Icons.phone_android_outlined), |
| 144 | title: Text('حالت محلی · فقط روی این دستگاه'), |
| 145 | subtitle: Text( |
| 146 | 'همگامسازی و پشتیبان سرور ندارد؛ حذف برنامه ممکن است اطلاعات را پاک کند.', |
| 147 | ), |
| 148 | ), |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | class LocalFeaturesNotice extends StatelessWidget { |
| 153 | const LocalFeaturesNotice({super.key}); |
| 154 | |
| 155 | @override |
| 156 | Widget build(BuildContext context) => const Card( |
| 157 | child: ListTile( |
| 158 | leading: Icon(Icons.info_outline), |
| 159 | title: Text('قابلیتهای غیرفعال در حالت محلی'), |
| 160 | subtitle: Text( |
| 161 | 'خانواده، آموزش ساختاریافته، ثبت حال و یادگیری، و همراه هوشمند فعال نیستند.', |
| 162 | ), |
| 163 | ), |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | class _OperationErrorCard extends StatelessWidget { |
| 168 | const _OperationErrorCard({required this.message, required this.onRetry}); |
| 169 | final String message; |
| 170 | final VoidCallback onRetry; |
| 171 | |
| 172 | @override |
| 173 | Widget build(BuildContext context) => Card( |
| 174 | child: Padding( |
| 175 | padding: const EdgeInsets.all(16), |
| 176 | child: Column( |
| 177 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 178 | children: [ |
| 179 | Text(message, |
| 180 | style: TextStyle(color: Theme.of(context).colorScheme.error)), |
| 181 | const SizedBox(height: 8), |
| 182 | TextButton( |
| 183 | onPressed: onRetry, |
| 184 | child: const Text('تلاش دوباره'), |
| 185 | ), |
| 186 | ], |
| 187 | ), |
| 188 | ), |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | class TodayPage extends StatefulWidget { |
| 193 | const TodayPage({super.key, required this.api, this.localOnly = false}); |
| 194 | final IdentityApi api; |
| 195 | final bool localOnly; |
| 196 | @override |
| 197 | State<TodayPage> createState() => _TodayPageState(); |
| 198 | } |
| 199 | |
| 200 | class _TodayPageState extends State<TodayPage> { |
| 201 | late Future<LoadResult> future; |
| 202 | final busyItems = <String>{}; |
| 203 | String? operationError; |
| 204 | VoidCallback? retryOperation; |
| 205 | |
| 206 | @override |
| 207 | void initState() { |
| 208 | super.initState(); |
| 209 | future = load(); |
| 210 | } |
| 211 | |
| 212 | Future<LoadResult> load() async { |
| 213 | final now = DateTime.now(); |
| 214 | final from = DateTime(now.year, now.month, now.day); |
| 215 | final to = from.add(const Duration(days: 1)); |
| 216 | try { |
| 217 | final items = await widget.api.getToday(from: from, to: to); |
| 218 | if (widget.localOnly) return LoadResult(items, false); |
| 219 | final reminders = await widget.api.claimDueReminders(); |
| 220 | await OfflineStore.instance.cacheToday(items); |
| 221 | return LoadResult(items, false, reminders: reminders); |
| 222 | } catch (error) { |
| 223 | if (widget.localOnly) return LoadResult(const [], false, error: error); |
| 224 | return LoadResult(await OfflineStore.instance.readToday(), true); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | Future<void> refresh() async { |
| 229 | if (!mounted) return; |
| 230 | setState(() { |
| 231 | future = load(); |
| 232 | }); |
| 233 | await future; |
| 234 | } |
| 235 | |
| 236 | Future<void> complete(Map<String, dynamic> item) async { |
| 237 | final id = item['id'].toString(); |
| 238 | if (busyItems.contains(id)) return; |
| 239 | setState(() { |
| 240 | busyItems.add(id); |
| 241 | operationError = null; |
| 242 | retryOperation = null; |
| 243 | }); |
| 244 | try { |
| 245 | await widget.api.updatePlanItem(id, {'status': 'completed'}); |
| 246 | await refresh(); |
| 247 | } catch (error) { |
| 248 | if (widget.localOnly) { |
| 249 | if (mounted) { |
| 250 | setState(() { |
| 251 | operationError = localDataErrorText(error); |
| 252 | retryOperation = () => complete(item); |
| 253 | }); |
| 254 | } |
| 255 | } else { |
| 256 | try { |
| 257 | await OfflineStore.instance.enqueue({ |
| 258 | 'id': |
| 259 | '${DateTime.now().microsecondsSinceEpoch}-${Random().nextInt(99999)}', |
| 260 | 'entityType': 'plan_item', |
| 261 | 'entityId': item['id'], |
| 262 | 'operation': 'complete', |
| 263 | 'clientUpdatedAt': DateTime.now().toUtc().toIso8601String(), |
| 264 | 'payload': {'status': 'completed'}, |
| 265 | }); |
| 266 | if (mounted) { |
| 267 | ScaffoldMessenger.of(context).showSnackBar(const SnackBar( |
| 268 | content: Text('آفلاین هستی؛ تغییر برای همگامسازی ذخیره شد.'), |
| 269 | )); |
| 270 | } |
| 271 | } catch (queueError) { |
| 272 | if (mounted) { |
| 273 | setState(() { |
| 274 | operationError = _planErrorText(queueError, false); |
| 275 | retryOperation = () => complete(item); |
| 276 | }); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } finally { |
| 281 | if (mounted) setState(() => busyItems.remove(id)); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | @override |
| 286 | Widget build(BuildContext context) => FutureBuilder<LoadResult>( |
| 287 | future: future, |
| 288 | builder: (context, snapshot) { |
| 289 | if (snapshot.hasError) { |
| 290 | return _OperationErrorCard( |
| 291 | message: _planErrorText(snapshot.error!, widget.localOnly), |
| 292 | onRetry: refresh, |
| 293 | ); |
| 294 | } |
| 295 | if (!snapshot.hasData) { |
| 296 | return const Center(child: CircularProgressIndicator()); |
| 297 | } |
| 298 | final result = snapshot.data!; |
| 299 | if (result.error != null) { |
| 300 | return ListView(children: [ |
| 301 | _OperationErrorCard( |
| 302 | message: localDataErrorText(result.error!), |
| 303 | onRetry: refresh, |
| 304 | ), |
| 305 | ]); |
| 306 | } |
| 307 | return RefreshIndicator( |
| 308 | onRefresh: refresh, |
| 309 | child: ListView( |
| 310 | padding: const EdgeInsets.all(16), |
| 311 | children: [ |
| 312 | if (operationError != null) |
| 313 | _OperationErrorCard( |
| 314 | message: operationError!, |
| 315 | onRetry: retryOperation!, |
| 316 | ), |
| 317 | if (result.offline) |
| 318 | const Card( |
| 319 | child: ListTile( |
| 320 | leading: Icon(Icons.cloud_off_outlined), |
| 321 | title: Text('نمایش نسخه آفلاین'), |
| 322 | )), |
| 323 | ...result.reminders.map((reminder) => Card( |
| 324 | child: ListTile( |
| 325 | leading: |
| 326 | const Icon(Icons.notifications_active_outlined), |
| 327 | title: |
| 328 | Text('یادآوری · ${reminder['title'] ?? 'برنامه'}'), |
| 329 | subtitle: |
| 330 | const Text('زمان این مورد رسیده یا نزدیک است.'), |
| 331 | ), |
| 332 | )), |
| 333 | Card( |
| 334 | child: Padding( |
| 335 | padding: const EdgeInsets.all(18), |
| 336 | child: Column( |
| 337 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 338 | children: [ |
| 339 | Text('امروز', |
| 340 | style: Theme.of(context).textTheme.headlineSmall), |
| 341 | const SizedBox(height: 6), |
| 342 | Text('${result.items.length} مورد در برنامه امروز'), |
| 343 | ], |
| 344 | ), |
| 345 | ), |
| 346 | ), |
| 347 | const SizedBox(height: 8), |
| 348 | if (result.items.isEmpty) |
| 349 | const EmptyCard( |
| 350 | icon: Icons.check_circle_outline, |
| 351 | title: 'امروز خلوت است', |
| 352 | subtitle: |
| 353 | 'از برنامهریز یک کار، تکلیف یا جلسه مطالعه اضافه کن.', |
| 354 | ) |
| 355 | else |
| 356 | ...result.items.map((item) => Card( |
| 357 | child: ListTile( |
| 358 | leading: const Icon(Icons.radio_button_unchecked), |
| 359 | title: Text(item['title']?.toString() ?? ''), |
| 360 | subtitle: Text( |
| 361 | '${faKind(item['kind']?.toString() ?? 'task')} · ${shortDate(item['starts_at'] ?? item['due_at'])}', |
| 362 | ), |
| 363 | trailing: IconButton( |
| 364 | tooltip: 'انجام شد', |
| 365 | onPressed: busyItems.contains(item['id'].toString()) |
| 366 | ? null |
| 367 | : () => complete(item), |
| 368 | icon: busyItems.contains(item['id'].toString()) |
| 369 | ? const SizedBox( |
| 370 | width: 20, |
| 371 | height: 20, |
| 372 | child: CircularProgressIndicator( |
| 373 | strokeWidth: 2)) |
| 374 | : const Icon(Icons.check_circle_outline), |
| 375 | ), |
| 376 | ), |
| 377 | )), |
| 378 | ], |
| 379 | ), |
| 380 | ); |
| 381 | }, |
| 382 | ); |
| 383 | } |
| 384 | |
| 385 | class PlannerPage extends StatefulWidget { |
| 386 | const PlannerPage({super.key, required this.api, this.localOnly = false}); |
| 387 | final IdentityApi api; |
| 388 | final bool localOnly; |
| 389 | @override |
| 390 | State<PlannerPage> createState() => _PlannerPageState(); |
| 391 | } |
| 392 | |
| 393 | class _PlannerPageState extends State<PlannerPage> { |
| 394 | late Future<LoadResult> future; |
| 395 | final busyItems = <String>{}; |
| 396 | bool openingForm = false; |
| 397 | String? operationError; |
| 398 | VoidCallback? retryOperation; |
| 399 | |
| 400 | @override |
| 401 | void initState() { |
| 402 | super.initState(); |
| 403 | future = load(); |
| 404 | if (!widget.localOnly) flushQueue(); |
| 405 | } |
| 406 | |
| 407 | Future<void> flushQueue() async { |
| 408 | try { |
| 409 | final queue = await OfflineStore.instance.readQueue(); |
| 410 | if (queue.isEmpty) return; |
| 411 | final result = await widget.api.submitSyncMutations(queue); |
| 412 | final applied = <String>{}; |
| 413 | for (final raw in (result['results'] as List<dynamic>? ?? const [])) { |
| 414 | final row = Map<String, dynamic>.from(raw as Map); |
| 415 | if (row['status'] == 'accepted' || row['status'] == 'already_applied') { |
| 416 | applied.add(row['id'].toString()); |
| 417 | } |
| 418 | } |
| 419 | await OfflineStore.instance.removeQueued(applied); |
| 420 | } catch (_) {} |
| 421 | } |
| 422 | |
| 423 | Future<LoadResult> load() async { |
| 424 | final now = DateTime.now(); |
| 425 | final from = DateTime(now.year, now.month, now.day) |
| 426 | .subtract(const Duration(days: 1)); |
| 427 | final to = from.add(const Duration(days: 15)); |
| 428 | try { |
| 429 | final items = widget.localOnly |
| 430 | ? await widget.api.listPlanItems() |
| 431 | : await widget.api.listPlanItems(from: from, to: to); |
| 432 | if (!widget.localOnly) await OfflineStore.instance.cachePlanner(items); |
| 433 | return LoadResult(items, false); |
| 434 | } catch (error) { |
| 435 | if (widget.localOnly) return LoadResult(const [], false, error: error); |
| 436 | return LoadResult(await OfflineStore.instance.readPlanner(), true); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | Future<void> reload() async { |
| 441 | if (!mounted) return; |
| 442 | setState(() { |
| 443 | future = load(); |
| 444 | }); |
| 445 | await future; |
| 446 | } |
| 447 | |
| 448 | Future<void> showItemForm({Map<String, dynamic>? item}) async { |
| 449 | if (openingForm) return; |
| 450 | setState(() { |
| 451 | openingForm = true; |
| 452 | operationError = null; |
| 453 | retryOperation = null; |
| 454 | }); |
| 455 | try { |
| 456 | String? familyId; |
| 457 | if (!widget.localOnly && item == null) { |
| 458 | final families = await widget.api.listFamilies(); |
| 459 | familyId = families.isEmpty ? null : families.first['id']?.toString(); |
| 460 | } |
| 461 | if (!mounted) return; |
| 462 | final saved = await showDialog<bool>( |
| 463 | context: context, |
| 464 | barrierDismissible: false, |
| 465 | builder: (_) => _PlanItemDialog( |
| 466 | api: widget.api, |
| 467 | localOnly: widget.localOnly, |
| 468 | item: item, |
| 469 | familyId: familyId, |
| 470 | ), |
| 471 | ); |
| 472 | if (saved == true) await reload(); |
| 473 | } catch (error) { |
| 474 | if (mounted) { |
| 475 | setState(() { |
| 476 | operationError = _planErrorText(error, widget.localOnly); |
| 477 | retryOperation = () => showItemForm(item: item); |
| 478 | }); |
| 479 | } |
| 480 | } finally { |
| 481 | if (mounted) setState(() => openingForm = false); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | Future<void> mutate( |
| 486 | Map<String, dynamic> item, |
| 487 | Map<String, dynamic> data, { |
| 488 | String? offlineOperation, |
| 489 | }) async { |
| 490 | final id = item['id'].toString(); |
| 491 | if (busyItems.contains(id)) return; |
| 492 | setState(() { |
| 493 | busyItems.add(id); |
| 494 | operationError = null; |
| 495 | retryOperation = null; |
| 496 | }); |
| 497 | try { |
| 498 | await widget.api.updatePlanItem(id, data); |
| 499 | await reload(); |
| 500 | } catch (error) { |
| 501 | var message = _planErrorText(error, widget.localOnly); |
| 502 | if (!widget.localOnly && offlineOperation != null) { |
| 503 | try { |
| 504 | await OfflineStore.instance.enqueue({ |
| 505 | 'id': |
| 506 | '${DateTime.now().microsecondsSinceEpoch}-${Random().nextInt(99999)}', |
| 507 | 'entityType': 'plan_item', |
| 508 | 'entityId': item['id'], |
| 509 | 'operation': offlineOperation, |
| 510 | 'clientUpdatedAt': DateTime.now().toUtc().toIso8601String(), |
| 511 | 'payload': data, |
| 512 | }); |
| 513 | if (mounted) { |
| 514 | ScaffoldMessenger.of(context).showSnackBar(const SnackBar( |
| 515 | content: Text('آفلاین هستی؛ تغییر برای همگامسازی ذخیره شد.'), |
| 516 | )); |
| 517 | } |
| 518 | return; |
| 519 | } catch (queueError) { |
| 520 | message = _planErrorText(queueError, false); |
| 521 | } |
| 522 | } |
| 523 | if (mounted) { |
| 524 | setState(() { |
| 525 | operationError = message; |
| 526 | retryOperation = |
| 527 | () => mutate(item, data, offlineOperation: offlineOperation); |
| 528 | }); |
| 529 | } |
| 530 | } finally { |
| 531 | if (mounted) setState(() => busyItems.remove(id)); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | Future<void> reschedule(Map<String, dynamic> item) async { |
| 536 | final start = parseDate(item['starts_at']); |
| 537 | final due = parseDate(item['due_at']); |
| 538 | final data = <String, dynamic>{ |
| 539 | if (start != null) |
| 540 | 'startsAt': |
| 541 | start.add(const Duration(days: 1)).toUtc().toIso8601String(), |
| 542 | if (due != null) |
| 543 | 'dueAt': due.add(const Duration(days: 1)).toUtc().toIso8601String(), |
| 544 | if (start == null && due == null) |
| 545 | 'dueAt': DateTime.now() |
| 546 | .add(const Duration(days: 1)) |
| 547 | .toUtc() |
| 548 | .toIso8601String(), |
| 549 | }; |
| 550 | await mutate(item, data, offlineOperation: 'reschedule'); |
| 551 | } |
| 552 | |
| 553 | Future<void> archive(Map<String, dynamic> item) async { |
| 554 | final confirmed = await showDialog<bool>( |
| 555 | context: context, |
| 556 | builder: (dialogContext) => AlertDialog( |
| 557 | title: const Text('بایگانی مورد'), |
| 558 | content: |
| 559 | Text('«${item['title'] ?? ''}» از امروز و برنامه کنار گذاشته شود؟'), |
| 560 | actions: [ |
| 561 | TextButton( |
| 562 | onPressed: () => Navigator.pop(dialogContext, false), |
| 563 | child: const Text('انصراف')), |
| 564 | FilledButton( |
| 565 | onPressed: () => Navigator.pop(dialogContext, true), |
| 566 | child: const Text('بایگانی')), |
| 567 | ], |
| 568 | ), |
| 569 | ); |
| 570 | if (confirmed == true && mounted) { |
| 571 | await mutate(item, {'status': 'cancelled'}); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | @override |
| 576 | Widget build(BuildContext context) => FutureBuilder<LoadResult>( |
| 577 | future: future, |
| 578 | builder: (context, snapshot) { |
| 579 | if (snapshot.hasError) { |
| 580 | return _OperationErrorCard( |
| 581 | message: _planErrorText(snapshot.error!, widget.localOnly), |
| 582 | onRetry: reload, |
| 583 | ); |
| 584 | } |
| 585 | if (!snapshot.hasData) { |
| 586 | return const Center(child: CircularProgressIndicator()); |
| 587 | } |
| 588 | final result = snapshot.data!; |
| 589 | return Scaffold( |
| 590 | backgroundColor: Colors.transparent, |
| 591 | floatingActionButton: result.error == null |
| 592 | ? FloatingActionButton.extended( |
| 593 | onPressed: openingForm ? null : () => showItemForm(), |
| 594 | icon: const Icon(Icons.add), |
| 595 | label: const Text('جدید'), |
| 596 | ) |
| 597 | : null, |
| 598 | body: RefreshIndicator( |
| 599 | onRefresh: reload, |
| 600 | child: ListView( |
| 601 | padding: const EdgeInsets.fromLTRB(16, 16, 16, 96), |
| 602 | children: [ |
| 603 | if (result.error != null) |
| 604 | _OperationErrorCard( |
| 605 | message: localDataErrorText(result.error!), |
| 606 | onRetry: reload, |
| 607 | ) |
| 608 | else ...[ |
| 609 | if (operationError != null) |
| 610 | _OperationErrorCard( |
| 611 | message: operationError!, |
| 612 | onRetry: retryOperation!, |
| 613 | ), |
| 614 | if (result.offline) |
| 615 | const Card( |
| 616 | child: ListTile( |
| 617 | leading: Icon(Icons.cloud_off_outlined), |
| 618 | title: Text('حالت آفلاین'), |
| 619 | subtitle: |
| 620 | Text('آخرین برنامه ذخیرهشده نمایش داده میشود.'), |
| 621 | )), |
| 622 | if (result.items.isEmpty) |
| 623 | const EmptyCard( |
| 624 | icon: Icons.event_note_outlined, |
| 625 | title: 'هنوز برنامهای نداری', |
| 626 | subtitle: |
| 627 | 'کار، تکلیف، رویداد، روتین، هدف یا جلسه مطالعه اضافه کن.', |
| 628 | ) |
| 629 | else |
| 630 | ...result.items.map((item) => Card( |
| 631 | child: ListTile( |
| 632 | title: Text(item['title']?.toString() ?? ''), |
| 633 | subtitle: Text( |
| 634 | '${faKind(item['kind']?.toString() ?? 'task')} · ${shortDate(item['starts_at'] ?? item['due_at'])}', |
| 635 | ), |
| 636 | trailing: busyItems |
| 637 | .contains(item['id'].toString()) |
| 638 | ? const SizedBox( |
| 639 | width: 24, |
| 640 | height: 24, |
| 641 | child: CircularProgressIndicator( |
| 642 | strokeWidth: 2)) |
| 643 | : PopupMenuButton<String>( |
| 644 | tooltip: 'عملیات برنامه', |
| 645 | onSelected: (value) async { |
| 646 | switch (value) { |
| 647 | case 'edit': |
| 648 | await showItemForm(item: item); |
| 649 | case 'complete': |
| 650 | await mutate( |
| 651 | item, {'status': 'completed'}); |
| 652 | case 'tomorrow': |
| 653 | await reschedule(item); |
| 654 | case 'archive': |
| 655 | await archive(item); |
| 656 | } |
| 657 | }, |
| 658 | itemBuilder: (_) => [ |
| 659 | const PopupMenuItem( |
| 660 | value: 'edit', |
| 661 | child: Text('ویرایش عنوان')), |
| 662 | const PopupMenuItem( |
| 663 | value: 'complete', |
| 664 | child: Text('انجام شد')), |
| 665 | const PopupMenuItem( |
| 666 | value: 'tomorrow', |
| 667 | child: Text('انتقال به فردا')), |
| 668 | if (widget.localOnly) |
| 669 | const PopupMenuItem( |
| 670 | value: 'archive', |
| 671 | child: Text('بایگانی')), |
| 672 | ], |
| 673 | ), |
| 674 | ), |
| 675 | )), |
| 676 | ], |
| 677 | ], |
| 678 | ), |
| 679 | ), |
| 680 | ); |
| 681 | }, |
| 682 | ); |
| 683 | } |
| 684 | |
| 685 | class _PlanItemDialog extends StatefulWidget { |
| 686 | const _PlanItemDialog( |
| 687 | {required this.api, required this.localOnly, this.item, this.familyId}); |
| 688 | final IdentityApi api; |
| 689 | final bool localOnly; |
| 690 | final Map<String, dynamic>? item; |
| 691 | final String? familyId; |
| 692 | |
| 693 | @override |
| 694 | State<_PlanItemDialog> createState() => _PlanItemDialogState(); |
| 695 | } |
| 696 | |
| 697 | class _PlanItemDialogState extends State<_PlanItemDialog> { |
| 698 | late final TextEditingController title; |
| 699 | String kind = 'task'; |
| 700 | String visibility = 'private'; |
| 701 | bool saving = false; |
| 702 | String? error; |
| 703 | |
| 704 | @override |
| 705 | void initState() { |
| 706 | super.initState(); |
| 707 | title = |
| 708 | TextEditingController(text: widget.item?['title']?.toString() ?? ''); |
| 709 | kind = widget.item?['kind']?.toString() ?? 'task'; |
| 710 | } |
| 711 | |
| 712 | @override |
| 713 | void dispose() { |
| 714 | title.dispose(); |
| 715 | super.dispose(); |
| 716 | } |
| 717 | |
| 718 | Future<void> save() async { |
| 719 | if (saving) return; |
| 720 | if (title.text.trim().isEmpty) { |
| 721 | setState(() => error = 'عنوان را وارد کن.'); |
| 722 | return; |
| 723 | } |
| 724 | setState(() { |
| 725 | saving = true; |
| 726 | error = null; |
| 727 | }); |
| 728 | try { |
| 729 | if (widget.item != null) { |
| 730 | await widget.api.updatePlanItem(widget.item!['id'].toString(), { |
| 731 | 'title': title.text.trim(), |
| 732 | }); |
| 733 | } else { |
| 734 | final when = DateTime.now().add(const Duration(hours: 1)); |
| 735 | await widget.api.createPlanItem({ |
| 736 | 'kind': kind, |
| 737 | 'title': title.text.trim(), |
| 738 | 'startsAt': when.toUtc().toIso8601String(), |
| 739 | 'dueAt': when.add(const Duration(hours: 1)).toUtc().toIso8601String(), |
| 740 | 'visibility': widget.localOnly ? 'private' : visibility, |
| 741 | if (!widget.localOnly && |
| 742 | visibility != 'private' && |
| 743 | widget.familyId != null) |
| 744 | 'familyId': widget.familyId, |
| 745 | if (!widget.localOnly) 'reminderMinutesBefore': [15], |
| 746 | }); |
| 747 | } |
| 748 | if (mounted) Navigator.pop(context, true); |
| 749 | } catch (failure) { |
| 750 | if (mounted) |
| 751 | setState(() => error = _planErrorText(failure, widget.localOnly)); |
| 752 | } finally { |
| 753 | if (mounted) setState(() => saving = false); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | @override |
| 758 | Widget build(BuildContext context) => PopScope( |
| 759 | canPop: !saving, |
| 760 | child: AlertDialog( |
| 761 | title: Text(widget.item == null ? 'مورد جدید' : 'ویرایش عنوان'), |
| 762 | content: SingleChildScrollView( |
| 763 | child: Column(mainAxisSize: MainAxisSize.min, children: [ |
| 764 | TextField( |
| 765 | key: const Key('plan-title'), |
| 766 | controller: title, |
| 767 | enabled: !saving, |
| 768 | decoration: const InputDecoration(labelText: 'عنوان'), |
| 769 | ), |
| 770 | if (widget.item == null) ...[ |
| 771 | const SizedBox(height: 12), |
| 772 | DropdownButtonFormField<String>( |
| 773 | key: const Key('plan-kind'), |
| 774 | initialValue: kind, |
| 775 | decoration: const InputDecoration(labelText: 'نوع'), |
| 776 | items: const [ |
| 777 | DropdownMenuItem(value: 'task', child: Text('کار')), |
| 778 | DropdownMenuItem(value: 'assignment', child: Text('تکلیف')), |
| 779 | DropdownMenuItem(value: 'event', child: Text('رویداد')), |
| 780 | DropdownMenuItem(value: 'routine', child: Text('روتین')), |
| 781 | DropdownMenuItem(value: 'goal', child: Text('هدف')), |
| 782 | DropdownMenuItem( |
| 783 | value: 'study_session', child: Text('جلسه مطالعه')), |
| 784 | ], |
| 785 | onChanged: saving |
| 786 | ? null |
| 787 | : (value) { |
| 788 | if (value != null) setState(() => kind = value); |
| 789 | }, |
| 790 | ), |
| 791 | if (!widget.localOnly) ...[ |
| 792 | const SizedBox(height: 12), |
| 793 | DropdownButtonFormField<String>( |
| 794 | initialValue: visibility, |
| 795 | decoration: const InputDecoration(labelText: 'اشتراک'), |
| 796 | items: const [ |
| 797 | DropdownMenuItem(value: 'private', child: Text('خصوصی')), |
| 798 | DropdownMenuItem(value: 'family', child: Text('خانواده')), |
| 799 | DropdownMenuItem( |
| 800 | value: 'parent_guardian', |
| 801 | child: Text('والد / سرپرست')), |
| 802 | ], |
| 803 | onChanged: saving |
| 804 | ? null |
| 805 | : (value) { |
| 806 | if (value != null) |
| 807 | setState(() => visibility = value); |
| 808 | }, |
| 809 | ), |
| 810 | ], |
| 811 | ], |
| 812 | if (error != null) ...[ |
| 813 | const SizedBox(height: 12), |
| 814 | Text(error!, |
| 815 | style: |
| 816 | TextStyle(color: Theme.of(context).colorScheme.error)), |
| 817 | ], |
| 818 | ]), |
| 819 | ), |
| 820 | actions: [ |
| 821 | TextButton( |
| 822 | onPressed: saving ? null : () => Navigator.pop(context, false), |
| 823 | child: const Text('انصراف')), |
| 824 | FilledButton( |
| 825 | onPressed: saving ? null : save, |
| 826 | child: Text(saving ? 'در حال ذخیره...' : 'ذخیره')), |
| 827 | ], |
| 828 | ), |
| 829 | ); |
| 830 | } |
| 831 | |
| 832 | class SchoolPage extends StatefulWidget { |
| 833 | const SchoolPage({super.key, required this.api}); |
| 834 | final IdentityApi api; |
| 835 | @override |
| 836 | State<SchoolPage> createState() => _SchoolPageState(); |
| 837 | } |
| 838 | |
| 839 | class _SchoolPageState extends State<SchoolPage> { |
| 840 | late Future<SchoolLoad> future; |
| 841 | @override |
| 842 | void initState() { |
| 843 | super.initState(); |
| 844 | future = load(); |
| 845 | } |
| 846 | |
| 847 | Future<SchoolLoad> load() async { |
| 848 | final profile = await widget.api.getProfile(); |
| 849 | final userId = profile['user_id']?.toString() ?? ''; |
| 850 | if (userId.isEmpty) return SchoolLoad(userId, const {}, false); |
| 851 | try { |
| 852 | final overview = await widget.api.getSchoolOverview(userId); |
| 853 | await OfflineStore.instance.cacheSchool([ |
| 854 | {'userId': userId, 'overview': overview} |
| 855 | ]); |
| 856 | return SchoolLoad(userId, overview, false); |
| 857 | } catch (_) { |
| 858 | final cached = await OfflineStore.instance.readSchool(); |
| 859 | if (cached.isNotEmpty) { |
| 860 | return SchoolLoad( |
| 861 | cached.first['userId']?.toString() ?? userId, |
| 862 | Map<String, dynamic>.from( |
| 863 | cached.first['overview'] as Map? ?? const {}), |
| 864 | true, |
| 865 | ); |
| 866 | } |
| 867 | return SchoolLoad(userId, const {}, true); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | Future<void> reload() async { |
| 872 | setState(() => future = load()); |
| 873 | await future; |
| 874 | } |
| 875 | |
| 876 | Future<void> quickSetup() async { |
| 877 | var contexts = await widget.api.listLifeContexts(); |
| 878 | Map<String, dynamic>? student; |
| 879 | for (final row in contexts) { |
| 880 | if (row['kind'] == 'student') { |
| 881 | student = row; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | student ??= |
| 886 | await widget.api.createLifeContext(kind: 'student', title: 'مدرسه'); |
| 887 | final now = DateTime.now(); |
| 888 | final year = await widget.api.createAcademicYear({ |
| 889 | 'lifeContextId': student['id'], |
| 890 | 'title': |
| 891 | 'سال تحصیلی ' + now.year.toString() + '-' + (now.year + 1).toString(), |
| 892 | 'startsOn': now.year.toString() + '-09-01', |
| 893 | 'endsOn': (now.year + 1).toString() + '-06-30', |
| 894 | }); |
| 895 | final term = await widget.api.createAcademicTerm(year['id'].toString(), { |
| 896 | 'title': 'نیمسال اول', |
| 897 | 'startsOn': now.year.toString() + '-09-01', |
| 898 | 'endsOn': (now.year + 1).toString() + '-01-31', |
| 899 | }); |
| 900 | await widget.api.createSubject(term['id'].toString(), {'name': 'ریاضی'}); |
| 901 | await reload(); |
| 902 | } |
| 903 | |
| 904 | Future<void> addSchoolItem(String kind) async { |
| 905 | final data = await future; |
| 906 | final subjects = data.overview['subjects'] as List<dynamic>? ?? const []; |
| 907 | if (subjects.isEmpty) return; |
| 908 | final title = TextEditingController(); |
| 909 | final first = Map<String, dynamic>.from(subjects.first as Map); |
| 910 | final selected = ValueNotifier<String>(first['id'].toString()); |
| 911 | final saved = await showDialog<bool>( |
| 912 | context: context, |
| 913 | builder: (dialogContext) => AlertDialog( |
| 914 | title: Text(kind == 'exam' ? 'امتحان جدید' : 'تکلیف جدید'), |
| 915 | content: Column(mainAxisSize: MainAxisSize.min, children: [ |
| 916 | TextField( |
| 917 | controller: title, |
| 918 | decoration: const InputDecoration(labelText: 'عنوان')), |
| 919 | const SizedBox(height: 12), |
| 920 | ValueListenableBuilder<String>( |
| 921 | valueListenable: selected, |
| 922 | builder: (_, value, __) => DropdownButtonFormField<String>( |
| 923 | initialValue: value, |
| 924 | decoration: const InputDecoration(labelText: 'درس'), |
| 925 | items: subjects.map((raw) { |
| 926 | final row = Map<String, dynamic>.from(raw as Map); |
| 927 | return DropdownMenuItem<String>( |
| 928 | value: row['id'].toString(), |
| 929 | child: Text(row['name']?.toString() ?? ''), |
| 930 | ); |
| 931 | }).toList(), |
| 932 | onChanged: (v) { |
| 933 | if (v != null) selected.value = v; |
| 934 | }, |
| 935 | ), |
| 936 | ), |
| 937 | ]), |
| 938 | actions: [ |
| 939 | TextButton( |
| 940 | onPressed: () => Navigator.pop(dialogContext, false), |
| 941 | child: const Text('انصراف')), |
| 942 | FilledButton( |
| 943 | onPressed: () async { |
| 944 | if (title.text.trim().isEmpty) return; |
| 945 | final due = DateTime.now().add(const Duration(days: 2)); |
| 946 | await widget.api.createPlanItem({ |
| 947 | 'kind': kind, |
| 948 | 'title': title.text.trim(), |
| 949 | 'subjectId': selected.value, |
| 950 | 'dueAt': due.toUtc().toIso8601String(), |
| 951 | 'visibility': 'private', |
| 952 | 'reminderMinutesBefore': [60, 1440], |
| 953 | }); |
| 954 | if (dialogContext.mounted) Navigator.pop(dialogContext, true); |
| 955 | }, |
| 956 | child: const Text('ذخیره'), |
| 957 | ), |
| 958 | ], |
| 959 | ), |
| 960 | ); |
| 961 | title.dispose(); |
| 962 | selected.dispose(); |
| 963 | if (saved == true && mounted) await reload(); |
| 964 | } |
| 965 | |
| 966 | @override |
| 967 | Widget build(BuildContext context) => FutureBuilder<SchoolLoad>( |
| 968 | future: future, |
| 969 | builder: (context, snapshot) { |
| 970 | if (!snapshot.hasData) |
| 971 | return const Center(child: CircularProgressIndicator()); |
| 972 | final data = snapshot.data!; |
| 973 | final years = data.overview['years'] as List<dynamic>? ?? const []; |
| 974 | final subjects = |
| 975 | data.overview['subjects'] as List<dynamic>? ?? const []; |
| 976 | final workload = |
| 977 | data.overview['workload'] as List<dynamic>? ?? const []; |
| 978 | final grades = data.overview['grades'] as List<dynamic>? ?? const []; |
| 979 | return ListView( |
| 980 | padding: const EdgeInsets.all(16), |
| 981 | children: [ |
| 982 | if (data.offline) |
| 983 | const Card( |
| 984 | child: ListTile( |
| 985 | leading: Icon(Icons.cloud_off_outlined), |
| 986 | title: Text('اطلاعات مدرسه از کش نمایش داده میشود'))), |
| 987 | if (years.isEmpty) |
| 988 | EmptyCard( |
| 989 | icon: Icons.school_outlined, |
| 990 | title: 'مدرسه را آماده کن', |
| 991 | subtitle: |
| 992 | 'سال تحصیلی، نیمسال و اولین درس را با شروع سریع بساز.', |
| 993 | action: FilledButton.icon( |
| 994 | onPressed: quickSetup, |
| 995 | icon: const Icon(Icons.auto_fix_high), |
| 996 | label: const Text('شروع سریع'), |
| 997 | ), |
| 998 | ) |
| 999 | else ...[ |
| 1000 | Card( |
| 1001 | child: Padding( |
| 1002 | padding: const EdgeInsets.all(16), |
| 1003 | child: Column( |
| 1004 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 1005 | children: [ |
| 1006 | Text('مدرسه', |
| 1007 | style: Theme.of(context).textTheme.headlineSmall), |
| 1008 | const SizedBox(height: 10), |
| 1009 | Wrap(spacing: 8, runSpacing: 8, children: [ |
| 1010 | Chip( |
| 1011 | label: |
| 1012 | Text(subjects.length.toString() + ' درس')), |
| 1013 | Chip( |
| 1014 | label: Text(workload.length.toString() + |
| 1015 | ' کار درسی پیشرو')), |
| 1016 | Chip( |
| 1017 | label: |
| 1018 | Text(grades.length.toString() + ' نمره')), |
| 1019 | ]), |
| 1020 | const SizedBox(height: 12), |
| 1021 | Row(children: [ |
| 1022 | Expanded( |
| 1023 | child: FilledButton.icon( |
| 1024 | onPressed: () => addSchoolItem('assignment'), |
| 1025 | icon: const Icon(Icons.assignment_add), |
| 1026 | label: const Text('تکلیف'), |
| 1027 | )), |
| 1028 | const SizedBox(width: 8), |
| 1029 | Expanded( |
| 1030 | child: OutlinedButton.icon( |
| 1031 | onPressed: () => addSchoolItem('exam'), |
| 1032 | icon: const Icon(Icons.quiz_outlined), |
| 1033 | label: const Text('امتحان'), |
| 1034 | )), |
| 1035 | ]), |
| 1036 | ]), |
| 1037 | ), |
| 1038 | ), |
| 1039 | const SizedBox(height: 12), |
| 1040 | Text('درسها', style: Theme.of(context).textTheme.titleMedium), |
| 1041 | ...subjects.map((raw) { |
| 1042 | final row = Map<String, dynamic>.from(raw as Map); |
| 1043 | return Card( |
| 1044 | child: ListTile( |
| 1045 | leading: const Icon(Icons.menu_book_outlined), |
| 1046 | title: Text(row['name']?.toString() ?? ''), |
| 1047 | subtitle: Text(row['teacher_name']?.toString() ?? |
| 1048 | row['term_title']?.toString() ?? |
| 1049 | ''), |
| 1050 | )); |
| 1051 | }), |
| 1052 | const SizedBox(height: 12), |
| 1053 | Text('کارهای درسی پیشرو', |
| 1054 | style: Theme.of(context).textTheme.titleMedium), |
| 1055 | ...workload.map((raw) { |
| 1056 | final row = Map<String, dynamic>.from(raw as Map); |
| 1057 | return Card( |
| 1058 | child: ListTile( |
| 1059 | title: Text(row['title']?.toString() ?? ''), |
| 1060 | subtitle: Text(faKind(row['kind']?.toString() ?? '') + |
| 1061 | ' · ' + |
| 1062 | shortDate(row['due_at'] ?? row['starts_at'])), |
| 1063 | )); |
| 1064 | }), |
| 1065 | ], |
| 1066 | ], |
| 1067 | ); |
| 1068 | }, |
| 1069 | ); |
| 1070 | } |
| 1071 | |
| 1072 | class ParentFamilyDashboard extends StatefulWidget { |
| 1073 | const ParentFamilyDashboard({super.key, required this.api}); |
| 1074 | final IdentityApi api; |
| 1075 | @override |
| 1076 | State<ParentFamilyDashboard> createState() => _ParentFamilyDashboardState(); |
| 1077 | } |
| 1078 | |
| 1079 | class _ParentFamilyDashboardState extends State<ParentFamilyDashboard> { |
| 1080 | late Future<List<Map<String, dynamic>>> families; |
| 1081 | @override |
| 1082 | void initState() { |
| 1083 | super.initState(); |
| 1084 | families = widget.api.listFamilies(); |
| 1085 | } |
| 1086 | |
| 1087 | @override |
| 1088 | Widget build(BuildContext context) => |
| 1089 | FutureBuilder<List<Map<String, dynamic>>>( |
| 1090 | future: families, |
| 1091 | builder: (context, snapshot) { |
| 1092 | if (!snapshot.hasData) |
| 1093 | return const Center(child: CircularProgressIndicator()); |
| 1094 | if (snapshot.data!.isEmpty) { |
| 1095 | return const EmptyCard( |
| 1096 | icon: Icons.family_restroom, |
| 1097 | title: 'خانوادهای متصل نیست', |
| 1098 | subtitle: 'از بخش پروفایل فضای خانواده بساز یا به آن بپیوند.', |
| 1099 | ); |
| 1100 | } |
| 1101 | return ListView( |
| 1102 | padding: const EdgeInsets.all(16), |
| 1103 | children: snapshot.data! |
| 1104 | .map((family) => |
| 1105 | FamilyDashboardCard(api: widget.api, family: family)) |
| 1106 | .toList(), |
| 1107 | ); |
| 1108 | }, |
| 1109 | ); |
| 1110 | } |
| 1111 | |
| 1112 | class FamilyDashboardCard extends StatefulWidget { |
| 1113 | const FamilyDashboardCard( |
| 1114 | {super.key, required this.api, required this.family}); |
| 1115 | final IdentityApi api; |
| 1116 | final Map<String, dynamic> family; |
| 1117 | @override |
| 1118 | State<FamilyDashboardCard> createState() => _FamilyDashboardCardState(); |
| 1119 | } |
| 1120 | |
| 1121 | class _FamilyDashboardCardState extends State<FamilyDashboardCard> { |
| 1122 | late Future<List<Map<String, dynamic>>> members; |
| 1123 | @override |
| 1124 | void initState() { |
| 1125 | super.initState(); |
| 1126 | members = widget.api.listFamilyMembers(widget.family['id'].toString()); |
| 1127 | } |
| 1128 | |
| 1129 | Future<void> openFamilyGuidance( |
| 1130 | String familyId, |
| 1131 | String childId, |
| 1132 | String childName, |
| 1133 | ) async { |
| 1134 | final question = TextEditingController(); |
| 1135 | String? advice; |
| 1136 | bool busy = false; |
| 1137 | await showDialog<void>( |
| 1138 | context: context, |
| 1139 | builder: (dialogContext) => StatefulBuilder( |
| 1140 | builder: (context, setLocalState) => AlertDialog( |
| 1141 | title: Text('مشورت درباره $childName'), |
| 1142 | content: SizedBox( |
| 1143 | width: 520, |
| 1144 | child: Column( |
| 1145 | mainAxisSize: MainAxisSize.min, |
| 1146 | children: [ |
| 1147 | const Text( |
| 1148 | 'راهنما فقط از خلاصههای مجاز استفاده میکند و متن خصوصی فرزند را نمیبیند.', |
| 1149 | ), |
| 1150 | const SizedBox(height: 12), |
| 1151 | TextField( |
| 1152 | controller: question, |
| 1153 | maxLines: 4, |
| 1154 | decoration: const InputDecoration( |
| 1155 | labelText: 'سؤال شما', |
| 1156 | hintText: |
| 1157 | 'مثلاً چطور بدون فشار درباره امتحانها با او صحبت کنم؟', |
| 1158 | ), |
| 1159 | ), |
| 1160 | if (advice != null) ...[ |
| 1161 | const SizedBox(height: 14), |
| 1162 | Align( |
| 1163 | alignment: Alignment.centerRight, |
| 1164 | child: Text( |
| 1165 | advice!, |
| 1166 | style: Theme.of(context).textTheme.bodyLarge, |
| 1167 | ), |
| 1168 | ), |
| 1169 | ], |
| 1170 | ], |
| 1171 | ), |
| 1172 | ), |
| 1173 | actions: [ |
| 1174 | TextButton( |
| 1175 | onPressed: busy ? null : () => Navigator.pop(dialogContext), |
| 1176 | child: const Text('بستن'), |
| 1177 | ), |
| 1178 | FilledButton( |
| 1179 | onPressed: busy |
| 1180 | ? null |
| 1181 | : () async { |
| 1182 | if (question.text.trim().isEmpty) return; |
| 1183 | setLocalState(() => busy = true); |
| 1184 | try { |
| 1185 | final result = await widget.api.requestFamilyGuidance( |
| 1186 | familyId, |
| 1187 | childId, |
| 1188 | question.text.trim(), |
| 1189 | ); |
| 1190 | setLocalState(() { |
| 1191 | advice = result['advice']?.toString() ?? |
| 1192 | 'پیشنهادی دریافت نشد.'; |
| 1193 | }); |
| 1194 | } catch (_) { |
| 1195 | setLocalState(() { |
| 1196 | advice = |
| 1197 | 'دریافت راهنمایی انجام نشد. دوباره تلاش کنید.'; |
| 1198 | }); |
| 1199 | } finally { |
| 1200 | setLocalState(() => busy = false); |
| 1201 | } |
| 1202 | }, |
| 1203 | child: Text(busy ? 'در حال بررسی...' : 'دریافت راهنمایی'), |
| 1204 | ), |
| 1205 | ], |
| 1206 | ), |
| 1207 | ), |
| 1208 | ); |
| 1209 | question.dispose(); |
| 1210 | } |
| 1211 | |
| 1212 | Future<void> openChild(Map<String, dynamic> child) async { |
| 1213 | final familyId = widget.family['id'].toString(); |
| 1214 | final childId = child['user_id'].toString(); |
| 1215 | final summary = await widget.api.getChildSupportSummary(familyId, childId); |
| 1216 | Map<String, dynamic>? wellbeing; |
| 1217 | try { |
| 1218 | wellbeing = |
| 1219 | await widget.api.getGuardianWellbeingSummary(familyId, childId); |
| 1220 | } catch (_) { |
| 1221 | wellbeing = null; |
| 1222 | } |
| 1223 | if (!mounted) return; |
| 1224 | final metrics = |
| 1225 | Map<String, dynamic>.from(summary['metrics'] as Map? ?? const {}); |
| 1226 | final upcoming = summary['upcoming'] as List<dynamic>? ?? const []; |
| 1227 | final wellbeingSummary = wellbeing == null |
| 1228 | ? const <String, dynamic>{} |
| 1229 | : Map<String, dynamic>.from( |
| 1230 | wellbeing['summary'] as Map? ?? const <String, dynamic>{}, |
| 1231 | ); |
| 1232 | final safety = wellbeing == null |
| 1233 | ? const <String, dynamic>{} |
| 1234 | : Map<String, dynamic>.from( |
| 1235 | wellbeing['safety'] as Map? ?? const <String, dynamic>{}, |
| 1236 | ); |
| 1237 | await showDialog<void>( |
| 1238 | context: context, |
| 1239 | builder: (dialogContext) => AlertDialog( |
| 1240 | title: Text( |
| 1241 | 'خلاصه حمایت · ' + (child['display_name']?.toString() ?? 'فرزند')), |
| 1242 | content: SizedBox( |
| 1243 | width: 520, |
| 1244 | child: SingleChildScrollView( |
| 1245 | child: Column( |
| 1246 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 1247 | children: [ |
| 1248 | Wrap(spacing: 8, runSpacing: 8, children: [ |
| 1249 | Chip( |
| 1250 | label: Text((metrics['overdue'] ?? 0).toString() + |
| 1251 | ' عقبافتاده')), |
| 1252 | Chip( |
| 1253 | label: Text( |
| 1254 | (metrics['completedLast7Days'] ?? 0).toString() + |
| 1255 | ' انجامشده')), |
| 1256 | Chip( |
| 1257 | label: Text( |
| 1258 | (metrics['studyMinutesLast7Days'] ?? 0).toString() + |
| 1259 | ' دقیقه مطالعه')), |
| 1260 | if (metrics['gradePercent'] != null) |
| 1261 | Chip( |
| 1262 | label: Text('میانگین ' + |
| 1263 | metrics['gradePercent'].toString() + |
| 1264 | '٪')), |
| 1265 | ]), |
| 1266 | if (wellbeing != null) ...[ |
| 1267 | const SizedBox(height: 14), |
| 1268 | Text('خلاصه حال خوب', |
| 1269 | style: Theme.of(context).textTheme.titleMedium), |
| 1270 | const SizedBox(height: 8), |
| 1271 | Wrap(spacing: 8, runSpacing: 8, children: [ |
| 1272 | Chip( |
| 1273 | label: Text( |
| 1274 | (wellbeingSummary['checkin_count'] ?? 0).toString() + |
| 1275 | ' ثبت مجاز', |
| 1276 | ), |
| 1277 | ), |
| 1278 | if (wellbeingSummary['mood_average'] != null) |
| 1279 | Chip( |
| 1280 | label: Text( |
| 1281 | 'حال ' + |
| 1282 | wellbeingSummary['mood_average'].toString() + |
| 1283 | '/5', |
| 1284 | ), |
| 1285 | ), |
| 1286 | if (wellbeingSummary['stress_average'] != null) |
| 1287 | Chip( |
| 1288 | label: Text( |
| 1289 | 'استرس ' + |
| 1290 | wellbeingSummary['stress_average'].toString() + |
| 1291 | '/5', |
| 1292 | ), |
| 1293 | ), |
| 1294 | if ((safety['open_urgent_count'] ?? 0) != 0) |
| 1295 | Chip( |
| 1296 | avatar: |
| 1297 | const Icon(Icons.warning_amber_rounded, size: 18), |
| 1298 | label: Text( |
| 1299 | safety['open_urgent_count'].toString() + |
| 1300 | ' هشدار ایمنی نیازمند توجه', |
| 1301 | ), |
| 1302 | ), |
| 1303 | ]), |
| 1304 | const SizedBox(height: 6), |
| 1305 | const Text( |
| 1306 | 'این بخش فقط خلاصه مجاز و سیگنال ایمنی را نشان میدهد؛ متن خصوصی گفتوگو یا یادداشت نمایش داده نمیشود.', |
| 1307 | ), |
| 1308 | ], |
| 1309 | const SizedBox(height: 10), |
| 1310 | const Text('موارد پیشرو'), |
| 1311 | ...upcoming.take(8).map((raw) { |
| 1312 | final row = Map<String, dynamic>.from(raw as Map); |
| 1313 | return ListTile( |
| 1314 | dense: true, |
| 1315 | title: Text(row['title']?.toString() ?? ''), |
| 1316 | subtitle: Text(faKind(row['kind']?.toString() ?? '') + |
| 1317 | ' · ' + |
| 1318 | shortDate(row['due_at'] ?? row['starts_at'])), |
| 1319 | ); |
| 1320 | }), |
| 1321 | ]), |
| 1322 | ), |
| 1323 | ), |
| 1324 | actions: [ |
| 1325 | TextButton( |
| 1326 | onPressed: () => Navigator.pop(dialogContext), |
| 1327 | child: const Text('بستن'), |
| 1328 | ), |
| 1329 | FilledButton.icon( |
| 1330 | onPressed: () { |
| 1331 | Navigator.pop(dialogContext); |
| 1332 | openFamilyGuidance( |
| 1333 | familyId, |
| 1334 | childId, |
| 1335 | child['display_name']?.toString() ?? 'فرزند', |
| 1336 | ); |
| 1337 | }, |
| 1338 | icon: const Icon(Icons.psychology_alt_outlined), |
| 1339 | label: const Text('مشورت با راهنمای خانواده'), |
| 1340 | ), |
| 1341 | ], |
| 1342 | ), |
| 1343 | ); |
| 1344 | } |
| 1345 | |
| 1346 | @override |
| 1347 | Widget build(BuildContext context) => Card( |
| 1348 | child: Padding( |
| 1349 | padding: const EdgeInsets.all(14), |
| 1350 | child: |
| 1351 | Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [ |
| 1352 | Text(widget.family['name']?.toString() ?? 'خانواده', |
| 1353 | style: Theme.of(context).textTheme.titleLarge), |
| 1354 | const SizedBox(height: 8), |
| 1355 | FutureBuilder<List<Map<String, dynamic>>>( |
| 1356 | future: members, |
| 1357 | builder: (context, snapshot) { |
| 1358 | if (!snapshot.hasData) return const LinearProgressIndicator(); |
| 1359 | final children = snapshot.data! |
| 1360 | .where((row) => row['role'] == 'teen_minor') |
| 1361 | .toList(); |
| 1362 | if (children.isEmpty) |
| 1363 | return const Text( |
| 1364 | 'هنوز فرزند/نوجوانی به این خانواده متصل نیست.'); |
| 1365 | return Column( |
| 1366 | children: children |
| 1367 | .map((child) => ListTile( |
| 1368 | leading: const CircleAvatar( |
| 1369 | child: Icon(Icons.school_outlined)), |
| 1370 | title: Text( |
| 1371 | child['display_name']?.toString() ?? 'فرزند'), |
| 1372 | subtitle: const Text( |
| 1373 | 'برنامه، تکالیف، امتحانها و پیشرفت مجاز'), |
| 1374 | trailing: const Icon(Icons.chevron_left), |
| 1375 | onTap: () => openChild(child), |
| 1376 | )) |
| 1377 | .toList(), |
| 1378 | ); |
| 1379 | }, |
| 1380 | ), |
| 1381 | ]), |
| 1382 | ), |
| 1383 | ); |
| 1384 | } |
| 1385 | |
| 1386 | class EmptyCard extends StatelessWidget { |
| 1387 | const EmptyCard({ |
| 1388 | super.key, |
| 1389 | required this.icon, |
| 1390 | required this.title, |
| 1391 | required this.subtitle, |
| 1392 | this.action, |
| 1393 | }); |
| 1394 | final IconData icon; |
| 1395 | final String title; |
| 1396 | final String subtitle; |
| 1397 | final Widget? action; |
| 1398 | |
| 1399 | @override |
| 1400 | Widget build(BuildContext context) => Card( |
| 1401 | child: Padding( |
| 1402 | padding: const EdgeInsets.all(22), |
| 1403 | child: Column(children: [ |
| 1404 | Icon(icon, size: 42), |
| 1405 | const SizedBox(height: 10), |
| 1406 | Text(title, style: Theme.of(context).textTheme.titleMedium), |
| 1407 | const SizedBox(height: 6), |
| 1408 | Text(subtitle, textAlign: TextAlign.center), |
| 1409 | if (action != null) ...[const SizedBox(height: 14), action!], |
| 1410 | ]), |
| 1411 | ), |
| 1412 | ); |
| 1413 | } |
| 1414 | |
| 1415 | class LoadResult { |
| 1416 | const LoadResult(this.items, this.offline, |
| 1417 | {this.reminders = const [], this.error}); |
| 1418 | final List<Map<String, dynamic>> items; |
| 1419 | final bool offline; |
| 1420 | final List<Map<String, dynamic>> reminders; |
| 1421 | final Object? error; |
| 1422 | } |
| 1423 | |
| 1424 | class SchoolLoad { |
| 1425 | const SchoolLoad(this.userId, this.overview, this.offline); |
| 1426 | final String userId; |
| 1427 | final Map<String, dynamic> overview; |
| 1428 | final bool offline; |
| 1429 | } |