-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataService.swift
191 lines (149 loc) · 5.33 KB
/
DataService.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// DataService.swift
// ShoeShock2
//
// Created by Dante Ausonio on 12/22/22.
//
import Foundation
enum BrandOption {
case Adidas
case Jordans
case Nike
case Sneakers
case Vans
var title: String {
switch self {
case .Adidas:
return "Adidas"
case .Jordans:
return "Jordans"
case .Nike:
return "Nike"
case .Sneakers:
return "Sneakers"
case . Vans:
return "Vans"
}
}
}
class DataService {
static let instance = DataService()
// Declarations
private let brands: [BrandOption] = [.Adidas, .Jordans, .Nike, .Sneakers, .Vans]
private let adidas = [
Shoe(imgName: "adidas1", name: "Adidas 1", brand: "Adidas", price: "$120", isFavorite: false),
Shoe(imgName: "adidas2", name: "Adidas 2", brand: "Adidas", price: "$125", isFavorite: false),
Shoe(imgName: "adidas3", name: "Adidas 3", brand: "Adidas", price: "$225", isFavorite: false),
Shoe(imgName: "adidas4", name: "Adidas 4", brand: "Adidas", price: "$95", isFavorite: false),
Shoe(imgName: "adidas5", name: "Adidas 5", brand: "Adidas", price: "$110", isFavorite: false),
Shoe(imgName: "adidas6", name: "Adidas 6", brand: "Adidas", price: "$175", isFavorite: false)
]
private let jordans = [
Shoe(imgName: "jordans1", name: "Jordans 1", brand: "Jordans", price: "$230", isFavorite: false),
Shoe(imgName: "jordans2", name: "Jordans 2", brand: "Jordans", price: "$200", isFavorite: false),
Shoe(imgName: "jordans3", name: "Jordans 3", brand: "Jordans", price: "$195", isFavorite: false)
]
private let nike = [
Shoe(imgName: "nike1", name: "Nike 1", brand: "Nike", price: "$65", isFavorite: false),
Shoe(imgName: "nike2", name: "Nike 2", brand: "Nike", price: "$80", isFavorite: false),
Shoe(imgName: "nike3", name: "Nike 3", brand: "Nike", price: "$50", isFavorite: false),
]
private let sneakers = [
Shoe(imgName: "sneaker1", name: "Sneakers 1", brand: "Sneakers", price: "$30", isFavorite: false),
Shoe(imgName: "sneaker2", name: "Sneakers 2", brand: "Sneakers", price: "$45", isFavorite: false),
Shoe(imgName: "sneaker3", name: "Sneakers 3", brand: "Sneakers", price: "$65", isFavorite: false),
Shoe(imgName: "sneaker4", name: "Sneakers 4", brand: "Sneakers", price: "$40", isFavorite: false),
]
private let vans = [
Shoe(imgName: "vans1", name: "Vans 1", brand: "Vans", price: "$70", isFavorite: false),
Shoe(imgName: "vans2", name: "Vans 2", brand: "Vans", price: "$65", isFavorite: false),
Shoe(imgName: "vans3", name: "Vans 3", brand: "Vans", price: "$75", isFavorite: false),
]
private var selectedBrand: BrandOption = .Adidas
private var favoriteShoes = [Shoe]()
private var cart = [Shoe]()
//Accessor & Mutator Methods
func getShoes(forBrand brand: BrandOption) -> [Shoe] {
switch brand {
case .Adidas:
return adidas
case .Jordans:
return jordans
case .Nike:
return nike
case .Sneakers:
return sneakers
case .Vans:
return vans
}
}
// Access & Mutate Brands
func getBrands() -> [BrandOption] {
return brands
}
func setSelectedBrand(brand: BrandOption) {
selectedBrand = brand
}
func getSelectedBrand() -> BrandOption {
return selectedBrand
}
// Access & Mutate Favorites
func addFavorite(shoe: Shoe) {
favoriteShoes.append(shoe)
}
func removeFavorite(shoe: Shoe?, index: Int?) {
if shoe != nil {
for (i, favShoe) in favoriteShoes.enumerated() {
if favShoe == shoe {
favoriteShoes.remove(at: i)
break
}
}
} else if index != nil {
favoriteShoes.remove(at: index!)
}
}
func getFavorites() -> [Shoe] {
return favoriteShoes
}
// Access & Mutate Cart
func getCart() -> [Shoe] {
return cart
}
func getShoeFromCart(shoe: Shoe) -> Shoe {
for shoes in cart {
if shoes == shoe {
return shoes
}
}
return adidas[0] //Create a default Empty Shoe
}
func addCartItem(shoe: Shoe) {
// Add Cart Item
if !cart.contains(shoe) {
cart.append(shoe)
}
// Update Count of Cart Item
for (i, cartShoe) in cart.enumerated() {
if cartShoe == shoe {
cart[i].itemCartCount = shoe.itemCartCount
}
}
}
func removeCartItem(shoe: Shoe?, index: Int?) {
if shoe != nil {
for (i, cartShoe) in cart.enumerated() {
if cartShoe == shoe {
cart.remove(at: i)
break
}
}
} else if index != nil {
cart[index!].itemCartCount = 0
cart.remove(at: index!)
}
}
func emptyCart() {
cart.removeAll()
}
}