mirror of
https://github.com/revanced/revanced-manager-compose-old.git
synced 2025-04-30 06:24:28 +02:00
fix: don't block ui thread when fetching
This commit is contained in:
parent
dd81b79fac
commit
a1052bff3e
@ -8,6 +8,7 @@ import com.vk.knet.core.Knet
|
|||||||
import com.vk.knet.core.http.HttpMethod
|
import com.vk.knet.core.http.HttpMethod
|
||||||
import com.vk.knet.core.http.HttpRequest
|
import com.vk.knet.core.http.HttpRequest
|
||||||
import com.vk.knet.cornet.CronetKnetEngine
|
import com.vk.knet.cornet.CronetKnetEngine
|
||||||
|
import kotlinx.coroutines.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class API(private val repository: GitHubRepository, private val prefs: PreferencesManager, cronet: CronetKnetEngine) {
|
class API(private val repository: GitHubRepository, private val prefs: PreferencesManager, cronet: CronetKnetEngine) {
|
||||||
@ -25,8 +26,13 @@ class API(private val repository: GitHubRepository, private val prefs: Preferenc
|
|||||||
|
|
||||||
suspend fun downloadPatchBundle(workdir: File): File {
|
suspend fun downloadPatchBundle(workdir: File): File {
|
||||||
return try {
|
return try {
|
||||||
val (_, out) = downloadAsset(workdir, findAsset(prefs.srcPatches.toString(), ".jar"))
|
withContext(Dispatchers.IO) {
|
||||||
|
val (_, out) = downloadAsset(
|
||||||
|
workdir,
|
||||||
|
findAsset(prefs.srcPatches.toString(), ".jar")
|
||||||
|
)
|
||||||
out
|
out
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw Exception("Failed to download patch bundle", e)
|
throw Exception("Failed to download patch bundle", e)
|
||||||
}
|
}
|
||||||
@ -56,9 +62,13 @@ class API(private val repository: GitHubRepository, private val prefs: Preferenc
|
|||||||
return assets to out
|
return assets to out
|
||||||
}
|
}
|
||||||
Log.d("ReVanced Manager", "Downloading asset ${assets.asset.name}")
|
Log.d("ReVanced Manager", "Downloading asset ${assets.asset.name}")
|
||||||
val file = client.execute(HttpRequest(HttpMethod.GET, assets.asset.downloadUrl)).body?.asBytes()
|
val file = client.execute(
|
||||||
file?.let { out.writeBytes(it) }
|
HttpRequest(
|
||||||
|
HttpMethod.GET,
|
||||||
|
assets.asset.downloadUrl
|
||||||
|
)
|
||||||
|
).body!!.asBytes()
|
||||||
|
out.writeBytes(file)
|
||||||
|
|
||||||
return assets to out
|
return assets to out
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,12 @@ class GitHubRepository(cronet: CronetKnetEngine) {
|
|||||||
|
|
||||||
val client = Knet.Build(cronet)
|
val client = Knet.Build(cronet)
|
||||||
|
|
||||||
suspend fun fetchAssets() = withContext(Dispatchers.IO) {
|
suspend fun fetchAssets() = withContext(Dispatchers.Default) {
|
||||||
val stream = client.execute(HttpRequest(HttpMethod.GET, "$apiUrl/tools")).body!!.asString()
|
val stream = client.execute(HttpRequest(HttpMethod.GET, "$apiUrl/tools")).body!!.asString()
|
||||||
Json.decodeFromString(stream) as Tools
|
Json.decodeFromString(stream) as Tools
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun fetchContributors() = withContext(Dispatchers.IO) {
|
suspend fun fetchContributors() = withContext(Dispatchers.Default) {
|
||||||
val stream = client.execute(HttpRequest(HttpMethod.GET,"$apiUrl/contributors")).body!!.asString()
|
val stream = client.execute(HttpRequest(HttpMethod.GET,"$apiUrl/contributors")).body!!.asString()
|
||||||
Json.decodeFromString(stream) as Repositories
|
Json.decodeFromString(stream) as Repositories
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,9 @@ import app.revanced.manager.repository.GitHubRepository
|
|||||||
import app.revanced.manager.util.ghManager
|
import app.revanced.manager.util.ghManager
|
||||||
import app.revanced.manager.util.ghPatcher
|
import app.revanced.manager.util.ghPatcher
|
||||||
import app.revanced.manager.util.tag
|
import app.revanced.manager.util.tag
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -33,7 +35,7 @@ class DashboardViewModel(private val repository: GitHubRepository) : ViewModel()
|
|||||||
private fun fetchLastCommit() {
|
private fun fetchLastCommit() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
try {
|
try {
|
||||||
val repo = repository.fetchAssets()
|
val repo = withContext(Dispatchers.Default) {repository.fetchAssets()}
|
||||||
for (asset in repo.tools) {
|
for (asset in repo.tools) {
|
||||||
when (asset.repository) {
|
when (asset.repository) {
|
||||||
ghPatcher -> {
|
ghPatcher -> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user