fix: surround filter requests with try-catch to avoid crash (#11997)

* fix: surround filter requests with try-catch to avoid crash

* Manga Cross: use Date's property syntax
This commit is contained in:
kasperskier 2022-05-28 09:49:43 +08:00 committed by GitHub
parent 41142bfc87
commit 50f1eb1770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.multisrc.sinmh
import android.util.Log
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.source.model.Filter
import eu.kanade.tachiyomi.source.model.FilterList
@ -120,6 +121,21 @@ abstract class SinMH(
}
private lateinit var categories: List<Category>
private var isFetchingCategories = false
private fun tryFetchCategories() {
if (isFetchingCategories) return
isFetchingCategories = true
thread {
try {
fetchCategories()
} catch (e: Exception) {
Log.e("SinMH", "Failed to fetch categories ($e)")
} finally {
isFetchingCategories = false
}
}
}
protected open fun fetchCategories() {
val document = client.newCall(GET("$baseUrl/list/", headers)).execute().asJsoup()
@ -133,16 +149,17 @@ abstract class SinMH(
}
init {
thread {
fetchCategories()
}
tryFetchCategories()
}
override fun getFilterList() =
if (::categories.isInitialized) FilterList(
Filter.Header("如果使用文本搜索,将会忽略分类筛选"),
*categories.map(Category::toUriPartFilter).toTypedArray()
) else FilterList(
Filter.Header("分类尚未获取,请返回上一页后重试")
)
) else {
tryFetchCategories()
FilterList(
Filter.Header("分类尚未获取,请返回上一页后重试")
)
}
}

View File

@ -6,7 +6,7 @@ import generator.ThemeSourceGenerator
class SinMHGenerator : ThemeSourceGenerator {
override val themeClass = "SinMH"
override val themePkg = "sinmh"
override val baseVersionCode = 1
override val baseVersionCode = 2
override val sources = listOf(
SingleLang(
name = "Gufeng Manhua", baseUrl = "https://www.gufengmh9.com", lang = "zh",

View File

@ -6,7 +6,7 @@ ext {
extName = 'Manga Cross'
pkgNameSuffix = 'ja.mangacross'
extClass = '.MangaCross'
extVersionCode = 1
extVersionCode = 2
}
apply from: "$rootDir/common.gradle"

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.ja.mangacross
import android.util.Log
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.asObservableSuccess
import eu.kanade.tachiyomi.source.model.Filter
@ -79,22 +80,38 @@ class MangaCross : HttpSource() {
override fun imageUrlParse(response: Response) = throw UnsupportedOperationException("Not used.")
private lateinit var tags: List<Pair<String, MCComicTag?>>
private var isFetchingTags = false
private fun fetchTags() {
if (isFetchingTags) return
isFetchingTags = true
thread {
try {
val response = client.newCall(GET("$baseUrl/api/menus.json", headers)).execute()
val filterList = json.decodeFromString<MCMenu>(response.body!!.string()).toFilterList()
tags = listOf(Pair("None", null)) + filterList
} catch (e: Exception) {
Log.e("MangaCross", "Failed to fetch filters ($e)")
} finally {
isFetchingTags = false
}
}
}
init {
thread {
val response = client.newCall(GET("$baseUrl/api/menus.json", headers)).execute()
val filterList = json.decodeFromString<MCMenu>(response.body!!.string()).toFilterList()
tags = listOf(Pair("None", null)) + filterList
}
fetchTags()
}
override fun getFilterList() =
if (::tags.isInitialized) FilterList(
Filter.Header("NOTE: Ignored if using text search!"),
TagFilter("Tag", tags)
) else FilterList(
Filter.Header("Tags not fetched yet. Go back and retry."),
)
) else {
fetchTags()
FilterList(
Filter.Header("Tags not fetched yet. Go back and retry."),
)
}
private class TagFilter(displayName: String, private val tags: List<Pair<String, MCComicTag?>>) :
Filter.Select<String>(displayName, tags.map { it.first }.toTypedArray()) {

View File

@ -128,8 +128,9 @@ data class MCEpisode(
fun getNextDatePrefix(): String? = when {
!episode_next_date.isNullOrEmpty() -> {
val date = parseJSTDate(episode_next_date)
date.setTime(date.getTime() + 10 * 3600 * 1000) // 10 am JST
val date = parseJSTDate(episode_next_date).apply {
time += 10 * 3600 * 1000 // 10 am JST
}
"【Next: ${localDate.format(date)}"
}
!next_date_customize_text.isNullOrEmpty() -> "$next_date_customize_text】"