Files
oott/frontend/lib/home/notification_card.dart
T
rzuastiandClaude Opus 4.7 61f2579152 Make notification mark-as-read reliably update the list
Three intertwined fixes so that marking a notification as read from the
trailing menu, swipe-dismiss, and 'Mark all as read' all consistently
update the UI and surface backend failures.

- Guard the mark-read, mark-new, and mark-all calls with try/catch; on
  failure show a snackbar with the mapped error and leave the list
  untouched.
- Use the State's own `mounted` and `context` after the await instead of
  the per-card BuildContext, so the SliverList rebuild reliably runs
  even when the originating NotificationCard's element has been swapped
  out by the reconciliation. _setRead and _markAllAsRead no longer take
  a BuildContext.
- Replace `Dismissible(key: UniqueKey())` with `ValueKey(item.id)` in
  the notification card. UniqueKey was regenerated on every build,
  forcing the Dismissible/PopupMenuButton subtree to be disposed on any
  parent rebuild; the open menu's `mounted` check would then drop
  `onSelected` silently.
- Add `ValueKey(device.macAddress)` to the device list rows for the
  same reason, anchoring each row's Element (and its PopupMenuButton
  State) to its device identity instead of its list position.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 10:28:23 -04:00

98 lines
3.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../model/notification.dart' as oott_model;
import '../utils/friendly_date_formatter.dart';
class NotificationCard extends StatelessWidget {
final oott_model.Notification item;
final FriendlyDateFormatter formatter;
final Future<bool> Function(bool read) onSetRead;
const NotificationCard({
required this.item,
required this.formatter,
required this.onSetRead,
super.key,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Card(
color: item.isNew ? theme.colorScheme.secondaryContainer : null,
child: Dismissible(
key: ValueKey(item.id),
confirmDismiss: (direction) =>
onSetRead(direction != DismissDirection.startToEnd),
background: Container(
color: theme.colorScheme.tertiaryContainer,
alignment: Alignment.centerLeft,
padding: const EdgeInsets.only(left: 16),
child: const Icon(Icons.mark_email_unread),
),
secondaryBackground: Container(
color: theme.colorScheme.primaryContainer,
alignment: Alignment.centerRight,
padding: const EdgeInsets.only(right: 16),
child: const Icon(Icons.done),
),
child: ListTile(
leading: Icon(
item.notificationType.icon,
color: item.isNew ? theme.colorScheme.primary : null,
),
title: Text(
'${formatter.format(item.createdOn)} - ${item.title}',
style: item.isNew
? const TextStyle(fontWeight: FontWeight.bold)
: null,
),
subtitle: Text(item.body, maxLines: 5),
trailing: PopupMenuButton<String>(
icon: const Icon(Icons.more_vert),
onSelected: (value) async {
if (value == 'view_device') {
if (item.isNew) await onSetRead(true);
if (context.mounted) {
context.push('/devices/${item.macAddress}');
}
} else if (value == 'mark_read') {
await onSetRead(true);
} else if (value == 'mark_new') {
await onSetRead(false);
}
},
itemBuilder: (context) => [
if (item.macAddress != null)
const PopupMenuItem(
value: 'view_device',
child: Text('View device'),
),
if (item.isNew)
const PopupMenuItem(
value: 'mark_read',
child: Text('Mark as read'),
),
if (!item.isNew)
const PopupMenuItem(
value: 'mark_new',
child: Text('Mark as unread'),
),
],
),
onTap: item.macAddress != null
? () async {
if (item.isNew) await onSetRead(true);
if (context.mounted) {
context.push('/devices/${item.macAddress}');
}
}
: null,
isThreeLine: true,
),
),
);
}
}