@@ -18,32 +18,57 @@ struct ProductCell: View {
1818 VStack ( alignment: . leading) {
1919 Text ( verbatim: product. productName)
2020 . font ( . headline)
21-
22- HStack ( spacing: 0 ) {
23- Text ( product. unitPrice ?? 0 , format: . currency( code: " USD " ) )
24- if unitsInStock > 0 {
25- Text ( " , \( unitsInStock) in stock " )
26- }
27- else if product. discontinued != " 0 " {
28- Text ( " , " ) +
29- Text( " discontinued " ) . foregroundColor ( . red)
30- }
31- else {
32- Text ( " , " ) +
33- Text( " out of stock. " ) . foregroundColor ( . red)
34- if let ordered = product. unitsOnOrder, ordered > 0 {
35- Text ( " , ordered \( ordered) " )
36- }
37- }
38- }
39-
40- if unitsInStock > 0 && product. discontinued != " 0 " {
41- Text ( " Discontinued " )
42- . foregroundColor ( . red)
43- }
21+
22+ detailText
4423 }
4524 } ,
4625 icon: { Image ( systemName: " p.circle " ) }
4726 )
4827 }
28+
29+ var detailText : Text {
30+ var text = Text ( product. unitPrice ?? 0 , format: . currency( code: " USD " ) )
31+
32+ if unitsInStock > 0 {
33+ text = text + Text( " , \( unitsInStock) in stock " )
34+ }
35+ else {
36+ text = text + Text( " , " )
37+ text = text + Text( " out of stock " ) . foregroundColor ( . red)
38+ if let ordered = product. unitsOnOrder, ordered > 0 {
39+ text = text + Text( " , ordered \( ordered) " )
40+ }
41+ }
42+
43+ if product. discontinued != " 0 " {
44+ text = text + Text( " , " )
45+ text = text + Text( " discontinued " ) . foregroundColor ( . red)
46+ }
47+ return text
48+ }
49+
50+ @ViewBuilder var productDetails : some View {
51+ HStack ( spacing: 0 ) {
52+ Text ( product. unitPrice ?? 0 , format: . currency( code: " USD " ) )
53+
54+ if unitsInStock > 0 {
55+ Text ( " , \( unitsInStock) in stock " )
56+ if product. discontinued != " 0 " {
57+ Text ( " , " ) +
58+ Text( " discontinued " ) . foregroundColor ( . red)
59+ }
60+ }
61+ else if product. discontinued != " 0 " {
62+ Text ( " , " ) +
63+ Text( " discontinued " ) . foregroundColor ( . red)
64+ }
65+ else {
66+ Text ( " , " ) +
67+ Text( " out of stock " ) . foregroundColor ( . red)
68+ if let ordered = product. unitsOnOrder, ordered > 0 {
69+ Text ( " , ordered \( ordered) " )
70+ }
71+ }
72+ }
73+ }
4974}
0 commit comments