mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 10:06:19 +00:00
refactor
This commit is contained in:
@ -1,30 +1,38 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
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/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)
|
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(
|
final existingProduct = state.firstWhere(
|
||||||
(p) => p.productId == productId,
|
(p) => p.productId == event.productId,
|
||||||
orElse: () => SelectedProduct(productId: productId, count: 0),
|
orElse: () => SelectedProduct(productId: event.productId, count: 0),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (count > 0) {
|
if (event.count > 0) {
|
||||||
if (!state.contains(existingProduct)) {
|
if (!state.contains(existingProduct)) {
|
||||||
emit([...state, SelectedProduct(productId: productId, count: count)]);
|
emit([
|
||||||
|
...state,
|
||||||
|
SelectedProduct(productId: event.productId, count: event.count)
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
final updatedList = state.map((p) {
|
final updatedList = state.map((p) {
|
||||||
if (p.productId == productId) {
|
if (p.productId == event.productId) {
|
||||||
return SelectedProduct(productId: p.productId, count: count);
|
return SelectedProduct(productId: p.productId, count: event.count);
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
}).toList();
|
}).toList();
|
||||||
emit(updatedList);
|
emit(updatedList);
|
||||||
}
|
}
|
||||||
} else {
|
} 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(
|
CounterWidget(
|
||||||
initialCount: selectedProduct.count,
|
initialCount: selectedProduct.count,
|
||||||
onCountChanged: (newCount) {
|
onCountChanged: (newCount) {
|
||||||
context
|
context.read<AddDeviceTypeModelBloc>().add(
|
||||||
.read<AddDeviceTypeModelBloc>()
|
UpdateProductCountEvent(
|
||||||
.updateProductCount(product.uuid, newCount);
|
productId: product.uuid, count: newCount),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
Reference in New Issue
Block a user