Refactor and cleanup a bunch of code
This commit is contained in:
@@ -4,7 +4,7 @@ import eu.kanade.tachiyomi.source.model.FilterList
|
||||
import eu.kanade.tachiyomi.source.online.all.MangaDex
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.Pager
|
||||
import exh.source.EnhancedHttpSource.Companion.getMainSource
|
||||
import exh.source.getMainSource
|
||||
|
||||
/**
|
||||
* Presenter of [MangaDexFollowsController]. Inherit BrowseCataloguePresenter.
|
||||
|
||||
@@ -15,6 +15,7 @@ import exh.metadata.metadata.base.RaisedTag
|
||||
import exh.metadata.metadata.base.getFlatMetadataForManga
|
||||
import exh.metadata.metadata.base.insertFlatMetadata
|
||||
import exh.util.floor
|
||||
import exh.util.nullIfZero
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
@@ -126,7 +127,7 @@ class ApiMangaParser(private val langs: List<String>) {
|
||||
genres.add("Hentai")
|
||||
}
|
||||
|
||||
if (tags.size != 0) tags.clear()
|
||||
if (tags.isNotEmpty()) tags.clear()
|
||||
tags += genres.map { RaisedTag(null, it, MangaDexSearchMetadata.TAG_TYPE_DEFAULT) }
|
||||
} catch (e: Exception) {
|
||||
XLog.e(e)
|
||||
@@ -157,10 +158,8 @@ class ApiMangaParser(private val langs: List<String>) {
|
||||
}
|
||||
}
|
||||
val removeOneshots = filteredChapters.asSequence()
|
||||
.map { it.value.chapter!!.toDoubleOrNull() }
|
||||
.filter { it != null }
|
||||
.map { it!!.floor() }
|
||||
.filter { it != 0 }
|
||||
.map { it.value.chapter?.toDoubleOrNull()?.floor()?.nullIfZero() }
|
||||
.filterNotNull()
|
||||
.toList().distinctBy { it }
|
||||
return removeOneshots.toList().size == finalChapterNumber.toDouble().floor()
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.track.TrackManager
|
||||
import eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.network.POST
|
||||
import eu.kanade.tachiyomi.network.asObservable
|
||||
import eu.kanade.tachiyomi.network.await
|
||||
import eu.kanade.tachiyomi.source.model.MangasPage
|
||||
import eu.kanade.tachiyomi.source.model.MetadataMangasPage
|
||||
import eu.kanade.tachiyomi.source.model.SManga
|
||||
@@ -14,8 +15,6 @@ import exh.md.handlers.serializers.FollowsPageResult
|
||||
import exh.md.handlers.serializers.Result
|
||||
import exh.md.utils.FollowStatus
|
||||
import exh.md.utils.MdUtil
|
||||
import exh.md.utils.MdUtil.Companion.baseUrl
|
||||
import exh.md.utils.MdUtil.Companion.getMangaId
|
||||
import exh.metadata.metadata.MangaDexSearchMetadata
|
||||
import exh.util.floor
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -101,7 +100,7 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
|
||||
if (result[0].chapter.isNotBlank()) {
|
||||
track.last_chapter_read = follow.chapter.toFloat().floor()
|
||||
}
|
||||
track.tracking_url = baseUrl + follow.manga_id.toString()
|
||||
track.tracking_url = MdUtil.baseUrl + follow.manga_id.toString()
|
||||
track.title = follow.title
|
||||
}
|
||||
return track
|
||||
@@ -111,7 +110,7 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
|
||||
*
|
||||
*/
|
||||
private fun followsListRequest(): Request {
|
||||
return GET("$baseUrl${MdUtil.followsAllApi}", headers, CacheControl.FORCE_NETWORK)
|
||||
return GET("${MdUtil.baseUrl}${MdUtil.followsAllApi}", headers, CacheControl.FORCE_NETWORK)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,66 +138,66 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
|
||||
if (followStatus == FollowStatus.UNFOLLOWED) {
|
||||
client.newCall(
|
||||
GET(
|
||||
"$baseUrl/ajax/actions.ajax.php?function=manga_unfollow&id=$mangaID&type=$mangaID",
|
||||
"${MdUtil.baseUrl}/ajax/actions.ajax.php?function=manga_unfollow&id=$mangaID&type=$mangaID",
|
||||
headers,
|
||||
CacheControl.FORCE_NETWORK
|
||||
)
|
||||
)
|
||||
.execute()
|
||||
.await()
|
||||
} else {
|
||||
val status = followStatus.int
|
||||
client.newCall(
|
||||
GET(
|
||||
"$baseUrl/ajax/actions.ajax.php?function=manga_follow&id=$mangaID&type=$status",
|
||||
"${MdUtil.baseUrl}/ajax/actions.ajax.php?function=manga_follow&id=$mangaID&type=$status",
|
||||
headers,
|
||||
CacheControl.FORCE_NETWORK
|
||||
)
|
||||
)
|
||||
.execute()
|
||||
.await()
|
||||
}
|
||||
|
||||
response.body!!.string().isEmpty()
|
||||
withContext(Dispatchers.IO) { response.body!!.string().isEmpty() }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateReadingProgress(track: Track): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val mangaID = getMangaId(track.tracking_url)
|
||||
val mangaID = MdUtil.getMangaId(track.tracking_url)
|
||||
val formBody = FormBody.Builder()
|
||||
.add("chapter", track.last_chapter_read.toString())
|
||||
XLog.d("chapter to update %s", track.last_chapter_read.toString())
|
||||
val response = client.newCall(
|
||||
POST(
|
||||
"$baseUrl/ajax/actions.ajax.php?function=edit_progress&id=$mangaID",
|
||||
"${MdUtil.baseUrl}/ajax/actions.ajax.php?function=edit_progress&id=$mangaID",
|
||||
headers,
|
||||
formBody.build()
|
||||
)
|
||||
).execute()
|
||||
).await()
|
||||
|
||||
val response2 = client.newCall(
|
||||
client.newCall(
|
||||
GET(
|
||||
"$baseUrl/ajax/actions.ajax.php?function=manga_rating&id=$mangaID&rating=${track.score.toInt()}",
|
||||
"${MdUtil.baseUrl}/ajax/actions.ajax.php?function=manga_rating&id=$mangaID&rating=${track.score.toInt()}",
|
||||
headers
|
||||
)
|
||||
)
|
||||
.execute()
|
||||
.await()
|
||||
|
||||
response.body!!.string().isEmpty()
|
||||
withContext(Dispatchers.IO) { response.body!!.string().isEmpty() }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateRating(track: Track): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val mangaID = getMangaId(track.tracking_url)
|
||||
val mangaID = MdUtil.getMangaId(track.tracking_url)
|
||||
val response = client.newCall(
|
||||
GET(
|
||||
"$baseUrl/ajax/actions.ajax.php?function=manga_rating&id=$mangaID&rating=${track.score.toInt()}",
|
||||
"${MdUtil.baseUrl}/ajax/actions.ajax.php?function=manga_rating&id=$mangaID&rating=${track.score.toInt()}",
|
||||
headers
|
||||
)
|
||||
)
|
||||
.execute()
|
||||
.await()
|
||||
|
||||
response.body!!.string().isEmpty()
|
||||
withContext(Dispatchers.IO) { response.body!!.string().isEmpty() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +207,7 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
|
||||
suspend fun fetchAllFollows(forceHd: Boolean): List<Pair<SManga, MangaDexSearchMetadata>> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val listManga = mutableListOf<Pair<SManga, MangaDexSearchMetadata>>()
|
||||
val response = client.newCall(followsListRequest()).execute()
|
||||
val response = client.newCall(followsListRequest()).await()
|
||||
val mangasPage = followsParseMangaPage(response, forceHd)
|
||||
listManga.addAll(
|
||||
mangasPage.mangas.mapIndexed { index, sManga ->
|
||||
@@ -222,11 +221,11 @@ class FollowsHandler(val client: OkHttpClient, val headers: Headers, val prefere
|
||||
suspend fun fetchTrackingInfo(url: String): Track {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val request = GET(
|
||||
"$baseUrl${MdUtil.followsMangaApi}" + getMangaId(url),
|
||||
"${MdUtil.baseUrl}${MdUtil.followsMangaApi}" + MdUtil.getMangaId(url),
|
||||
headers,
|
||||
CacheControl.FORCE_NETWORK
|
||||
)
|
||||
val response = client.newCall(request).execute()
|
||||
val response = client.newCall(request).await()
|
||||
val track = followStatusParse(response)
|
||||
|
||||
track
|
||||
|
||||
@@ -22,10 +22,10 @@ class MangaHandler(val client: OkHttpClient, val headers: Headers, val langs: Li
|
||||
// TODO make use of this
|
||||
suspend fun fetchMangaAndChapterDetails(manga: SManga): Pair<SManga, List<SChapter>> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val response = client.newCall(apiRequest(manga)).execute()
|
||||
val response = client.newCall(apiRequest(manga)).await()
|
||||
val parser = ApiMangaParser(langs)
|
||||
|
||||
val jsonData = response.body!!.string()
|
||||
val jsonData = withContext(Dispatchers.IO) { response.body!!.string() }
|
||||
if (response.code != 200) {
|
||||
XLog.e("error from MangaDex with response code ${response.code} \n body: \n$jsonData")
|
||||
throw Exception("Error from MangaDex Response code ${response.code} ")
|
||||
@@ -43,14 +43,14 @@ class MangaHandler(val client: OkHttpClient, val headers: Headers, val langs: Li
|
||||
suspend fun getMangaIdFromChapterId(urlChapterId: String): Int {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val request = GET(MdUtil.baseUrl + MdUtil.apiChapter + urlChapterId + MdUtil.apiChapterSuffix, headers, CacheControl.FORCE_NETWORK)
|
||||
val response = client.newCall(request).execute()
|
||||
val response = client.newCall(request).await()
|
||||
ApiMangaParser(langs).chapterParseForMangaId(response)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun fetchMangaDetails(manga: SManga): SManga {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val response = client.newCall(apiRequest(manga)).execute()
|
||||
val response = client.newCall(apiRequest(manga)).await()
|
||||
ApiMangaParser(langs).parseToManga(manga, response, forceLatestCovers).await()
|
||||
manga.apply {
|
||||
initialized = true
|
||||
@@ -82,7 +82,7 @@ class MangaHandler(val client: OkHttpClient, val headers: Headers, val langs: Li
|
||||
|
||||
suspend fun fetchChapterList(manga: SManga): List<SChapter> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val response = client.newCall(apiRequest(manga)).execute()
|
||||
val response = client.newCall(apiRequest(manga)).await()
|
||||
ApiMangaParser(langs).chapterListParse(response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.source.model.FilterList
|
||||
import eu.kanade.tachiyomi.source.online.all.MangaDex
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter
|
||||
import eu.kanade.tachiyomi.ui.browse.source.browse.Pager
|
||||
import exh.source.EnhancedHttpSource.Companion.getMainSource
|
||||
import exh.source.getMainSource
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,9 @@ import eu.kanade.tachiyomi.source.SourceManager
|
||||
import eu.kanade.tachiyomi.source.model.SChapter
|
||||
import eu.kanade.tachiyomi.source.model.SManga
|
||||
import eu.kanade.tachiyomi.source.online.all.MangaDex
|
||||
import exh.source.getMainSource
|
||||
import exh.util.floor
|
||||
import exh.util.nullIfZero
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.jsoup.parser.Parser
|
||||
import uy.kohesive.injekt.Injekt
|
||||
@@ -14,6 +16,7 @@ import uy.kohesive.injekt.api.get
|
||||
import java.net.URI
|
||||
import java.net.URISyntaxException
|
||||
|
||||
@Suppress("unused")
|
||||
class MdUtil {
|
||||
|
||||
companion object {
|
||||
@@ -201,8 +204,8 @@ class MdUtil {
|
||||
/*}*/
|
||||
}.sortedByDescending { it.chapter_number }
|
||||
|
||||
remove0ChaptersFromCount.firstOrNull()?.let {
|
||||
val chpNumber = it.chapter_number.floor()
|
||||
remove0ChaptersFromCount.firstOrNull()?.let { chapter ->
|
||||
val chpNumber = chapter.chapter_number.floor()
|
||||
val allChapters = (1..chpNumber).toMutableSet()
|
||||
|
||||
remove0ChaptersFromCount.forEach {
|
||||
@@ -217,8 +220,9 @@ class MdUtil {
|
||||
|
||||
fun getEnabledMangaDex(preferences: PreferencesHelper = Injekt.get(), sourceManager: SourceManager = Injekt.get()): MangaDex? {
|
||||
return getEnabledMangaDexs(preferences, sourceManager).let { mangadexs ->
|
||||
val preferredMangaDexId = preferences.preferredMangaDexId().get().toLongOrNull()
|
||||
mangadexs.firstOrNull { preferredMangaDexId != null && preferredMangaDexId != 0L && it.id == preferredMangaDexId } ?: mangadexs.firstOrNull()
|
||||
preferences.preferredMangaDexId().get().toLongOrNull()?.nullIfZero()?.let { preferredMangaDexId ->
|
||||
mangadexs.firstOrNull { it.id == preferredMangaDexId }
|
||||
} ?: mangadexs.firstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,10 +230,11 @@ class MdUtil {
|
||||
val languages = preferences.enabledLanguages().get()
|
||||
val disabledSourceIds = preferences.disabledSources().get()
|
||||
|
||||
return sourceManager.getDelegatedCatalogueSources()
|
||||
return sourceManager.getVisibleOnlineSources()
|
||||
.map { it.getMainSource() }
|
||||
.filterIsInstance<MangaDex>()
|
||||
.filter { it.lang in languages }
|
||||
.filterNot { it.id.toString() in disabledSourceIds }
|
||||
.filterIsInstance(MangaDex::class.java)
|
||||
}
|
||||
|
||||
fun mapMdIdToMangaUrl(id: Int) = "/manga/$id/"
|
||||
|
||||
Reference in New Issue
Block a user