Optimize page loading
This commit is contained in:
@@ -16,7 +16,7 @@ import kotlin.coroutines.CoroutineContext
|
||||
class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
|
||||
|
||||
private val job = SupervisorJob()
|
||||
private val tasks = HashMap<String, Job>()
|
||||
private val tasks = HashMap<String, Deferred<File>>()
|
||||
private val okHttp by inject<OkHttpClient>()
|
||||
private val cache by inject<PagesCache>()
|
||||
|
||||
@@ -30,8 +30,13 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
|
||||
return it
|
||||
}
|
||||
}
|
||||
val task = tasks[url]?.takeUnless { it.isCancelled }
|
||||
return (task ?: loadAsync(url).also { tasks[url] = it }).await()
|
||||
}
|
||||
|
||||
private fun loadAsync(url: String) = async(Dispatchers.IO) {
|
||||
val uri = Uri.parse(url)
|
||||
return if (uri.scheme == "cbz") {
|
||||
if (uri.scheme == "cbz") {
|
||||
val zip = ZipFile(uri.schemeSpecificPart)
|
||||
val entry = zip.getEntry(uri.fragment)
|
||||
zip.getInputStream(entry).use {
|
||||
@@ -60,5 +65,6 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
|
||||
|
||||
override fun dispose() {
|
||||
coroutineContext.cancel()
|
||||
tasks.clear()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.koitharu.kotatsu.utils.ext
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.delay
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.R
|
||||
@@ -43,4 +44,12 @@ fun Throwable.getDisplayMessage(resources: Resources) = when (this) {
|
||||
} else {
|
||||
resources.getString(R.string.error_occurred)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> measured(tag: String, block: () -> T): T {
|
||||
val time = System.currentTimeMillis()
|
||||
val res = block()
|
||||
val spent = System.currentTimeMillis() - time
|
||||
Log.d("measured", "$tag ${spent.format(1)} ms")
|
||||
return res
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:background="?android:windowBackground"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
Reference in New Issue
Block a user