diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt deleted file mode 100644 index 3b702877df..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt +++ /dev/null @@ -1,32 +0,0 @@ -package eu.kanade.tachiyomi.data.updater.github - -import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory -import eu.kanade.tachiyomi.network.NetworkHelper -import kotlinx.serialization.json.Json -import okhttp3.MediaType.Companion.toMediaType -import retrofit2.Retrofit -import retrofit2.http.GET -import retrofit2.http.Path -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get - -/** - * Used to connect with the GitHub API to get the latest release version from a repo. - */ -interface GithubService { - - companion object { - fun create(): GithubService { - val restAdapter = Retrofit.Builder() - .baseUrl("https://api.github.com") - .addConverterFactory(Json { ignoreUnknownKeys = true }.asConverterFactory("application/json".toMediaType())) - .client(Injekt.get().client) - .build() - - return restAdapter.create(GithubService::class.java) - } - } - - @GET("/repos/{repo}/releases/latest") - suspend fun getLatestVersion(@Path("repo", encoded = true) repo: String): GithubRelease -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt index b158cac3ba..704c9ab7ce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt @@ -2,10 +2,17 @@ package eu.kanade.tachiyomi.data.updater.github import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.data.updater.UpdateResult +import eu.kanade.tachiyomi.network.GET +import eu.kanade.tachiyomi.network.NetworkHelper +import eu.kanade.tachiyomi.network.await +import eu.kanade.tachiyomi.network.withResponse +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import uy.kohesive.injekt.injectLazy class GithubUpdateChecker { - private val service: GithubService = GithubService.create() + private val networkService: NetworkHelper by injectLazy() private val repo: String by lazy { if (BuildConfig.DEBUG) { @@ -16,13 +23,18 @@ class GithubUpdateChecker { } suspend fun checkForUpdate(): UpdateResult { - val release = service.getLatestVersion(repo) - - // Check if latest version is different from current version - return if (isNewVersion(release.version)) { - GithubUpdateResult.NewUpdate(release) - } else { - GithubUpdateResult.NoNewUpdate() + return withContext(Dispatchers.IO) { + networkService.client + .newCall(GET("https://api.github.com/repos/$repo/releases/latest")) + .await() + .withResponse { + // Check if latest version is different from current version + if (isNewVersion(it.version)) { + GithubUpdateResult.NewUpdate(it) + } else { + GithubUpdateResult.NoNewUpdate() + } + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt index 442cfa4b4a..02a2295a29 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt @@ -5,6 +5,10 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.LoadResult import eu.kanade.tachiyomi.extension.util.ExtensionLoader +import eu.kanade.tachiyomi.network.GET +import eu.kanade.tachiyomi.network.NetworkHelper +import eu.kanade.tachiyomi.network.await +import eu.kanade.tachiyomi.network.withResponse import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.json.JsonArray @@ -16,14 +20,17 @@ import java.util.Date internal class ExtensionGithubApi { + private val networkService: NetworkHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy() suspend fun findExtensions(): List { - val service: ExtensionGithubService = ExtensionGithubService.create() - return withContext(Dispatchers.IO) { - val response = service.getRepo() - parseResponse(response) + networkService.client + .newCall(GET("${REPO_URL_PREFIX}index.min.json")) + .await() + .withResponse> { + parseResponse(it) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt deleted file mode 100644 index ecc5940aed..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt +++ /dev/null @@ -1,32 +0,0 @@ -package eu.kanade.tachiyomi.extension.api - -import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory -import eu.kanade.tachiyomi.network.NetworkHelper -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonArray -import okhttp3.MediaType.Companion.toMediaType -import retrofit2.Retrofit -import retrofit2.http.GET -import uy.kohesive.injekt.injectLazy - -/** - * Used to get the extension repo listing from GitHub. - */ -interface ExtensionGithubService { - - companion object { - fun create(): ExtensionGithubService { - val network: NetworkHelper by injectLazy() - val adapter = Retrofit.Builder() - .baseUrl(ExtensionGithubApi.BASE_URL) - .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) - .client(network.client) - .build() - - return adapter.create(ExtensionGithubService::class.java) - } - } - - @GET("${ExtensionGithubApi.REPO_URL_PREFIX}index.min.json") - suspend fun getRepo(): JsonArray -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt index 2e059a9812..484ccdac52 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -1,6 +1,8 @@ package eu.kanade.tachiyomi.network import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json import okhttp3.Call import okhttp3.Callback import okhttp3.OkHttpClient @@ -9,6 +11,8 @@ import okhttp3.Response import rx.Observable import rx.Producer import rx.Subscription +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.io.IOException import java.util.concurrent.atomic.AtomicBoolean import kotlin.coroutines.resume @@ -105,3 +109,11 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene return progressClient.newCall(request) } + +inline fun Response.withResponse(block: (T) -> R): R { + this.use { + val responseBody = it.body?.string().orEmpty() + val response = Injekt.get().decodeFromString(responseBody) + return block(response) + } +}