import 'package:flutter/material.dart'; import 'package:logging/logging.dart'; import 'package:provider/provider.dart'; import '../controls/app_bottom_nav_bar.dart'; import '../controls/notifications_control.dart'; import '../controls/standard_appbar.dart'; import '../controls/status_and_refresh_button.dart'; import '../globals.dart'; import '../services/network_status_service.dart'; import '../services/notifications_manager.dart'; import '../utils/snackbar_builder.dart'; class NotificationsScreen extends StatelessWidget { static final _logger = Logger('$NotificationsScreen'); const NotificationsScreen({super.key}); @override Widget build(BuildContext context) { _logger.finest('Building'); final nss = getIt(); final manager = context.watch(); final notifications = manager.notifications; late final String title; late final Widget body; if (notifications.isEmpty) { manager.updateNotifications(); title = 'Notifications'; body = Center( child: Column( children: [ const Center(child: Text('No notifications')), ], )); } else { final unreadCount = notifications.where((e) => !e.dismissed).length; title = 'Notifications ($unreadCount)'; body = RefreshIndicator( onRefresh: () async { manager.updateNotifications(); }, child: ListView.separated( itemBuilder: (context, index) { if (index == 0) { return TextButton( onPressed: () async => await manager.loadNewerNotifications(), child: const Text('Load newer notifications')); } if (index == notifications.length + 1) { return TextButton( onPressed: () async => await manager.loadOlderNotifications(), child: const Text('Load older notifications')); } return NotificationControl( notification: notifications[index - 1]); }, separatorBuilder: (context, index) { return const Divider( color: Colors.black54, height: 0.0, ); }, itemCount: notifications.length + 2), ); } return Scaffold( appBar: StandardAppBar.build( context, title, actions: [ StatusAndRefreshButton( valueListenable: nss.notificationsUpdateStatus, refreshFunction: () async => manager.updateNotifications(), busyColor: Theme.of(context).colorScheme.background, ), IconButton( onPressed: () async => _clearAllNotifications(context, manager), icon: const Icon(Icons.cleaning_services), ), ], ), body: body, bottomNavigationBar: const AppBottomNavBar( currentButton: NavBarButtons.notifications, ), ); } Future _clearAllNotifications( BuildContext context, NotificationsManager manager) async { final confirmed = await showYesNoDialog(context, 'Clear all notifications?'); if (confirmed == true) { final message = (await manager.markAllAsRead()).fold( onSuccess: (_) => 'Marked all notifications as read', onError: (error) => 'Error marking notifications: $error'); buildSnackbar(context, message); } } }