This commit is contained in:
2026-05-28 23:48:30 +02:00
parent aed841dc0b
commit f15a2aa6e6
5 changed files with 144 additions and 120 deletions

View File

@@ -1,4 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
@@ -14,7 +16,6 @@ part 'attachments_state.dart';
class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> { class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
final _repository = GetIt.I.get<AttachmentsRepository>(); final _repository = GetIt.I.get<AttachmentsRepository>();
final String? companyId = GetIt.I.get<SessionCubit>().state.company?.id;
AttachmentsBloc({String? parentId, required AttachmentParentType parentType}) AttachmentsBloc({String? parentId, required AttachmentParentType parentType})
: super( : super(
@@ -36,8 +37,8 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
on<SelectAllAttachmentsEvent>(_onSelectAllAttachments); on<SelectAllAttachmentsEvent>(_onSelectAllAttachments);
on<ClearAttachmentSelectionEvent>(_onClearAttachmentSelection); on<ClearAttachmentSelectionEvent>(_onClearAttachmentSelection);
// Se il BLoC nasce già con un ID, carichiamo i file final currentCompanyId = GetIt.I.get<SessionCubit>().state.company?.id;
if (parentId != null && companyId != null) { if (parentId != null && currentCompanyId != null) {
add(LoadAttachmentsEvent(parentId: parentId)); add(LoadAttachmentsEvent(parentId: parentId));
} }
} }
@@ -46,6 +47,8 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
ParentEntitySavedEvent event, ParentEntitySavedEvent event,
Emitter<AttachmentsState> emit, Emitter<AttachmentsState> emit,
) async { ) async {
final companyId = GetIt.I.get<SessionCubit>().state.company?.id;
emit( emit(
state.copyWith( state.copyWith(
parentId: event.newParentId, parentId: event.newParentId,
@@ -117,14 +120,30 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
Emitter<AttachmentsState> emit, Emitter<AttachmentsState> emit,
) async { ) async {
final currentId = state.parentId; final currentId = state.parentId;
final currentCompanyId = GetIt.I.get<SessionCubit>().state.company?.id;
// BIVIO 1: PRATICA NUOVA (Salvataggio locale) if (currentCompanyId == null) {
emit(
state.copyWith(
status: AttachmentsStatus.failure,
error: "Company ID non trovato nella sessione",
),
);
return;
}
// BIVIO 1: PRATICA NUOVA (Salvataggio locale in memoria)
if (currentId == null) { if (currentId == null) {
final newLocalFiles = event.files.map((file) { final newLocalFiles = event.files.map((file) {
// Assegniamo i campi dinamicamente in base al parentType! // FISCHIO SALVAVITA PER DESKTOP: se i bytes sono nulli, li leggiamo dal path fisico!
Uint8List? rawBytes = file.bytes;
if (rawBytes == null && file.path != null) {
rawBytes = File(file.path!).readAsBytesSync();
}
return AttachmentModel( return AttachmentModel(
id: null, id: null,
companyId: companyId!, companyId: currentCompanyId,
operationId: state.parentType == AttachmentParentType.operation operationId: state.parentType == AttachmentParentType.operation
? '' ? ''
: null, : null,
@@ -136,7 +155,7 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
extension: file.name.fileExtension(), extension: file.name.fileExtension(),
storagePath: '', storagePath: '',
fileSize: file.size, fileSize: file.size,
localBytes: file.bytes, localBytes: rawBytes, // Ora i byte ci sono al 100% anche su Mac!
); );
}).toList(); }).toList();
@@ -157,7 +176,7 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
parentId: currentId, parentId: currentId,
parentType: state.parentType, parentType: state.parentType,
pickedFile: file, pickedFile: file,
companyId: companyId!, companyId: currentCompanyId,
bucket: _getBucketForParentType, bucket: _getBucketForParentType,
); );
}).toList(); }).toList();

View File

@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flux/core/blocs/session/session_cubit.dart'; import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/core/enums_and_consts/consts.dart';
import 'package:flux/features/notes/models/note_model.dart'; import 'package:flux/features/notes/models/note_model.dart';
@@ -130,13 +131,26 @@ class NotesRepository {
await _supabase.from('note_collaborators').delete().eq('note_id', noteId); await _supabase.from('note_collaborators').delete().eq('note_id', noteId);
// 3. RE-INSERIMENTO DELLA LISTA AGGIORNATA // 3. RE-INSERIMENTO DELLA LISTA AGGIORNATA
// Se ci sono collaboratori da inserire, li prepariamo in blocco (Bulk Insert)
if (note.collaboratorIds.isNotEmpty) { if (note.collaboratorIds.isNotEmpty) {
final collaboratorsToInsert = note.collaboratorIds final collaboratorsToInsert = note.collaboratorIds
.map((staffId) => {'note_id': noteId, 'staff_id': staffId}) .map(
(staffId) => {
'note_id': noteId,
'staff_id': staffId,
'company_id': note.companyId, // Aggiunto questo!
},
)
.toList(); .toList();
await _supabase.from('note_collaborators').insert(collaboratorsToInsert); // Consiglio da pro: avvolgi l'insert in un try-catch per stampare l'errore esatto a console
try {
await _supabase
.from(Tables.noteCollaborators)
.insert(collaboratorsToInsert);
} catch (e) {
debugPrint('Errore inserimento collaboratori: $e');
throw Exception('Impossibile aggiungere i collaboratori alla nota.');
}
} }
// Restituiamo l'id alla UI (fondamentale per la nostra logica Ninja di creazione) // Restituiamo l'id alla UI (fondamentale per la nostra logica Ninja di creazione)

View File

@@ -134,73 +134,76 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
builder: (context) { builder: (context) {
return StatefulBuilder( return StatefulBuilder(
builder: (context, setModalState) { builder: (context, setModalState) {
return Column( return Material(
children: [ color: Colors.transparent,
Padding( child: Column(
padding: const EdgeInsets.all(16.0), children: [
child: Text( Padding(
'Seleziona Collaboratori', padding: const EdgeInsets.all(16.0),
style: Theme.of(context).textTheme.titleLarge, child: Text(
'Seleziona Collaboratori',
style: Theme.of(context).textTheme.titleLarge,
),
), ),
), Expanded(
Expanded( child: ListView.builder(
child: ListView.builder( itemCount: allStaff.length,
itemCount: allStaff.length, itemBuilder: (context, index) {
itemBuilder: (context, index) { final staff = allStaff[index];
final staff = allStaff[index];
// Capiamo se questo membro dello staff è il creatore // Capiamo se questo membro dello staff è il creatore
final isCreator = staff.id == creatorId; final isCreator = staff.id == creatorId;
// È spuntato se è il creatore OPPURE se è nella lista dei collaboratori // È spuntato se è il creatore OPPURE se è nella lista dei collaboratori
final isSelected = final isSelected =
isCreator || _selectedStaffIds.contains(staff.id); isCreator || _selectedStaffIds.contains(staff.id);
return CheckboxListTile( return CheckboxListTile(
title: RichText( title: RichText(
text: TextSpan( text: TextSpan(
style: Theme.of(context).textTheme.bodyLarge, style: Theme.of(context).textTheme.bodyLarge,
children: [ children: [
TextSpan(text: staff.name), TextSpan(text: staff.name),
if (isCreator) if (isCreator)
const TextSpan( const TextSpan(
text: ' (Proprietario)', text: ' (Proprietario)',
style: TextStyle( style: TextStyle(
color: Colors.grey, color: Colors.grey,
fontStyle: FontStyle.italic, fontStyle: FontStyle.italic,
fontSize: 12, fontSize: 12,
),
), ),
), ],
], ),
), ),
), value: isSelected,
value: isSelected, activeColor: Theme.of(context).colorScheme.primary,
activeColor: Theme.of(context).colorScheme.primary, // IL TRUCCO NINJA: se è il creatore, passiamo null per disabilitare la spunta!
// IL TRUCCO NINJA: se è il creatore, passiamo null per disabilitare la spunta! onChanged: isCreator
onChanged: isCreator ? null
? null : (bool? value) {
: (bool? value) { setModalState(() {
setModalState(() { if (value == true) {
if (value == true) { _selectedStaffIds.add(staff.id!);
_selectedStaffIds.add(staff.id!); } else {
} else { _selectedStaffIds.remove(staff.id!);
_selectedStaffIds.remove(staff.id!); }
} });
}); setState(() {});
setState(() {}); _triggerAutoSave();
_triggerAutoSave(); },
}, );
); },
}, ),
), ),
), Padding(
Padding( padding: const EdgeInsets.all(16.0),
padding: const EdgeInsets.all(16.0), child: FilledButton(
child: FilledButton( onPressed: () => Navigator.pop(context),
onPressed: () => Navigator.pop(context), child: const Text('Fatto'),
child: const Text('Fatto'), ),
), ),
), ],
], ),
); );
}, },
); );
@@ -403,24 +406,27 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
const SizedBox(height: 12), const SizedBox(height: 12),
// --- CONDIVISIONE --- // --- CONDIVISIONE ---
SwitchListTile( Material(
title: const Text( color: Colors.transparent,
'Condividi con tutti', child: SwitchListTile(
style: TextStyle( title: const Text(
color: Colors.black87, 'Condividi con tutti',
fontWeight: FontWeight.w500, style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.w500,
),
), ),
value: _isSharedAll,
activeThumbColor: Colors.black87,
contentPadding: EdgeInsets.zero,
onChanged: (val) {
setState(() {
_isSharedAll = val;
if (val) _selectedStaffIds.clear();
});
_triggerAutoSave();
},
), ),
value: _isSharedAll,
activeThumbColor: Colors.black87,
contentPadding: EdgeInsets.zero,
onChanged: (val) {
setState(() {
_isSharedAll = val;
if (val) _selectedStaffIds.clear();
});
_triggerAutoSave();
},
), ),
if (!_isSharedAll) ...[ if (!_isSharedAll) ...[

View File

@@ -2,30 +2,23 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>com.apple.security.app-sandbox</key> <key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/> <true/>
<key>com.apple.security.cs.allow-jit</key>
<key>com.apple.security.files.downloads.read-write</key> <true/>
<key>com.apple.security.network.server</key>
<true/> <true/>
<key>com.apple.security.network.client</key> <key>com.apple.security.network.client</key>
<true/> <true/>
<key>com.apple.security.device.camera</key> <key>com.apple.security.device.camera</key>
<true/> <true/>
<key>com.apple.security.device.audio-input</key>
<key>com.apple.security.device.audio-input</key> <true/>
<true/> <key>com.apple.security.print</key>
<key>com.apple.security.print</key> <true/>
<key>com.apple.security.files.downloads.read-write</key> <key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<true/> <true/>
<true/>
</dict> </dict>
</plist> </plist>

View File

@@ -2,27 +2,19 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>com.apple.security.app-sandbox</key> <key>com.apple.security.app-sandbox</key>
<true/> <true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.cs.allow-jit</key> <key>com.apple.security.cs.allow-jit</key>
<true/> <true/>
<key>com.apple.security.network.client</key>
<key>com.apple.security.files.downloads.read-write</key>
<true/> <true/>
<key>com.apple.security.device.camera</key> <key>com.apple.security.device.camera</key>
<true/> <true/>
<key>com.apple.security.device.audio-input</key> <key>com.apple.security.device.audio-input</key>
<true/> <true/>
<key>com.apple.security.files.user-selected.read-write</key> <key>com.apple.security.files.user-selected.read-write</key>
<true/> <true/>
<key>com.apple.security.print</key> <key>com.apple.security.print</key>
<key>com.apple.security.files.downloads.read-write</key>
<true/> <true/>
<true/>
</dict> </dict>
</plist> </plist>