mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 01:56:24 +00:00
Enhance AddDeviceTypeWidget to support initial product counts and update selection logic. Modify AssignTagsDialog to pass initial products from space allocations, improving user experience and maintainability.
This commit is contained in:
@ -10,7 +10,12 @@ import 'package:syncrow_web/utils/color_manager.dart';
|
||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||
|
||||
class AddDeviceTypeWidget extends StatefulWidget {
|
||||
const AddDeviceTypeWidget({super.key});
|
||||
const AddDeviceTypeWidget({
|
||||
super.key,
|
||||
this.initialProducts = const [],
|
||||
});
|
||||
|
||||
final List<Product> initialProducts;
|
||||
|
||||
@override
|
||||
State<AddDeviceTypeWidget> createState() => _AddDeviceTypeWidgetState();
|
||||
@ -18,6 +23,16 @@ class AddDeviceTypeWidget extends StatefulWidget {
|
||||
|
||||
class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
||||
final Map<Product, int> _selectedProducts = {};
|
||||
final Map<Product, int> _initialProductCounts = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
for (final product in widget.initialProducts) {
|
||||
_initialProductCounts[product] = (_initialProductCounts[product] ?? 0) + 1;
|
||||
}
|
||||
_selectedProducts.addAll(_initialProductCounts);
|
||||
}
|
||||
|
||||
void _onIncrement(Product product) {
|
||||
setState(() {
|
||||
@ -27,8 +42,12 @@ class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
||||
|
||||
void _onDecrement(Product product) {
|
||||
setState(() {
|
||||
if ((_selectedProducts[product] ?? 0) > 0) {
|
||||
_selectedProducts[product] = _selectedProducts[product]! - 1;
|
||||
final initialCount = _initialProductCounts[product] ?? 0;
|
||||
final currentCount = _selectedProducts[product] ?? 0;
|
||||
if (currentCount > initialCount) {
|
||||
_selectedProducts[product] = currentCount - 1;
|
||||
} else if (currentCount > 0 && initialCount == 0) {
|
||||
_selectedProducts[product] = currentCount - 1;
|
||||
if (_selectedProducts[product] == 0) {
|
||||
_selectedProducts.remove(product);
|
||||
}
|
||||
@ -63,7 +82,22 @@ class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
||||
actions: [
|
||||
SpaceDetailsActionButtons(
|
||||
onSave: () {
|
||||
final result = _selectedProducts.entries
|
||||
final resultMap = <Product, int>{};
|
||||
resultMap.addAll(_selectedProducts);
|
||||
|
||||
for (final entry in _initialProductCounts.entries) {
|
||||
final product = entry.key;
|
||||
final initialCount = entry.value;
|
||||
final currentCount = resultMap[product] ?? 0;
|
||||
|
||||
if (currentCount > initialCount) {
|
||||
resultMap[product] = currentCount - initialCount;
|
||||
} else {
|
||||
resultMap.remove(product);
|
||||
}
|
||||
}
|
||||
|
||||
final result = resultMap.entries
|
||||
.expand((entry) => List.generate(entry.value, (_) => entry.key))
|
||||
.toList();
|
||||
Navigator.of(context).pop(result);
|
||||
|
@ -205,7 +205,14 @@ class _AssignTagsDialogState extends State<AssignTagsDialog> {
|
||||
onCancel: () async {
|
||||
final newProducts = await showDialog<List<Product>>(
|
||||
context: context,
|
||||
builder: (context) => const AddDeviceTypeWidget(),
|
||||
builder: (context) => AddDeviceTypeWidget(
|
||||
initialProducts: [
|
||||
..._space.productAllocations.map((e) => e.product),
|
||||
..._space.subspaces
|
||||
.expand((s) => s.productAllocations)
|
||||
.map((e) => e.product),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (newProducts == null || newProducts.isEmpty) return;
|
||||
|
Reference in New Issue
Block a user