import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:household_keeper/core/database/database.dart'; import 'package:household_keeper/core/providers/database_provider.dart'; import 'package:household_keeper/features/tasks/presentation/task_providers.dart'; import 'package:household_keeper/features/tasks/presentation/task_row.dart'; import 'package:household_keeper/l10n/app_localizations.dart'; /// Screen displaying all tasks within a room, sorted by due date. /// /// Shows an empty state when no tasks exist, with a button to create one. /// FAB always visible for quick task creation. /// AppBar shows room name with edit and delete actions. class TaskListScreen extends ConsumerWidget { const TaskListScreen({super.key, required this.roomId}); final int roomId; @override Widget build(BuildContext context, WidgetRef ref) { final l10n = AppLocalizations.of(context); final asyncTasks = ref.watch(tasksInRoomProvider(roomId)); return Scaffold( appBar: AppBar( title: _RoomTitle(roomId: roomId), actions: [ IconButton( icon: const Icon(Icons.edit), onPressed: () => context.go('/rooms/$roomId/edit'), ), IconButton( icon: const Icon(Icons.delete), onPressed: () => _showRoomDeleteConfirmation(context, ref, l10n), ), ], ), body: asyncTasks.when( data: (tasks) { if (tasks.isEmpty) { return _EmptyState(l10n: l10n, roomId: roomId); } return _TaskListView( tasks: tasks, l10n: l10n, ); }, loading: () => const Center(child: CircularProgressIndicator()), error: (error, stack) => Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('Fehler: $error'), const SizedBox(height: 16), FilledButton.tonal( onPressed: () => ref.invalidate(tasksInRoomProvider(roomId)), child: const Text('Erneut versuchen'), ), ], ), ), ), floatingActionButton: FloatingActionButton( onPressed: () => context.go('/rooms/$roomId/tasks/new'), child: const Icon(Icons.add), ), ); } void _showRoomDeleteConfirmation( BuildContext context, WidgetRef ref, AppLocalizations l10n, ) { showDialog( context: context, builder: (ctx) => AlertDialog( title: Text(l10n.roomDeleteConfirmTitle), content: Text(l10n.roomDeleteConfirmMessage), actions: [ TextButton( onPressed: () => Navigator.pop(ctx), child: Text(l10n.cancel), ), FilledButton( onPressed: () { Navigator.pop(ctx); final db = ref.read(appDatabaseProvider); db.roomsDao.deleteRoom(roomId); // Navigate back to rooms list context.go('/rooms'); }, style: FilledButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.error, foregroundColor: Theme.of(context).colorScheme.onError, ), child: Text(l10n.roomDeleteConfirmAction), ), ], ), ); } } /// Async room name loader for the AppBar title. class _RoomTitle extends ConsumerWidget { const _RoomTitle({required this.roomId}); final int roomId; @override Widget build(BuildContext context, WidgetRef ref) { // Use a FutureBuilder-like approach: read room name from DB return FutureBuilder( future: ref.read(appDatabaseProvider).roomsDao.getRoomById(roomId), builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data!.name); } return const Text('...'); }, ); } } /// Empty state shown when the room has no tasks. class _EmptyState extends StatelessWidget { const _EmptyState({required this.l10n, required this.roomId}); final AppLocalizations l10n; final int roomId; @override Widget build(BuildContext context) { final theme = Theme.of(context); return Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 32), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( Icons.task_alt, size: 80, color: theme.colorScheme.onSurface.withValues(alpha: 0.4), ), const SizedBox(height: 24), Text( l10n.taskEmptyTitle, style: theme.textTheme.headlineSmall, textAlign: TextAlign.center, ), const SizedBox(height: 8), Text( l10n.taskEmptyMessage, style: theme.textTheme.bodyLarge?.copyWith( color: theme.colorScheme.onSurface.withValues(alpha: 0.6), ), textAlign: TextAlign.center, ), const SizedBox(height: 24), FilledButton.tonal( onPressed: () => context.go('/rooms/$roomId/tasks/new'), child: Text(l10n.taskEmptyAction), ), ], ), ), ); } } /// List view of task rows with long-press delete support. class _TaskListView extends ConsumerWidget { const _TaskListView({required this.tasks, required this.l10n}); final List tasks; final AppLocalizations l10n; @override Widget build(BuildContext context, WidgetRef ref) { return ListView.builder( itemCount: tasks.length, itemBuilder: (context, index) { final task = tasks[index]; return TaskRow( key: ValueKey(task.id), task: task, onDelete: () => _showDeleteConfirmation(context, ref, task), ); }, ); } void _showDeleteConfirmation( BuildContext context, WidgetRef ref, Task task, ) { showDialog( context: context, builder: (ctx) => AlertDialog( title: Text(l10n.taskDeleteConfirmTitle), content: Text(l10n.taskDeleteConfirmMessage), actions: [ TextButton( onPressed: () => Navigator.pop(ctx), child: Text(l10n.cancel), ), FilledButton( onPressed: () { Navigator.pop(ctx); ref.read(taskActionsProvider.notifier).deleteTask(task.id); }, style: FilledButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.error, foregroundColor: Theme.of(context).colorScheme.onError, ), child: Text(l10n.taskDeleteConfirmAction), ), ], ), ); } }