Finish batch add screen.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package exh
|
||||
|
||||
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import eu.kanade.tachiyomi.data.source.SourceManager
|
||||
import eu.kanade.tachiyomi.util.UrlUtil
|
||||
import exh.metadata.MetadataHelper
|
||||
import exh.metadata.copyTo
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
|
||||
class GalleryAdder {
|
||||
|
||||
private val db: DatabaseHelper by injectLazy()
|
||||
|
||||
private val sourceManager: SourceManager by injectLazy()
|
||||
|
||||
private val metadataHelper = MetadataHelper()
|
||||
|
||||
fun addGallery(url: String, fav: Boolean = false): Manga {
|
||||
val source = when(URL(url).host) {
|
||||
"g.e-hentai.org" -> 1
|
||||
"exhentai.org" -> 2
|
||||
else -> throw MalformedURLException("Not a valid gallery URL!")
|
||||
}
|
||||
|
||||
val sourceObj = sourceManager.get(source)
|
||||
|
||||
val pathOnlyUrl = UrlUtil.getPath(url)
|
||||
|
||||
//Use manga in DB if possible, otherwise, make a new manga
|
||||
val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking()
|
||||
?: Manga.create(pathOnlyUrl, source).apply {
|
||||
title = url
|
||||
}
|
||||
|
||||
sourceObj?.let {
|
||||
//Copy basics
|
||||
manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first())
|
||||
|
||||
//Apply metadata
|
||||
metadataHelper.fetchMetadata(url, source == 2)?.copyTo(manga)
|
||||
}
|
||||
|
||||
if(fav) manga.favorite = true
|
||||
|
||||
db.insertManga(manga).executeAsBlocking().insertedId()?.let {
|
||||
manga.id = it
|
||||
}
|
||||
|
||||
return manga
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
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 com.afollestad.materialdialogs.MaterialDialog
|
||||
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
|
||||
|
||||
/**
|
||||
* LoginActivity
|
||||
*/
|
||||
|
||||
class BatchAddFragment : BaseFragment() {
|
||||
|
||||
private val galleryAdder by lazy { GalleryAdder() }
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?)
|
||||
= inflater.inflate(R.layout.eh_fragment_batch_add, container, false)!!
|
||||
|
||||
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()
|
||||
|
||||
//Too lazy to actually deal with orientation changes
|
||||
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
|
||||
|
||||
val splitGalleries = galleries.split("\n").map {
|
||||
it.trim().nullIfBlank()
|
||||
}.filterNotNull()
|
||||
|
||||
val dialog = MaterialDialog.Builder(context)
|
||||
.title("Adding galleries...")
|
||||
.progress(false, splitGalleries.size, true)
|
||||
.cancelable(false)
|
||||
.canceledOnTouchOutside(false)
|
||||
.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
|
||||
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: ${succeeded.size} gallerie(s)\nFailed: ${failed.size} 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, dialogAction -> materialDialog.dismiss() }
|
||||
.cancelable(true)
|
||||
.canceledOnTouchOutside(true)
|
||||
.show()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun newInstance() = BatchAddFragment()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package exh.ui.intercept
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
|
||||
import eu.kanade.tachiyomi.ui.manga.MangaActivity
|
||||
import exh.GalleryAdder
|
||||
import kotlinx.android.synthetic.main.toolbar.*
|
||||
import timber.log.Timber
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class InterceptActivity : BaseActivity() {
|
||||
|
||||
private val galleryAdder = GalleryAdder()
|
||||
|
||||
var finished = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
setAppTheme()
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.eh_activity_intercept)
|
||||
|
||||
setupToolbar(toolbar, backNavigation = false)
|
||||
|
||||
if(savedInstanceState == null)
|
||||
thread { setup() }
|
||||
}
|
||||
|
||||
fun setup() {
|
||||
try {
|
||||
processLink()
|
||||
} catch(t: Throwable) {
|
||||
Timber.e(t, "Could not intercept link!")
|
||||
if(!finished)
|
||||
runOnUiThread {
|
||||
MaterialDialog.Builder(this)
|
||||
.title("Error")
|
||||
.content("Could not load this gallery!")
|
||||
.cancelable(true)
|
||||
.canceledOnTouchOutside(true)
|
||||
.cancelListener { onBackPressed() }
|
||||
.positiveText("Ok")
|
||||
.onPositive { materialDialog, dialogAction -> onBackPressed() }
|
||||
.dismissListener { onBackPressed() }
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun processLink() {
|
||||
if(Intent.ACTION_VIEW == intent.action) {
|
||||
val manga = galleryAdder.addGallery(intent.dataString)
|
||||
|
||||
if(!finished)
|
||||
startActivity(MangaActivity.newIntent(this, manga, true))
|
||||
onBackPressed()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
android.R.id.home -> onBackPressed()
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if(!finished)
|
||||
runOnUiThread {
|
||||
super.onBackPressed()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
finished = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user