Simplify Source definition

This commit is contained in:
tom5079
2021-07-03 23:22:41 +09:00
parent 32d49833d8
commit a9a07ddcfa
9 changed files with 45 additions and 44 deletions

View File

@@ -106,22 +106,26 @@ data class ItemInfo(
}
}
enum class DefaultSortMode {
enum class DefaultSortMode : SortModeInterface {
DEFAULT
}
@Parcelize
class DefaultSearchSuggestion(override val body: String) : SearchSuggestion
typealias AnySource = Source<*, SearchSuggestion>
abstract class Source<Query_SortMode: Enum<Query_SortMode>, Suggestion: SearchSuggestion> {
interface SortModeInterface {
val ordinal: Int
val name: String
}
abstract class Source {
abstract val name: String
abstract val iconResID: Int
abstract val preferenceID: Int
abstract val availableSortMode: Array<Query_SortMode>
abstract val availableSortMode: List<SortModeInterface>
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 search(query: String, range: IntRange, sortMode: SortModeInterface) : Pair<Channel<ItemInfo>, Int>
abstract suspend fun suggestion(query: String) : List<SearchSuggestion>
abstract suspend fun images(itemID: String) : List<String>
abstract suspend fun info(itemID: String) : ItemInfo
@@ -129,12 +133,12 @@ abstract class Source<Query_SortMode: Enum<Query_SortMode>, Suggestion: SearchSu
return emptyMap()
}
open fun onSuggestionBind(binding: SearchSuggestionItemBinding, item: Suggestion) {
open fun onSuggestionBind(binding: SearchSuggestionItemBinding, item: SearchSuggestion) {
binding.leftIcon.setImageResource(R.drawable.tag)
}
}
typealias SourceEntry = Pair<String, AnySource>
typealias SourceEntry = Pair<String, Source>
typealias SourceEntries = Set<SourceEntry>
typealias SourcePreferenceID = Pair<String, Int>
typealias SourcePreferenceIDs = Set<SourcePreferenceID>
@@ -143,13 +147,13 @@ val sourceModule = DI.Module(name = "source") {
bindSet<SourceEntry>()
bindSet<SourcePreferenceID>()
listOf(
listOf<Source>(
Hitomi()
).forEach { source ->
inSet { multiton { _: Unit -> source.name to (source as AnySource) } }
inSet { multiton { _: Unit -> source.name to source } }
inSet { singleton { source.name to source.preferenceID } }
}
bind { factory { source: String -> History(di, source) } }
inSet { singleton { Downloads(di).let { it.name to (it as AnySource) } } }
inSet { singleton { Downloads(di).let { it.name to it as Source } } }
}

View File

@@ -33,7 +33,7 @@ import xyz.quaver.pupil.util.DownloadManager
import kotlin.math.max
import kotlin.math.min
class Downloads(override val di: DI) : Source<DefaultSortMode, SearchSuggestion>(), DIAware {
class Downloads(override val di: DI) : Source(), DIAware {
override val name: String
get() = "downloads"
@@ -41,11 +41,11 @@ class Downloads(override val di: DI) : Source<DefaultSortMode, SearchSuggestion>
get() = R.drawable.ic_download
override val preferenceID: Int
get() = R.xml.download_preferences
override val availableSortMode: Array<DefaultSortMode> = DefaultSortMode.values()
override val availableSortMode: List<DefaultSortMode> = DefaultSortMode.values().toList()
private val downloadManager: DownloadManager by instance()
override suspend fun search(query: String, range: IntRange, sortMode: Enum<*>): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
val downloads = downloadManager.downloads.toList()
val channel = Channel<ItemInfo>()

View File

@@ -26,13 +26,12 @@ import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.instance
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
import xyz.quaver.pupil.R
import xyz.quaver.pupil.util.SavedSourceSet
import xyz.quaver.pupil.util.source
class History(override val di: DI, source: String) : Source<DefaultSortMode, SearchSuggestion>(), DIAware {
class History(override val di: DI, source: String) : Source(), DIAware {
private val source: AnySource by source(source)
private val source: Source by source(source)
private val histories: SavedSourceSet by instance(tag = "histories")
override val name: String
@@ -41,9 +40,9 @@ class History(override val di: DI, source: String) : Source<DefaultSortMode, Sea
get() = source.iconResID
override val preferenceID: Int
get() = source.preferenceID
override val availableSortMode: Array<DefaultSortMode> = DefaultSortMode.values()
override val availableSortMode: List<DefaultSortMode> = DefaultSortMode.values().toList()
override suspend fun search(query: String, range: IntRange, sortMode: Enum<*>): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
val channel = Channel<ItemInfo>()
CoroutineScope(Dispatchers.IO).launch {

View File

@@ -35,9 +35,9 @@ import xyz.quaver.pupil.util.wordCapitalize
import kotlin.math.max
import kotlin.math.min
class Hitomi : Source<Hitomi.SortMode, Hitomi.TagSuggestion>() {
class Hitomi : Source() {
enum class SortMode {
enum class SortMode : SortModeInterface {
NEWEST,
POPULAR
}
@@ -58,13 +58,13 @@ class Hitomi : Source<Hitomi.SortMode, Hitomi.TagSuggestion>() {
override val name: String = "hitomi.la"
override val iconResID: Int = R.drawable.hitomi
override val preferenceID: Int = R.xml.hitomi_preferences
override val availableSortMode: Array<SortMode> = SortMode.values()
override val availableSortMode: List<SortModeInterface> = SortMode.values().toList()
var cachedQuery: String? = null
var cachedSortMode: SortMode? = null
val cache = mutableListOf<Int>()
override suspend fun search(query: String, range: IntRange, sortMode: Enum<*>): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
if (cachedQuery != query || cachedSortMode != sortMode || cache.isEmpty()) {
cachedQuery = null
cache.clear()
@@ -143,7 +143,9 @@ class Hitomi : Source<Hitomi.SortMode, Hitomi.TagSuggestion>() {
)
}
override fun onSuggestionBind(binding: SearchSuggestionItemBinding, item: TagSuggestion) {
override fun onSuggestionBind(binding: SearchSuggestionItemBinding, item: SearchSuggestion) {
item as TagSuggestion
binding.leftIcon.setImageResource(
when(item.n) {
"female" -> R.drawable.gender_female