Skip to content

Commit

Permalink
Improve update check
Browse files Browse the repository at this point in the history
  • Loading branch information
chippokiddo authored Dec 18, 2024
1 parent 0898540 commit 91866c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 18 additions & 10 deletions FairyLights/Utils/CheckForUpdates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 24,27 @@ func fetchLatestRelease() async throws -> (String, URL) {
let url = URL(string: "https://api.github.com/repos/chippokiddo/FairyLights/releases/latest")!
var request = URLRequest(url: url)
request.addValue("application/vnd.github.v3 json", forHTTPHeaderField: "Accept")
request.timeoutInterval = 15

// Perform the network request
let (data, _) = try await URLSession.shared.data(for: request)
let (data, response) = try await URLSession.shared.data(for: request)

// Decode the JSON response
let release = try JSONDecoder().decode(GitHubRelease.self, from: data)

// Check if there are assets available
guard let downloadURL = release.assets.first?.browserDownloadURL else {
throw NSError(domain: "GitHubReleaseError", code: 0, userInfo: [
NSLocalizedDescriptionKey: "No assets available in the latest release on GitHub."
])
// Validate HTTP response
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
throw GitHubReleaseError.invalidResponse
}

return (release.tagName, downloadURL)
do {
// Decode the JSON response
let release = try JSONDecoder().decode(GitHubRelease.self, from: data)

// Check if there are assets available
guard let downloadURL = release.assets.first?.browserDownloadURL else {
throw GitHubReleaseError.noAssetsAvailable
}

return (release.tagName, downloadURL)
} catch {
throw GitHubReleaseError.decodingError(error.localizedDescription)
}
}
6 changes: 6 additions & 0 deletions FairyLights/Utils/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 13,9 @@ enum AlertType: Identifiable {
}
}
}

enum GitHubReleaseError: Error {
case noAssetsAvailable
case invalidResponse
case decodingError(String)
}

0 comments on commit 91866c2

Please sign in to comment.