Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions ios/Shared/DataModels/AwsServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import SwiftUI

var fm = FileManager.default
var subUrl: URL?
Expand All @@ -14,6 +15,10 @@ var lastRandom: awsService = awsService(id: 10, name: "DeepRacer", longName: "AW

class AwsServices: ObservableObject {
@Published var services = [awsService]()
@Published var isLoading = true

// Static cache to avoid reloading data multiple times
private static var cachedServices: [awsService]?

func getNameOfLastRandom() -> String {
return lastRandom.longName
Expand All @@ -28,42 +33,64 @@ class AwsServices: ObservableObject {
return lastRandom
}
init() {
refresh()
// Check if we have cached data first
if let cached = AwsServices.cachedServices {
self.services = cached
self.isLoading = false
} else {
// Load asynchronously
Task {
await refresh()
}
}
}

func getData() {
func getData() async {
do {
let documentDirectory = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
subUrl = documentDirectory.appendingPathComponent("aws_services.json")
loadFile(mainPath: mainUrl!, subPath: subUrl!)
await loadFile(mainPath: mainUrl!, subPath: subUrl!)
} catch {
print(error)
}
}

func loadFile(mainPath: URL, subPath: URL){
func loadFile(mainPath: URL, subPath: URL) async {
if fm.fileExists(atPath: subPath.path){
decodeData(pathName: subPath)
await decodeData(pathName: subPath)

if services.isEmpty{
decodeData(pathName: mainPath)
await decodeData(pathName: mainPath)
}

}else{
decodeData(pathName: mainPath)
await decodeData(pathName: mainPath)
}
}

func decodeData(pathName: URL){
func decodeData(pathName: URL) async {
do{
let jsonData = try Data(contentsOf: pathName)
let decoder = JSONDecoder()
services = try decoder.decode([awsService].self, from: jsonData)
} catch {}
let decodedServices = try decoder.decode([awsService].self, from: jsonData)

await MainActor.run {
self.services = decodedServices
// Cache the services for future use
AwsServices.cachedServices = decodedServices
self.isLoading = false
}
} catch {
await MainActor.run {
self.isLoading = false
}
}
}

func refresh(){
getData()
services.sort {$0.name < $1.name}
func refresh() async {
await getData()
await MainActor.run {
self.services.sort {$0.name < $1.name}
}
}
}
50 changes: 28 additions & 22 deletions ios/Shared/MainViews/Glossary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ struct Glossary: View {
@State private var searchQuery = ""
@Environment(\.colorScheme) var colorScheme
@Environment(\.modelContext) private var modelContext
@Query var settings: [SystemSetting]
@Query(filter: #Predicate<SystemSetting> { $0.key == "awsServiceLogoWithLabel" }) var logoSettings: [SystemSetting]

// Computed property for awsServiceLogoWithLabel setting
// Cached property for awsServiceLogoWithLabel setting to reduce database queries
private var awsServiceLogoWithLabel: Bool {
return settings.first(where: { $0.key == "awsServiceLogoWithLabel" })?.boolValue ?? true
return logoSettings.first?.boolValue ?? true
}

var filteredAwsServices: [awsService] {
Expand All @@ -32,27 +32,33 @@ struct Glossary: View {
var body: some View {
NavigationStack{
ScrollView{
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 100))], content: {
ForEach(filteredAwsServices, id: \.self){ service in
NavigationLink(destination: DetailsView(service: service)){
VStack(alignment: .center, spacing: 4, content: {
AWSserviceImagePlaceHolderView(service: service, showLabel: awsServiceLogoWithLabel)
.frame(minHeight: 140)
if (!awsServiceLogoWithLabel){
Text(service.name)
.font(.subheadline)
.lineLimit(3)
}
Spacer()
})
}
}
}).padding(.horizontal, 12)
.accentColor(Color(colorScheme == .dark ? .white : .black))
if awsServices.isLoading {
ProgressView("Loading AWS Services...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding()
} else {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 100))], content: {
ForEach(filteredAwsServices, id: \.self){ service in
NavigationLink(destination: DetailsView(service: service)){
VStack(alignment: .center, spacing: 4, content: {
AWSserviceImagePlaceHolderView(service: service, showLabel: awsServiceLogoWithLabel)
.frame(minHeight: 140)
if (!awsServiceLogoWithLabel){
Text(service.name)
.font(.subheadline)
.lineLimit(3)
}
Spacer()
})
}
}
}).padding(.horizontal, 12)
.accentColor(Color(colorScheme == .dark ? .white : .black))
}
}
.refreshable {
AwsServices().refresh()
await awsServices.refresh()
}
.searchable(text: $searchQuery, placement: .navigationBarDrawer(displayMode: .always), prompt: "Search for an AWS Service")
.disableAutocorrection(true) // .autocorrectionDisabled() //only available on iOS 16
Expand Down