fix supabase storage

This commit is contained in:
2026-04-20 16:50:55 +02:00
parent 8dc1c661ed
commit de940cea1f
10 changed files with 172 additions and 100 deletions

View File

@@ -132,7 +132,6 @@ class ServicesCubit extends Cubit<ServicesState> {
companyId: _sessionBloc.state.company!.id,
),
status: ServicesStatus.ready,
files: [],
),
);
}
@@ -212,7 +211,7 @@ class ServicesCubit extends Cubit<ServicesState> {
final serviceToSave = state.currentService!.copyWith(isBozza: isBozza);
// 2. Salvataggio corazzato
await _repository.saveFullService(serviceToSave, state.files);
await _repository.saveFullService(serviceToSave);
// 3. Reset e ricaricamento
emit(state.copyWith(status: ServicesStatus.saved, currentService: null));
@@ -230,31 +229,33 @@ class ServicesCubit extends Cubit<ServicesState> {
// --- GESTIONE ALLEGATI LOCALI ---
void addAttachments(List<PlatformFile> files) {
// Trasformiamo i PlatformFile in ServiceFileModel "temporanei"
final newAttachments = files.map((file) {
return ServiceFileModel(
id: '', // ID vuoto perché non ancora su DB
id: null, // Meglio null se non è su DB
serviceId: state.currentService?.id ?? '',
name: file.name.fileNameWithoutExtension(),
extension: file.name.fileExtension(),
url: '', // URL vuoto perché non ancora caricato
url: '',
fileSize: file.size,
localBytes: file.bytes, // Fondamentale per l'upload!
localBytes: file.bytes,
createdAt: DateTime.now(),
);
}).toList();
// Uniamo i file esistenti (remoti + locali già aggiunti) con i nuovi
final updatedList = [
// Creiamo una nuova lista pulita
final List<ServiceFileModel> updatedList = [
...(state.currentService?.files ?? []),
...newAttachments,
];
emit(
state.copyWith(
currentService: state.currentService?.copyWith(files: updatedList),
),
);
// Emettiamo lo stato assicurandoci che il ServiceModel venga clonato
if (state.currentService != null) {
emit(
state.copyWith(
currentService: state.currentService!.copyWith(files: updatedList),
),
);
}
}
void removeAttachment(int index) {
@@ -295,7 +296,9 @@ class ServicesCubit extends Cubit<ServicesState> {
);
if (savedFile.url.isEmpty) {
throw Exception("Errore: URL del file non trovato dopo il salvataggio.");
throw Exception(
"Errore: URL del file non trovato dopo il salvataggio.",
);
}
// 3. Chiamiamo il repository per la copia fisica nel database del cliente
@@ -308,12 +311,13 @@ class ServicesCubit extends Cubit<ServicesState> {
// 4. Feedback all'utente
// Potresti emettere un successo o mostrare un toast
emit(state.copyWith(status: ServicesStatus.success));
} catch (e) {
emit(state.copyWith(
status: ServicesStatus.failure,
errorMessage: "Errore durante la copia del file: $e",
));
emit(
state.copyWith(
status: ServicesStatus.failure,
errorMessage: "Errore durante la copia del file: $e",
),
);
}
}
}