-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProductCell.swift
49 lines (41 loc) · 1.14 KB
/
ProductCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import SwiftUI
import Northwind
/**
* A tableview cell for a product. I.e. a view suitable to show Product
* information in a `List`.
*/
struct ProductCell: View {
let product : Product
// Incorrectly marked NULLable in Northwind
private var unitsInStock : Int { product.unitsInStock ?? 0 }
var body: some View {
Label(
title: {
VStack(alignment: .leading) {
Text(verbatim: product.productName)
.font(.headline)
detailText
}
},
icon: { Image(systemName: "p.circle") }
)
}
var detailText : Text {
var text = Text(product.unitPrice ?? 0, format: .currency(code: "USD"))
if unitsInStock > 0 {
text = text + Text(", \(unitsInStock) in stock")
}
else {
text = text + Text(", ")
text = text + Text("out of stock").foregroundColor(.red)
if let ordered = product.unitsOnOrder, ordered > 0 {
text = text + Text(", ordered \(ordered)")
}
}
if product.discontinued != "0" {
text = text + Text(", ")
text = text + Text("discontinued").foregroundColor(.red)
}
return text
}
}