mah....volare

This commit is contained in:
2026-06-03 19:16:15 +02:00
parent f27ede7625
commit 01515910b6
7 changed files with 560 additions and 243 deletions

View File

@@ -217,8 +217,6 @@ class OperationFormCubit extends Cubit<OperationFormState> {
String? reference,
String? note,
String? type,
String? providerId,
String? providerDisplayName,
String? subType,
String? description,
DateTime? expirationDate,
@@ -248,10 +246,6 @@ class OperationFormCubit extends Cubit<OperationFormState> {
final updated = current.copyWith(
reference: reference ?? current.reference,
note: note ?? current.note,
providerId: clearProvider ? null : (providerId ?? current.providerId),
providerDisplayName: clearProvider
? null
: (providerDisplayName ?? current.providerDisplayName),
quantity: newQuantity ?? current.quantity,
type: clearType ? null : (type ?? current.type),
description: clearDescription
@@ -274,6 +268,18 @@ class OperationFormCubit extends Cubit<OperationFormState> {
emit(state.copyWith(operation: updated));
}
void updateProvider(ProviderModel? newProvider) {
final current = state.operation;
final updatedOperation = current.copyWith(
// Se newProvider è null, passiamo una funzione che ritorna null per sbiancare i campi!
providerId: () => newProvider?.id,
provider: () => newProvider,
);
emit(state.copyWith(operation: updatedOperation));
}
void updateCustomer(CustomerModel customer) {
final bool isBusiness = customer.isBusiness;
final updatedOperation = state.operation.copyWith(
@@ -293,13 +299,8 @@ class OperationFormCubit extends Cubit<OperationFormState> {
}) {
// 1. Aggiorniamo il tipo nel modello in canna
// (Presumo tu abbia un metodo copyWith o simile)
final updatedOp = state.operation.copyWith(type: newType, subType: '');
// 2. Prepariamoci ad auto-selezionare il provider
String? newProviderId = updatedOp.providerId;
String? newProviderName = updatedOp.providerDisplayName;
// 3. LA LOGICA DI DEFAULT
// 2. LA LOGICA DI DEFAULT
if (defaultProviderId != null) {
// Troviamo il provider di default nella lista
final defaultProvider = allProviders
@@ -309,25 +310,13 @@ class OperationFormCubit extends Cubit<OperationFormState> {
if (defaultProvider != null) {
// Usiamo l'extension appena creata!
if (defaultProvider.supportsOperation(newType)) {
newProviderId = defaultProvider.id;
newProviderName = defaultProvider.name;
updateProvider(defaultProvider);
} else {
// Se cambi tipo (es. da Mobile a Luce) e il default non lo supporta, sbianchiamo
newProviderId = null;
newProviderName = null;
updateProvider(null);
}
}
}
// Emettiamo il nuovo stato
emit(
state.copyWith(
operation: updatedOp.copyWith(
providerId: newProviderId,
providerDisplayName: newProviderName,
),
),
);
}
void setTypeWithSmartDefaults({
@@ -338,7 +327,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
final currentOp = state.operation;
// -----------------------------------------
// 1. SMART DATES: Calcolo Scadenze Default
// 1. SMART DATES: Calcolo Scadenze Default (Invariato)
// -----------------------------------------
DateTime? defaultDate;
final now = DateTime.now();
@@ -354,28 +343,19 @@ class OperationFormCubit extends Cubit<OperationFormState> {
}
// -----------------------------------------
// 2. SMART PROVIDER: Filtro e Auto-Selezione
// 2. SMART PROVIDER: Filtro e Auto-Selezione ad Oggetti
// -----------------------------------------
String? newProviderId = currentOp.providerId;
String? newProviderName = currentOp.providerDisplayName;
// Pescatore direttamente l'oggetto dal modello corrente
ProviderModel? targetProvider = currentOp.provider;
// A) Il provider attuale è ancora compatibile col nuovo tipo scelto?
if (newProviderId != null && newProviderId.isNotEmpty) {
final currentProvider = allProviders
.where((p) => p.id == newProviderId)
.firstOrNull;
if (currentProvider == null ||
!currentProvider.supportsOperation(newType)) {
// Non è più compatibile (es. da TIM fisso passo a Energy). Lo sbianchiamo!
newProviderId = null;
newProviderName = null;
}
if (targetProvider != null && !targetProvider.supportsOperation(newType)) {
// Non è più compatibile (es. da TIM fisso passo a Energy). Lo sbianchiamo!
targetProvider = null;
}
// B) Se non c'è un provider selezionato, proviamo ad auto-inserire quello di default del negozio
if ((newProviderId == null || newProviderId.isEmpty) &&
defaultProviderId != null) {
if (targetProvider == null && defaultProviderId != null) {
final defaultProvider = allProviders
.where((p) => p.id == defaultProviderId)
.firstOrNull;
@@ -383,8 +363,7 @@ class OperationFormCubit extends Cubit<OperationFormState> {
// Controlliamo che il default del negozio supporti questa specifica operazione
if (defaultProvider != null &&
defaultProvider.supportsOperation(newType)) {
newProviderId = defaultProvider.id;
newProviderName = defaultProvider.name;
targetProvider = defaultProvider;
}
}
@@ -395,13 +374,16 @@ class OperationFormCubit extends Cubit<OperationFormState> {
state.copyWith(
operation: currentOp.copyWith(
type: newType,
subType:
'', // Resettiamo il sottotipo per evitare incongruenze (es. passo da Luce a DAZN)
subType: '', // Resettiamo il sottotipo per evitare incongruenze
expirationDate:
defaultDate, // Impostiamo la scadenza di default se calcolata
providerId: newProviderId,
providerDisplayName: newProviderName,
// 🥷 APPLICHIAMO IL TRUCCO NINJA DELLE FUNZIONI
// Se targetProvider è null, le funzioni ritorneranno null sbiancando il DB!
providerId: () => targetProvider?.id,
provider: () => targetProvider,
// Nota: Per azzerare davvero questi due, ricordati in futuro di applicare
// il trucco delle funzioni anche a modelId e modelDisplayName nel modello!
modelId: null,
modelDisplayName: null,
),