mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-28 03:57:50 +02:00
Migrate to Tachiyomi 6.1
Rewrite batch add UI Rewrite lock UI
This commit is contained in:
@ -1,131 +1,145 @@
|
||||
package exh.ui.batchadd
|
||||
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.jakewharton.rxbinding.view.clicks
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.ui.base.fragment.BaseFragment
|
||||
import exh.GalleryAdder
|
||||
import exh.metadata.nullIfBlank
|
||||
import kotlinx.android.synthetic.main.eh_fragment_batch_add.*
|
||||
import timber.log.Timber
|
||||
import kotlin.concurrent.thread
|
||||
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
|
||||
import eu.kanade.tachiyomi.util.combineLatest
|
||||
import eu.kanade.tachiyomi.util.plusAssign
|
||||
import kotlinx.android.synthetic.main.eh_fragment_batch_add.view.*
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
|
||||
/**
|
||||
* LoginActivity
|
||||
* Batch add screen
|
||||
*/
|
||||
class BatchAddController : NucleusController<BatchAddPresenter>() {
|
||||
override fun inflateView(inflater: LayoutInflater, container: ViewGroup) =
|
||||
inflater.inflate(R.layout.eh_fragment_batch_add, container, false)!!
|
||||
|
||||
class BatchAddFragment : BaseFragment() {
|
||||
override fun getTitle() = "Batch add"
|
||||
|
||||
private val galleryAdder by lazy { GalleryAdder() }
|
||||
override fun createPresenter() = BatchAddPresenter()
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?)
|
||||
= inflater.inflate(R.layout.eh_fragment_batch_add, container, false)!!
|
||||
override fun onViewCreated(view: View, savedViewState: Bundle?) {
|
||||
super.onViewCreated(view, savedViewState)
|
||||
|
||||
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
|
||||
setToolbarTitle("Batch add")
|
||||
|
||||
setup()
|
||||
}
|
||||
|
||||
fun setup() {
|
||||
btn_add_galleries.setOnClickListener {
|
||||
val galleries = galleries_box.text.toString()
|
||||
//Check text box has content
|
||||
if(galleries.isNullOrBlank()) {
|
||||
noGalleriesSpecified()
|
||||
return@setOnClickListener
|
||||
with(view) {
|
||||
btn_add_galleries.clicks().subscribeUntilDestroy {
|
||||
addGalleries(galleries_box.text.toString())
|
||||
}
|
||||
|
||||
//Too lazy to actually deal with orientation changes
|
||||
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
|
||||
progress_dismiss_btn.clicks().subscribeUntilDestroy {
|
||||
presenter.currentlyAddingRelay.call(false)
|
||||
}
|
||||
|
||||
val splitGalleries = galleries.split("\n").map {
|
||||
it.trim().nullIfBlank()
|
||||
}.filterNotNull()
|
||||
val progressSubscriptions = CompositeSubscription()
|
||||
|
||||
val dialog = MaterialDialog.Builder(context)
|
||||
.title("Adding galleries...")
|
||||
.progress(false, splitGalleries.size, true)
|
||||
.cancelable(false)
|
||||
.canceledOnTouchOutside(false)
|
||||
presenter.currentlyAddingRelay
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeUntilDestroy {
|
||||
progressSubscriptions.clear()
|
||||
if(it) {
|
||||
showProgress(this)
|
||||
progressSubscriptions += presenter.progressRelay
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.combineLatest(presenter.progressTotalRelay, { progress, total ->
|
||||
//Show hide dismiss button
|
||||
progress_dismiss_btn.visibility =
|
||||
if(progress == total)
|
||||
View.VISIBLE
|
||||
else View.GONE
|
||||
|
||||
formatProgress(progress, total)
|
||||
}).subscribeUntilDestroy {
|
||||
progress_text.text = it
|
||||
}
|
||||
|
||||
progressSubscriptions += presenter.progressTotalRelay
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeUntilDestroy {
|
||||
progress_bar.max = it
|
||||
}
|
||||
|
||||
progressSubscriptions += presenter.progressRelay
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeUntilDestroy {
|
||||
progress_bar.progress = it
|
||||
}
|
||||
|
||||
presenter.eventRelay
|
||||
?.observeOn(AndroidSchedulers.mainThread())
|
||||
?.subscribeUntilDestroy {
|
||||
progress_log.append("$it\n")
|
||||
}?.let {
|
||||
progressSubscriptions += it
|
||||
}
|
||||
} else hideProgress(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val View.progressViews
|
||||
get() = listOf(
|
||||
progress_title_view,
|
||||
progress_log_wrapper,
|
||||
progress_bar,
|
||||
progress_text,
|
||||
progress_dismiss_btn
|
||||
)
|
||||
|
||||
private val View.inputViews
|
||||
get() = listOf(
|
||||
input_title_view,
|
||||
galleries_box,
|
||||
btn_add_galleries
|
||||
)
|
||||
|
||||
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 = ""
|
||||
}
|
||||
|
||||
private fun hideProgress(target: View? = view) {
|
||||
target?.apply {
|
||||
progressViews.visibility = View.GONE
|
||||
inputViews.visibility = View.VISIBLE
|
||||
}?.galleries_box?.setText("", TextView.BufferType.EDITABLE)
|
||||
}
|
||||
|
||||
private fun formatProgress(progress: Int, total: Int) = "$progress/$total"
|
||||
|
||||
private fun addGalleries(galleries: String) {
|
||||
//Check text box has content
|
||||
if(galleries.isBlank()) {
|
||||
noGalleriesSpecified()
|
||||
return
|
||||
}
|
||||
|
||||
presenter.addGalleries(galleries)
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
val succeeded = mutableListOf<String>()
|
||||
val failed = mutableListOf<String>()
|
||||
|
||||
thread {
|
||||
splitGalleries.forEachIndexed { i, s ->
|
||||
activity.runOnUiThread {
|
||||
dialog.setContent("Processing: $s")
|
||||
}
|
||||
if(addGallery(s)) {
|
||||
succeeded.add(s)
|
||||
} else {
|
||||
failed.add(s)
|
||||
}
|
||||
activity.runOnUiThread {
|
||||
dialog.setProgress(i + 1)
|
||||
}
|
||||
}
|
||||
|
||||
//Show report
|
||||
val succeededCount = succeeded.size
|
||||
val failedCount = failed.size
|
||||
|
||||
if(succeeded.isEmpty()) succeeded += "None"
|
||||
if(failed.isEmpty()) failed += "None"
|
||||
val succeededReport = succeeded.joinToString(separator = "\n", prefix = "Added:\n")
|
||||
val failedReport = failed.joinToString(separator = "\n", prefix = "Failed:\n")
|
||||
|
||||
val summary = "Summary:\nAdded: $succeededCount gallerie(s)\nFailed: $failedCount gallerie(s)"
|
||||
|
||||
val report = listOf(succeededReport, failedReport, summary).joinToString(separator = "\n\n")
|
||||
|
||||
activity.runOnUiThread {
|
||||
//Enable orientation changes again
|
||||
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
|
||||
|
||||
dialog.dismiss()
|
||||
|
||||
MaterialDialog.Builder(context)
|
||||
.title("Batch add report")
|
||||
.content(report)
|
||||
.positiveText("Ok")
|
||||
.cancelable(true)
|
||||
.canceledOnTouchOutside(true)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun addGallery(url: String): Boolean {
|
||||
try {
|
||||
galleryAdder.addGallery(url, true)
|
||||
} catch(t: Throwable) {
|
||||
Timber.e(t, "Could not add gallery!")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun noGalleriesSpecified() {
|
||||
MaterialDialog.Builder(context)
|
||||
.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()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun newInstance() = BatchAddFragment()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user