Convert java threads to kotlin coroutines

This commit is contained in:
Jobobby04
2020-10-29 15:29:43 -04:00
parent 46998d81f4
commit 3b5249c8bc
10 changed files with 117 additions and 77 deletions
@@ -9,22 +9,27 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.toast
import kotlin.concurrent.thread
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ConfiguringDialogController : DialogController() {
private var materialDialog: MaterialDialog? = null
val scope = CoroutineScope(Job() + Dispatchers.Main)
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
if (savedViewState == null) {
thread {
scope.launch(Dispatchers.IO) {
try {
EHConfigurator(activity!!).configureAll()
launchUI {
activity?.toast(activity?.getString(R.string.eh_settings_successfully_uploaded))
}
} catch (e: Exception) {
activity?.let {
it.runOnUiThread {
withContext(Dispatchers.Main) {
activity?.let {
MaterialDialog(it)
.title(R.string.eh_settings_configuration_failed)
.message(text = it.getString(R.string.eh_settings_configuration_failed_message, e.message))
@@ -4,6 +4,7 @@ import android.content.Context
import com.elvishew.xlog.XLog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.all.EHentai
import eu.kanade.tachiyomi.util.asJsoup
@@ -26,7 +27,7 @@ class EHConfigurator(val context: Context) {
private fun EHentai.requestWithCreds(sp: Int = 1) = Request.Builder()
.addHeader("Cookie", cookiesHeader(sp))
private fun EHentai.execProfileActions(
private suspend fun EHentai.execProfileActions(
action: String,
name: String,
set: String,
@@ -44,11 +45,11 @@ class EHConfigurator(val context: Context) {
)
.build()
)
.execute()
.await()
private val EHentai.uconfigUrl get() = baseUrl + UCONFIG_URL
fun configureAll() {
suspend fun configureAll() {
val ehSource = sources.get(EH_SOURCE_ID) as EHentai
val exhSource = sources.get(EXH_SOURCE_ID) as EHentai
@@ -58,7 +59,7 @@ class EHConfigurator(val context: Context) {
.url(HATH_PERKS_URL)
.build()
)
.execute().asJsoup()
.await().asJsoup()
val hathPerks = EHHathPerksResponse()
@@ -85,10 +86,10 @@ class EHConfigurator(val context: Context) {
configure(exhSource, hathPerks)
}
private fun configure(source: EHentai, hathPerks: EHHathPerksResponse) {
private suspend fun configure(source: EHentai, hathPerks: EHHathPerksResponse) {
// Delete old app profiles
val scanReq = source.requestWithCreds().url(source.uconfigUrl).build()
val resp = configuratorClient.newCall(scanReq).execute().asJsoup()
val resp = configuratorClient.newCall(scanReq).await().asJsoup()
var lastDoc = resp
resp.select(PROFILE_SELECTOR).forEach {
if (it.text() == PROFILE_NAME) {
@@ -127,7 +128,7 @@ class EHConfigurator(val context: Context) {
.url(source.uconfigUrl)
.post(form)
.build()
).execute()
).await()
// Persist slot + sk
source.spPref().set(slot)