Reimplemented sort

[WIP] ImHentai
This commit is contained in:
tom5079
2021-07-24 09:48:54 +09:00
parent 8cc89101e7
commit 35ee438376
12 changed files with 179 additions and 63 deletions

View File

@@ -107,25 +107,21 @@ data class ItemInfo(
}
}
enum class DefaultSortMode : SortModeInterface {
DEFAULT
}
@Parcelize
class DefaultSearchSuggestion(override val body: String) : SearchSuggestion
interface SortModeInterface {
val ordinal: Int
val name: String
val name: Int
}
abstract class Source {
abstract val name: String
abstract val iconResID: Int
abstract val preferenceID: Int
abstract val availableSortMode: List<SortModeInterface>
abstract val availableSortMode: List<String>
abstract suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface) : Pair<Channel<ItemInfo>, Int>
abstract suspend fun search(query: String, range: IntRange, sortMode: Int) : 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
@@ -146,11 +142,13 @@ val sourceModule = DI.Module(name = "source") {
bindSet<SourceEntry>()
bindSet<SourcePreferenceID>()
listOf<Source>(
Hitomi()
).forEach { source ->
inSet { multiton { _: Unit -> source.name to source } }
inSet { singleton { source.name to source.preferenceID } }
onReady {
listOf<Source>(
Hitomi(instance())
).forEach { source ->
inSet { multiton { _: Unit -> source.name to source } }
inSet { singleton { source.name to source.preferenceID } }
}
}
bind { factory { source: String -> History(di, source) } }

View File

@@ -41,11 +41,11 @@ class Downloads(override val di: DI) : Source(), DIAware {
get() = R.drawable.ic_download
override val preferenceID: Int
get() = R.xml.download_preferences
override val availableSortMode: List<DefaultSortMode> = DefaultSortMode.values().toList()
override val availableSortMode: List<String> = emptyList()
private val downloadManager: DownloadManager by instance()
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: Int): Pair<Channel<ItemInfo>, Int> {
val downloads = downloadManager.downloads.toList()
val channel = Channel<ItemInfo>()

View File

@@ -40,9 +40,9 @@ class History(override val di: DI, source: String) : Source(), DIAware {
get() = source.iconResID
override val preferenceID: Int
get() = source.preferenceID
override val availableSortMode: List<DefaultSortMode> = DefaultSortMode.values().toList()
override val availableSortMode: List<String> = emptyList()
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: Int): Pair<Channel<ItemInfo>, Int> {
val channel = Channel<ItemInfo>()
CoroutineScope(Dispatchers.IO).launch {

View File

@@ -18,6 +18,7 @@
package xyz.quaver.pupil.sources
import android.app.Application
import android.view.LayoutInflater
import android.widget.TextView
import io.ktor.http.*
@@ -35,12 +36,7 @@ import xyz.quaver.pupil.util.wordCapitalize
import kotlin.math.max
import kotlin.math.min
class Hitomi : Source() {
enum class SortMode : SortModeInterface {
NEWEST,
POPULAR
}
class Hitomi(app: Application) : Source() {
@Parcelize
data class TagSuggestion(val s: String, val t: Int, val u: String, val n: String) : SearchSuggestion {
@@ -60,18 +56,18 @@ class Hitomi : Source() {
override val name: String = "hitomi.la"
override val iconResID: Int = R.drawable.hitomi
override val preferenceID: Int = R.xml.hitomi_preferences
override val availableSortMode: List<SortModeInterface> = SortMode.values().toList()
override val availableSortMode: List<String> = app.resources.getStringArray(R.array.hitomi_sort_mode).toList()
var cachedQuery: String? = null
var cachedSortMode: SortMode? = null
var cachedSortMode: Int = -1
val cache = mutableListOf<Int>()
override suspend fun search(query: String, range: IntRange, sortMode: SortModeInterface): Pair<Channel<ItemInfo>, Int> {
override suspend fun search(query: String, range: IntRange, sortMode: Int): Pair<Channel<ItemInfo>, Int> {
if (cachedQuery != query || cachedSortMode != sortMode || cache.isEmpty()) {
cachedQuery = null
cache.clear()
yield()
doSearch("$query ${Preferences["hitomi.default_query", ""]}", sortMode == SortMode.POPULAR).let {
doSearch("$query ${Preferences["hitomi.default_query", ""]}", sortMode == 1).let {
yield()
cache.addAll(it)
}

View File

@@ -0,0 +1,88 @@
/*
* Pupil, Hitomi.la viewer for Android
* Copyright (C) 2021 tom5079
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package xyz.quaver.pupil.sources
import android.app.Application
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.features.get
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
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
class ImHentai(override val di: DI) : Source(), DIAware {
private val app: Application by instance()
private val client: HttpClient by instance()
override val name: String
get() = ImHentai.name
override val iconResID: Int
get() = R.drawable.ic_imhentai
override val preferenceID: Int
get() = R.xml.imhentai_preferences
override val availableSortMode = app.resources.getStringArray(R.array.imhentai_sort_mode).toList()
override suspend fun search(query: String, range: IntRange, sortMode: Int): Pair<Channel<ItemInfo>, Int> = withContext(Dispatchers.IO) {
val channel = Channel<ItemInfo>()
val doc = Jsoup.connect("https://imhentai.xxx/search/?key=$query").get()
val count = countRegex.find(doc.getElementsByClass("heading2").text())?.groupValues?.get(1)?.toIntOrNull() ?: 0
launch {
doc.getElementsByClass("thumb")
}
return@withContext Pair(channel, count)
}
override suspend fun suggestion(query: String): List<SearchSuggestion> {
TODO("Not yet implemented")
}
override suspend fun images(itemID: String): List<String> {
TODO("Not yet implemented")
}
override suspend fun info(itemID: String): ItemInfo {
TODO("Not yet implemented")
}
companion object {
private const val name = "imhentai"
private val countRegex = Regex("""\(\d+\) results found.""")
private val idRegex = Regex("""/gallery/(\d+)/""")
private fun transform(item: Element) {
val caption = item.select(".caption a")
}
}
}

View File

@@ -41,7 +41,6 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.CircularProgressDrawable
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.orhanobut.logger.Logger
import kotlinx.coroutines.*
import org.kodein.di.DIAware
@@ -96,8 +95,18 @@ class MainActivity :
binding.contents.searchview.binding.querySection.menuView.menuItems.findMenu(R.id.sort).subMenu.apply {
clear()
it.forEach {
add(R.id.sort_mode_group_id, it.ordinal, Menu.NONE, it.name)
it.forEachIndexed { index, sortMode ->
add(R.id.sort_mode_group_id, index, Menu.NONE, sortMode).setOnMenuItemClickListener {
model.setPage(1)
model.sortModeIndex.value = it.itemId
children.forEachIndexed { menuIndex, menuItem ->
menuItem.isChecked = menuIndex == index
}
model.query()
true
}
}
setGroupCheckable(R.id.sort_mode_group_id, true, true)
@@ -417,32 +426,23 @@ class MainActivity :
}
private fun onActionMenuItemSelected(item: MenuItem?) {
if (item?.groupId == R.id.sort_mode_group_id) {
model.setPage(1)
model.sortMode.value = model.availableSortMode.value?.let { availableSortMode ->
availableSortMode.getOrElse(item.itemId) { availableSortMode.first() }
}
when(item?.itemId) {
R.id.main_menu_settings -> startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
R.id.source -> SourceSelectDialog().apply {
onSourceSelectedListener = {
model.setSourceAndReset(it)
model.query()
dismiss()
}
onSourceSettingsSelectedListener = {
startActivity(Intent(this@MainActivity, SettingsActivity::class.java).putExtra(SettingsActivity.SETTINGS_EXTRA, it))
refreshOnResume = true
dismiss()
}
}.show(supportFragmentManager, null)
}
else
when(item?.itemId) {
R.id.main_menu_settings -> startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
R.id.source -> SourceSelectDialog().apply {
onSourceSelectedListener = {
model.setSourceAndReset(it)
dismiss()
}
onSourceSettingsSelectedListener = {
startActivity(Intent(this@MainActivity, SettingsActivity::class.java).putExtra(SettingsActivity.SETTINGS_EXTRA, it))
refreshOnResume = true
dismiss()
}
}.show(supportFragmentManager, null)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {

View File

@@ -60,7 +60,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
val availableSortMode = Transformations.map(_source) {
it.availableSortMode
}
val sortMode = MutableLiveData<SortModeInterface>()
val sortModeIndex = MutableLiveData<Int>()
val sourceIcon = Transformations.map(_source) {
it.iconResID
@@ -86,7 +87,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
fun setSourceAndReset(sourceName: String) {
_source.value = sourceFactory(sourceName).also {
sortMode.value = it.availableSortMode.first()
sortModeIndex.value = 0
}
setQueryAndSearch()
@@ -119,7 +120,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
fun query() {
val perPage = Preferences["per_page", "25"].toInt()
val source = _source.value ?: error("Source is null")
val sortMode = sortMode.value ?: source.availableSortMode.first()
val sortModeIndex = sortModeIndex.value ?: 0
val currentPage = currentPage.value ?: 1
suggestionJob?.cancel()
@@ -134,7 +135,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
val (channel, count) = source.search(
query.value ?: "",
(currentPage - 1) * perPage until currentPage * perPage,
sortMode
sortModeIndex
)
totalItems.postValue(count)
@@ -168,7 +169,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
_source.value?.search(
query.value + Preferences["default_query", ""],
random .. random,
sortMode.value!!
sortModeIndex.value!!
)?.first?.receive()
}?.let(callback)
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@@ -19,4 +19,16 @@
<item>SOCKS</item>
</string-array>
<string-array name="hitomi_sort_mode">
<item>NEWEST</item>
<item>POPULAR</item>
</string-array>
<string-array name="imhentai_sort_mode">
<item>LATEST</item>
<item>DOWNLOADED</item>
<item>POPULAR</item>
<item>TOP RATED</item>
</string-array>
</resources>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Pupil, Hitomi.la viewer for Android
~ Copyright (C) 2021 tom5079
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>