-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProductCardView.swift
85 lines (72 loc) · 2.73 KB
/
ProductCardView.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// ProductCardView.swift
// TikTokFavourites
//
// Created by Wari Wahab on 7/9/23.
//
import SwiftUI
struct ProductCardView: View {
let product: Product
private var costFormatted: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2 // Set the desired number of decimal places
return formatter.string(from: NSNumber(value: product.cost)) ?? "$0.00"
}
private var discountFormatted: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2 // Set the desired number of decimal places
return formatter.string(from: NSNumber(value: product.discount)) ?? "$0.00"
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Image(product.imageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 150, height: 150)
.cornerRadius(8)
Text(product.title)
.font(.headline)
.foregroundColor(.black)
Text("$\(costFormatted)")
.font(.system(size: 16, weight: .bold))
.foregroundColor(.red)
HStack {
Text("$\(discountFormatted) off")
.font(.system(size: 10))
.foregroundColor(.red)
.background(Color(.systemGray6))
.cornerRadius(4)
Spacer()
Text("\(product.unitsSold) sold")
.font(.system(size: 14))
.foregroundColor(.gray)
}
}
.padding()
.background(Color.white)
.cornerRadius(8)
.shadow(radius: 4)
}
}
struct Product: Identifiable {
let id = UUID()
let imageName: String
let title: String
let cost: Double
let discount: Double
let unitsSold: Int
}
let products: [Product] = [
Product(imageName: "product1", title: "Product 1", cost: 20.0, discount: 5.0, unitsSold: 100),
Product(imageName: "product2", title: "Product 2", cost: 25.0, discount: 10.0, unitsSold: 200),
Product(imageName: "product3", title: "Product 2", cost: 25.0, discount: 10.0, unitsSold: 200),
Product(imageName: "product4", title: "Product 2", cost: 25.0, discount: 10.0, unitsSold: 200),
Product(imageName: "product5", title: "Product 2", cost: 25.0, discount: 10.0, unitsSold: 200),
]
struct ProductCardView_Previews: PreviewProvider {
static var previews: some View {
ProductCardView(product: products[0])
}
}