Add intercept activity source choosing

This commit is contained in:
Jobobby04 2020-10-19 18:41:41 -04:00
parent a4a1c2827c
commit 1dfe3890a9
2 changed files with 45 additions and 23 deletions

View File

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.UrlImportableSource import eu.kanade.tachiyomi.source.online.UrlImportableSource
import eu.kanade.tachiyomi.source.online.all.EHentai import eu.kanade.tachiyomi.source.online.all.EHentai
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import exh.source.EnhancedHttpSource.Companion.getMainSource
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.util.Date import java.util.Date
@ -19,6 +20,20 @@ class GalleryAdder {
private val sourceManager: SourceManager by injectLazy() private val sourceManager: SourceManager by injectLazy()
fun pickSource(url: String): List<UrlImportableSource> {
val uri = Uri.parse(url)
return sourceManager.getVisibleCatalogueSources()
.map { it.getMainSource() }
.filterIsInstance<UrlImportableSource>()
.filter {
try {
it.matchesUri(uri)
} catch (e: Exception) {
false
}
}
}
fun addGallery( fun addGallery(
context: Context, context: Context,
url: String, url: String,
@ -41,21 +56,12 @@ class GalleryAdder {
} }
} else { } else {
sourceManager.getVisibleCatalogueSources() sourceManager.getVisibleCatalogueSources()
.map { it.getMainSource() }
.filterIsInstance<UrlImportableSource>() .filterIsInstance<UrlImportableSource>()
.find { .find {
try { try {
it.matchesUri(uri) it.matchesUri(uri)
} catch (e: Exception) { } catch (e: Exception) {
XLog.e(context.getString(R.string.gallery_adder_source_uri_must_match), e)
false
}
} ?: sourceManager.getDelegatedCatalogueSources()
.filterIsInstance<UrlImportableSource>()
.find {
try {
it.matchesUri(uri)
} catch (e: Exception) {
XLog.e(context.getString(R.string.gallery_adder_source_uri_must_match), e)
false false
} }
} ?: return GalleryAddEvent.Fail.UnknownType(url, context) } ?: return GalleryAddEvent.Fail.UnknownType(url, context)

View File

@ -7,8 +7,10 @@ import androidx.core.view.isVisible
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onCancel import com.afollestad.materialdialogs.callbacks.onCancel
import com.afollestad.materialdialogs.callbacks.onDismiss import com.afollestad.materialdialogs.callbacks.onDismiss
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.EhActivityInterceptBinding import eu.kanade.tachiyomi.databinding.EhActivityInterceptBinding
import eu.kanade.tachiyomi.source.online.UrlImportableSource
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.manga.MangaController
@ -93,29 +95,43 @@ class InterceptActivity : BaseActivity<EhActivityInterceptBinding>() {
private val galleryAdder = GalleryAdder() private val galleryAdder = GalleryAdder()
val status: BehaviorSubject<InterceptResult> = BehaviorSubject.create<InterceptResult>(InterceptResult.Idle) val status: BehaviorSubject<InterceptResult> = BehaviorSubject.create(InterceptResult.Idle)
@Synchronized @Synchronized
fun loadGallery(gallery: String) { fun loadGallery(gallery: String) {
// Do not load gallery if already loading // Do not load gallery if already loading
if (status.value is InterceptResult.Idle) { if (status.value is InterceptResult.Idle) {
status.onNext(InterceptResult.Loading) status.onNext(InterceptResult.Loading)
val sources = galleryAdder.pickSource(gallery)
// Load gallery async if (sources.size > 1) {
thread { MaterialDialog(this)
val result = galleryAdder.addGallery(this, gallery) .title(R.string.select_source)
.listItemsSingleChoice(items = sources.map { it.toString() }) { _, index, _ ->
status.onNext( loadGalleryEnd(gallery, sources[index])
when (result) {
is GalleryAddEvent.Success -> result.manga.id?.let {
InterceptResult.Success(it)
} ?: InterceptResult.Failure(this.getString(R.string.manga_id_is_null))
is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
} }
) .show()
} else {
loadGalleryEnd(gallery)
} }
} }
} }
@Synchronized
private fun loadGalleryEnd(gallery: String, source: UrlImportableSource? = null) {
// Load gallery async
thread {
val result = galleryAdder.addGallery(this, gallery, forceSource = source)
status.onNext(
when (result) {
is GalleryAddEvent.Success -> result.manga.id?.let {
InterceptResult.Success(it)
} ?: InterceptResult.Failure(this.getString(R.string.manga_id_is_null))
is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
}
)
}
}
} }
sealed class InterceptResult { sealed class InterceptResult {