products data

This commit is contained in:
2026-04-12 19:21:54 +02:00
parent bdf928cca3
commit b8caff7636
9 changed files with 419 additions and 28 deletions

View File

@@ -0,0 +1,104 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_bloc.dart';
import 'package:flux/features/products/data/product_repository.dart';
import 'package:flux/features/products/models/brand_model.dart';
import 'package:flux/features/products/models/model_model.dart';
import 'package:get_it/get_it.dart';
part 'product_state.dart';
class ProductCubit extends Cubit<ProductState> {
final ProductRepository _repository = GetIt.I<ProductRepository>();
final SessionBloc _sessionBloc;
ProductCubit(this._sessionBloc) : super(const ProductState());
// Caricamento iniziale dei Brand
Future<void> loadBrands() async {
emit(state.copyWith(status: ProductStatus.loading));
try {
final brands = await _repository.getBrands(
_sessionBloc.state.company!.id,
);
emit(state.copyWith(status: ProductStatus.success, brands: brands));
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
// Selezione Brand e caricamento Modelli
Future<void> selectBrand(BrandModel? brand) async {
if (brand == null) {
emit(state.copyWith(selectedBrand: null, models: []));
return;
}
emit(state.copyWith(status: ProductStatus.loading, selectedBrand: brand));
try {
final models = await _repository.getModelsByBrand(brand.id!);
emit(state.copyWith(status: ProductStatus.success, models: models));
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
// Aggiungi/Modifica Brand
Future<void> saveBrand(String name, {String? id}) async {
try {
final brand = BrandModel(
id: id,
name: name,
companyId: _sessionBloc.state.company!.id,
);
await _repository.upsertBrand(brand);
await loadBrands(); // Ricarichiamo la lista aggiornata
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
// Aggiungi/Modifica Modello
Future<void> saveModel(String name, {String? id}) async {
if (state.selectedBrand == null) return;
try {
final model = ModelModel(
id: id,
name: name,
brandId: state.selectedBrand!.id!,
nameWithBrand: '', // Gestito dal trigger SQL
);
await _repository.upsertModel(model);
await selectBrand(
state.selectedBrand,
); // Ricarichiamo i modelli del brand
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
// Disattivazione (Soft Delete)
Future<void> toggleStatus(String table, String id, bool currentStatus) async {
try {
await _repository.toggleActiveStatus(table, id, !currentStatus);
if (table == 'brand') {
await loadBrands();
} else {
await selectBrand(state.selectedBrand);
}
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
}

View File

@@ -0,0 +1,44 @@
part of 'product_cubit.dart';
enum ProductStatus { initial, loading, success, error }
class ProductState extends Equatable {
final ProductStatus status;
final List<BrandModel> brands;
final List<ModelModel> models;
final BrandModel? selectedBrand; // Il brand attualmente selezionato
final String? errorMessage;
const ProductState({
this.status = ProductStatus.initial,
this.brands = const [],
this.models = const [],
this.selectedBrand,
this.errorMessage,
});
ProductState copyWith({
ProductStatus? status,
List<BrandModel>? brands,
List<ModelModel>? models,
BrandModel? selectedBrand,
String? errorMessage,
}) {
return ProductState(
status: status ?? this.status,
brands: brands ?? this.brands,
models: models ?? this.models,
selectedBrand: selectedBrand ?? this.selectedBrand,
errorMessage: errorMessage ?? this.errorMessage,
);
}
@override
List<Object?> get props => [
status,
brands,
models,
selectedBrand,
errorMessage,
];
}