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:
2026-04-20 16:52:20 +02:00
committed by brontomark
parent 667bbf6404
commit c3d4f3fac7
63 changed files with 4715 additions and 1371 deletions

View File

@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_bloc.dart';
@@ -10,9 +11,9 @@ part 'product_state.dart';
class ProductCubit extends Cubit<ProductState> {
final ProductRepository _repository = GetIt.I<ProductRepository>();
final SessionBloc _sessionBloc;
final SessionBloc _sessionBloc = GetIt.I<SessionBloc>();
ProductCubit(this._sessionBloc) : super(const ProductState());
ProductCubit() : super(const ProductState());
// Caricamento iniziale dei Brand
Future<void> loadBrands() async {
@@ -102,4 +103,55 @@ class ProductCubit extends Cubit<ProductState> {
);
}
}
Future<void> searchModels(String query) async {
if (query.isEmpty) {
// Se cancella tutto, potresti voler ricaricare i modelli del brand selezionato
// o svuotare la lista. Scegliamo di svuotare per pulizia.
emit(state.copyWith(models: []));
return;
}
// Opzionale: emetti loading solo se vuoi mostrare una barretta nella UI
// emit(state.copyWith(status: ProductStatus.loading));
try {
final results = await _repository.searchModels(query);
emit(state.copyWith(status: ProductStatus.success, models: results));
} catch (e) {
emit(
state.copyWith(status: ProductStatus.error, errorMessage: e.toString()),
);
}
}
Future<ModelModel?> quickCreateProduct({
required String brandName,
required String modelName,
}) async {
try {
await loadBrands();
BrandModel? brand = state.brands.firstWhereOrNull(
(b) => b.name.toLowerCase() == brandName.toLowerCase(),
);
// 1. Cerchiamo o creiamo il Brand
// (Usa una funzione upsert o una ricerca rapida nel repository)
brand ??= await _repository.upsertBrand(
BrandModel(name: brandName, companyId: _sessionBloc.state.company!.id),
);
// 2. Creiamo il Modello legato al Brand
final newModel = await _repository.upsertModel(
ModelModel(brandId: brand.id!, name: modelName),
);
// 3. Aggiorniamo lo stato locale così la lista modelli lo vede subito
emit(state.copyWith(models: [newModel, ...state.models]));
return newModel;
} catch (e) {
emit(state.copyWith(errorMessage: "Errore creazione rapida: $e"));
return null;
}
}
}

View File

@@ -4,14 +4,14 @@ import '../models/brand_model.dart';
import '../models/model_model.dart';
class ProductRepository {
final SupabaseClient _client = GetIt.I<SupabaseClient>();
final SupabaseClient _supabase = GetIt.I<SupabaseClient>();
// --- BRAND ---
/// Recupera tutti i brand dell'azienda
Future<List<BrandModel>> getBrands(String companyId) async {
try {
final response = await _client
final response = await _supabase
.from('brand')
.select()
.eq('company_id', companyId)
@@ -27,7 +27,7 @@ class ProductRepository {
/// Crea o aggiorna un brand
Future<BrandModel> upsertBrand(BrandModel brand) async {
try {
final response = await _client
final response = await _supabase
.from('brand')
.upsert(brand.toJson())
.select()
@@ -44,7 +44,7 @@ class ProductRepository {
/// Recupera i modelli di un brand specifico
Future<List<ModelModel>> getModelsByBrand(String brandId) async {
try {
final response = await _client
final response = await _supabase
.from('model')
.select()
.eq('brand_id', brandId)
@@ -61,7 +61,7 @@ class ProductRepository {
/// NOTA: name_with_brand verrà gestito dal trigger SQL che hai lanciato!
Future<ModelModel> upsertModel(ModelModel model) async {
try {
final response = await _client
final response = await _supabase
.from('model')
.upsert(model.toJson())
.select()
@@ -78,9 +78,24 @@ class ProductRepository {
/// Disattiva un brand o un modello (Soft Delete per non rompere le FK delle operazioni passate)
Future<void> toggleActiveStatus(String table, String id, bool status) async {
try {
await _client.from(table).update({'is_active': status}).eq('id', id);
await _supabase.from(table).update({'is_active': status}).eq('id', id);
} catch (e) {
throw 'Errore durante la modifica dello stato';
}
}
Future<List<ModelModel>> searchModels(String query) async {
try {
final response = await _supabase
.from('model')
.select()
.ilike('name_with_brand', '%$query%') // Cerca ovunque nel nome
.eq('is_active', true)
.limit(10); // Non esageriamo con i risultati
return (response as List).map((m) => ModelModel.fromJson(m)).toList();
} catch (e) {
throw 'Errore durante la ricerca: $e';
}
}
}

View File

@@ -12,7 +12,7 @@ class ModelModel extends Equatable {
const ModelModel({
this.id,
required this.name,
required this.nameWithBrand,
this.nameWithBrand = '',
required this.brandId,
this.isActive = true,
this.createdAt,

View File

@@ -0,0 +1,111 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
import 'package:flux/features/master_data/products/models/brand_model.dart';
class QuickProductDialog extends StatefulWidget {
final List<BrandModel> existingBrands;
const QuickProductDialog({super.key, required this.existingBrands});
@override
State<QuickProductDialog> createState() => _QuickProductDialogState();
}
class _QuickProductDialogState extends State<QuickProductDialog> {
final _modelCtrl = TextEditingController();
String _selectedBrandName = "";
bool _isLoading = false;
Future<void> _save() async {
final NavigatorState navigator = Navigator.of(context);
if (_selectedBrandName.isEmpty || _modelCtrl.text.isEmpty) return;
setState(() => _isLoading = true);
final newModel = await context.read<ProductCubit>().quickCreateProduct(
brandName: _selectedBrandName.trim(),
modelName: _modelCtrl.text.trim(),
);
setState(() => _isLoading = false);
if (context.mounted) {
navigator.pop(newModel); // Restituiamo il modello creato
}
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text("Nuovo Dispositivo"),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// AUTOCOMPLETE PER IL BRAND LOCALE
Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return const Iterable<String>.empty();
}
final query = textEditingValue.text.toLowerCase();
// Filtriamo i brand che contengono la stringa cercata
return widget.existingBrands
.map((b) => b.name)
.where((name) => name.toLowerCase().contains(query));
},
onSelected: (String selection) {
_selectedBrandName = selection;
},
fieldViewBuilder:
(
context,
textEditingController,
focusNode,
onFieldSubmitted,
) {
return TextField(
controller: textEditingController,
focusNode: focusNode,
autofocus: true,
decoration: const InputDecoration(
labelText: "Marca (es: Apple, Samsung)",
hintText: "Inizia a scrivere...",
),
onChanged: (val) => _selectedBrandName = val,
);
},
),
const SizedBox(height: 16),
TextField(
controller: _modelCtrl,
decoration: const InputDecoration(
labelText: "Modello (es: iPhone 15 Pro)",
),
textInputAction: TextInputAction.done,
onSubmitted: (_) => _save(),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text("Annulla"),
),
ElevatedButton(
onPressed: _isLoading ? null : _save,
child: _isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text("Crea"),
),
],
);
}
}