Rewrite Batch Add into kotlin flow
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package exh.ui.batchadd
|
||||
|
||||
import android.content.Context
|
||||
import com.jakewharton.rxrelay.BehaviorRelay
|
||||
import com.jakewharton.rxrelay.ReplayRelay
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
||||
@@ -10,50 +8,52 @@ import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
import exh.GalleryAddEvent
|
||||
import exh.GalleryAdder
|
||||
import exh.log.xLogE
|
||||
import exh.util.trimOrNull
|
||||
import exh.util.dropEmpty
|
||||
import exh.util.trimAll
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
||||
private val preferences: PreferencesHelper by injectLazy()
|
||||
|
||||
private val galleryAdder by lazy { GalleryAdder() }
|
||||
|
||||
val progressTotalRelay = BehaviorRelay.create(0)!!
|
||||
val progressRelay = BehaviorRelay.create(0)!!
|
||||
var eventRelay: ReplayRelay<String>? = null
|
||||
val currentlyAddingRelay = BehaviorRelay.create(STATE_IDLE)!!
|
||||
val progressTotalFlow = MutableStateFlow(0)
|
||||
val progressFlow = MutableStateFlow(0)
|
||||
var eventFlow: MutableSharedFlow<String>? = null
|
||||
val currentlyAddingFlow = MutableStateFlow(STATE_IDLE)
|
||||
|
||||
fun addGalleries(context: Context, galleries: String) {
|
||||
eventRelay = ReplayRelay.create()
|
||||
eventFlow = MutableSharedFlow(1)
|
||||
val regex =
|
||||
"""[0-9]*?\.[a-z0-9]*?:""".toRegex()
|
||||
val testedGalleries: String
|
||||
|
||||
testedGalleries = if (regex.containsMatchIn(galleries)) {
|
||||
val testedGalleries = if (regex.containsMatchIn(galleries)) {
|
||||
val url = if (preferences.enableExhentai().get()) {
|
||||
"https://exhentai.org/g/"
|
||||
} else {
|
||||
"https://e-hentai.org/g/"
|
||||
}
|
||||
regex.findAll(galleries).map { galleryKeys ->
|
||||
val linkParts = galleryKeys.value.split(".")
|
||||
val link = "${if (Injekt.get<PreferencesHelper>().enableExhentai().get()) {
|
||||
"https://exhentai.org/g/"
|
||||
} else {
|
||||
"https://e-hentai.org/g/"
|
||||
}}${linkParts[0]}/${linkParts[1].replace(":", "")}"
|
||||
link
|
||||
url + linkParts[0] + "/" + linkParts[1].replace(":", "")
|
||||
}.joinToString(separator = "\n")
|
||||
} else {
|
||||
galleries
|
||||
}
|
||||
val splitGalleries = testedGalleries.split("\n").mapNotNull {
|
||||
it.trimOrNull()
|
||||
}
|
||||
val splitGalleries = testedGalleries.split("\n")
|
||||
.trimAll()
|
||||
.dropEmpty()
|
||||
|
||||
progressRelay.call(0)
|
||||
progressTotalRelay.call(splitGalleries.size)
|
||||
progressFlow.value = 0
|
||||
progressTotalFlow.value = splitGalleries.size
|
||||
|
||||
currentlyAddingRelay.call(STATE_INPUT_TO_PROGRESS)
|
||||
currentlyAddingFlow.value = STATE_INPUT_TO_PROGRESS
|
||||
|
||||
val handler = CoroutineExceptionHandler { _, throwable ->
|
||||
xLogE("Batch add error", throwable)
|
||||
@@ -71,8 +71,8 @@ class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
||||
} else {
|
||||
failed.add(s)
|
||||
}
|
||||
progressRelay.call(i + 1)
|
||||
eventRelay?.call(
|
||||
progressFlow.value = i + 1
|
||||
eventFlow?.emit(
|
||||
(
|
||||
when (result) {
|
||||
is GalleryAddEvent.Success -> context.getString(R.string.batch_add_ok)
|
||||
@@ -84,7 +84,7 @@ class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
||||
|
||||
// Show report
|
||||
val summary = context.getString(R.string.batch_add_summary, succeeded.size, failed.size)
|
||||
eventRelay?.call(summary)
|
||||
eventFlow?.emit(summary)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user