This commit is contained in:
tom5079
2021-01-09 19:18:26 +09:00
parent c8aa26e2d9
commit 619730e2ab
32 changed files with 825 additions and 1385 deletions

View File

@@ -29,6 +29,10 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import org.kodein.di.DI
import org.kodein.di.bind
import org.kodein.di.contexted
import org.kodein.di.instance
import xyz.quaver.floatingsearchview.databinding.SearchSuggestionItemBinding
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
import xyz.quaver.pupil.R
@@ -110,6 +114,7 @@ enum class DefaultSortMode {
@Parcelize
class DefaultSearchSuggestion(override val body: String) : SearchSuggestion
typealias AnySource = Source<*, SearchSuggestion>
abstract class Source<Query_SortMode: Enum<Query_SortMode>, Suggestion: SearchSuggestion> {
abstract val name: String
abstract val iconResID: Int
@@ -117,10 +122,10 @@ abstract class Source<Query_SortMode: Enum<Query_SortMode>, Suggestion: SearchSu
abstract suspend fun search(query: String, range: IntRange, sortMode: Enum<*>) : Pair<Channel<ItemInfo>, Int>
abstract suspend fun suggestion(query: String) : List<Suggestion>
abstract suspend fun images(id: String) : List<String>
abstract suspend fun info(id: String) : ItemInfo
abstract suspend fun images(itemID: String) : List<String>
abstract suspend fun info(itemID: String) : ItemInfo
open fun getHeadersForImage(id: String, url: String): Map<String, String> {
open fun getHeadersForImage(itemID: String, url: String): Map<String, String> {
return emptyMap()
}
@@ -129,9 +134,21 @@ abstract class Source<Query_SortMode: Enum<Query_SortMode>, Suggestion: SearchSu
}
}
val sources = mutableMapOf<String, Source<*, SearchSuggestion>>()
@Deprecated("")
val sources = mutableMapOf<String, AnySource>()
val sourceIcons = mutableMapOf<String, Drawable?>()
@Suppress("UNCHECKED_CAST")
val sourceModule = DI.Module(name = "source") {
listOf(
Hitomi(),
Hiyobi()
).forEach {
bind<AnySource>(tag = it.name) with instance (it as AnySource)
}
}
@Deprecated("")
@Suppress("UNCHECKED_CAST")
fun initSources(context: Context) {
// Add Default Sources
@@ -139,7 +156,7 @@ fun initSources(context: Context) {
Hitomi(),
Hiyobi()
).forEach {
sources[it.name] = it as Source<*, SearchSuggestion>
sources[it.name] = it as AnySource
sourceIcons[it.name] = ContextCompat.getDrawable(context, it.iconResID)
}
}

View File

@@ -98,8 +98,8 @@ class Hitomi : Source<Hitomi.SortMode, Hitomi.TagSuggestion>() {
}
}
override suspend fun images(id: String): List<String> {
val galleryID = id.toInt()
override suspend fun images(itemID: String): List<String> {
val galleryID = itemID.toInt()
val reader = getGalleryInfo(galleryID)
@@ -108,32 +108,36 @@ class Hitomi : Source<Hitomi.SortMode, Hitomi.TagSuggestion>() {
}
}
override suspend fun info(id: String): ItemInfo = coroutineScope {
getGallery(id.toInt()).let {
ItemInfo(
name,
id,
it.title,
it.cover,
it.artists.joinToString { it.wordCapitalize() },
mapOf(
ExtraType.TYPE to async { it.type.wordCapitalize() },
ExtraType.GROUP to async { it.groups.joinToString { it.wordCapitalize() } },
ExtraType.LANGUAGE to async { languageMap[it.language] ?: it.language },
ExtraType.SERIES to async { it.series.joinToString { it.wordCapitalize() } },
ExtraType.CHARACTER to async { it.characters.joinToString { it.wordCapitalize() } },
ExtraType.TAGS to async { it.tags.joinToString() },
ExtraType.PREVIEW to async { it.thumbnails.joinToString() },
ExtraType.RELATED_ITEM to async { it.related.joinToString() },
ExtraType.PAGECOUNT to async { it.thumbnails.size.toString() },
override suspend fun info(itemID: String): ItemInfo = coroutineScope {
kotlin.runCatching {
getGallery(itemID.toInt()).let {
ItemInfo(
name,
itemID,
it.title,
it.cover,
it.artists.joinToString { it.wordCapitalize() },
mapOf(
ExtraType.TYPE to async { it.type.wordCapitalize() },
ExtraType.GROUP to async { it.groups.joinToString { it.wordCapitalize() } },
ExtraType.LANGUAGE to async { languageMap[it.language] ?: it.language },
ExtraType.SERIES to async { it.series.joinToString { it.wordCapitalize() } },
ExtraType.CHARACTER to async { it.characters.joinToString { it.wordCapitalize() } },
ExtraType.TAGS to async { it.tags.joinToString() },
ExtraType.PREVIEW to async { it.thumbnails.joinToString() },
ExtraType.RELATED_ITEM to async { it.related.joinToString() },
ExtraType.PAGECOUNT to async { it.thumbnails.size.toString() },
)
)
)
}
}.getOrElse {
transform(name, getGalleryBlock(itemID.toInt()))
}
}
override fun getHeadersForImage(id: String, url: String): Map<String, String> {
override fun getHeadersForImage(itemID: String, url: String): Map<String, String> {
return mapOf(
"Referer" to getReferer(id.toInt())
"Referer" to getReferer(itemID.toInt())
)
}

View File

@@ -72,14 +72,14 @@ class Hiyobi : Source<DefaultSortMode, DefaultSearchSuggestion>() {
return result.map { DefaultSearchSuggestion(it) }
}
override suspend fun images(id: String): List<String> {
return createImgList(id, getGalleryInfo(id), true).map {
override suspend fun images(itemID: String): List<String> {
return createImgList(itemID, getGalleryInfo(itemID), false).map {
it.path
}
}
override suspend fun info(id: String): ItemInfo {
return transform(name, getGalleryBlock(id))
override suspend fun info(itemID: String): ItemInfo {
return transform(name, getGalleryBlock(itemID))
}
override fun onSuggestionBind(binding: SearchSuggestionItemBinding, item: DefaultSearchSuggestion) {
@@ -105,7 +105,7 @@ class Hiyobi : Source<DefaultSortMode, DefaultSearchSuggestion>() {
}
companion object {
private fun downloadAllTags(): Deferred<List<String>> = CoroutineScope(Dispatchers.IO).async {
private fun downloadAllTagsAsync(): Deferred<List<String>> = CoroutineScope(Dispatchers.IO).async {
Json.decodeFromString(kotlin.runCatching {
client.newCall(Request.Builder().url("https://api.hiyobi.me/auto.json").build()).execute().also { if (it.code() != 200) throw IOException() }.body()?.use { it.string() }
}.getOrNull() ?: "[]")
@@ -114,7 +114,7 @@ class Hiyobi : Source<DefaultSortMode, DefaultSearchSuggestion>() {
private var _allTags: Deferred<List<String>>? = null
val allTags: Deferred<List<String>>
get() = if (_allTags == null || (_allTags!!.isCompleted && runBlocking { _allTags!!.await() }.isEmpty())) downloadAllTags().also {
get() = if (_allTags == null || (_allTags!!.isCompleted && runBlocking { _allTags!!.await() }.isEmpty())) downloadAllTagsAsync().also {
_allTags = it
} else _allTags!!