Files
flux/lib/features/master_data/providers/blocs/provider_cubit.dart

114 lines
3.4 KiB
Dart
Raw Normal View History

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/master_data/providers/data/provider_repository.dart';
import 'package:get_it/get_it.dart';
import '../models/provider_model.dart';
class ProvidersState extends Equatable {
final List<ProviderModel> allProviders; // Tutti i provider della company
final List<String>
associatedIds; // ID dei provider attivi nello store selezionato
final bool isLoading;
final String? errorMessage;
const ProvidersState({
this.allProviders = const [],
this.associatedIds = const [],
this.isLoading = false,
this.errorMessage,
});
ProvidersState copyWith({
List<ProviderModel>? allProviders,
List<String>? associatedIds,
bool? isLoading,
String? errorMessage,
}) {
return ProvidersState(
allProviders: allProviders ?? this.allProviders,
associatedIds: associatedIds ?? this.associatedIds,
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage,
);
}
@override
List<Object?> get props => [
allProviders,
associatedIds,
isLoading,
errorMessage,
];
}
class ProvidersCubit extends Cubit<ProvidersState> {
final ProviderRepository _repository = GetIt.I<ProviderRepository>();
ProvidersCubit() : super(const ProvidersState());
// Carica i provider della company e quelli associati a uno store specifico
Future<void> loadProviders(String companyId, String? storeId) async {
emit(state.copyWith(isLoading: true));
try {
final all = await _repository.fetchAllCompanyProviders(companyId);
List<String> associated = [];
if (storeId != null) {
associated = await _repository.fetchAssociatedProviderIds(storeId);
}
emit(
state.copyWith(
allProviders: all,
associatedIds: associated,
isLoading: false,
),
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
}
}
// Aggiunge o rimuove l'associazione con lo store
Future<void> toggleProviderAssociation({
required String providerId,
required String storeId,
required bool isCurrentlyAssociated,
}) async {
try {
if (isCurrentlyAssociated) {
await _repository.disassociateProviderFromStore(
providerId: providerId,
storeId: storeId,
);
// Aggiorniamo lo stato locale rimuovendo l'ID
final newIds = List<String>.from(state.associatedIds)
..remove(providerId);
emit(state.copyWith(associatedIds: newIds));
} else {
await _repository.associateProviderToStore(
providerId: providerId,
storeId: storeId,
);
// Aggiorniamo lo stato locale aggiungendo l'ID
final newIds = List<String>.from(state.associatedIds)..add(providerId);
emit(state.copyWith(associatedIds: newIds));
}
} catch (e) {
emit(state.copyWith(errorMessage: "Errore durante l'aggiornamento: $e"));
}
}
// Salvataggio/Update anagrafica (nuovo o modifica)
Future<void> saveProvider(ProviderModel provider) async {
emit(state.copyWith(isLoading: true));
try {
await _repository.saveProvider(provider);
// Ricarichiamo la lista per vedere le modifiche
await loadProviders(provider.companyId, null);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
}
}
}