Simplify Source definition
This commit is contained in:
@@ -23,7 +23,6 @@ import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
|
||||
import xyz.quaver.pupil.databinding.SourceSelectDialogItemBinding
|
||||
import xyz.quaver.pupil.sources.AnySource
|
||||
import xyz.quaver.pupil.sources.Source
|
||||
import xyz.quaver.pupil.sources.SourceEntries
|
||||
|
||||
@@ -35,7 +34,7 @@ class SourceAdapter(sources: SourceEntries) : RecyclerView.Adapter<SourceAdapter
|
||||
private val sources = sources.toList()
|
||||
|
||||
inner class ViewHolder(private val binding: SourceSelectDialogItemBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||
lateinit var source: AnySource
|
||||
lateinit var source: Source
|
||||
|
||||
init {
|
||||
binding.go.setOnClickListener {
|
||||
@@ -46,7 +45,7 @@ class SourceAdapter(sources: SourceEntries) : RecyclerView.Adapter<SourceAdapter
|
||||
}
|
||||
}
|
||||
|
||||
fun bind(source: AnySource) {
|
||||
fun bind(source: Source) {
|
||||
this.source = source
|
||||
|
||||
// TODO: save image somewhere else
|
||||
|
||||
@@ -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 } } }
|
||||
}
|
||||
@@ -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>()
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -26,8 +26,8 @@ import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.*
|
||||
import org.kodein.di.DIAware
|
||||
import org.kodein.di.android.x.closestDI
|
||||
import xyz.quaver.pupil.sources.AnySource
|
||||
import xyz.quaver.pupil.sources.ItemInfo
|
||||
import xyz.quaver.pupil.sources.Source
|
||||
import xyz.quaver.pupil.util.source
|
||||
|
||||
class GalleryDialogViewModel(app: Application) : AndroidViewModel(app), DIAware {
|
||||
@@ -41,7 +41,7 @@ class GalleryDialogViewModel(app: Application) : AndroidViewModel(app), DIAware
|
||||
val related: LiveData<List<ItemInfo>> = _related
|
||||
|
||||
fun load(source: String, itemID: String) {
|
||||
val source: AnySource by source(source)
|
||||
val source: Source by source(source)
|
||||
|
||||
viewModelScope.launch {
|
||||
_info.value = withContext(Dispatchers.IO) {
|
||||
|
||||
@@ -23,14 +23,11 @@ import android.app.Application
|
||||
import androidx.lifecycle.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.kodein.di.DIAware
|
||||
import org.kodein.di.android.x.di
|
||||
import org.kodein.di.android.x.closestDI
|
||||
import org.kodein.di.direct
|
||||
import org.kodein.di.instance
|
||||
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
|
||||
import xyz.quaver.pupil.sources.AnySource
|
||||
import xyz.quaver.pupil.sources.Downloads
|
||||
import xyz.quaver.pupil.sources.History
|
||||
import xyz.quaver.pupil.sources.ItemInfo
|
||||
import xyz.quaver.pupil.sources.*
|
||||
import xyz.quaver.pupil.util.Preferences
|
||||
import xyz.quaver.pupil.util.notify
|
||||
import xyz.quaver.pupil.util.source
|
||||
@@ -40,7 +37,7 @@ import kotlin.random.Random
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
|
||||
override val di by di()
|
||||
override val di by closestDI()
|
||||
|
||||
private val _searchResults = MutableLiveData<MutableList<ItemInfo>>()
|
||||
val searchResults = _searchResults as LiveData<List<ItemInfo>>
|
||||
@@ -53,17 +50,17 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
|
||||
val query = MutableLiveData<String>()
|
||||
private val queryStack = mutableListOf<String>()
|
||||
|
||||
private val defaultSourceFactory: (String) -> AnySource = {
|
||||
private val defaultSourceFactory: (String) -> Source = {
|
||||
direct.source(it)
|
||||
}
|
||||
private var sourceFactory: (String) -> AnySource = defaultSourceFactory
|
||||
private val _source = MutableLiveData<AnySource>()
|
||||
val source: LiveData<AnySource> = _source
|
||||
private var sourceFactory: (String) -> Source = defaultSourceFactory
|
||||
private val _source = MutableLiveData<Source>()
|
||||
val source: LiveData<Source> = _source
|
||||
|
||||
val availableSortMode = Transformations.map(_source) {
|
||||
it.availableSortMode
|
||||
}
|
||||
val sortMode = MutableLiveData<Enum<*>>()
|
||||
val sortMode = MutableLiveData<SortModeInterface>()
|
||||
|
||||
val sourceIcon = Transformations.map(_source) {
|
||||
it.iconResID
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.kodein.di.DIAware
|
||||
import org.kodein.di.android.x.closestDI
|
||||
import org.kodein.di.instance
|
||||
import xyz.quaver.pupil.adapters.ReaderItem
|
||||
import xyz.quaver.pupil.sources.AnySource
|
||||
import xyz.quaver.pupil.sources.Source
|
||||
import xyz.quaver.pupil.util.ImageCache
|
||||
import xyz.quaver.pupil.util.notify
|
||||
import xyz.quaver.pupil.util.source
|
||||
@@ -56,7 +56,7 @@ class ReaderViewModel(app: Application) : AndroidViewModel(app), DIAware {
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun load(sourceName: String, itemID: String) {
|
||||
val source: AnySource by source(sourceName)
|
||||
val source: Source by source(sourceName)
|
||||
|
||||
viewModelScope.launch {
|
||||
_title.value = withContext(Dispatchers.IO) {
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.kodein.di.DIAware
|
||||
import org.kodein.di.android.closestDI
|
||||
import xyz.quaver.io.FileX
|
||||
import xyz.quaver.io.util.*
|
||||
import xyz.quaver.pupil.sources.AnySource
|
||||
import xyz.quaver.pupil.sources.Source
|
||||
|
||||
class DownloadManager constructor(context: Context) : ContextWrapper(context), DIAware {
|
||||
|
||||
@@ -82,7 +82,7 @@ class DownloadManager constructor(context: Context) : ContextWrapper(context), D
|
||||
|
||||
@Synchronized
|
||||
fun download(source: String, itemID: String) = CoroutineScope(Dispatchers.IO).launch {
|
||||
val source: AnySource by source(source)
|
||||
val source: Source by source(source)
|
||||
val info = async { source.info(itemID) }
|
||||
val images = async { source.images(itemID) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user