-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBetTrackerView.swift
339 lines (287 loc) · 11.3 KB
/
BetTrackerView.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
337
338
339
//
// BetTrackerView.swift
// All In
//
// Created by jiwon jeong on 3/20/25.
//
import SwiftUI
import Charts
struct BetTrackerView: View {
// MARK: - Properties
@State private var timeFilter: TimeFilter = .weekly
@State private var showingFAQ = false
let user: User
var myGradient = Gradient(
colors: [
Constants.Colors.gradientBlue,
Constants.Colors.gradientLightBlue,
Constants.Colors.gradientPurple,
Constants.Colors.gradientLavender
]
)
enum TimeFilter {
case weekly
case monthly
}
/// Calculates the Total Gain/Losses for the Selected User
private func calculateTotalGainLoss() -> Int {
let data = timeFilter == .weekly ? user.weeklyGainLoss : user.monthlyGainLoss
return data.reduce(0) { $0 + $1.value }
}
/// Calculates the user's current week gain/Loss
private func calculateCurrentWeekGainLoss() -> String {
let currentWeekTotal = user.weeklyGainLoss.reduce(0) { $0 + $1.value }
return String(format: "%.2f", Double(currentWeekTotal) / 100.0)
}
/// Calculates the user's last week gain/loss
private func calculateLastWeekGainLoss() -> Int {
// TODO: need to make network requests for this
400
}
/// Calculates the day's since user's account creation
private func daysSinceAccountCreation() -> Int {
let calendar = Calendar.current
let components = calendar.dateComponents([.day], from: user.createdAt, to: Date())
return components.day ?? 0
}
/// Calculating the scaling so that it is easily viewable
private func calculateMaxScaleValue() -> Int {
let data = timeFilter == .weekly ? user.weeklyGainLoss : user.monthlyGainLoss
let maxAbsValue = data.map { abs($0.value) }.max() ?? 5000
// Round up to nearest 1000 for cleaner scale intervals
let roundedUp = Int(ceil(Double(maxAbsValue) / 1000.0) * 1000)
return max(1000, roundedUp)
}
// MARK: - UI
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 16) {
trackerCard
HStack(spacing: 17) {
totalProfitCard
rankingCard
}
HStack(spacing: 17) {
contractsSoldCard
accountAgeCard
}
}
.padding(24)
}
.ignoresSafeArea(edges: .bottom)
.background(Constants.Colors.background)
.navigationDestination(isPresented: $showingFAQ) {
FrequentAskedQuestion()
}
}
}
private var trackerCard: some View {
VStack(alignment: .leading, spacing: 24) {
HStack {
Text("Your Bet Tracker")
.font(Constants.Fonts.header)
.foregroundStyle(Constants.Colors.white)
Spacer()
Button(action: {
showingFAQ = true
}) {
Image(systemName: "questionmark.circle")
.foregroundStyle(Constants.Colors.white)
.font(.system(size: 24))
}
}
HStack {
timeFilterPills
Spacer()
HStack(spacing: 7) {
Image(systemName: "dollarsign.circle")
.foregroundStyle(Constants.Colors.white)
.font(.system(size: 24))
Text("\(user.balance)")
.font(Constants.Fonts.subheaderProfile)
.foregroundStyle(Constants.Colors.white)
}
}
gainLossChart
}
.cornerRadius(16)
}
private var timeFilterPills: some View {
HStack(spacing: 0) {
Pill(title: "Weekly", isSelected: timeFilter == .weekly) {
timeFilter = .weekly
}
Pill(title: "Monthly", isSelected: timeFilter == .monthly) {
timeFilter = .monthly
}
}
.padding(4)
.background(Constants.Colors.blackBlue)
.cornerRadius(28)
}
private var gainLossChart: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("Gains/Losses")
.font(Constants.Fonts.profileCard)
.foregroundStyle(Constants.Colors.white)
Spacer()
Text("+$\(calculateTotalGainLoss())")
.font(Constants.Fonts.subheaderProfile)
.foregroundStyle(Constants.Colors.white)
}
HStack {
Spacer()
VStack(alignment: .trailing, spacing: 2) {
HStack(spacing: 0) {
Text("+\(calculateCurrentWeekGainLoss())")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.moneyGreen)
Text(" This week")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
}
HStack(spacing: 0) {
Text("+$\(calculateLastWeekGainLoss())")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.moneyGreen)
Text(" Last week")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
}
}
}
Chart {
ForEach(timeFilter == .weekly ? user.weeklyGainLoss : user.monthlyGainLoss, id: \.day) { item in
BarMark(
x: .value("Day", item.day),
y: .value("Value", item.value)
)
.foregroundStyle(item.value >= 0 ? Constants.Colors.greenChart : Constants.Colors.redChart)
.cornerRadius(0)
}
RuleMark(y: .value("Zero", 0))
.lineStyle(StrokeStyle(lineWidth: 1, dash: [2, 2]))
.foregroundStyle(Constants.Colors.white)
RuleMark(y: .value("Upper Line", -calculateMaxScaleValue()))
.lineStyle(StrokeStyle(lineWidth: 1))
.foregroundStyle(Constants.Colors.white)
}
.chartYScale(domain: -calculateMaxScaleValue()...calculateMaxScaleValue())
.chartYAxis {
let maxValue = calculateMaxScaleValue()
AxisMarks(position: .leading, values: [-maxValue, -maxValue/2, 0, maxValue/2, maxValue]) { value in
AxisGridLine()
AxisTick()
AxisValueLabel {
if let intValue = value.as(Int.self) {
Text("\(intValue)")
.font(Constants.Fonts.caption)
.foregroundStyle(Constants.Colors.white)
}
}
}
}
.chartXAxis {
AxisMarks(values: timeFilter == .weekly ? ["M", "T", "W", "TH", "F", "S", "SU"] : Array(1...12).map { "\($0)" }) { value in
AxisValueLabel {
if let day = value.as(String.self) {
Text(day)
.font(Constants.Fonts.caption)
.foregroundStyle(Constants.Colors.white)
}
}
}
}
.frame(height: 165)
.padding(.top, 8)
}
.padding(16)
.background(Constants.Colors.blackBlue)
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(LinearGradient(gradient: myGradient, startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1)
)
}
private var totalProfitCard: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Total Profit")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
Text("$\(user.totalProfit)")
.font(Constants.Fonts.cardContent)
.foregroundStyle(Constants.Colors.white)
.padding(.top, 8)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(16)
.background(Constants.Colors.blackBlue)
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(LinearGradient(gradient: myGradient, startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1)
)
}
private var rankingCard: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Ranking No.")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
Text("\(user.ranking)")
.font(Constants.Fonts.cardContent)
.foregroundStyle(Constants.Colors.white)
.padding(.top, 8)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(16)
.background(Constants.Colors.blackBlue)
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(LinearGradient(gradient: myGradient, startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1)
)
}
private var contractsSoldCard: some View {
VStack(alignment: .leading, spacing: 8) {
Text("No. of Contracts Sold")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
Text("\(user.sellerTransactions.count)")
.font(Constants.Fonts.cardContent)
.foregroundStyle(Constants.Colors.white)
.padding(.top, 8)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(16)
.background(Constants.Colors.blackBlue)
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(LinearGradient(gradient: myGradient, startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1)
)
}
private var accountAgeCard: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Age of Account")
.font(Constants.Fonts.cardHeader)
.foregroundStyle(Constants.Colors.white)
Text("\(daysSinceAccountCreation()) days")
.font(Constants.Fonts.cardContent)
.foregroundStyle(Constants.Colors.white)
.padding(.top, 8)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(16)
.background(Constants.Colors.blackBlue)
.cornerRadius(16)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(LinearGradient(gradient: myGradient, startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1)
)
}
}
#Preview {
BetTrackerView(user: User.dummyData[0])
}