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:io';
import 'dart:typed_data';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
@@ -14,7 +16,6 @@ part 'attachments_state.dart';
class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
final _repository = GetIt.I.get<AttachmentsRepository>();
final String? companyId = GetIt.I.get<SessionCubit>().state.company?.id;
AttachmentsBloc({String? parentId, required AttachmentParentType parentType})
: super(
@@ -36,8 +37,8 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
on<SelectAllAttachmentsEvent>(_onSelectAllAttachments);
on<ClearAttachmentSelectionEvent>(_onClearAttachmentSelection);
// Se il BLoC nasce già con un ID, carichiamo i file
if (parentId != null && companyId != null) {
final currentCompanyId = GetIt.I.get<SessionCubit>().state.company?.id;
if (parentId != null && currentCompanyId != null) {
add(LoadAttachmentsEvent(parentId: parentId));
}
}
@@ -46,6 +47,8 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
ParentEntitySavedEvent event,
Emitter<AttachmentsState> emit,
) async {
final companyId = GetIt.I.get<SessionCubit>().state.company?.id;
emit(
state.copyWith(
parentId: event.newParentId,
@@ -117,14 +120,30 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
Emitter<AttachmentsState> emit,
) async {
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) {
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(
id: null,
companyId: companyId!,
companyId: currentCompanyId,
operationId: state.parentType == AttachmentParentType.operation
? ''
: null,
@@ -136,7 +155,7 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
extension: file.name.fileExtension(),
storagePath: '',
fileSize: file.size,
localBytes: file.bytes,
localBytes: rawBytes, // Ora i byte ci sono al 100% anche su Mac!
);
}).toList();
@@ -157,7 +176,7 @@ class AttachmentsBloc extends Bloc<AttachmentsEvent, AttachmentsState> {
parentId: currentId,
parentType: state.parentType,
pickedFile: file,
companyId: companyId!,
companyId: currentCompanyId,
bucket: _getBucketForParentType,
);
}).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/enums_and_consts/consts.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);
// 3. RE-INSERIMENTO DELLA LISTA AGGIORNATA
// Se ci sono collaboratori da inserire, li prepariamo in blocco (Bulk Insert)
if (note.collaboratorIds.isNotEmpty) {
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();
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)

View File

@@ -134,7 +134,9 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
builder: (context) {
return StatefulBuilder(
builder: (context, setModalState) {
return Column(
return Material(
color: Colors.transparent,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
@@ -201,6 +203,7 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
),
),
],
),
);
},
);
@@ -403,7 +406,9 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
const SizedBox(height: 12),
// --- CONDIVISIONE ---
SwitchListTile(
Material(
color: Colors.transparent,
child: SwitchListTile(
title: const Text(
'Condividi con tutti',
style: TextStyle(
@@ -422,6 +427,7 @@ class _NoteFormScreenState extends State<NoteFormScreen> {
_triggerAutoSave();
},
),
),
if (!_isSharedAll) ...[
const SizedBox(height: 16),

View File

@@ -8,24 +8,17 @@
<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/>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.print</key>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
</dict>
</plist>

View File

@@ -4,25 +4,17 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.files.downloads.read-write</key>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.print</key>
<key>com.apple.security.files.downloads.read-write</key>
<true/>
<true/>
</dict>
</plist>