-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutView.swift
More file actions
375 lines (342 loc) · 15.3 KB
/
AboutView.swift
File metadata and controls
375 lines (342 loc) · 15.3 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// SettingsView.swift
// awsary
//
// Created by Tiago Rodrigues on 20/07/2022.
//
import SwiftUI
import SwiftData
import StoreKit
import RevenueCat
import RevenueCatUI
struct AboutView: View {
@Environment(\.dismiss) var dismiss
@Environment(\.modelContext) private var modelContext
@ObservedObject var userModel = UserViewModel.shared
@ObservedObject var awsServices = AwsServices()
@Query var settings: [SystemSetting]
@Query var cachedStats: [CachedAppStats]
private var developerAppsURL: URL? {
URL(string: "https://apps.apple.com/developer/tiagorodrigues/id1634871093")
}
// Add state variable to store the random service
@State private var randomAWSservice: awsService?
// State variables for app statistics
@State private var appStats: AppStats?
@State private var isLoadingStats = false
// Computed property for awsServiceLogoWithLabel setting
private var awsServiceLogoWithLabel: Bool {
get {
return settings.first(where: { $0.key == "awsServiceLogoWithLabel" })?.boolValue ?? true
}
set {
updateSetting(key: "awsServiceLogoWithLabel", boolValue: newValue)
}
}
// Helper method to update or create a setting
private func updateSetting(key: String, boolValue: Bool) {
if let existingSetting = settings.first(where: { $0.key == key }) {
existingSetting.boolValue = boolValue
existingSetting.updatedAt = Date()
} else {
let newSetting = SystemSetting(key: key, value: boolValue)
modelContext.insert(newSetting)
}
do {
try modelContext.save()
} catch {
print("Failed to save setting: \(error)")
}
}
// Function to load cached stats and fetch fresh data if needed
private func loadAppStats() {
// First, load from cache
loadFromCache()
// Then check if we need to fetch fresh data
if shouldFetchFreshData() {
fetchAppStatsFromAPI()
}
}
private func loadFromCache() {
if let cached = cachedStats.first {
// Convert cached data to display format
appStats = AppStats(
currentVersion: cached.currentVersion,
ratings: RatingInfo(
version: cached.ratingVersion,
ratingCount: cached.ratingCount,
customMessage: cached.customMessage,
lastUpdated: cached.lastUpdated
),
featuredMessage: cached.featuredMessage,
updateAvailable: cached.updateAvailable
)
}
}
private func shouldFetchFreshData() -> Bool {
guard let cached = cachedStats.first else {
return true // No cache, need to fetch
}
// Cache is stale if older than 1 hour
let cacheAge = Date().timeIntervalSince(cached.cachedAt)
return cacheAge > 3600 // 1 hour in seconds
}
private func fetchAppStatsFromAPI() {
guard !isLoadingStats else { return }
isLoadingStats = true
guard let url = URL(string: "https://0gbyxcbo3d.execute-api.eu-west-1.amazonaws.com/prod/app-stats") else {
isLoadingStats = false
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
DispatchQueue.main.async {
isLoadingStats = false
if let data = data {
do {
let stats = try JSONDecoder().decode(AppStats.self, from: data)
self.appStats = stats
self.saveToCacheAndUpdate(stats)
} catch {
print("Failed to decode app stats: \(error)")
}
}
}
}.resume()
}
private func saveToCacheAndUpdate(_ stats: AppStats) {
// Remove old cached data
for oldStats in cachedStats {
modelContext.delete(oldStats)
}
// Save new data
let newCachedStats = CachedAppStats(from: stats)
modelContext.insert(newCachedStats)
do {
try modelContext.save()
} catch {
print("Failed to save app stats to cache: \(error)")
}
}
var body: some View {
NavigationStack{
List{
Section(header: Text("Configure service logos")){
VStack{
Toggle(isOn: Binding(
get: { awsServiceLogoWithLabel },
set: { updateSetting(key: "awsServiceLogoWithLabel", boolValue: $0) }
)){
Text("Show name on service logo")
}
// .disabled(!self.userModel.subscriptionActive)
// Text("")
// Text("Drag-and-drop each of the icons bellow, to test it on your diagrams.\n\nTap to load a diferent random icon, purchange a subscription to enable on all logos.")
if let service = randomAWSservice {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 110))], content: {
if (awsServiceLogoWithLabel){
AWSserviceImagePlaceHolderView(service: service, showLabel: false)
AWSserviceImagePlaceHolderView(service: service, showLabel: true)
.padding(.horizontal, 8)
.padding(.vertical, 6)
.background(Color(red:1.0, green: 0.5, blue: 0.0))
.cornerRadius(8.0)
}else{
AWSserviceImagePlaceHolderView(service: service, showLabel: false)
.padding(.horizontal, 8)
.padding(.vertical, 6)
.background(Color(red:1.0, green: 0.5, blue: 0.0))
.cornerRadius(8.0)
AWSserviceImagePlaceHolderView(service: service, showLabel: true)
}
}
).frame(minHeight: 160)
}
}
}
Section(header: Text("AWSary Premium")){
Label("AWSary Merch Store", systemImage: "storefront").onTapGesture {
guard let url = URL(string: "https://bit.ly/awsary-merch") else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
if self.userModel.subscriptionActive{
Label("Discount Code: GSC293ZEQD", systemImage: "doc.on.doc").onTapGesture {
UIPasteboard.general.string = "GSC293ZEQD"
}
NavigationLink(destination:PaywallView(displayCloseButton: true)){
Label("Manage Subscription", systemImage: "heart.fill")
}
} else {
NavigationLink(destination:PaywallView(displayCloseButton: true)){
Label("Purchase Subscription", systemImage: "creditcard")
}
Label("Restore Purchase", systemImage: "arrow.triangle.2.circlepath").onTapGesture {
Purchases.shared.restorePurchases { customerInfo, error in
//... check customerInfo to see if entitlement is now active
}
}
}
}
Section(header: Text("Feedback")){
// Label("Send Feedback", systemImage: "envelope")
Label {
VStack(alignment: .leading){
Text("Rate version \(Bundle.main.appVersionLong) of AWSary")
if let stats = appStats {
Text("Join the \(stats.ratings.ratingCount) wonderful people who have already rated this version!").font(.footnote).opacity(0.6)
} else if isLoadingStats {
Text("Loading rating info...").font(.footnote).opacity(0.6)
} else {
Text("Be one of the first to rate this version!").font(.footnote).opacity(0.6)
}
}
} icon:{
Image(systemName: "star.fill") // .frame(width: 40, height: 40)
// .foregroundColor(Color.white)
// .background(Color.orange)
// .cornerRadius(7)
}.onTapGesture {
let appId = "1634871091"
let url_string = "itms-apps://itunes.apple.com/app/id\(appId)?mt=8&action=write-review"
guard let url = URL(string: url_string) else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Label {
VStack(alignment: .leading){
Text("Send Feedback")
Text("Feedback emails are lovely to read!").font(.footnote).opacity(0.6)
}
} icon:{
Image(systemName: "envelope")
}.onTapGesture {
let address = "mail@tig.pt"
let subject = "Feedback on AWSary"
// Example email body with useful info for bug reports
let body = "\n\n--\nAWSary Version: \(Bundle.main.appVersionLong) (\(Bundle.main.appBuild))\n\nScreen: About"
// Build the URL from its components
var components = URLComponents()
components.scheme = "mailto"
components.path = address
components.queryItems = [
URLQueryItem(name: "subject", value: subject),
URLQueryItem(name: "body", value: body)
]
guard let email_url = components.url else {
NSLog("Failed to create mailto URL")
return
}
UIApplication.shared.open(email_url) { success in
// handle success or failure
}
}
}
Section(header: Text("Why AWSary")){
Text("I'm an AWS Cloud Consultant and Trainer.\n\nNew AWS Services are released all the time, and sometimes you just want a quick dictionary definition.\n\nI also draw AWS Cloud Architecture diagrams daily on iPad, to explore ideas either with Colleagues, Clients or Students.\n\nGood drawing Applications don't have AWS services logos, so on top of this dictionary I enabled the drag and drop of the logos to 3rd party drawing tools.\n\n This App is a great AWS Cloud Consultant companion tool.\n\nHelp develop this app at [GitHub](https://github.com/tigpt/AWSary/).")
}
Section(header: Text("How to use AWSary")){
Text("Search for the name of an AWS service, you can open and look for the definition of it. You can also drag and drop the service logo to your favorite drawing application. (Check video below)")
MyYoutubePlayer(youtube_id: "c0SjbhRR3lk")
}
Section(header: Text("More iOS Apps")){
if let developerAppsURL {
Link(destination: developerAppsURL) {
Label {
VStack(alignment: .leading){
Text("Discover my other apps")
Text("Explore all iOS apps by Tiago Rodrigues on the App Store.").font(.footnote).opacity(0.6)
}
} icon:{
Image(systemName: "apps.iphone")
}
}
}
}
Section(){
Link("Terms of Use (EULA)", destination: URL(string: "https://www.apple.com/legal/internet-services/itunes/dev/stdeula")!)
Link("Privacy Policy", destination: URL(string: "https://tig.pt/awsary-privacy")!)
}
// Section(header: Text("AWSary.com")){
// Text("This is a hobby project from Tiago Rodrigues to help more people learn about Cloud, specialy AWS. Special tanks to tecRacer for supporting the backend.").lineLimit(100)
// }
// Section(header: Text("Help developing this app")){
// Text("Get involved in this application")
// }
// Section(header: Text("Contact & Help")){
// Text("FAQ - Frequently Asked Questions")
// }
// Section(header: Text("info")){
// Text("Acknowledgements")
// Text("Colophon")
// Text("Privacy Policy")
// }
// Section(header: Text("Icon")){
// Text("Icon 1")
// Text("Icon 2")
// Text("Icon 3")
// }
}
.navigationTitle("Settings")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction){
Button("Done", action: {
dismiss()
})
}
}
.onAppear {
// Initialize the random service only once when the view appears
if randomAWSservice == nil {
randomAWSservice = awsServices.getRandomElement()
}
// Load app statistics (from cache first, then API if needed)
loadAppStats()
}
}.accentColor(Color(red:1.0, green: 0.5, blue: 0.0))
}
// var body: some View {
// NavigationStack{
//// VStack{
//// HStack{
//// Spacer()
//// Button(action: {
//// dismiss()
//// }){
//// Text("Done").font(.title3 .bold()).padding(10)
//// }
//// }.background(Color .yellow)
//// ScrollView{
//// Text("AWSary.com").font(.largeTitle)
//// Text("Multiline \ntext \nis called \nTextEditor")
//// }.background(Color .red)
//// .font(.title)
//// Spacer()
//// }
// Text("hello world")
// }.background(Color .pink)
// .navigationTitle("Settings")
// .toolbar {
// Button("Done", action: {
// dismiss()
// })
// }
// }
}
#Preview {
AboutView()
}
extension Bundle {
public var appName: String { getInfo("CFBundleName") }
public var displayName: String { getInfo("CFBundleDisplayName") }
public var language: String { getInfo("CFBundleDevelopmentRegion") }
public var identifier: String { getInfo("CFBundleIdentifier") }
public var copyright: String { getInfo("NSHumanReadableCopyright").replacingOccurrences(of: "\\\\n", with: "\n") }
public var appBuild: String { getInfo("CFBundleVersion") }
public var appVersionLong: String { getInfo("CFBundleShortVersionString") }
//public var appVersionShort: String { getInfo("CFBundleShortVersion") }
fileprivate func getInfo(_ str: String) -> String { infoDictionary?[str] as? String ?? "⚠️" }
}