2017-08-24 17:24:23 +02:00
|
|
|
package exh.ui.batchadd
|
|
|
|
|
|
2020-07-16 01:16:21 +02:00
|
|
|
import android.content.Context
|
2022-09-25 16:07:06 +02:00
|
|
|
import eu.kanade.domain.UnsortedPreferences
|
2020-07-16 01:16:21 +02:00
|
|
|
import eu.kanade.tachiyomi.R
|
2022-09-25 16:07:06 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
2021-01-11 01:36:24 +01:00
|
|
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
2017-08-24 18:28:54 +02:00
|
|
|
import exh.GalleryAddEvent
|
|
|
|
|
import exh.GalleryAdder
|
2021-03-07 06:23:23 +01:00
|
|
|
import exh.log.xLogE
|
2022-07-18 15:59:09 +02:00
|
|
|
import exh.util.trimOrNull
|
2020-10-29 20:29:43 +01:00
|
|
|
import kotlinx.coroutines.CoroutineExceptionHandler
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.ensureActive
|
2022-01-01 21:59:30 +01:00
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2020-10-29 20:29:43 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2022-01-01 21:59:30 +01:00
|
|
|
import uy.kohesive.injekt.injectLazy
|
2017-08-24 18:28:54 +02:00
|
|
|
|
2022-09-25 16:07:06 +02:00
|
|
|
class BatchAddPresenter : BasePresenter<BatchAddController>() {
|
|
|
|
|
private val preferences: UnsortedPreferences by injectLazy()
|
2017-08-24 18:28:54 +02:00
|
|
|
|
|
|
|
|
private val galleryAdder by lazy { GalleryAdder() }
|
|
|
|
|
|
2022-01-01 21:59:30 +01:00
|
|
|
val progressTotalFlow = MutableStateFlow(0)
|
|
|
|
|
val progressFlow = MutableStateFlow(0)
|
|
|
|
|
var eventFlow: MutableSharedFlow<String>? = null
|
|
|
|
|
val currentlyAddingFlow = MutableStateFlow(STATE_IDLE)
|
2017-08-24 18:28:54 +02:00
|
|
|
|
2020-07-16 01:16:21 +02:00
|
|
|
fun addGalleries(context: Context, galleries: String) {
|
2022-01-01 21:59:30 +01:00
|
|
|
eventFlow = MutableSharedFlow(1)
|
2020-05-18 19:00:13 +02:00
|
|
|
|
2022-07-18 15:59:09 +02:00
|
|
|
val splitGalleries = if (ehVisitedRegex.containsMatchIn(galleries)) {
|
2022-01-01 21:59:30 +01:00
|
|
|
val url = if (preferences.enableExhentai().get()) {
|
|
|
|
|
"https://exhentai.org/g/"
|
|
|
|
|
} else {
|
|
|
|
|
"https://e-hentai.org/g/"
|
|
|
|
|
}
|
2022-07-18 15:59:09 +02:00
|
|
|
ehVisitedRegex.findAll(galleries).map { galleryKeys ->
|
2020-08-05 01:32:36 +02:00
|
|
|
val linkParts = galleryKeys.value.split(".")
|
2022-01-01 21:59:30 +01:00
|
|
|
url + linkParts[0] + "/" + linkParts[1].replace(":", "")
|
2022-07-18 15:59:09 +02:00
|
|
|
}.toList()
|
2020-05-18 19:00:13 +02:00
|
|
|
} else {
|
2022-07-18 15:59:09 +02:00
|
|
|
galleries.split("\n")
|
|
|
|
|
.mapNotNull(String::trimOrNull)
|
2020-05-18 19:00:13 +02:00
|
|
|
}
|
2017-08-24 18:28:54 +02:00
|
|
|
|
2022-01-01 21:59:30 +01:00
|
|
|
progressFlow.value = 0
|
|
|
|
|
progressTotalFlow.value = splitGalleries.size
|
2017-08-24 18:28:54 +02:00
|
|
|
|
2022-01-01 21:59:30 +01:00
|
|
|
currentlyAddingFlow.value = STATE_INPUT_TO_PROGRESS
|
2017-08-24 18:28:54 +02:00
|
|
|
|
2020-10-29 20:29:43 +01:00
|
|
|
val handler = CoroutineExceptionHandler { _, throwable ->
|
2021-03-09 01:50:41 +01:00
|
|
|
xLogE("Batch add error", throwable)
|
2020-10-29 20:29:43 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-08 05:12:13 +01:00
|
|
|
presenterScope.launch(Dispatchers.IO + handler) {
|
2017-08-24 18:28:54 +02:00
|
|
|
val succeeded = mutableListOf<String>()
|
|
|
|
|
val failed = mutableListOf<String>()
|
|
|
|
|
|
|
|
|
|
splitGalleries.forEachIndexed { i, s ->
|
2020-10-29 20:29:43 +01:00
|
|
|
ensureActive()
|
2021-01-11 01:36:24 +01:00
|
|
|
val result = withIOContext { galleryAdder.addGallery(context, s, true) }
|
2020-04-04 22:30:05 +02:00
|
|
|
if (result is GalleryAddEvent.Success) {
|
2017-08-24 18:28:54 +02:00
|
|
|
succeeded.add(s)
|
|
|
|
|
} else {
|
|
|
|
|
failed.add(s)
|
|
|
|
|
}
|
2022-01-01 21:59:30 +01:00
|
|
|
progressFlow.value = i + 1
|
|
|
|
|
eventFlow?.emit(
|
2020-05-02 06:46:24 +02:00
|
|
|
(
|
|
|
|
|
when (result) {
|
2020-07-16 01:16:21 +02:00
|
|
|
is GalleryAddEvent.Success -> context.getString(R.string.batch_add_ok)
|
|
|
|
|
is GalleryAddEvent.Fail -> context.getString(R.string.batch_add_error)
|
2020-05-02 06:46:24 +02:00
|
|
|
}
|
2022-04-08 21:30:30 +02:00
|
|
|
) + " " + result.logMessage,
|
2020-05-02 06:46:24 +02:00
|
|
|
)
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 22:30:05 +02:00
|
|
|
// Show report
|
2020-07-16 01:16:21 +02:00
|
|
|
val summary = context.getString(R.string.batch_add_summary, succeeded.size, failed.size)
|
2022-01-01 21:59:30 +01:00
|
|
|
eventFlow?.emit(summary)
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-24 23:11:43 +02:00
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
const val STATE_IDLE = 0
|
|
|
|
|
const val STATE_INPUT_TO_PROGRESS = 1
|
|
|
|
|
const val STATE_PROGRESS_TO_INPUT = 2
|
2022-07-18 15:59:09 +02:00
|
|
|
|
|
|
|
|
val ehVisitedRegex = """[0-9]*?\.[a-z0-9]*?:""".toRegex()
|
2017-08-24 23:11:43 +02:00
|
|
|
}
|
2017-08-24 18:28:54 +02:00
|
|
|
}
|