Use async version of call.execute()

This commit is contained in:
FooIbar authored and tarsin committed 2023-03-10 01:58:38 +08:00
1 parent da9aeb54f0
commit b2c4cd721c
4 files changed
+18 -17

No files matched your search

@@ -94,9 +94,11 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
Settings.initialize()
ReadableTime.initialize(this)
AppConfig.initialize(this)
EhTagDatabase.update()
AppCompatDelegate.setDefaultNightMode(Settings.theme)
launchIO {
launchIO {
EhTagDatabase.update()
}
launchIO {
ehDatabase
}
@@ -22,12 +22,13 @@ import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
import com.hippo.ehviewer.R
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.copyToFile
import eu.kanade.tachiyomi.util.lang.launchIO
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.executeAsync
import okio.BufferedSource
import okio.HashingSink.Companion.sha1
import okio.blackholeSink
@@ -45,6 +46,7 @@ private typealias TagGroups = Map<String, TagGroup>
object EhTagDatabase {
private const val NAMESPACE_PREFIX = "n"
private lateinit var tagGroups: TagGroups
private val updateLock = Mutex()
fun isInitialized(): Boolean {
return this::tagGroups.isInitialized
@@ -179,11 +181,11 @@ object EhTagDatabase {
return sha1 != null && sha1 == getFileSha1(data)
}
private fun save(client: OkHttpClient, url: String, file: File): Boolean {
private suspend fun save(client: OkHttpClient, url: String, file: File): Boolean {
val request: Request = Request.Builder().url(url).build()
val call = client.newCall(request)
runCatching {
call.execute().use { response ->
call.executeAsync().use { response ->
if (!response.isSuccessful) {
return false
}
@@ -199,16 +201,13 @@ object EhTagDatabase {
return false
}
@OptIn(DelicateCoroutinesApi::class)
@JvmStatic
fun update() {
launchIO {
suspend fun update() {
updateLock.withLock {
updateInternal()
}
}
@Synchronized
private fun updateInternal() {
private suspend fun updateInternal() {
val urls = getMetadata(EhApplication.application)
urls?.let {
val sha1Name = urls[0]
@@ -375,7 +375,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
}.getOrNull()
}
fun getPTokenFromMultiPageViewer(index: Int): String? {
suspend fun getPTokenFromMultiPageViewer(index: Int): String? {
val spiderInfo = mSpiderInfo
val url = getGalleryMultiPageViewerUrl(
galleryInfo.gid, galleryInfo.token!!
@@ -383,7 +383,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
val referer = referer
val request = EhRequestBuilder(url, referer).build()
try {
plainTextOkHttpClient.newCall(request).execute().use { response ->
plainTextOkHttpClient.newCall(request).executeAsync().use { response ->
val body = response.body.string()
val list = GalleryMultiPageViewerPTokenParser.parse(body)
for (i in list.indices) {
@@ -397,7 +397,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
}
}
fun getPTokenFromInternet(index: Int): String? {
suspend fun getPTokenFromInternet(index: Int): String? {
val spiderInfo = mSpiderInfo
// Check previewIndex
@@ -416,7 +416,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
val referer = referer
val request = EhRequestBuilder(url, referer).build()
try {
plainTextOkHttpClient.newCall(request).execute().use { response ->
plainTextOkHttpClient.newCall(request).executeAsync().use { response ->
val body = response.body.string()
readPreviews(body, previewIndex, spiderInfo)
return spiderInfo.pTokenMap[index]
@@ -591,7 +591,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
}
private suspend fun doInJob(index: Int, force: Boolean) {
fun getPToken(index: Int): String? {
suspend fun getPToken(index: Int): String? {
if (index !in 0 until size) return null
return mSpiderInfo.pTokenMap[index].takeIf { it != TOKEN_FAILED }
?: getPTokenFromInternet(index)
@@ -129,7 +129,7 @@ class EhFragment : BasePreferenceFragment() {
requireActivity().setResult(Activity.RESULT_OK)
} else if (Settings.KEY_SHOW_TAG_TRANSLATIONS == key) {
if (java.lang.Boolean.TRUE == newValue) {
EhTagDatabase.update()
lifecycleScope.launchNonCancellable { EhTagDatabase.update() }
}
} else if (Settings.KEY_BLACK_DARK_THEME == key) {
if (requireActivity().resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_YES > 0) {