I have a code that checks the availability of attributes. That is, if there is a black color with sizes s, l, xl and m, and there is a purple color with size s, then block unavailable size combinations for purple. However, both colors have a common size s. Also, the user can immediately select the black color on the product card and go to the detailed card where he can select other combinations of sizes and colors. And in this case, if the user selected the black color size M and went to the card, but the size s is blocked, then switching to purple will be blocked. There is a direct dependency between what the user initially selected and which common elements are unavailable for selection. I wrote code that checks the availability of colors and sizes and blocks unavailable non-common combinations, but how to get rid of the hardcode? This approach is not flexible.
An approximate structure from which I create my algorithm, so that it is clearer what I mean
[{“name”: “Purple”,”characteristics”: [{“name”: “dress_size”,”value”: “S”},{“name”: “color”,”value”: “Purple”}],{“name”: “black, size XL”,”characteristics”: [{“name”: “dress_size”,”value”: “XL”},{“name”: “color”,”value”: “Black”}],{“name”: “black, size L”,”characteristics”: [{“name”: “dress_size”,”value”: “L”},{“name”: “color”,”value”: “Black”}],{“name”: “black, size M”,”characteristics”: [{“name”: “dress_size”,”value”: “M”}{“name”: “color”,”value”: “Black”}],{“name”: “black, size C”,”characteristics”: [{“name”: “dress_size”,”value”: “S”}{“name”: “color”,”value”: “Black”}],]
also my code in which I rely on sizes and colors, but it is not flexible and will no longer work if something is added or changed final Map<CharacteristicEnum, Set<String>> availableAttributes;
bool isAttributeAvailable(CharacteristicEnum characteristic, String value) {
final Map<CharacteristicEnum, String> selectedAttributes = selectedAttributeMap;
final Iterable<ProductDetailsModel>? availableProducts = productOffer.value?.where((ProductDetailsModel product) {
return selectedAttributes.entries.every((MapEntry<CharacteristicEnum, String> entry) {
if (entry.key == characteristic) return true;
final List<String>? attributeValues = product.groupedCharacteristics[entry.key];
return attributeValues != null && attributeValues.contains(entry.value);
});
});
if (availableProducts != null && availableProducts.isNotEmpty) {
for (final ProductDetailsModel product in availableProducts) {
final List<String>? availableValues = product.groupedCharacteristics[characteristic];
if (availableValues != null && availableValues.contains(value)) {
return true;
}
}
}
if (characteristic == CharacteristicEnum.color) {
return _canSwitchToAvailableAttribute(characteristic, value);
}
return false;
}
bool _canSwitchToAvailableAttribute(CharacteristicEnum characteristic, String value) {
for (final ProductDetailsModel product in productOffer.value ?? <ProductDetailsModel>[]) {
final List<String>? availableValues = product.groupedCharacteristics[characteristic];
if (availableValues != null && availableValues.contains(value)) {
return true;
}
}
return false;
}
onAsync<SelectAttributeProductDetailEvent>((SelectAttributeProductDetailEvent event) async* {
final Map<CharacteristicEnum, String> selectedMap = state.selectedAttributeMap.copy();
selectedMap[event.nameAttribute] = event.attributeValue;
if (event.nameAttribute == CharacteristicEnum.color) {
final Iterable<String>? availableSizesForNewColor = state.productOffer.value
?.where(
(ProductDetailsModel product) =>
product.groupedCharacteristics[CharacteristicEnum.color]?.contains(event.attributeValue) ?? false,
)
.expand(
(ProductDetailsModel product) =>
product.groupedCharacteristics[CharacteristicEnum.dressSize] ?? <String>[],
);
final String? currentSize = selectedMap[CharacteristicEnum.dressSize];
if (currentSize == null ||
(availableSizesForNewColor != null && !availableSizesForNewColor.contains(currentSize))) {
if (availableSizesForNewColor != null && availableSizesForNewColor.isNotEmpty) {
selectedMap[CharacteristicEnum.dressSize] = availableSizesForNewColor.first;
}
}
}
final Map<CharacteristicEnum, Set<String>> availableAttributes = <CharacteristicEnum, Set<String>>{};
for (final CharacteristicEnum characteristic in state.groupedCharacteristics.keys) {
availableAttributes[characteristic] = state.groupedCharacteristics[characteristic]
?.where((String value) => state.isAttributeAvailable(characteristic, value))
.toSet() ??
<String>{};
}
yield state.copyWith(
selectedAttributeMap: selectedMap,
availableAttributes: availableAttributes,
);
});
}