@@ -67,6 +67,12 @@ class ProductDetailsBloc
6767
6868 final ProductVariation previousSelectedVariation = state.selectedVariation;
6969
70+ // Finds the selected variation of a product based on the given event.
71+ //
72+ // Search for the first variation in product variations
73+ // that satisfies the currently selected property and value,
74+ // as well as other previously selected variation's property.
75+ // If no variation is found, it searches for the variation with the given id.
7076 final ProductVariation selectedVariation = state.product.variations
7177 .firstWhereOrNull (
7278 (v) => v.productPropertiesValues.every (
@@ -95,11 +101,14 @@ class ProductDetailsBloc
95101 );
96102 }
97103
104+ /// Filters the available properties of a product based on the selected variation.
105+ /// Returns a list of [AvailablePropertyValues] objects.
98106 List <AvailablePropertyValues > _filterAvailableProps (
99107 Product product,
100108 ProductVariation selectedVariation,
101109 ) {
102110 return product.variations
111+ // First filter for the selected variation's color
103112 .where (
104113 (v) => v.productPropertiesValues.any (
105114 (pv) =>
@@ -112,16 +121,22 @@ class ProductDetailsBloc
112121 .value,
113122 ),
114123 )
124+ // Get its properties values
115125 .map ((v) => v.productPropertiesValues)
116126 .expand ((e) => e)
127+ // Group them by property
117128 .groupFoldBy <PropertyType , List <String >>(
118129 (pv) => pv.property,
119130 (g, pv) => [...g ?? [], pv.value],
120131 )
121132 .entries
122133 .map (
134+ // Convert the grouped properties to [AvailablePropertyValues]
123135 (e) => AvailablePropertyValues (
124136 property: e.key,
137+ // Get available values of each property from product
138+ // then filter its values based on the selected variation's property values
139+ // except for the color property, which always shows all available colors
125140 values: e.value
126141 .map (
127142 (v) => product.availableProperties
@@ -135,6 +150,7 @@ class ProductDetailsBloc
135150 .toList (),
136151 ),
137152 )
153+ // Sort the properties by their enum index
138154 .sortedByCompare (
139155 (p) => p.property.index,
140156 (a, b) => a.compareTo (b),
0 commit comments