diff --git a/lib/features/materials/presentation/pages/teacher_materials_page.dart b/lib/features/materials/presentation/pages/teacher_materials_page.dart index 8703ef7..e30bbaa 100644 --- a/lib/features/materials/presentation/pages/teacher_materials_page.dart +++ b/lib/features/materials/presentation/pages/teacher_materials_page.dart @@ -176,10 +176,15 @@ class _TeacherMaterialsPageState extends State { final fileType = extension == '.pdf' ? 'pdf' : (extension == '.jpg' || extension == '.jpeg' || extension == '.png') ? 'image' : 'other'; + final docId = materials[index].id; + final url = material['url'] as String?; + return _buildMaterialCard( + docId: docId, fileName: fileName, fileType: fileType, createdAt: createdAt, + url: url, ); }, ); @@ -459,6 +464,75 @@ class _TeacherMaterialsPageState extends State { } } + Future _deleteMaterial({ + required String docId, + required String fileName, + String? url, + }) async { + try { + // Apagar ficheiro do Firebase Storage + if (url != null && url.isNotEmpty) { + try { + final ref = _storage.refFromURL(url); + await ref.delete(); + } catch (_) { + // Se o ficheiro já não existir no Storage, continuar na mesma + } + } + + // Apagar documento no Firestore + await _firestore.collection('materials').doc(docId).delete(); + + if (mounted) { + _showSuccessSnackBar('Material eliminado com sucesso!'); + } + } catch (e) { + if (mounted) { + _showErrorSnackBar('Erro ao eliminar material: $e'); + } + } + } + + void _showDeleteConfirmation({ + required String docId, + required String fileName, + String? url, + }) { + showDialog( + context: context, + builder: (dialogContext) => AlertDialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + title: const Text( + 'Eliminar Material', + style: TextStyle(fontWeight: FontWeight.bold), + ), + content: Text( + 'Tens a certeza que queres eliminar "$fileName"?\nEsta ação não pode ser desfeita.', + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(dialogContext).pop(), + child: const Text('Cancelar'), + ), + ElevatedButton( + onPressed: () { + Navigator.of(dialogContext).pop(); + _deleteMaterial(docId: docId, fileName: fileName, url: url); + }, + style: ElevatedButton.styleFrom( + backgroundColor: Colors.red, + foregroundColor: Colors.white, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10), + ), + ), + child: const Text('Eliminar'), + ), + ], + ), + ); + } + void _showSuccessSnackBar(String message) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( @@ -496,9 +570,11 @@ class _TeacherMaterialsPageState extends State { } Widget _buildMaterialCard({ + required String docId, required String fileName, required String fileType, required Timestamp? createdAt, + String? url, }) { IconData iconData; Color iconColor; @@ -566,6 +642,15 @@ class _TeacherMaterialsPageState extends State { fontSize: 13, ), ), + trailing: IconButton( + icon: const Icon(Icons.delete_outline, color: Colors.red), + tooltip: 'Eliminar', + onPressed: () => _showDeleteConfirmation( + docId: docId, + fileName: fileName, + url: url, + ), + ), ), ); }