b
This commit is contained in:
106
lib/features/customers/blocs/customer_form_cubit.dart
Normal file
106
lib/features/customers/blocs/customer_form_cubit.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/features/customers/data/customer_repository.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'customer_form_state.dart';
|
||||
|
||||
class CustomerFormCubit extends Cubit<CustomerFormState> {
|
||||
final CustomerRepository _repository = GetIt.I<CustomerRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I<SessionCubit>();
|
||||
|
||||
CustomerFormCubit({CustomerModel? existingCustomer})
|
||||
: super(
|
||||
CustomerFormState(customer: existingCustomer ?? CustomerModel.empty()),
|
||||
);
|
||||
|
||||
Future<void> initForm({
|
||||
CustomerModel? existingCustomer,
|
||||
String? customerId,
|
||||
}) async {
|
||||
emit(state.copyWith(status: CustomerFormStatus.loading));
|
||||
|
||||
try {
|
||||
if (existingCustomer != null) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
customer: existingCustomer,
|
||||
status: CustomerFormStatus.ready,
|
||||
),
|
||||
);
|
||||
} else if (customerId != null) {
|
||||
final customer = await _repository.getCustomerById(customerId);
|
||||
emit(
|
||||
state.copyWith(customer: customer, status: CustomerFormStatus.ready),
|
||||
);
|
||||
} else {
|
||||
// Nuovo cliente, inizializziamo con valori vuoti
|
||||
emit(
|
||||
state.copyWith(
|
||||
customer: CustomerModel.empty().copyWith(
|
||||
companyId: _sessionCubit.state.company!.id!,
|
||||
),
|
||||
status: CustomerFormStatus.ready,
|
||||
),
|
||||
);
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void updateDoNotDisturb(bool value) {
|
||||
emit(
|
||||
state.copyWith(customer: state.customer.copyWith(doNotDisturb: value)),
|
||||
);
|
||||
}
|
||||
|
||||
void updateFields({
|
||||
String? name,
|
||||
String? phoneNumber,
|
||||
String? email,
|
||||
String? note,
|
||||
bool? doNotDisturb,
|
||||
}) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
customer: state.customer.copyWith(
|
||||
name: name ?? state.customer.name,
|
||||
phoneNumber: phoneNumber ?? state.customer.phoneNumber,
|
||||
email: email ?? state.customer.email,
|
||||
note: note ?? state.customer.note,
|
||||
doNotDisturb: doNotDisturb ?? state.customer.doNotDisturb,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> saveCustomer() async {
|
||||
emit(state.copyWith(status: CustomerFormStatus.saving));
|
||||
|
||||
try {
|
||||
if (state.customer.id != null) {
|
||||
// Aggiorna cliente esistente
|
||||
await _repository.updateCustomer(state.customer);
|
||||
} else {
|
||||
// Crea nuovo cliente
|
||||
await _repository.insertCustomer(state.customer);
|
||||
}
|
||||
emit(state.copyWith(status: CustomerFormStatus.success));
|
||||
} on Exception catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
lib/features/customers/blocs/customer_form_state.dart
Normal file
30
lib/features/customers/blocs/customer_form_state.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
part of 'customer_form_cubit.dart';
|
||||
|
||||
enum CustomerFormStatus { initial, loading, ready, saving, success, failure }
|
||||
|
||||
class CustomerFormState extends Equatable {
|
||||
final CustomerFormStatus status;
|
||||
final CustomerModel customer;
|
||||
final String? errorMessage;
|
||||
|
||||
const CustomerFormState({
|
||||
this.status = CustomerFormStatus.initial,
|
||||
required this.customer,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
CustomerFormState copyWith({
|
||||
CustomerFormStatus? status,
|
||||
CustomerModel? customer,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return CustomerFormState(
|
||||
status: status ?? this.status,
|
||||
customer: customer ?? this.customer,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, customer, errorMessage];
|
||||
}
|
||||
Reference in New Issue
Block a user