2017-05-05 05:38:17 +02:00
|
|
|
package exh.ui.batchadd
|
|
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2017-08-24 18:28:54 +02:00
|
|
|
import android.widget.TextView
|
2017-05-05 05:38:17 +02:00
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
2020-04-21 03:03:28 +02:00
|
|
|
import eu.kanade.tachiyomi.databinding.EhFragmentBatchAddBinding
|
2017-08-24 18:28:54 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
|
2020-03-31 22:38:40 +02:00
|
|
|
import eu.kanade.tachiyomi.util.lang.combineLatest
|
2020-04-21 22:18:20 +02:00
|
|
|
import eu.kanade.tachiyomi.util.lang.launchInUI
|
2020-03-31 22:38:40 +02:00
|
|
|
import eu.kanade.tachiyomi.util.lang.plusAssign
|
2020-04-04 23:01:05 +02:00
|
|
|
import kotlinx.android.synthetic.main.eh_fragment_batch_add.view.galleries_box
|
|
|
|
|
import kotlinx.android.synthetic.main.eh_fragment_batch_add.view.progress_log
|
2020-04-21 22:18:20 +02:00
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
import reactivecircus.flowbinding.android.view.clicks
|
2017-08-24 18:28:54 +02:00
|
|
|
import rx.android.schedulers.AndroidSchedulers
|
|
|
|
|
import rx.subscriptions.CompositeSubscription
|
2017-05-05 05:38:17 +02:00
|
|
|
|
|
|
|
|
/**
|
2017-08-24 18:28:54 +02:00
|
|
|
* Batch add screen
|
2017-05-05 05:38:17 +02:00
|
|
|
*/
|
2020-04-21 03:03:28 +02:00
|
|
|
class BatchAddController : NucleusController<EhFragmentBatchAddBinding, BatchAddPresenter>() {
|
|
|
|
|
override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
|
|
|
|
|
binding = EhFragmentBatchAddBinding.inflate(inflater)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
override fun getTitle() = "Batch add"
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
override fun createPresenter() = BatchAddPresenter()
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-12-09 00:21:26 +01:00
|
|
|
override fun onViewCreated(view: View) {
|
|
|
|
|
super.onViewCreated(view)
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
with(view) {
|
2020-04-21 22:18:20 +02:00
|
|
|
binding.btnAddGalleries.clicks()
|
|
|
|
|
.onEach {
|
|
|
|
|
addGalleries(binding.galleriesBox.text.toString())
|
|
|
|
|
}
|
|
|
|
|
.launchInUI()
|
|
|
|
|
|
|
|
|
|
binding.progressDismissBtn.clicks()
|
|
|
|
|
.onEach {
|
|
|
|
|
presenter.currentlyAddingRelay.call(BatchAddPresenter.STATE_PROGRESS_TO_INPUT)
|
|
|
|
|
}
|
|
|
|
|
.launchInUI()
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
val progressSubscriptions = CompositeSubscription()
|
|
|
|
|
|
|
|
|
|
presenter.currentlyAddingRelay
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribeUntilDestroy {
|
|
|
|
|
progressSubscriptions.clear()
|
2020-04-04 22:30:05 +02:00
|
|
|
if (it == BatchAddPresenter.STATE_INPUT_TO_PROGRESS) {
|
2017-08-24 18:28:54 +02:00
|
|
|
showProgress(this)
|
|
|
|
|
progressSubscriptions += presenter.progressRelay
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2020-04-21 03:03:28 +02:00
|
|
|
.combineLatest(presenter.progressTotalRelay) { progress, total ->
|
2020-04-04 22:30:05 +02:00
|
|
|
// Show hide dismiss button
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.progressDismissBtn.visibility =
|
|
|
|
|
if (progress == total)
|
|
|
|
|
View.VISIBLE
|
|
|
|
|
else View.GONE
|
2017-08-24 18:28:54 +02:00
|
|
|
|
|
|
|
|
formatProgress(progress, total)
|
2020-04-21 03:03:28 +02:00
|
|
|
}.subscribeUntilDestroy {
|
|
|
|
|
binding.progressText.text = it
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressSubscriptions += presenter.progressTotalRelay
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribeUntilDestroy {
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.progressBar.max = it
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressSubscriptions += presenter.progressRelay
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribeUntilDestroy {
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.progressBar.progress = it
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
presenter.eventRelay
|
|
|
|
|
?.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
?.subscribeUntilDestroy {
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.progressLog.append("$it\n")
|
2017-08-24 18:28:54 +02:00
|
|
|
}?.let {
|
|
|
|
|
progressSubscriptions += it
|
|
|
|
|
}
|
2020-04-04 22:30:05 +02:00
|
|
|
} else if (it == BatchAddPresenter.STATE_PROGRESS_TO_INPUT) {
|
2017-08-24 23:11:43 +02:00
|
|
|
hideProgress(this)
|
|
|
|
|
presenter.currentlyAddingRelay.call(BatchAddPresenter.STATE_IDLE)
|
|
|
|
|
}
|
2017-05-05 05:38:17 +02:00
|
|
|
}
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
private val View.progressViews
|
|
|
|
|
get() = listOf(
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.progressTitleView,
|
|
|
|
|
binding.progressLogWrapper,
|
|
|
|
|
binding.progressBar,
|
|
|
|
|
binding.progressText,
|
|
|
|
|
binding.progressDismissBtn
|
2017-08-24 18:28:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private val View.inputViews
|
|
|
|
|
get() = listOf(
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.inputTitleView,
|
|
|
|
|
binding.galleriesBox,
|
|
|
|
|
binding.btnAddGalleries
|
2017-08-24 18:28:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private var List<View>.visibility: Int
|
|
|
|
|
get() = throw UnsupportedOperationException()
|
|
|
|
|
set(v) { forEach { it.visibility = v } }
|
|
|
|
|
|
|
|
|
|
private fun showProgress(target: View? = view) {
|
|
|
|
|
target?.apply {
|
|
|
|
|
progressViews.visibility = View.VISIBLE
|
|
|
|
|
inputViews.visibility = View.GONE
|
|
|
|
|
}?.progress_log?.text = ""
|
|
|
|
|
}
|
2017-05-05 05:38:17 +02:00
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
private fun hideProgress(target: View? = view) {
|
|
|
|
|
target?.apply {
|
|
|
|
|
progressViews.visibility = View.GONE
|
|
|
|
|
inputViews.visibility = View.VISIBLE
|
|
|
|
|
}?.galleries_box?.setText("", TextView.BufferType.EDITABLE)
|
2017-05-05 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
private fun formatProgress(progress: Int, total: Int) = "$progress/$total"
|
|
|
|
|
|
|
|
|
|
private fun addGalleries(galleries: String) {
|
2020-04-04 22:30:05 +02:00
|
|
|
// Check text box has content
|
|
|
|
|
if (galleries.isBlank()) {
|
2017-08-24 18:28:54 +02:00
|
|
|
noGalleriesSpecified()
|
|
|
|
|
return
|
2017-05-05 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
presenter.addGalleries(galleries)
|
2017-05-05 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-24 18:28:54 +02:00
|
|
|
private fun noGalleriesSpecified() {
|
|
|
|
|
activity?.let {
|
|
|
|
|
MaterialDialog.Builder(it)
|
|
|
|
|
.title("No galleries to add!")
|
|
|
|
|
.content("You must specify at least one gallery to add!")
|
|
|
|
|
.positiveText("Ok")
|
|
|
|
|
.onPositive { materialDialog, _ -> materialDialog.dismiss() }
|
|
|
|
|
.cancelable(true)
|
|
|
|
|
.canceledOnTouchOutside(true)
|
|
|
|
|
.show()
|
|
|
|
|
}
|
2017-05-05 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
}
|