This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MenuExtrasView.swift
333 lines (290 loc) · 11.6 KB
/
MenuExtrasView.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
//
// MenuView.swift
// PaimonMenuBar
//
// Created by Spencer Woo on 2022/3/25.
//
import Defaults
import Foundation
import Kingfisher
import SwiftUI
class RelativeFormatter {
private let df = DateFormatter()
init() {
df.dateStyle = DateFormatter.Style.long
df.timeStyle = DateFormatter.Style.short
df.doesRelativeDateFormatting = true
}
func string(time: Date) -> String {
return df.string(from: time)
}
}
/// Return the formatted time interval in a human-readable string
/// - Parameter timeInterval: A time interval represented in seconds
/// - Returns: A human-readable string representing the time interval
private func formatTimeInterval(timeInterval: String) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
formatter.unitsStyle = .abbreviated
return formatter.string(from: (TimeInterval(timeInterval) ?? TimeInterval("0"))!) ?? ""
}
/// Format a date that is of 'timeInterval' seconds away from now
/// - Parameter timeInterval: The number of seconds away from current time
/// - Returns: A human-readable string describing the future date
private func formatFutureDate(timeInterval: String) -> String {
let currentTime = Date()
let futureTime = currentTime.addingTimeInterval((TimeInterval(timeInterval) ?? TimeInterval("0"))!)
if Calendar.current.isDateInToday(futureTime) {
return "\(String.localized("Today")) \(futureTime.shortenedFormatted)"
}
if Calendar.current.isDateInTomorrow(futureTime) {
return "\(String.localized("Tomorrow")) \(futureTime.shortenedFormatted)"
}
// This should not happen, but just in case.
return futureTime.defaultFormatted
}
struct MenuExtrasView: View {
@Default(.lastGameRecord) private var lastGameRecord
var body: some View {
ZStack {
VStack(spacing: 8) {
ResinView(
currentResin: lastGameRecord.data.current_resin,
maxResin: lastGameRecord.data.max_resin,
resinRecoveryTime: lastGameRecord.data.resin_recovery_time,
fetchAt: lastGameRecord.fetchAt
)
ExpeditionView(
expeditions: lastGameRecord.data.expeditions,
maxExpeditionNum: lastGameRecord.data.max_expedition_num,
currentExpeditionNum: lastGameRecord.data.current_expedition_num
)
DailyCommissionView(
finishedTaskNum: lastGameRecord.data.finished_task_num,
totalTaskNum: lastGameRecord.data.total_task_num
)
ExtraTaskRewardView(
remainResinDiscountNum: lastGameRecord.data.remain_resin_discount_num,
resinDiscountNumLimit: lastGameRecord.data.resin_discount_num_limit,
isExtraTaskRewardReceived: lastGameRecord.data.is_extra_task_reward_received
)
HomeCoinView(
currentHomeCoin: lastGameRecord.data.current_home_coin,
maxHomeCoin: lastGameRecord.data.max_home_coin,
homeCoinRecoveryTime: lastGameRecord.data.home_coin_recovery_time
)
ParametricTransformerView(transformer: lastGameRecord.data.transformer)
}
.padding([.horizontal])
.padding([.vertical], 8)
.opacity(lastGameRecord.fetchAt == nil ? 0.4 : 1.0)
.blur(radius: lastGameRecord.fetchAt == nil ? 4 : 0)
if lastGameRecord.fetchAt == nil {
VStack(spacing: 16) {
Spacer()
Image("KokomiSad").resizable().frame(width: 72, height: 72)
Text("NOT INITIALIZED")
.font(.custom("Avenir Next Bold", size: 24, relativeTo: .largeTitle).italic())
Text(
"Please go to _Preferences > Configuration_ and setup your in-game **_UID_**, **_server_**, and **_cookie_**."
)
Spacer()
HStack {
Label("Open preferences here", systemImage: "arrow.down")
Spacer()
Link(destination: URL(string: "https://paimon.swo.moe/#how-to-get-my-cookie")!) {
Button("?") {
print("Navigating to help page.")
}.clipShape(Circle()).shadow(radius: 1)
}
}
}
.padding()
}
}.onAppear {
GameRecordUpdater.shared.tryFetchGameRecordAndRender()
}
}
}
struct ResinView: View {
@Default(.fetchFailed) private var fetchFailed
let currentResin: Int
let maxResin: Int
let resinRecoveryTime: String
let fetchAt: Date?
private let formatter = RelativeFormatter()
var body: some View {
VStack(spacing: 8) {
HStack {
Label {
Text((fetchAt == nil) ? "Not updated" :
(fetchFailed ? "Update failed, check the official APP for captchas" :
"Update: \(formatter.string(time: fetchAt!))"))
.font(.caption)
} icon: {
Image(systemName: "circle.fill").scaleEffect(0.4).frame(width: 5)
.foregroundColor(fetchFailed ? Color.red : Color.green)
}.opacity(0.6)
}
HStack(spacing: 4) {
Image("FragileResin")
.resizable()
.frame(width: 16, height: 16)
Text("Current Resin")
.font(.subheadline)
.opacity(0.6)
Spacer()
}
Text("\(currentResin)/\(maxResin)")
.frame(maxWidth: .infinity, alignment: .leading)
.font(.custom("Avenir Next Bold", size: 25, relativeTo: .largeTitle).italic())
HStack {
Label("Fully replenished", systemImage: "moon.circle")
Spacer()
Text(formatTimeInterval(timeInterval: resinRecoveryTime))
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
HStack {
Label("ETA", systemImage: "clock")
Spacer()
Text(formatFutureDate(timeInterval: resinRecoveryTime))
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
Divider()
}
}
}
struct ExpeditionView: View {
let expeditions: [Expeditions]
let maxExpeditionNum: Int
let currentExpeditionNum: Int
var body: some View {
VStack(spacing: 10) {
HStack {
Text("Expeditions \(currentExpeditionNum)/\(maxExpeditionNum)")
.font(.subheadline)
.opacity(0.6)
Spacer()
Image("Expedition").resizable().frame(width: 18, height: 18)
}
ForEach(expeditions, id: \.self) { expedition in
ExpeditionItemView(
status: expedition.status, avatar: expedition.avatar_side_icon,
remainedTime: expedition.remained_time
)
}
Divider()
}
}
}
struct ExpeditionItemView: View {
let status: String
let avatar: String
let remainedTime: String
// 20 hours in seconds
let totalExpeditionTime: Float = 20 * 60 * 60
var body: some View {
HStack {
KFImage.url(URL(string: avatar))
.resizable()
.placeholder { Color.gray.opacity(0.3) }
.clipShape(Circle())
.overlay(Circle().stroke(status == "Finished" ? Color.green : Color.gray))
.frame(width: 22, height: 22)
VStack(spacing: 6) {
HStack {
Text(status == "Finished" ? String.localized("Complete") : String.localized("Exploring"))
Spacer()
Text(formatTimeInterval(timeInterval: remainedTime))
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
ProgressView(value: totalExpeditionTime - (Float(remainedTime) ?? 0), total: totalExpeditionTime)
.progressViewStyle(LinearProgressViewStyle(tint: status == "Finished" ?
Color.green : Color(red: 0.89, green: 0.90, blue: 0.92)))
.frame(height: 1).scaleEffect(x: 1, y: 0.4, anchor: .center)
}
}
}
}
struct DailyCommissionView: View {
let finishedTaskNum: Int
let totalTaskNum: Int
var body: some View {
HStack {
Image("Commision")
.resizable()
.frame(width: 20, height: 20, alignment: .leading)
Text("Daily commissions")
Spacer()
Text("\(totalTaskNum - finishedTaskNum) left")
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
}
}
struct HomeCoinView: View {
let currentHomeCoin: Int
let maxHomeCoin: Int
let homeCoinRecoveryTime: String
var body: some View {
HStack {
Image("JarOfRiches")
.resizable()
.scaledToFit()
.frame(width: 20, height: 20, alignment: .center)
Text("Realm currency")
Spacer()
Text("\(currentHomeCoin)/\(maxHomeCoin)")
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
}
}
struct ExtraTaskRewardView: View {
let remainResinDiscountNum: Int
let resinDiscountNumLimit: Int
let isExtraTaskRewardReceived: Bool
var body: some View {
HStack {
Image("Domain")
.resizable()
.scaledToFit()
.frame(width: 20, height: 20, alignment: .center)
Text("Weekly bosses")
Spacer()
Text("\(remainResinDiscountNum) left")
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
}
}
struct ParametricTransformerView: View {
let transformer: Transformer
func formatRecoveryTime(recoveryTime: RecoveryTime) -> String {
if recoveryTime.reached {
return "Ready"
} else {
return recoveryTime
.Day != 0 ? "\(recoveryTime.Day) \(String.localized(recoveryTime.Day == 1 ? "day" : "days"))" :
recoveryTime
.Hour != 0 ? "\(recoveryTime.Hour) \(String.localized(recoveryTime.Hour == 1 ? "hour" : "hours"))"
: "\(recoveryTime.Minute) \(String.localized(recoveryTime.Minute == 1 ? "min" : "mins"))"
}
}
var body: some View {
HStack {
Image("ParametricTransformer")
.resizable()
.scaledToFit()
.frame(width: 20, height: 20, alignment: .center)
Text("Parametric transformer")
Spacer()
Text(transformer.obtained ? formatRecoveryTime(recoveryTime: transformer.recovery_time) : "Unavailable")
.font(.custom("Avenir Next Demi Bold", size: 13, relativeTo: .body).italic())
}
}
}
struct MenuView_Previews: PreviewProvider {
static var previews: some View {
MenuExtrasView()
.frame(width: 290.0)
.frame(height: 472.0)
}
}