feat-insert-service (#5)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/5 Co-authored-by: mark-cachy <marco@catelli.it> Co-committed-by: mark-cachy <marco@catelli.it>
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.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_events.dart';
|
||||
part 'customer_state.dart';
|
||||
|
||||
class CustomerBloc extends Bloc<CustomerEvent, CustomerState> {
|
||||
final CustomerRepository _repository = GetIt.I<CustomerRepository>();
|
||||
|
||||
CustomerBloc() : super(const CustomerState()) {
|
||||
on<LoadCustomersRequested>(_onLoadCustomers);
|
||||
on<CreateCustomerRequested>(_onCreateCustomer);
|
||||
on<SearchCustomersRequested>(_onSearchCustomers);
|
||||
on<UpdateCustomerRequested>(_onUpdateCustomer);
|
||||
}
|
||||
|
||||
Future<void> _onLoadCustomers(
|
||||
LoadCustomersRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final customers = await _repository.getCustomers(event.companyId);
|
||||
emit(
|
||||
state.copyWith(status: CustomerStatus.success, customers: customers),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCreateCustomer(
|
||||
CreateCustomerRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final newCustomer = await _repository.createCustomer(event.customer);
|
||||
|
||||
// Aggiorniamo la lista locale aggiungendo il nuovo cliente in cima
|
||||
final updatedList = List<CustomerModel>.from(state.customers)
|
||||
..insert(0, newCustomer);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.success,
|
||||
customers: updatedList,
|
||||
lastCreatedCustomer:
|
||||
newCustomer, // Lo passiamo per le Dialog "al volo"
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onUpdateCustomer(
|
||||
UpdateCustomerRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
// Qui dovresti aggiungere un metodo updateCustomer nel Repository
|
||||
// Simile al create ma usando .update().eq('id', customer.id)
|
||||
final updatedCustomer = await _repository.updateCustomer(event.customer);
|
||||
|
||||
final updatedList = List<CustomerModel>.from(state.customers);
|
||||
final index = updatedList.indexWhere((c) => c.id == updatedCustomer.id);
|
||||
|
||||
if (index != -1) {
|
||||
updatedList[index] = updatedCustomer;
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.success,
|
||||
customers: updatedList,
|
||||
lastCreatedCustomer: updatedCustomer,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSearchCustomers(
|
||||
SearchCustomersRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
// Non mettiamo loading per evitare flickering durante la digitazione
|
||||
try {
|
||||
final results = await _repository.searchCustomers(
|
||||
event.companyId,
|
||||
event.query,
|
||||
);
|
||||
emit(state.copyWith(status: CustomerStatus.success, customers: results));
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
160
lib/features/customers/blocs/customer_cubit.dart
Normal file
160
lib/features/customers/blocs/customer_cubit.dart
Normal file
@@ -0,0 +1,160 @@
|
||||
import 'dart:async'; // Serve per il Timer del debounce
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.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_state.dart';
|
||||
|
||||
class CustomerCubit extends Cubit<CustomerState> {
|
||||
final CustomerRepository _repository = GetIt.I<CustomerRepository>();
|
||||
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
|
||||
|
||||
// Variabile per gestire il debounce della ricerca
|
||||
Timer? _searchDebounce;
|
||||
|
||||
CustomerCubit() : super(const CustomerState());
|
||||
|
||||
// --- LETTURA ---
|
||||
Future<void> loadCustomers() async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final customers = await _repository.getCustomers(
|
||||
_sessionBloc.state.company!.id,
|
||||
);
|
||||
emit(
|
||||
state.copyWith(status: CustomerStatus.success, customers: customers),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- CREAZIONE ---
|
||||
Future<void> createCustomer(CustomerModel customer) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final newCustomer = await _repository.saveCustomer(customer);
|
||||
|
||||
// Aggiorniamo la lista locale aggiungendo il nuovo cliente in cima
|
||||
final updatedList = List<CustomerModel>.from(state.customers)
|
||||
..insert(0, newCustomer);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.success,
|
||||
customers: updatedList,
|
||||
lastCreatedCustomer: newCustomer,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- AGGIORNAMENTO ---
|
||||
Future<void> updateCustomer(CustomerModel customer) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final updatedCustomer = await _repository.updateCustomer(customer);
|
||||
|
||||
final updatedList = List<CustomerModel>.from(state.customers);
|
||||
final index = updatedList.indexWhere((c) => c.id == updatedCustomer.id);
|
||||
|
||||
if (index != -1) {
|
||||
updatedList[index] = updatedCustomer;
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.success,
|
||||
customers: updatedList,
|
||||
lastCreatedCustomer:
|
||||
updatedCustomer, // Utile se modifichi un cliente appena creato
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- RICERCA CON DEBOUNCE ---
|
||||
void searchCustomers(String query) {
|
||||
// 1. Se c'è già una ricerca in attesa (l'utente sta digitando veloce), la annulliamo
|
||||
if (_searchDebounce?.isActive ?? false) _searchDebounce!.cancel();
|
||||
|
||||
// 2. Facciamo partire un timer di 400 millisecondi
|
||||
_searchDebounce = Timer(const Duration(milliseconds: 300), () async {
|
||||
// Se cancella tutto e la query è vuota, ricarichiamo la lista base
|
||||
if (query.trim().isEmpty) {
|
||||
await loadCustomers();
|
||||
return;
|
||||
}
|
||||
|
||||
// Nessun "loading" state qui, per evitare sfarfallii visivi mentre si scrive
|
||||
try {
|
||||
final results = await _repository.searchCustomers(
|
||||
_sessionBloc.state.company!.id,
|
||||
query,
|
||||
);
|
||||
emit(
|
||||
state.copyWith(status: CustomerStatus.success, customers: results),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<CustomerModel?> quickCreateCustomer({
|
||||
required String name,
|
||||
String? phone,
|
||||
String? email,
|
||||
}) async {
|
||||
final newCustomer = CustomerModel(
|
||||
nome: name,
|
||||
telefono: phone ?? '',
|
||||
email: email ?? '',
|
||||
companyId: _sessionBloc.state.company!.id,
|
||||
note: '',
|
||||
);
|
||||
|
||||
try {
|
||||
final saved = await _repository.saveCustomer(newCustomer);
|
||||
// Lo aggiungiamo in cima ai suggerimenti
|
||||
emit(state.copyWith(customers: [saved, ...state.customers]));
|
||||
return saved;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Pulizia della memoria quando il Cubit viene distrutto
|
||||
@override
|
||||
Future<void> close() {
|
||||
_searchDebounce?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
part of 'customer_bloc.dart';
|
||||
|
||||
abstract class CustomerEvent extends Equatable {
|
||||
const CustomerEvent();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
// Carica tutti i clienti dell'azienda
|
||||
class LoadCustomersRequested extends CustomerEvent {
|
||||
final String companyId;
|
||||
const LoadCustomersRequested(this.companyId);
|
||||
}
|
||||
|
||||
// Crea un cliente (usato sia dalla lista che dalla Dialog operazioni)
|
||||
class CreateCustomerRequested extends CustomerEvent {
|
||||
final CustomerModel customer;
|
||||
const CreateCustomerRequested(this.customer);
|
||||
}
|
||||
|
||||
// Ricerca in tempo reale
|
||||
class SearchCustomersRequested extends CustomerEvent {
|
||||
final String companyId;
|
||||
final String query;
|
||||
const SearchCustomersRequested(this.companyId, this.query);
|
||||
}
|
||||
|
||||
class UpdateCustomerRequested extends CustomerEvent {
|
||||
final CustomerModel customer;
|
||||
const UpdateCustomerRequested(this.customer);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [customer];
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
part of 'customer_bloc.dart';
|
||||
part of 'customer_cubit.dart';
|
||||
|
||||
enum CustomerStatus { initial, loading, success, failure }
|
||||
|
||||
class CustomerState extends Equatable {
|
||||
final CustomerStatus status;
|
||||
final List<CustomerModel> customers; // Per la lista generale
|
||||
final CustomerModel?
|
||||
lastCreatedCustomer; // <--- Fondamentale per la Dialog "al volo"
|
||||
final List<CustomerModel> customers;
|
||||
final CustomerModel? lastCreatedCustomer;
|
||||
final String? errorMessage;
|
||||
|
||||
const CustomerState({
|
||||
|
||||
Reference in New Issue
Block a user