fix: webview loading crash

This commit is contained in:
rebelonion 2024-03-18 17:27:32 -05:00
parent 39d6f0fbd6
commit 6a8e422a30
4 changed files with 17 additions and 11 deletions

View file

@ -1,18 +1,24 @@
package eu.kanade.tachiyomi.network
import android.webkit.CookieManager
import ani.dantotsu.snackString
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
class AndroidCookieJar : CookieJar {
val manager: CookieManager = CookieManager.getInstance()
val manager: CookieManager? = try {
CookieManager.getInstance()
} catch (e: Exception) {
snackString("Webview is outdated, please update your webview")
null
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
val urlString = url.toString()
cookies.forEach { manager.setCookie(urlString, it.toString()) }
cookies.forEach { manager?.setCookie(urlString, it.toString()) }
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
@ -20,9 +26,9 @@ class AndroidCookieJar : CookieJar {
}
fun get(url: HttpUrl): List<Cookie> {
val cookies = manager.getCookie(url.toString())
val cookies = manager?.getCookie(url.toString())
return if (cookies != null && cookies.isNotEmpty()) {
return if (!cookies.isNullOrEmpty()) {
cookies.split(";").mapNotNull { Cookie.parse(url, it) }
} else {
emptyList()
@ -31,7 +37,7 @@ class AndroidCookieJar : CookieJar {
fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1): Int {
val urlString = url.toString()
val cookies = manager.getCookie(urlString) ?: return 0
val cookies = manager?.getCookie(urlString) ?: return 0
fun List<String>.filterNames(): List<String> {
return if (cookieNames != null) {
@ -49,6 +55,6 @@ class AndroidCookieJar : CookieJar {
}
fun removeAll() {
manager.removeAllCookies {}
manager?.removeAllCookies {}
}
}