|
|
@ -8,42 +8,43 @@ import com.franmontiel.persistentcookiejar.persistence.SerializableCookie |
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.data.entities.Cookie |
|
|
|
import io.legado.app.data.entities.Cookie |
|
|
|
import io.legado.app.help.coroutine.Coroutine |
|
|
|
import io.legado.app.help.coroutine.Coroutine |
|
|
|
|
|
|
|
import io.legado.app.help.http.api.CookieManager |
|
|
|
import io.legado.app.utils.NetworkUtils |
|
|
|
import io.legado.app.utils.NetworkUtils |
|
|
|
|
|
|
|
|
|
|
|
object CookieStore : CookiePersistor { |
|
|
|
object CookieStore : CookiePersistor, CookieManager { |
|
|
|
|
|
|
|
|
|
|
|
fun setCookie(url: String, cookie: String?) { |
|
|
|
override fun putCookie(url: String, cookie: String?) { |
|
|
|
Coroutine.async { |
|
|
|
Coroutine.async { |
|
|
|
val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") |
|
|
|
val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") |
|
|
|
App.db.cookieDao().insert(cookieBean) |
|
|
|
App.db.cookieDao().insert(cookieBean) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun replaceCookie(url: String, cookie: String) { |
|
|
|
override fun replaceCookie(url: String, cookie: String) { |
|
|
|
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(cookie)) { |
|
|
|
if (TextUtils.isEmpty(url) || TextUtils.isEmpty(cookie)) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
val oldCookie = getCookie(url) |
|
|
|
val oldCookie = getCookie(url) |
|
|
|
if (TextUtils.isEmpty(oldCookie)) { |
|
|
|
if (TextUtils.isEmpty(oldCookie)) { |
|
|
|
setCookie(url, cookie) |
|
|
|
putCookie(url, cookie) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
val cookieMap = cookieToMap(oldCookie) |
|
|
|
val cookieMap = cookieToMap(oldCookie) |
|
|
|
cookieMap.putAll(cookieToMap(cookie)) |
|
|
|
cookieMap.putAll(cookieToMap(cookie)) |
|
|
|
val newCookie = mapToCookie(cookieMap) |
|
|
|
val newCookie = mapToCookie(cookieMap) |
|
|
|
setCookie(url, newCookie) |
|
|
|
putCookie(url, newCookie) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getCookie(url: String): String { |
|
|
|
override fun getCookie(url: String): String { |
|
|
|
val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url)) |
|
|
|
val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url)) |
|
|
|
return cookieBean?.cookie ?: "" |
|
|
|
return cookieBean?.cookie ?: "" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun removeCookie(url: String) { |
|
|
|
override fun removeCookie(url: String) { |
|
|
|
App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) |
|
|
|
App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun cookieToMap(cookie: String): MutableMap<String, String> { |
|
|
|
override fun cookieToMap(cookie: String): MutableMap<String, String> { |
|
|
|
val cookieMap = mutableMapOf<String, String>() |
|
|
|
val cookieMap = mutableMapOf<String, String>() |
|
|
|
if (cookie.isBlank()) { |
|
|
|
if (cookie.isBlank()) { |
|
|
|
return cookieMap |
|
|
|
return cookieMap |
|
|
@ -63,7 +64,7 @@ object CookieStore : CookiePersistor { |
|
|
|
return cookieMap |
|
|
|
return cookieMap |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun mapToCookie(cookieMap: Map<String, String>?): String? { |
|
|
|
override fun mapToCookie(cookieMap: Map<String, String>?): String? { |
|
|
|
if (cookieMap == null || cookieMap.isEmpty()) { |
|
|
|
if (cookieMap == null || cookieMap.isEmpty()) { |
|
|
|
return null |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|