2017-08-24 11:24:23 -04:00
|
|
|
package exh.ui.batchadd
|
|
|
|
|
2017-08-24 12:28:54 -04:00
|
|
|
import com.jakewharton.rxrelay.BehaviorRelay
|
|
|
|
import com.jakewharton.rxrelay.ReplayRelay
|
|
|
|
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
|
|
|
import exh.GalleryAddEvent
|
|
|
|
import exh.GalleryAdder
|
|
|
|
import exh.metadata.nullIfBlank
|
|
|
|
import kotlin.concurrent.thread
|
|
|
|
|
|
|
|
class BatchAddPresenter: BasePresenter<BatchAddController>() {
|
|
|
|
|
|
|
|
private val galleryAdder by lazy { GalleryAdder() }
|
|
|
|
|
|
|
|
val progressTotalRelay = BehaviorRelay.create(0)!!
|
|
|
|
val progressRelay = BehaviorRelay.create(0)!!
|
|
|
|
var eventRelay: ReplayRelay<String>? = null
|
2017-08-24 17:11:43 -04:00
|
|
|
val currentlyAddingRelay = BehaviorRelay.create(STATE_IDLE)!!
|
2017-08-24 12:28:54 -04:00
|
|
|
|
|
|
|
fun addGalleries(galleries: String) {
|
|
|
|
eventRelay = ReplayRelay.create()
|
2017-08-24 17:11:43 -04:00
|
|
|
val splitGalleries = galleries.split("\n").mapNotNull {
|
2017-08-24 12:28:54 -04:00
|
|
|
it.trim().nullIfBlank()
|
2017-08-24 17:11:43 -04:00
|
|
|
}
|
2017-08-24 12:28:54 -04:00
|
|
|
|
|
|
|
progressRelay.call(0)
|
|
|
|
progressTotalRelay.call(splitGalleries.size)
|
|
|
|
|
2017-08-24 17:11:43 -04:00
|
|
|
currentlyAddingRelay.call(STATE_INPUT_TO_PROGRESS)
|
2017-08-24 12:28:54 -04:00
|
|
|
|
|
|
|
thread {
|
|
|
|
val succeeded = mutableListOf<String>()
|
|
|
|
val failed = mutableListOf<String>()
|
|
|
|
|
|
|
|
splitGalleries.forEachIndexed { i, s ->
|
|
|
|
val result = galleryAdder.addGallery(s, true)
|
|
|
|
if(result is GalleryAddEvent.Success) {
|
|
|
|
succeeded.add(s)
|
|
|
|
} else {
|
|
|
|
failed.add(s)
|
|
|
|
}
|
|
|
|
progressRelay.call(i + 1)
|
|
|
|
eventRelay?.call(result.logMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Show report
|
|
|
|
val summary = "\nSummary:\nAdded: ${succeeded.size} gallerie(s)\nFailed: ${failed.size} gallerie(s)"
|
|
|
|
eventRelay?.call(summary)
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 17:11:43 -04:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val STATE_IDLE = 0
|
|
|
|
const val STATE_INPUT_TO_PROGRESS = 1
|
|
|
|
const val STATE_PROGRESS_TO_INPUT = 2
|
|
|
|
}
|
2017-08-24 12:28:54 -04:00
|
|
|
}
|