Эх сурвалжийг харах

Remove usage of Retrofit for update check and extensions list

arkon 4 жил өмнө
parent
commit
61a594493c

+ 0 - 32
app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt

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

+ 20 - 8
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<GithubRelease, UpdateResult> {
+                    // Check if latest version is different from current version
+                    if (isNewVersion(it.version)) {
+                        GithubUpdateResult.NewUpdate(it)
+                    } else {
+                        GithubUpdateResult.NoNewUpdate()
+                    }
+                }
         }
     }
 

+ 11 - 4
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<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)
+                }
         }
     }
 

+ 0 - 32
app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt

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

+ 12 - 0
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 <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)
+    }
+}