import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:notas/models/category.dart'; import 'package:notas/models/note.dart'; import 'package:notas/theme/app_palette.dart'; import 'package:notas/widgets/note_card.dart'; class NoteListView extends StatelessWidget { const NoteListView({ super.key, required this.notes, required this.isLoading, required this.selectedNoteId, required this.showSelectionBorder, required this.categoryForNote, required this.lastSyncAt, required this.onRefresh, required this.onReorder, required this.onNoteTap, required this.onDeleteNote, required this.onChangeCategory, }); final List notes; final bool isLoading; final String? selectedNoteId; final bool showSelectionBorder; final Category? Function(String? categoryId) categoryForNote; final DateTime? lastSyncAt; final Future Function() onRefresh; final Future Function(int oldIndex, int newIndex) onReorder; final void Function(Note note) onNoteTap; final Future Function(Note note) onDeleteNote; final Future Function(BuildContext buttonContext, Note note) onChangeCategory; String _formatLastSyncAt() { if (lastSyncAt == null) { return 'Última sincronización: nunca'; } return 'Última sincronización: ${DateFormat('dd/MM/yyyy HH:mm').format(lastSyncAt!)}'; } @override Widget build(BuildContext context) { final AppPalette palette = Theme.of(context).extension() ?? AppPalette.fromBrightness(Theme.of(context).brightness); if (isLoading) { return const Center(child: CircularProgressIndicator()); } if (notes.isEmpty) { return Center( child: Text( 'No hay notas para mostrar', style: TextStyle(color: palette.textSecondary), ), ); } return RefreshIndicator( onRefresh: onRefresh, child: ReorderableListView.builder( padding: const EdgeInsets.fromLTRB(10, 10, 10, 14), buildDefaultDragHandles: false, itemCount: notes.length, onReorderItem: onReorder, footer: Padding( padding: const EdgeInsets.only(top: 4, bottom: 72), child: Center( child: Text( _formatLastSyncAt(), style: TextStyle(color: palette.textSecondary, fontSize: 12), ), ), ), itemBuilder: (BuildContext context, int index) { final Note note = notes[index]; return Padding( key: ValueKey(note.id), padding: const EdgeInsets.only(bottom: 6), child: ReorderableDelayedDragStartListener( index: index, child: NoteCard( note: note, category: categoryForNote(note.categoryId), isSelected: note.id == selectedNoteId, showSelectionBorder: showSelectionBorder, onTap: () => onNoteTap(note), onDelete: () => onDeleteNote(note), onChangeCategory: (BuildContext buttonContext) => onChangeCategory(buttonContext, note), ), ), ); }, ), ); } }