From 21b620ee863794d914ae4055a8b5b836699b2d2b Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Sat, 1 Jan 2022 15:59:30 -0500 Subject: [PATCH] Rewrite Batch Add into kotlin flow --- .../exh/ui/batchadd/BatchAddController.kt | 98 ++++++++++--------- .../java/exh/ui/batchadd/BatchAddPresenter.kt | 54 +++++----- 2 files changed, 77 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/exh/ui/batchadd/BatchAddController.kt b/app/src/main/java/exh/ui/batchadd/BatchAddController.kt index c4e83196c..6e2a64728 100755 --- a/app/src/main/java/exh/ui/batchadd/BatchAddController.kt +++ b/app/src/main/java/exh/ui/batchadd/BatchAddController.kt @@ -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 + 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() { + 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? = null - val currentlyAddingRelay = BehaviorRelay.create(STATE_IDLE)!! + val progressTotalFlow = MutableStateFlow(0) + val progressFlow = MutableStateFlow(0) + var eventFlow: MutableSharedFlow? = 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().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() { } 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() { // Show report val summary = context.getString(R.string.batch_add_summary, succeeded.size, failed.size) - eventRelay?.call(summary) + eventFlow?.emit(summary) } }