mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
refactor
This commit is contained in:
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
@ -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),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
|
Reference in New Issue
Block a user