Remove usage of Retrofit for update check and extensions list

This commit is contained in:
arkon 2020-12-26 16:59:24 -05:00
parent 62ab70f889
commit 61a594493c
5 changed files with 43 additions and 76 deletions

View File

@ -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<NetworkHelper>().client)
.build()
return restAdapter.create(GithubService::class.java)
}
}
@GET("/repos/{repo}/releases/latest")
suspend fun getLatestVersion(@Path("repo", encoded = true) repo: String): GithubRelease
}

View File

@ -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,15 +23,20 @@ class GithubUpdateChecker {
}
suspend fun checkForUpdate(): UpdateResult {
val release = service.getLatestVersion(repo)
return withContext(Dispatchers.IO) {
networkService.client
.newCall(GET("https://api.github.com/repos/$repo/releases/latest"))
.await()
.withResponse<GithubRelease, UpdateResult> {
// Check if latest version is different from current version
return if (isNewVersion(release.version)) {
GithubUpdateResult.NewUpdate(release)
if (isNewVersion(it.version)) {
GithubUpdateResult.NewUpdate(it)
} else {
GithubUpdateResult.NoNewUpdate()
}
}
}
}
private fun isNewVersion(versionTag: String): Boolean {
// Removes prefixes like "r" or "v"

View File

@ -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<Extension.Available> {
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<JsonArray, List<Extension.Available>> {
parseResponse(it)
}
}
}

View File

@ -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
}

View File

@ -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 <reified T, R> Response.withResponse(block: (T) -> R): R {
this.use {
val responseBody = it.body?.string().orEmpty()
val response = Injekt.get<Json>().decodeFromString<T>(responseBody)
return block(response)
}
}