LifeMate — Personal, Family & Learning Companion

fix/study-duration-display
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
dart 99 lines 3.35 KB
Raw
1 import 'dart:async';
2 import 'package:flutter/material.dart';
3 import 'api.dart';
4 import 'notification_delivery.dart';
5
6 class NotificationInboxPage extends StatefulWidget {
7 const NotificationInboxPage({super.key, required this.api, this.delivery});
8 final IdentityApi api;
9 final NotificationDeliveryPort? delivery;
10 @override
11 State<NotificationInboxPage> createState() => _NotificationInboxPageState();
12 }
13
14 class _NotificationInboxPageState extends State<NotificationInboxPage>
15 with WidgetsBindingObserver {
16 Timer? timer;
17 List<Map<String, dynamic>> items = [];
18 Object? error;
19 DateTime? refreshedAt;
20 bool loading = false;
21 late final NotificationDeliveryPort? delivery;
22 @override
23 void initState() {
24 super.initState();
25 delivery = widget.delivery ??
26 (widget.api is HttpIdentityApi
27 ? PollingInAppNotificationDelivery(widget.api as HttpIdentityApi)
28 : null);
29 WidgetsBinding.instance.addObserver(this);
30 reload();
31 timer = Timer.periodic(const Duration(seconds: 30), (_) => reload());
32 }
33
34 @override
35 void didChangeAppLifecycleState(AppLifecycleState state) {
36 timer?.cancel();
37 if (state == AppLifecycleState.resumed) {
38 reload();
39 timer = Timer.periodic(const Duration(seconds: 30), (_) => reload());
40 }
41 }
42
43 Future<void> reload() async {
44 if (loading) return;
45 setState(() => loading = true);
46 try {
47 if (delivery == null) {
48 throw const ApiException(503, 'notification_provider_unavailable');
49 }
50 final next = await delivery!.fetch();
51 if (!mounted) return;
52 setState(() {
53 items = next;
54 error = null;
55 refreshedAt = DateTime.now();
56 });
57 } catch (failure) {
58 if (mounted) setState(() => error = failure);
59 } finally {
60 if (mounted) setState(() => loading = false);
61 }
62 }
63
64 @override
65 void dispose() {
66 timer?.cancel();
67 WidgetsBinding.instance.removeObserver(this);
68 super.dispose();
69 }
70
71 @override
72 Widget build(BuildContext context) => Scaffold(
73 appBar: AppBar(title: const Text('یادآوری‌های لایف‌گاید'), actions: [
74 IconButton(
75 onPressed: loading ? null : reload,
76 icon: const Icon(Icons.refresh))
77 ]),
78 body: ListView(padding: const EdgeInsets.all(16), children: [
79 const Text(
80 'این یادآوری‌ها هنگام باز بودن برنامه تازه می‌شوند. اعلان پس‌زمینه فعال نیست.'),
81 if (loading) const LinearProgressIndicator(),
82 if (error != null)
83 const Text(
84 'تازه‌سازی انجام نشد؛ موارد زیر آخرین داده دریافت‌شده‌اند، زنده نیستند.'),
85 if (refreshedAt != null)
86 Text('آخرین دریافت: ${refreshedAt!.toLocal()}'),
87 if (!loading && items.isEmpty) const Text('یادآوری دریافت نشده است.'),
88 ...items.map((item) {
89 final payload =
90 item['payload'] is Map ? item['payload'] as Map : const {};
91 return Card(
92 child: ListTile(
93 title:
94 Text(payload['title']?.toString() ?? 'یادآوری برنامه'),
95 subtitle: Text(item['created_at']?.toString() ?? '')));
96 }),
97 ]),
98 );
99 }