mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-03 23:58:55 +01:00 
			
		
		
		
	Rewrite link intercept activity
Fix compatibility issues between lewd sources and manga info screen
This commit is contained in:
		@@ -74,8 +74,10 @@ class FavoritesSyncHelper(val activity: Activity) {
 | 
			
		||||
 | 
			
		||||
        (exSource ?: ehSource)?.let { source ->
 | 
			
		||||
            val favResponse = source.fetchFavorites()
 | 
			
		||||
            val ourCategories = ArrayList<Category>(db.getCategories().executeAsBlocking())
 | 
			
		||||
            val ourMangas = ArrayList<Manga>(db.getMangas().executeAsBlocking())
 | 
			
		||||
            val ourCategories = db.getCategories().executeAsBlocking().toMutableList()
 | 
			
		||||
            val ourMangas = db.getMangas().executeAsBlocking().filter {
 | 
			
		||||
                it.source == EH_SOURCE_ID || it.source == EXH_SOURCE_ID
 | 
			
		||||
            }.toMutableList()
 | 
			
		||||
            //Add required categories (categories do not sync upwards)
 | 
			
		||||
            favResponse.second.filter { theirCategory ->
 | 
			
		||||
                ourCategories.find {
 | 
			
		||||
@@ -93,7 +95,7 @@ class FavoritesSyncHelper(val activity: Activity) {
 | 
			
		||||
                    val categoryMap = (it + ourCategories).associateBy { it.name }
 | 
			
		||||
 | 
			
		||||
                    //Insert new mangas
 | 
			
		||||
                    val mangaToInsert = java.util.ArrayList<Manga>()
 | 
			
		||||
                    val mangaToInsert = mutableListOf<Manga>()
 | 
			
		||||
                    favResponse.first.map {
 | 
			
		||||
                        val category = categoryMap[it.fav]!!
 | 
			
		||||
                        var manga = it.manga
 | 
			
		||||
 
 | 
			
		||||
@@ -83,3 +83,11 @@ fun buildTagsDescription(metadata: SearchableGalleryMetadata)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun joinTagsToGenreString(metadata: SearchableGalleryMetadata)
 | 
			
		||||
    = metadata.tags.joinToString { "${it.namespace}: ${it.name}" }
 | 
			
		||||
 | 
			
		||||
fun joinEmulatedTagsToGenreString(metadata: SearchableGalleryMetadata)
 | 
			
		||||
        = metadata.tags.filter { it.namespace == EMULATED_TAG_NAMESPACE }.joinToString { it.name.toString() }
 | 
			
		||||
 | 
			
		||||
val EMULATED_TAG_NAMESPACE = "tag"
 | 
			
		||||
@@ -4,10 +4,7 @@ import android.net.Uri
 | 
			
		||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 | 
			
		||||
import eu.kanade.tachiyomi.data.preference.getOrDefault
 | 
			
		||||
import eu.kanade.tachiyomi.source.model.SManga
 | 
			
		||||
import exh.metadata.EX_DATE_FORMAT
 | 
			
		||||
import exh.metadata.ONGOING_SUFFIX
 | 
			
		||||
import exh.metadata.buildTagsDescription
 | 
			
		||||
import exh.metadata.humanReadableByteCount
 | 
			
		||||
import exh.metadata.*
 | 
			
		||||
import exh.plusAssign
 | 
			
		||||
import io.realm.RealmList
 | 
			
		||||
import io.realm.RealmObject
 | 
			
		||||
@@ -110,12 +107,9 @@ open class ExGalleryMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        tags.filter { it.namespace == EH_ARTIST_NAMESPACE }.let {
 | 
			
		||||
            if(it.isNotEmpty()) manga.artist = it.joinToString(transform = { it.name!! })
 | 
			
		||||
        }
 | 
			
		||||
        //Set author (if we can find one)
 | 
			
		||||
        tags.filter { it.namespace == EH_AUTHOR_NAMESPACE }.let {
 | 
			
		||||
            if(it.isNotEmpty()) manga.author = it.joinToString(transform = { it.name!! })
 | 
			
		||||
        }
 | 
			
		||||
        //Set genre
 | 
			
		||||
        genre?.let { manga.genre = it }
 | 
			
		||||
 | 
			
		||||
        //Copy tags -> genres
 | 
			
		||||
        manga.genre = joinTagsToGenreString(this)
 | 
			
		||||
 | 
			
		||||
        //Try to automatically identify if it is ongoing, we try not to be too lenient here to avoid making mistakes
 | 
			
		||||
        //We default to completed
 | 
			
		||||
@@ -134,6 +128,7 @@ open class ExGalleryMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        altTitle?.let { titleDesc += "Alternate Title: $it\n" }
 | 
			
		||||
 | 
			
		||||
        val detailsDesc = StringBuilder()
 | 
			
		||||
        genre?.let { detailsDesc += "Genre: $it\n" }
 | 
			
		||||
        uploader?.let { detailsDesc += "Uploader: $it\n" }
 | 
			
		||||
        datePosted?.let { detailsDesc += "Posted: ${EX_DATE_FORMAT.format(Date(it))}\n" }
 | 
			
		||||
        visible?.let { detailsDesc += "Visible: $it\n" }
 | 
			
		||||
@@ -176,7 +171,6 @@ open class ExGalleryMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        private const val EH_ARTIST_NAMESPACE = "artist"
 | 
			
		||||
        private const val EH_AUTHOR_NAMESPACE = "author"
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,10 +3,7 @@ package exh.metadata.models
 | 
			
		||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 | 
			
		||||
import eu.kanade.tachiyomi.data.preference.getOrDefault
 | 
			
		||||
import eu.kanade.tachiyomi.source.model.SManga
 | 
			
		||||
import exh.metadata.EX_DATE_FORMAT
 | 
			
		||||
import exh.metadata.ONGOING_SUFFIX
 | 
			
		||||
import exh.metadata.buildTagsDescription
 | 
			
		||||
import exh.metadata.nullIfBlank
 | 
			
		||||
import exh.metadata.*
 | 
			
		||||
import exh.plusAssign
 | 
			
		||||
import io.realm.RealmList
 | 
			
		||||
import io.realm.RealmObject
 | 
			
		||||
@@ -106,10 +103,14 @@ open class NHentaiMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
            if(it.isNotEmpty()) manga.artist = it.joinToString(transform = { it.name!! })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var category: String? = null
 | 
			
		||||
        tags.filter { it.namespace == NHENTAI_CATEGORIES_NAMESPACE }.let {
 | 
			
		||||
            if(it.isNotEmpty()) manga.genre = it.joinToString(transform = { it.name!! })
 | 
			
		||||
            if(it.isNotEmpty()) category = it.joinToString(transform = { it.name!! })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Copy tags -> genres
 | 
			
		||||
        manga.genre = joinEmulatedTagsToGenreString(this)
 | 
			
		||||
 | 
			
		||||
        //Try to automatically identify if it is ongoing, we try not to be too lenient here to avoid making mistakes
 | 
			
		||||
        //We default to completed
 | 
			
		||||
        manga.status = SManga.COMPLETED
 | 
			
		||||
@@ -127,6 +128,7 @@ open class NHentaiMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        shortTitle?.let { titleDesc += "Short Title: $it\n" }
 | 
			
		||||
 | 
			
		||||
        val detailsDesc = StringBuilder()
 | 
			
		||||
        category?.let { detailsDesc += "Category: $it\n" }
 | 
			
		||||
        uploadDate?.let { detailsDesc += "Upload Date: ${EX_DATE_FORMAT.format(Date(it * 1000))}\n" }
 | 
			
		||||
        pageImageTypes.size.let { detailsDesc += "Length: $it pages\n" }
 | 
			
		||||
        favoritesCount?.let { detailsDesc += "Favorited: $it times\n" }
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.source.model.SManga
 | 
			
		||||
import exh.PERV_EDEN_EN_SOURCE_ID
 | 
			
		||||
import exh.PERV_EDEN_IT_SOURCE_ID
 | 
			
		||||
import exh.metadata.buildTagsDescription
 | 
			
		||||
import exh.metadata.joinEmulatedTagsToGenreString
 | 
			
		||||
import exh.plusAssign
 | 
			
		||||
import io.realm.RealmList
 | 
			
		||||
import io.realm.RealmObject
 | 
			
		||||
@@ -77,7 +78,6 @@ open class PervEdenGalleryMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        type?.let {
 | 
			
		||||
            manga.genre = it
 | 
			
		||||
            detailsDesc += "Type: $it\n"
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -94,6 +94,9 @@ open class PervEdenGalleryMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
            detailsDesc += "Rating: %.2\n".format(it)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Copy tags -> genres
 | 
			
		||||
        manga.genre = joinEmulatedTagsToGenreString(this)
 | 
			
		||||
 | 
			
		||||
        val tagsDesc = buildTagsDescription(this)
 | 
			
		||||
 | 
			
		||||
        manga.description = listOf(titleDesc.toString(), detailsDesc.toString(), tagsDesc.toString())
 | 
			
		||||
@@ -164,6 +167,7 @@ open class PervEdenTitle(var metadata: PervEdenGalleryMetadata? = null,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum class PervEdenLang(val id: Long) {
 | 
			
		||||
    //DO NOT RENAME THESE TO CAPITAL LETTERS! The enum names are used to build URLs
 | 
			
		||||
    en(PERV_EDEN_EN_SOURCE_ID),
 | 
			
		||||
    it(PERV_EDEN_IT_SOURCE_ID);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import android.net.Uri
 | 
			
		||||
import eu.kanade.tachiyomi.source.model.SManga
 | 
			
		||||
import exh.metadata.EX_DATE_FORMAT
 | 
			
		||||
import exh.metadata.buildTagsDescription
 | 
			
		||||
import exh.metadata.joinEmulatedTagsToGenreString
 | 
			
		||||
import exh.plusAssign
 | 
			
		||||
import io.realm.RealmList
 | 
			
		||||
import io.realm.RealmObject
 | 
			
		||||
@@ -96,7 +97,6 @@ open class TsuminoMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        length?.let { detailsDesc += "Length: $it pages\n" }
 | 
			
		||||
        ratingString?.let { detailsDesc += "Rating: $it\n" }
 | 
			
		||||
        category?.let {
 | 
			
		||||
            manga.genre = it
 | 
			
		||||
            detailsDesc += "Category: $it\n"
 | 
			
		||||
        }
 | 
			
		||||
        collection?.let { detailsDesc += "Collection: $it\n" }
 | 
			
		||||
@@ -109,7 +109,10 @@ open class TsuminoMetadata : RealmObject(), SearchableGalleryMetadata {
 | 
			
		||||
        if(charactersString.isNotEmpty()) {
 | 
			
		||||
            detailsDesc += "Character: $charactersString\n"
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        //Copy tags -> genres
 | 
			
		||||
        manga.genre = joinEmulatedTagsToGenreString(this)
 | 
			
		||||
 | 
			
		||||
        val tagsDesc = buildTagsDescription(this)
 | 
			
		||||
        
 | 
			
		||||
        manga.description = listOf(titleDesc, detailsDesc.toString(), tagsDesc.toString())
 | 
			
		||||
 
 | 
			
		||||
@@ -5,32 +5,19 @@ import android.os.Bundle
 | 
			
		||||
import android.view.MenuItem
 | 
			
		||||
import com.afollestad.materialdialogs.MaterialDialog
 | 
			
		||||
import eu.kanade.tachiyomi.R
 | 
			
		||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 | 
			
		||||
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
 | 
			
		||||
import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity
 | 
			
		||||
import eu.kanade.tachiyomi.ui.main.MainActivity
 | 
			
		||||
import eu.kanade.tachiyomi.ui.manga.MangaController
 | 
			
		||||
import exh.GalleryAddEvent
 | 
			
		||||
import exh.GalleryAdder
 | 
			
		||||
import kotlinx.android.synthetic.main.eh_activity_intercept.*
 | 
			
		||||
import uy.kohesive.injekt.injectLazy
 | 
			
		||||
import kotlin.concurrent.thread
 | 
			
		||||
import nucleus.factory.RequiresPresenter
 | 
			
		||||
import rx.Subscription
 | 
			
		||||
import rx.android.schedulers.AndroidSchedulers
 | 
			
		||||
 | 
			
		||||
class InterceptActivity : BaseActivity() {
 | 
			
		||||
 | 
			
		||||
    private val preferences: PreferencesHelper by injectLazy()
 | 
			
		||||
 | 
			
		||||
    private val galleryAdder = GalleryAdder()
 | 
			
		||||
 | 
			
		||||
    var finished = false
 | 
			
		||||
@RequiresPresenter(InterceptActivityPresenter::class)
 | 
			
		||||
class InterceptActivity : BaseRxActivity<InterceptActivityPresenter>() {
 | 
			
		||||
    private var statusSubscription: Subscription? = null
 | 
			
		||||
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        //Set theme
 | 
			
		||||
        setTheme(when (preferences.theme()) {
 | 
			
		||||
            2 -> R.style.Theme_Tachiyomi_Dark
 | 
			
		||||
            3 -> R.style.Theme_Tachiyomi_Amoled
 | 
			
		||||
            else -> R.style.Theme_Tachiyomi
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
        setContentView(R.layout.eh_activity_intercept)
 | 
			
		||||
 | 
			
		||||
@@ -38,37 +25,12 @@ class InterceptActivity : BaseActivity() {
 | 
			
		||||
        setSupportActionBar(toolbar)
 | 
			
		||||
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
 | 
			
		||||
 | 
			
		||||
        if(savedInstanceState == null)
 | 
			
		||||
            thread { processLink() }
 | 
			
		||||
        processLink()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun processLink() {
 | 
			
		||||
        if(Intent.ACTION_VIEW == intent.action) {
 | 
			
		||||
            val result = galleryAdder.addGallery(intent.dataString)
 | 
			
		||||
 | 
			
		||||
            when(result) {
 | 
			
		||||
                is GalleryAddEvent.Success ->
 | 
			
		||||
                    if(!finished)
 | 
			
		||||
                        startActivity(Intent(this, MainActivity::class.java)
 | 
			
		||||
                                .setAction(MainActivity.SHORTCUT_MANGA)
 | 
			
		||||
                                .putExtra(MangaController.MANGA_EXTRA, result.manga.id))
 | 
			
		||||
                is GalleryAddEvent.Fail ->
 | 
			
		||||
                    if(!finished)
 | 
			
		||||
                        runOnUiThread {
 | 
			
		||||
                            MaterialDialog.Builder(this)
 | 
			
		||||
                                    .title("Error")
 | 
			
		||||
                                    .content("Could not open this gallery:\n\n${result.logMessage}")
 | 
			
		||||
                                    .cancelable(true)
 | 
			
		||||
                                    .canceledOnTouchOutside(true)
 | 
			
		||||
                                    .cancelListener { onBackPressed() }
 | 
			
		||||
                                    .positiveText("Ok")
 | 
			
		||||
                                    .onPositive { _, _ -> onBackPressed() }
 | 
			
		||||
                                    .dismissListener { onBackPressed() }
 | 
			
		||||
                                    .show()
 | 
			
		||||
                        }
 | 
			
		||||
            }
 | 
			
		||||
            onBackPressed()
 | 
			
		||||
        }
 | 
			
		||||
        if(Intent.ACTION_VIEW == intent.action)
 | 
			
		||||
            presenter.loadGallery(intent.dataString)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
 | 
			
		||||
@@ -79,15 +41,36 @@ class InterceptActivity : BaseActivity() {
 | 
			
		||||
        return true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onBackPressed() {
 | 
			
		||||
        if(!finished)
 | 
			
		||||
            runOnUiThread {
 | 
			
		||||
                super.onBackPressed()
 | 
			
		||||
            }
 | 
			
		||||
    override fun onStart() {
 | 
			
		||||
        super.onStart()
 | 
			
		||||
        statusSubscription?.unsubscribe()
 | 
			
		||||
        statusSubscription = presenter.status
 | 
			
		||||
                .observeOn(AndroidSchedulers.mainThread())
 | 
			
		||||
                .subscribe {
 | 
			
		||||
                    when(it) {
 | 
			
		||||
                        is InterceptResult.Success -> {
 | 
			
		||||
                            startActivity(Intent(this, MainActivity::class.java)
 | 
			
		||||
                                    .setAction(MainActivity.SHORTCUT_MANGA)
 | 
			
		||||
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
 | 
			
		||||
                                    .putExtra(MangaController.MANGA_EXTRA, it.mangaId))
 | 
			
		||||
                            onBackPressed()
 | 
			
		||||
                        }
 | 
			
		||||
                        is InterceptResult.Failure ->
 | 
			
		||||
                            MaterialDialog.Builder(this)
 | 
			
		||||
                                    .title("Error")
 | 
			
		||||
                                    .content("Could not open this gallery:\n\n${it.reason}")
 | 
			
		||||
                                    .cancelable(true)
 | 
			
		||||
                                    .canceledOnTouchOutside(true)
 | 
			
		||||
                                    .positiveText("Ok")
 | 
			
		||||
                                    .cancelListener { onBackPressed() }
 | 
			
		||||
                                    .dismissListener { onBackPressed() }
 | 
			
		||||
                                    .show()
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onStop() {
 | 
			
		||||
        super.onStop()
 | 
			
		||||
        finished = true
 | 
			
		||||
        statusSubscription?.unsubscribe()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,40 @@
 | 
			
		||||
package exh.ui.intercept
 | 
			
		||||
 | 
			
		||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
 | 
			
		||||
import exh.GalleryAddEvent
 | 
			
		||||
import exh.GalleryAdder
 | 
			
		||||
import rx.subjects.BehaviorSubject
 | 
			
		||||
import kotlin.concurrent.thread
 | 
			
		||||
 | 
			
		||||
class InterceptActivityPresenter : BasePresenter<InterceptActivity>() {
 | 
			
		||||
    private val galleryAdder = GalleryAdder()
 | 
			
		||||
 | 
			
		||||
    val status = BehaviorSubject.create<InterceptResult>(InterceptResult.Idle())
 | 
			
		||||
 | 
			
		||||
    @Synchronized
 | 
			
		||||
    fun loadGallery(gallery: String) {
 | 
			
		||||
        //Do not load gallery if already loading
 | 
			
		||||
        if(status.value is InterceptResult.Idle) {
 | 
			
		||||
            status.onNext(InterceptResult.Loading())
 | 
			
		||||
 | 
			
		||||
            //Load gallery async
 | 
			
		||||
            thread {
 | 
			
		||||
                val result = galleryAdder.addGallery(gallery)
 | 
			
		||||
 | 
			
		||||
                status.onNext(when (result) {
 | 
			
		||||
                    is GalleryAddEvent.Success -> result.manga.id?.let {
 | 
			
		||||
                        InterceptResult.Success(it)
 | 
			
		||||
                    } ?: InterceptResult.Failure("Manga ID is null!")
 | 
			
		||||
                    is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
 | 
			
		||||
                })
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sealed class InterceptResult {
 | 
			
		||||
    class Idle : InterceptResult()
 | 
			
		||||
    class Loading : InterceptResult()
 | 
			
		||||
    data class Success(val mangaId: Long): InterceptResult()
 | 
			
		||||
    data class Failure(val reason: String): InterceptResult()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user