Skip to content
Open
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
4 changes: 2 additions & 2 deletions ios/AWSary/resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<dict>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string></string>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string>Calendar access is only used to add the classroom schedules you request.</string>
</dict>
</plist>
116 changes: 73 additions & 43 deletions ios/Shared/DataModels/AwsServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

var fm = FileManager.default
var subUrl: URL?
var mainUrl: URL? = Bundle.main.url(forResource: "aws_services", withExtension: "json")
var lastRandom: awsService = awsService(id: 10, name: "DeepRacer", longName: "AWS DeepRacer", shortDesctiption: "DeepRacer", imageURL: "https://static.tig.pt/awsary/logos/Arch_AWS-DeepRacer_64.svg", youtube_id: "")

Expand All @@ -23,47 +22,78 @@ class AwsServices: ObservableObject {
return lastRandom
}

func getRandomElement() -> awsService{
lastRandom = services.randomElement()!
return lastRandom
func getRandomElement() -> awsService {
if services.isEmpty {
refresh()
}

guard let randomService = services.randomElement() else {
return lastRandom
}

lastRandom = randomService
return randomService
}

init() {
refresh()
}

private func servicesFromDocuments() -> [awsService] {
do {
let documentDirectory = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let documentURL = documentDirectory.appendingPathComponent("aws_services.json")

guard fm.fileExists(atPath: documentURL.path) else {
return []
}

return decodeData(pathName: documentURL)
} catch {
print("Failed to resolve documents directory for aws_services.json: \(error)")
return []
}
}

private func servicesFromBundle() -> [awsService] {
guard let bundleURL = mainUrl else {
print("Missing bundled aws_services.json")
return []
}

return decodeData(pathName: bundleURL)
}

private func decodeData(pathName: URL) -> [awsService] {
do {
let jsonData = try Data(contentsOf: pathName)
let decoder = JSONDecoder()
let decoded = try decoder.decode([awsService].self, from: jsonData)

if decoded.isEmpty {
print("Decoded zero services from \(pathName.lastPathComponent)")
}

return decoded
} catch {
print("Failed to decode aws_services.json from \(pathName): \(error)")
return []
}
}

func refresh() {
var loadedServices = servicesFromDocuments()

if loadedServices.isEmpty {
loadedServices = servicesFromBundle()
}

if loadedServices.isEmpty {
print("Falling back to last known service to prevent empty catalogue")
services = [lastRandom]
} else {
loadedServices.sort { $0.name < $1.name }
services = loadedServices
}
}
init() {
refresh()
}

func getData() {
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!)
} catch {
print(error)
}
}

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

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

}else{
decodeData(pathName: mainPath)
}
}

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

func refresh(){
getData()
services.sort {$0.name < $1.name}
}
}