-
Notifications
You must be signed in to change notification settings - Fork 4
/
8.SneakersShop.swift
336 lines (294 loc) 路 12.4 KB
/
8.SneakersShop.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// 8.SneakersShop.swift
// SUIChallenges
//
// Created by Alexander Kraev on 15.10.2022.
//
import SwiftUI
struct Hoka: Identifiable {
let id: Int
let image: String
let name: String
let sizes: [String]
let colors: [Color]
}
struct SneakersShop: View {
@State var sneakers: [Hoka] = [.init(id: 1, image: "hoka_1", name: "Hoka Clifton 3", sizes: ["41", "42", "42.5", "43", "44"], colors: [.black, .yellow, .white]),
.init(id: 2, image: "hoka_2", name: "Hoka Rincon", sizes: ["40", "42.5"], colors: [.black, .red]),
.init(id: 3, image: "hoka_3", name: "Hoka Clifton 4", sizes: ["41", "42", "42.5", "43"], colors: [.yellow, .black, .blue])]
@State var selectedId: Int = 0
@State var searchText: String = ""
@State var inCartCount: Int = 0
var body: some View {
GeometryReader { proxy in
ZStack {
Color.gray.opacity(0.3)
.ignoresSafeArea(.all, edges: .all)
Cloud(alignment: .topTrailing, proxy: proxy, offset: .init(width: 230, height: -100), frameHeightRatio: 1.6, color: .red)
.opacity(0.8)
Cloud(alignment: .bottomLeading, proxy: proxy, offset: .init(width: -230, height: 120), frameHeightRatio: 1.4, color: .blue)
.opacity(0.7)
ScrollView {
VStack {
HStack {
Text("Welcome 馃馃徎")
.font(.largeTitle)
Spacer()
Button {
} label: {
ZStack {
Image(systemName: "cart.fill")
.resizable()
.padding()
.foregroundColor(.white)
.background(
.black.opacity(0.7)
)
.cornerRadius(8.0)
Text("\(inCartCount > 99 ? "99+" : "\(inCartCount)")")
.kerning(0.3)
.lineLimit(1)
.font(.subheadline)
.truncationMode(.tail)
.foregroundColor(.yellow.opacity(0.9))
.padding(.horizontal, 4)
.cornerRadius(50)
.opacity(inCartCount == 0 ? 0.0 : 1.0)
.offset(x: 16.0, y: -8.0)
}
}.frame(width: 50)
}
HStack {
SearchBar(text: $searchText) {
withAnimation(.linear) {
selectedId = 0
}
}
Button {
} label: {
Image(systemName: "slider.horizontal.3")
.resizable()
.padding()
.foregroundColor(.white)
.background(
.black.opacity(0.7)
)
.cornerRadius(8.0)
}.frame(width: 50)
}
.frame(height: 50)
HStack {
Button {
} label: {
Image(systemName: "square.grid.2x2.fill")
.padding()
.foregroundColor(.white)
.opacity(0.8)
}
.frame(width: 30, height: 30)
Text("MOST POPULAR")
.font(.caption)
.fontWeight(.heavy)
Spacer()
Button {
} label: {
Text("Show all")
.foregroundColor(.white)
.font(.caption2)
.fontWeight(.bold)
.opacity(0.7)
}
}
.offset(y: 30)
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(sneakers, id: \.id) { sneaker in
Sneaker(isSelected: sneaker.id == selectedId,
imageName: sneaker.image,
name: sneaker.name,
availableSizes: sneaker.sizes,
colors: sneaker.colors) {
inCartCount = inCartCount + 1
}
.onTapGesture {
withAnimation(.linear) {
selectedId = sneaker.id
}
}
.frame(height: 350.0)
}
}
}
}
}
.padding(.horizontal, 12)
}
}
.onTapGesture {
withAnimation(.linear) {
selectedId = 0
}
}
}
}
struct Cloud: View {
let alignment: Alignment
let proxy: GeometryProxy
let offset: CGSize
let frameHeightRatio: CGFloat
let color: Color
var body: some View {
Circle()
.fill(color)
.frame(height: proxy.size.height / frameHeightRatio)
.offset(offset)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: alignment)
}
}
struct Sneaker: View {
let isSelected: Bool
let imageName: String
let name: String
let availableSizes: [String]
let colors: [Color]
let action: (() -> ())?
private let imageSize: CGSize = .init(width: 150, height: 70.0)
var body: some View {
VStack {
VStack(spacing: 10.0) {
Image(imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: isSelected ? imageSize.width * 2.5: imageSize.width,
height: isSelected ? imageSize.height * 2.5 : imageSize.height)
.offset(x: isSelected ? 30 : 0.0, y: isSelected ? -60.0 : 0.0)
.rotationEffect(isSelected ? .degrees(-30.0) : .degrees(0.0))
Text(name)
.fontWeight(.heavy)
.offset(y: isSelected ? -60.0 : 0.0)
}
if isSelected {
Group {
HStack {
Text("SIZE:")
.fontWeight(.medium)
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(availableSizes, id: \.self) {
Text($0)
.foregroundColor(.black)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(.white)
)
}
}
}
}
HStack {
Text("COLOR:")
.fontWeight(.medium)
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(colors, id: \.self) {
Circle()
.stroke(.black, lineWidth: 2)
.background(Circle().fill($0))
.frame(width: 20, height: 22)
}
}
.padding(.horizontal, 8.0)
}
}
.frame(height: 20.0)
Button {
action?()
} label: {
Capsule()
.fill(.white)
.overlay(
Text("BUY NOW")
.fontWeight(.medium)
.foregroundColor(.black)
)
.frame(height: 30)
}
.buttonStyle(ScaleButtonStyle())
}
.offset(y: isSelected ? -50.0 : 0.0)
.opacity(isSelected ? 1.0 : 0.0)
}
}
.padding(8.0)
.frame(width: 170.0, height: 250.0)
.background(
.ultraThinMaterial,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
)
}
}
struct SneakersShop_Previews: PreviewProvider {
static var previews: some View {
SneakersShop()
}
}
struct Sneaker_Previews: PreviewProvider {
static var previews: some View {
Sneaker(isSelected: true,
imageName: "hoka_1",
name: "Hoka Clifton",
availableSizes: ["41", "42", "42.5", "43"], colors: [.blue, .red, .green]) { }
}
}
struct SearchBar: View {
@Binding var text: String
@State var placeholder: String = "Search for the sneakers"
let action: (() -> ())?
var body: some View {
Group{
HStack {
Image(systemName: "magnifyingglass")
.renderingMode(.template)
.frame(width: 20.0, height: 20.0)
.foregroundColor(.black)
.padding(.leading, 16)
.padding(.trailing, 11)
.opacity(0.3)
Rectangle()
.frame(width: 2.0)
.padding(.horizontal, 3.0)
.padding(.vertical, 8.0)
.opacity(0.3)
TextField(placeholder, text: $text)
.onTapGesture {
action?()
}
}
.buttonStyle(PlainButtonStyle())
.background(
.ultraThinMaterial,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
)
}
.cornerRadius(8)
}
}
struct SearchBar_Previews: PreviewProvider {
static var previews: some View {
SearchBar(text: .constant("")) {}
}
}
private struct ScaleButtonStyle: ButtonStyle {
public init() {}
public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 0.95 : 1)
.animation(.linear(duration: 0.2), value: configuration.isPressed)
.brightness(configuration.isPressed ? -0.05 : 0)
}
}
private extension ButtonStyle where Self == ScaleButtonStyle {
static var scale: ScaleButtonStyle {
ScaleButtonStyle()
}
}