Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixUI/hesabix_ui/lib/widgets/notification/notification_bell_button.dart

424 lines
18 KiB
Dart
Executable file

import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hesabix_ui/l10n/app_localizations.dart';
import 'package:hesabix_ui/utils/error_extractor.dart';
import '../../core/api_client.dart';
import '../../core/auth_store.dart';
import '../../services/announcements_service.dart';
import '../../services/in_app_notification_preferences_controller.dart';
import '../../services/in_app_notifications_hub.dart';
import '../../utils/announcement_navigation.dart';
import '../../utils/snackbar_helper.dart';
import 'package:hesabix_ui/theme/semantic_color_resolver.dart';
String _localizedAnnouncementLevel(BuildContext context, String raw) {
final t = AppLocalizations.of(context);
final level = raw.toLowerCase().trim();
switch (level) {
case 'info':
return t.notificationCenterLevelInfo;
case 'warning':
return t.notificationCenterLevelWarning;
case 'critical':
return t.notificationCenterLevelCritical;
default:
return t.notificationCenterLevelUnknown(raw);
}
}
/// دکمهٔ زنگولهٔ اعلانات با badge و دیالوگ مرکز اعلان.
/// در پنل کاربر و پنل کسب‌وکار قابل استفاده است.
///
/// اتصال WebSocket و اعلان سیستم اندروید توسط [InAppNotificationsHub] مدیریت می‌شود.
class NotificationBellButton extends StatefulWidget {
final AuthStore authStore;
final Color? iconColor;
/// هم‌خط با آیکن‌های نوار ابزار در [BusinessShell] (ارتفاع ۴۴).
final bool denseToolbar;
const NotificationBellButton({
super.key,
required this.authStore,
this.iconColor,
this.denseToolbar = false,
});
@override
State<NotificationBellButton> createState() => _NotificationBellButtonState();
}
class _NotificationBellButtonState extends State<NotificationBellButton> {
final Set<int> _busyAnnIds = <int>{};
bool _clearingAll = false;
InAppNotificationsHub get _hub => InAppNotificationsHub.instance;
void _onHubChanged() {
if (mounted) setState(() {});
}
void _onPrefsChanged() {
if (mounted) setState(() {});
}
void _onForegroundAlert(Map<String, dynamic> item) {
if (!mounted) return;
final title = '${item['title'] ?? 'پیام'}';
final body = '${item['body'] ?? ''}';
final messenger = ScaffoldMessenger.of(Navigator.of(context, rootNavigator: true).context);
messenger.hideCurrentSnackBar();
messenger.showSnackBar(
SnackBar(
content: Text('$title: $body'),
duration: const Duration(seconds: 4),
action: AnnouncementNavigation.hasNavigableTarget(item)
? SnackBarAction(
label: 'مشاهده',
onPressed: () {
unawaited(
AnnouncementNavigation.handleTap(
context,
item,
onMarkedRead: (id) => _hub.markLocallyRead(id),
),
);
},
)
: null,
),
);
}
@override
void initState() {
super.initState();
InAppNotificationPreferencesController.instance.addListener(_onPrefsChanged);
unawaited(InAppNotificationPreferencesController.instance.refreshFromApi());
_hub.addListener(_onHubChanged);
_hub.addForegroundAlertListener(_onForegroundAlert);
final apiKey = widget.authStore.apiKey;
if (apiKey != null && apiKey.isNotEmpty) {
unawaited(_hub.startForApiKey(apiKey));
}
}
@override
void dispose() {
InAppNotificationPreferencesController.instance.removeListener(_onPrefsChanged);
_hub.removeListener(_onHubChanged);
_hub.removeForegroundAlertListener(_onForegroundAlert);
super.dispose();
}
Future<void> _confirmClearAllNotifications(BuildContext dialogContext, StateSetter dialogSetState) async {
final t = AppLocalizations.of(dialogContext);
final ok = await showDialog<bool>(
context: dialogContext,
builder: (ctx) => AlertDialog(
title: Text(t.notificationCenterClearAllTitle),
content: Text(t.notificationCenterClearAllMessage),
actions: [
TextButton(onPressed: () => Navigator.of(ctx).pop(false), child: Text(t.notificationCenterClearAllCancel)),
FilledButton(onPressed: () => Navigator.of(ctx).pop(true), child: Text(t.notificationCenterClearAllConfirm)),
],
),
);
if (ok != true || !mounted) return;
setState(() => _clearingAll = true);
dialogSetState(() {});
try {
await AnnouncementsService(ApiClient()).clearAllVisible();
if (!mounted) return;
_hub.clearLocal();
setState(() => _clearingAll = false);
dialogSetState(() {});
if (mounted) {
SnackBarHelper.show(context, message: t.notificationCenterCleared);
}
} catch (e) {
if (mounted) {
setState(() => _clearingAll = false);
dialogSetState(() {});
SnackBarHelper.showError(context, message: ErrorExtractor.forContext(e, context));
}
}
}
void _openNotificationCenter() {
showDialog<void>(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, dialogSetState) {
final items = _hub.notifications.take(10).toList();
final ColorScheme cs = Theme.of(context).colorScheme;
final bool isDark = Theme.of(context).brightness == Brightness.dark;
final Color headerOn = isDark ? cs.onSurface : Colors.white;
final double dialogWidth = math.max(
280,
math.min(MediaQuery.sizeOf(context).width - 88, 1200),
);
return AlertDialog(
constraints: BoxConstraints(minWidth: dialogWidth, maxWidth: dialogWidth),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
titlePadding: EdgeInsets.zero,
title: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: isDark ? cs.surfaceContainerHighest : null,
gradient: isDark
? null
: LinearGradient(colors: [cs.primary, cs.primary.withValues(alpha: 0.8)]),
borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
),
child: Row(
children: [
Icon(Icons.notifications_active, color: headerOn),
const SizedBox(width: 8),
Text('مرکز اعلان‌ها', style: TextStyle(color: headerOn, fontWeight: FontWeight.bold)),
const Spacer(),
IconButton(
tooltip: AppLocalizations.of(context).notificationCenterClearAllTooltip,
icon: _clearingAll
? SizedBox(width: 22, height: 22, child: CircularProgressIndicator(strokeWidth: 2, color: headerOn))
: Icon(Icons.delete_outline, color: headerOn),
onPressed: _clearingAll ? null : () => _confirmClearAllNotifications(context, dialogSetState),
),
IconButton(
tooltip: 'بستن',
icon: Icon(Icons.close, color: headerOn),
onPressed: () => Navigator.of(context).pop(),
),
],
),
),
content: SizedBox(
width: double.infinity,
height: 420,
child: items.isEmpty
? const Center(child: Text('اعلانی وجود ندارد'))
: ListView.separated(
padding: EdgeInsets.zero,
shrinkWrap: true,
itemBuilder: (_, i) {
final it = items[i];
final level = '${it['level'] ?? 'info'}'.toLowerCase().trim();
IconData icon;
Color levelColor;
switch (level) {
case 'warning':
icon = Icons.warning_amber_rounded;
levelColor = SemanticColorResolver.warning(context);
break;
case 'critical':
icon = Icons.error_outline;
levelColor = SemanticColorResolver.negative(context);
break;
default:
icon = Icons.notifications_none;
levelColor = cs.primary;
}
final int? annId = AnnouncementNavigation.parseAnnouncementId(it['id']);
final bool canNavigate = AnnouncementNavigation.hasNavigableTarget(it);
final bool busy = annId != null && _busyAnnIds.contains(annId);
return InkWell(
onTap: canNavigate
? () async {
await AnnouncementNavigation.handleTap(
context,
it,
onBeforeNavigate: () => Navigator.of(context).pop(),
onMarkedRead: (id) {
_hub.markLocallyRead(id);
dialogSetState(() {});
},
);
}
: null,
borderRadius: BorderRadius.circular(12),
child: Container(
decoration: BoxDecoration(
color: cs.surface,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(color: cs.shadow.withValues(alpha: 0.05), blurRadius: 6, offset: const Offset(0, 2)),
],
border: Border(left: BorderSide(color: levelColor, width: 3)),
),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 36,
height: 36,
decoration: BoxDecoration(color: levelColor.withValues(alpha: 0.12), shape: BoxShape.circle),
child: Icon(icon, color: levelColor),
),
const SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${it['title'] ?? 'اعلان'}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
Text(
'${it['body'] ?? ''}',
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: cs.onSurfaceVariant),
),
const SizedBox(height: 6),
Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: levelColor.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(12),
),
child: Text(
_localizedAnnouncementLevel(context, level),
style: TextStyle(color: levelColor, fontSize: 11),
),
),
const Spacer(),
TextButton.icon(
onPressed: () {
Navigator.of(context).pop();
context.go('/user/profile/announcements');
},
icon: const Icon(Icons.open_in_new, size: 16),
label: const Text('جزئیات'),
),
],
),
],
),
),
if (annId != null)
IconButton(
tooltip: 'خوانده شد',
icon: busy
? const SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2))
: const Icon(Icons.done_all, size: 20),
onPressed: busy ? null : () async => _markNotificationRead(annId, dialogSetState: dialogSetState),
),
],
),
),
);
},
separatorBuilder: (_, _) => const SizedBox(height: 8),
itemCount: items.length,
),
),
actionsPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('بستن'),
),
FilledButton.icon(
onPressed: () {
Navigator.of(context).pop();
context.go('/user/profile/announcements');
},
icon: const Icon(Icons.notifications, size: 18),
label: const Text('مشاهده همه اعلان‌ها'),
),
],
);
},
);
},
);
}
Future<void> _markNotificationRead(int id, {StateSetter? dialogSetState}) async {
void refreshDialog() {
if (dialogSetState != null) {
dialogSetState(() {});
}
}
setState(() => _busyAnnIds.add(id));
refreshDialog();
try {
await AnnouncementsService(ApiClient()).markRead(id);
if (!mounted) return;
_hub.markLocallyRead(id);
refreshDialog();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('به‌عنوان خوانده‌شده علامت خورد')));
}
} catch (e) {
if (mounted) {
SnackBarHelper.showError(context, message: 'خطا: ${ErrorExtractor.forContext(e, context)}');
}
} finally {
if (mounted) {
setState(() => _busyAnnIds.remove(id));
} else {
_busyAnnIds.remove(id);
}
refreshDialog();
}
}
@override
Widget build(BuildContext context) {
final color = widget.iconColor ?? Theme.of(context).colorScheme.onSurface;
final unread = _hub.unreadCount;
final icon = Icon(Icons.notifications_none, size: widget.denseToolbar ? 21 : null);
return Padding(
padding: EdgeInsetsDirectional.only(end: widget.denseToolbar ? 2 : 4),
child: Stack(
clipBehavior: Clip.none,
children: [
IconButton(
tooltip: 'اعلان‌ها',
visualDensity: widget.denseToolbar ? VisualDensity.compact : VisualDensity.standard,
style: widget.denseToolbar
? IconButton.styleFrom(
minimumSize: const Size(38, 38),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
)
: null,
onPressed: _openNotificationCenter,
icon: icon,
color: color,
),
if (unread > 0)
Positioned(
right: 4,
top: 6,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(10),
),
constraints: const BoxConstraints(minWidth: 18),
child: Text(
unread > 99 ? '99+' : '$unread',
style: const TextStyle(color: Colors.white, fontSize: 10, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
],
),
);
}
}