Skip to content

Commit

Permalink
Update DriversFetcher.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan09811 authored Feb 19, 2025
1 parent 5c6d06a commit f458119
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions app/src/main/java/emu/skyline/utils/DriversFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ object DriversFetcher {
@Serializable
data class Asset(val browser_download_url: String)

suspend fun fetchReleases(repoUrl: String): List<Pair<String, String?>> {
suspend fun fetchReleases(repoUrl: String, bypassValidation: Boolean = false): FetchResultOutput {
val repoPath = repoUrl.removePrefix("https://github.com/")
val validationUrl = "https://api.github.com/repos/$repoPath/contents/.adrenoDrivers"
val apiUrl = "https://api.github.com/repos/$repoPath/releases"

return try {
val response: HttpResponse = withContext(Dispatchers.IO) {
httpClient.get(apiUrl)
}

if (response.status.value != 200)
return FetchResultOutput(emptyList(), FetchResult.Error("Failed to fetch drivers"))

val isValid = withContext(Dispatchers.IO) {
try {
httpClient.get(validationUrl).status.value == 200
Expand All @@ -53,36 +60,34 @@ object DriversFetcher {
}
}

if (!isValid) {
Log.d("DriversFetcher", "Provided driver repo url is not valid.")
return emptyList()
if (!isValid && !bypassValidation) {
return FetchResultOutput(emptyList(), FetchResult.Warning("Provided driver repo url is not valid."))
}

val releases: List<GitHubRelease> = withContext(Dispatchers.IO) {
httpClient.get(apiUrl).body()
}
val releases: List<GitHubRelease> = response.body()
releases.map { release ->
val assetUrl = release.assets.firstOrNull()?.browser_download_url
release.name to assetUrl
}
FetchResultOutput(drivers, FetchResult.Success)
} catch (e: Exception) {
Log.e("DriversFetcher", "Error fetching releases: ${e.message}", e)
emptyList()
FetchResultOutput(emptyList(), FetchResult.Error("Error fetching releases: ${e.message}"))
}
}

suspend fun downloadAsset(
context: Context,
assetUrl: String,
destinationUri: Uri,
destinationFile: File,
progressCallback: (Long, Long) -> Unit
): DownloadResult {
return try {
withContext(Dispatchers.IO) {
val response: HttpResponse = httpClient.get(assetUrl)
val contentLength = response.headers[HttpHeaders.ContentLength]?.toLong() ?: -1L

context.contentResolver.openOutputStream(destinationUri)?.use { outputStream ->
FileOutputStream(destinationFile)?.use { outputStream ->
writeResponseToStream(response, outputStream, contentLength, progressCallback)
} ?: return@withContext DownloadResult.Error("Failed to open ${destinationUri}")
}
Expand Down Expand Up @@ -118,4 +123,15 @@ object DriversFetcher {
object Success : DownloadResult()
data class Error(val message: String?) : DownloadResult()
}

data class FetchResultOutput(
val fetchedDrivers: List<Pair<String, String?>>,
val result: FetchResult
)

sealed class FetchResult {
object Success : FetchResult()
data class Error(val message: String?) : FetchResult()
data class Warning(val message: String?) : FetchResult()
}
}

0 comments on commit f458119

Please sign in to comment.