Rewrite Batch Add into kotlin flow

This commit is contained in:
Jobobby04 2022-01-01 15:59:30 -05:00
parent 08f6a7fbd6
commit 21b620ee86
2 changed files with 77 additions and 75 deletions

View File

@ -9,15 +9,17 @@ import dev.chrisbanes.insetter.applyInsetter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.EhFragmentBatchAddBinding
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.util.lang.combineLatest
import eu.kanade.tachiyomi.util.lang.plusAssign
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import reactivecircus.flowbinding.android.view.clicks
import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription
/**
* Batch add screen
@ -40,7 +42,7 @@ class BatchAddController : NucleusController<EhFragmentBatchAddBinding, BatchAdd
binding.progressDismissBtn.clicks()
.onEach {
presenter.currentlyAddingRelay.call(BatchAddPresenter.STATE_PROGRESS_TO_INPUT)
presenter.currentlyAddingFlow.value = BatchAddPresenter.STATE_PROGRESS_TO_INPUT
}
.launchIn(viewScope)
@ -50,53 +52,53 @@ class BatchAddController : NucleusController<EhFragmentBatchAddBinding, BatchAdd
}
}
val progressSubscriptions = CompositeSubscription()
presenter.currentlyAddingFlow
.buffer(capacity = 0, onBufferOverflow = BufferOverflow.SUSPEND)
.mapLatest { event ->
when (event) {
BatchAddPresenter.STATE_INPUT_TO_PROGRESS -> coroutineScope {
showProgress(binding)
presenter.progressFlow
.buffer(capacity = Channel.RENDEZVOUS)
.combine(presenter.progressTotalFlow) { progress, total ->
// Show hide dismiss button
binding.progressDismissBtn.isVisible = progress == total
formatProgress(progress, total)
}
.onEach {
binding.progressText.text = it
}
.launchIn(this)
presenter.currentlyAddingRelay
.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread())
.subscribeUntilDestroy {
progressSubscriptions.clear()
if (it == BatchAddPresenter.STATE_INPUT_TO_PROGRESS) {
showProgress(binding)
progressSubscriptions += presenter.progressRelay
.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread())
.combineLatest(presenter.progressTotalRelay) { progress, total ->
// Show hide dismiss button
binding.progressDismissBtn.isVisible = progress == total
formatProgress(progress, total)
}.subscribeUntilDestroy {
binding.progressText.text = it
}
presenter.progressTotalFlow
.buffer(capacity = Channel.RENDEZVOUS)
.onEach {
binding.progressBar.max = it
}
.launchIn(this)
progressSubscriptions += presenter.progressTotalRelay
.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread())
.subscribeUntilDestroy {
binding.progressBar.max = it
}
presenter.progressFlow
.buffer(capacity = Channel.RENDEZVOUS)
.onEach {
binding.progressBar.progress = it
}
.launchIn(this)
progressSubscriptions += presenter.progressRelay
.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread())
.subscribeUntilDestroy {
binding.progressBar.progress = it
}
presenter.eventRelay
?.onBackpressureBuffer()
?.observeOn(AndroidSchedulers.mainThread())
?.subscribeUntilDestroy {
binding.progressLog.append("$it\n")
}?.let {
progressSubscriptions += it
}
} else if (it == BatchAddPresenter.STATE_PROGRESS_TO_INPUT) {
hideProgress(binding)
presenter.currentlyAddingRelay.call(BatchAddPresenter.STATE_IDLE)
presenter.eventFlow
?.buffer(capacity = Channel.RENDEZVOUS)
?.onEach {
binding.progressLog.append("$it\n")
}
?.launchIn(this)
Unit
}
BatchAddPresenter.STATE_PROGRESS_TO_INPUT -> {
hideProgress(binding)
presenter.currentlyAddingFlow.value = BatchAddPresenter.STATE_IDLE
}
}
}
.launchIn(viewScope)
}
private val EhFragmentBatchAddBinding.progressViews
@ -150,7 +152,7 @@ class BatchAddController : NucleusController<EhFragmentBatchAddBinding, BatchAdd
return
}
presenter.addGalleries(activity!!, galleries)
presenter.addGalleries(applicationContext!!, galleries)
}
private fun noGalleriesSpecified() {

View File

@ -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)
}
}