@@ -49,15 +49,15 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
4949 ///
5050 private let manageStock : Bool
5151
52- /// Product variation attributes
52+ /// Display mode for a product variation.
53+ /// Determines which details to display in the product details label.
5354 ///
54- private let variationAttributes : [ VariationAttributeViewModel ] ?
55+ private let variationDisplayMode : VariationDisplayMode ?
5556
5657 /// Label showing product details. Can include stock status or attributes, price, and variations (if any).
5758 ///
5859 var productDetailsLabel : String {
59- // When provided, the variation attributes should replace the stock status
60- let stockOrAttributesLabel = variationAttributes != nil ? createAttributesText ( ) : createStockText ( )
60+ let stockOrAttributesLabel = createStockOrAttributesText ( )
6161 let priceLabel = createPriceText ( )
6262 let variationsLabel = createVariationsText ( )
6363
@@ -105,7 +105,7 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
105105 canChangeQuantity: Bool ,
106106 imageURL: URL ? ,
107107 numberOfVariations: Int = 0 ,
108- variationAttributes : [ VariationAttributeViewModel ] ? = nil ,
108+ variationDisplayMode : VariationDisplayMode ? = nil ,
109109 currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ) {
110110 self . id = id ?? productOrVariationID. description
111111 self . productOrVariationID = productOrVariationID
@@ -120,7 +120,7 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
120120 self . imageURL = imageURL
121121 self . currencyFormatter = currencyFormatter
122122 self . numberOfVariations = numberOfVariations
123- self . variationAttributes = variationAttributes
123+ self . variationDisplayMode = variationDisplayMode
124124 }
125125
126126 /// Initialize `ProductRowViewModel` with a `Product`
@@ -160,7 +160,7 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
160160 name: String ,
161161 quantity: Decimal = 1 ,
162162 canChangeQuantity: Bool ,
163- attributes : [ VariationAttributeViewModel ] ? = nil ,
163+ displayMode : VariationDisplayMode ,
164164 currencyFormatter: CurrencyFormatter = CurrencyFormatter ( currencySettings: ServiceLocator . currencySettings) ) {
165165 let imageURL : URL ?
166166 if let encodedImageURLString = productVariation. image? . src. addingPercentEncoding ( withAllowedCharacters: . urlQueryAllowed) {
@@ -180,10 +180,32 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
180180 quantity: quantity,
181181 canChangeQuantity: canChangeQuantity,
182182 imageURL: imageURL,
183- variationAttributes : attributes ,
183+ variationDisplayMode : displayMode ,
184184 currencyFormatter: currencyFormatter)
185185 }
186186
187+ /// Determines which product variation details to display.
188+ ///
189+ enum VariationDisplayMode {
190+ /// Displays the variation's stock status
191+ case stock
192+
193+ /// Displays the provided list of variation attributes
194+ case attributes( [ VariationAttributeViewModel ] )
195+ }
196+
197+ /// Creates the stock or variation attributes text.
198+ /// Returns stock text for non-variations; uses variation display mode to determine the text for variations.
199+ ///
200+ private func createStockOrAttributesText( ) -> String {
201+ switch variationDisplayMode {
202+ case . attributes( let attributes) :
203+ return createAttributesText ( from: attributes)
204+ default :
205+ return createStockText ( )
206+ }
207+ }
208+
187209 /// Create the stock text based on a product's stock status/quantity.
188210 ///
189211 private func createStockText( ) -> String {
@@ -202,10 +224,7 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
202224
203225 /// Create the attributes text based on the provided product variation attributes.
204226 ///
205- private func createAttributesText( ) -> String ? {
206- guard let attributes = variationAttributes else {
207- return nil
208- }
227+ private func createAttributesText( from attributes: [ VariationAttributeViewModel ] ) -> String {
209228 return attributes. map { $0. nameOrValue } . joined ( separator: " , " )
210229 }
211230
0 commit comments