This commit is contained in:
hannathkadher
2025-01-05 16:15:35 +04:00
parent ae09cbda1e
commit 58b469b92a
3 changed files with 38 additions and 13 deletions

View File

@ -1,30 +1,38 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/selected_product_model.dart';
import 'package:syncrow_web/pages/spaces_management/tag_model/bloc/add_device_type_model_event.dart';
class AddDeviceTypeModelBloc extends Cubit<List<SelectedProduct>> {
class AddDeviceTypeModelBloc
extends Bloc<AddDeviceTypeModelEvent, List<SelectedProduct>> {
AddDeviceTypeModelBloc(List<SelectedProduct> initialProducts)
: super(initialProducts);
: super(initialProducts) {
on<UpdateProductCountEvent>(_onUpdateProductCount);
}
void updateProductCount(String productId, int count) {
void _onUpdateProductCount(
UpdateProductCountEvent event, Emitter<List<SelectedProduct>> emit) {
final existingProduct = state.firstWhere(
(p) => p.productId == productId,
orElse: () => SelectedProduct(productId: productId, count: 0),
(p) => p.productId == event.productId,
orElse: () => SelectedProduct(productId: event.productId, count: 0),
);
if (count > 0) {
if (event.count > 0) {
if (!state.contains(existingProduct)) {
emit([...state, SelectedProduct(productId: productId, count: count)]);
emit([
...state,
SelectedProduct(productId: event.productId, count: event.count)
]);
} else {
final updatedList = state.map((p) {
if (p.productId == productId) {
return SelectedProduct(productId: p.productId, count: count);
if (p.productId == event.productId) {
return SelectedProduct(productId: p.productId, count: event.count);
}
return p;
}).toList();
emit(updatedList);
}
} else {
emit(state.where((p) => p.productId != productId).toList());
emit(state.where((p) => p.productId != event.productId).toList());
}
}
}

View File

@ -0,0 +1,16 @@
import 'package:equatable/equatable.dart';
abstract class AddDeviceTypeModelEvent extends Equatable {
@override
List<Object> get props => [];
}
class UpdateProductCountEvent extends AddDeviceTypeModelEvent {
final String productId;
final int count;
UpdateProductCountEvent({required this.productId, required this.count});
@override
List<Object> get props => [productId, count];
}

View File

@ -45,9 +45,10 @@ class DeviceTypeTileWidget extends StatelessWidget {
CounterWidget(
initialCount: selectedProduct.count,
onCountChanged: (newCount) {
context
.read<AddDeviceTypeModelBloc>()
.updateProductCount(product.uuid, newCount);
context.read<AddDeviceTypeModelBloc>().add(
UpdateProductCountEvent(
productId: product.uuid, count: newCount),
);
},
),
],