From 38950f7bc8b10ccf86995ebacc83f86ca1a6958e Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 3 Sep 2022 10:38:56 -0400 Subject: [PATCH] Address some OkHttp nullability changes --- .../eu/kanade/tachiyomi/data/cache/ChapterCache.kt | 4 ++-- .../eu/kanade/tachiyomi/data/download/Downloader.kt | 4 ++-- .../kanade/tachiyomi/data/track/bangumi/BangumiApi.kt | 10 +++------- .../tachiyomi/data/track/bangumi/BangumiInterceptor.kt | 2 +- .../tachiyomi/data/track/kitsu/KitsuInterceptor.kt | 2 +- .../data/track/shikimori/ShikimoriInterceptor.kt | 2 +- .../kanade/tachiyomi/data/updater/AppUpdateService.kt | 2 +- .../eu/kanade/tachiyomi/network/OkHttpExtensions.kt | 6 +++--- .../java/eu/kanade/tachiyomi/util/JsoupExtensions.kt | 2 +- 9 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index bab08dd7e..361f710b8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -164,12 +164,12 @@ class ChapterCache(private val context: Context) { editor = diskCache.edit(key) ?: throw IOException("Unable to edit key") // Get OutputStream and write image with Okio. - response.body!!.source().saveTo(editor.newOutputStream(0)) + response.body.source().saveTo(editor.newOutputStream(0)) diskCache.flush() editor.commit() } finally { - response.body?.close() + response.body.close() editor?.abortUnlessCommitted() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 3fc0ffb76..856418c75 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -425,7 +425,7 @@ class Downloader( .map { response -> val file = tmpDir.createFile("$filename.tmp") try { - response.body!!.source().saveTo(file.openOutputStream()) + response.body.source().saveTo(file.openOutputStream()) val extension = getImageExtension(response, file) file.renameTo("$filename.$extension") } catch (e: Exception) { @@ -470,7 +470,7 @@ class Downloader( */ private fun getImageExtension(response: Response, file: UniFile): String { // Read content type if available. - val mime = response.body?.contentType()?.run { if (type == "image") "image/$subtype" else null } + val mime = response.body.contentType()?.run { if (type == "image") "image/$subtype" else null } // Else guess from the uri. ?: context.contentResolver.getType(file.uri) // Else read magic numbers. diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt index c2a6d9eb8..ddf3495bd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt @@ -80,7 +80,7 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept authClient.newCall(GET(url.toString())) .await() .use { - var responseBody = it.body?.string().orEmpty() + var responseBody = it.body.string() if (responseBody.isEmpty()) { throw Exception("Null Response") } @@ -135,8 +135,8 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept .build() // TODO: get user readed chapter here - var response = authClient.newCall(requestUserRead).await() - var responseBody = response.body?.string().orEmpty() + val response = authClient.newCall(requestUserRead).await() + val responseBody = response.body.string() if (responseBody.isEmpty()) { throw Exception("Null Response") } @@ -183,10 +183,6 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept private const val redirectUrl = "tachiyomi://bangumi-auth" private const val baseMangaUrl = "$apiUrl/mangas" - fun mangaUrl(remoteId: Int): String { - return "$baseMangaUrl/$remoteId" - } - fun authUrl(): Uri = loginUrl.toUri().buildUpon() .appendQueryParameter("client_id", clientId) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt index 391b5e9e1..852198198 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt @@ -24,7 +24,7 @@ class BangumiInterceptor(val bangumi: Bangumi) : Interceptor { if (currAuth.isExpired()) { val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refresh_token!!)) if (response.isSuccessful) { - newAuth(json.decodeFromString(response.body!!.string())) + newAuth(json.decodeFromString(response.body.string())) } else { response.close() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuInterceptor.kt index 5f698731b..395d5daa9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuInterceptor.kt @@ -26,7 +26,7 @@ class KitsuInterceptor(val kitsu: Kitsu) : Interceptor { if (currAuth.isExpired()) { val response = chain.proceed(KitsuApi.refreshTokenRequest(refreshToken)) if (response.isSuccessful) { - newAuth(json.decodeFromString(response.body!!.string())) + newAuth(json.decodeFromString(response.body.string())) } else { response.close() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt index ba9074af8..51934d049 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt @@ -26,7 +26,7 @@ class ShikimoriInterceptor(val shikimori: Shikimori) : Interceptor { if (currAuth.isExpired()) { val response = chain.proceed(ShikimoriApi.refreshTokenRequest(refreshToken)) if (response.isSuccessful) { - newAuth(json.decodeFromString(response.body!!.string())) + newAuth(json.decodeFromString(response.body.string())) } else { response.close() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt index 0620676e9..dc4fff33c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt @@ -127,7 +127,7 @@ class AppUpdateService : Service() { val apkFile = File(externalCacheDir, "update.apk") if (response.isSuccessful) { - response.body!!.source().saveTo(apkFile) + response.body.source().saveTo(apkFile) } else { response.close() throw Exception("Unsuccessful response") 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 0d7e45e3b..0b21f28ed 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -70,7 +70,7 @@ suspend fun Call.await(): Response { } continuation.resume(response) { - response.body?.closeQuietly() + response.body.closeQuietly() } } @@ -107,7 +107,7 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene .addNetworkInterceptor { chain -> val originalResponse = chain.proceed(chain.request()) originalResponse.newBuilder() - .body(ProgressResponseBody(originalResponse.body!!, listener)) + .body(ProgressResponseBody(originalResponse.body, listener)) .build() } .build() @@ -119,7 +119,7 @@ inline fun Response.parseAs(): T { // Avoiding Injekt.get() due to compiler issues val json = Injekt.getInstance(fullType().type) this.use { - val responseBody = it.body?.string().orEmpty() + val responseBody = it.body.string() return json.decodeFromString(responseBody) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/JsoupExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/JsoupExtensions.kt index 2dc39e109..6c166448a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/JsoupExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/JsoupExtensions.kt @@ -22,5 +22,5 @@ fun Element.attrOrText(css: String): String { * @param html the body of the response. Use only if the body was read before calling this method. */ fun Response.asJsoup(html: String? = null): Document { - return Jsoup.parse(html ?: body!!.string(), request.url.toString()) + return Jsoup.parse(html ?: body.string(), request.url.toString()) }