Convert java threads to kotlin coroutines
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package exh.ui.batchadd
|
||||
|
||||
import android.content.Context
|
||||
import com.elvishew.xlog.XLog
|
||||
import com.jakewharton.rxrelay.BehaviorRelay
|
||||
import com.jakewharton.rxrelay.ReplayRelay
|
||||
import eu.kanade.tachiyomi.R
|
||||
@@ -9,13 +10,20 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
||||
import exh.GalleryAddEvent
|
||||
import exh.GalleryAdder
|
||||
import exh.util.trimOrNull
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
||||
|
||||
private val galleryAdder by lazy { GalleryAdder() }
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
|
||||
val progressTotalRelay = BehaviorRelay.create(0)!!
|
||||
val progressRelay = BehaviorRelay.create(0)!!
|
||||
@@ -50,12 +58,17 @@ class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
||||
|
||||
currentlyAddingRelay.call(STATE_INPUT_TO_PROGRESS)
|
||||
|
||||
thread {
|
||||
val handler = CoroutineExceptionHandler { _, throwable ->
|
||||
XLog.e(throwable)
|
||||
}
|
||||
|
||||
scope.launch(Dispatchers.IO + handler) {
|
||||
val succeeded = mutableListOf<String>()
|
||||
val failed = mutableListOf<String>()
|
||||
|
||||
splitGalleries.forEachIndexed { i, s ->
|
||||
val result = galleryAdder.addGallery(context, s, true)
|
||||
ensureActive()
|
||||
val result = withContext(Dispatchers.IO) { galleryAdder.addGallery(context, s, true) }
|
||||
if (result is GalleryAddEvent.Success) {
|
||||
succeeded.add(s)
|
||||
} else {
|
||||
|
||||
@@ -25,6 +25,9 @@ import exh.source.DelegatedHttpSource
|
||||
import exh.util.melt
|
||||
import kotlinx.android.synthetic.main.eh_activity_captcha.toolbar
|
||||
import kotlinx.android.synthetic.main.eh_activity_captcha.webview
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
@@ -179,13 +182,13 @@ class BrowserActionActivity : AppCompatActivity() {
|
||||
return true
|
||||
}
|
||||
|
||||
fun captchaSolveFail() {
|
||||
suspend fun captchaSolveFail() {
|
||||
currentLoopId = null
|
||||
validateCurrentLoopId = null
|
||||
XLog.e(IllegalStateException("Captcha solve failure!"))
|
||||
runOnUiThread {
|
||||
withContext(Dispatchers.Main) {
|
||||
webview.evaluateJavascript(SOLVE_UI_SCRIPT_HIDE, null)
|
||||
MaterialDialog(this)
|
||||
MaterialDialog(this@BrowserActionActivity)
|
||||
.title(R.string.captcha_solve_failure)
|
||||
.message(R.string.captcha_solve_failure_message)
|
||||
.cancelable(true)
|
||||
@@ -196,7 +199,7 @@ class BrowserActionActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun callback(result: String?, loopId: String, stage: Int) {
|
||||
suspend fun callback(result: String?, loopId: String, stage: Int) {
|
||||
if (loopId != currentLoopId) return
|
||||
|
||||
when (stage) {
|
||||
@@ -259,7 +262,7 @@ class BrowserActionActivity : AppCompatActivity() {
|
||||
}
|
||||
},
|
||||
{
|
||||
captchaSolveFail()
|
||||
runBlocking { captchaSolveFail() }
|
||||
}
|
||||
)
|
||||
} else {
|
||||
@@ -456,7 +459,7 @@ class BrowserActionActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun validateCaptchaCallback(result: Boolean, loopId: String) {
|
||||
suspend fun validateCaptchaCallback(result: Boolean, loopId: String) {
|
||||
if (loopId != validateCurrentLoopId) return
|
||||
|
||||
if (result) {
|
||||
|
||||
@@ -16,10 +16,11 @@ import eu.kanade.tachiyomi.ui.main.MainActivity
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaController
|
||||
import exh.GalleryAddEvent
|
||||
import exh.GalleryAdder
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import rx.Subscription
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subjects.BehaviorSubject
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
|
||||
private var statusSubscription: Subscription? = null
|
||||
@@ -119,14 +120,14 @@ class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
|
||||
@Synchronized
|
||||
private fun loadGalleryEnd(gallery: String, source: UrlImportableSource? = null) {
|
||||
// Load gallery async
|
||||
thread {
|
||||
val result = galleryAdder.addGallery(this, gallery, forceSource = source)
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val result = galleryAdder.addGallery(this@InterceptActivity, gallery, forceSource = source)
|
||||
|
||||
status.onNext(
|
||||
when (result) {
|
||||
is GalleryAddEvent.Success -> result.manga.id?.let {
|
||||
InterceptResult.Success(it)
|
||||
} ?: InterceptResult.Failure(this.getString(R.string.manga_id_is_null))
|
||||
} ?: InterceptResult.Failure(this@InterceptActivity.getString(R.string.manga_id_is_null))
|
||||
is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user