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

@@ -74,14 +74,14 @@ dependencies {
implementation "io.ktor:ktor-client-serialization:1.6.1" implementation "io.ktor:ktor-client-serialization:1.6.1"
implementation "androidx.appcompat:appcompat:1.3.0" implementation "androidx.appcompat:appcompat:1.3.0"
implementation "androidx.activity:activity-ktx:1.3.0-rc01" implementation "androidx.activity:activity-ktx:1.3.0-rc02"
implementation "androidx.fragment:fragment-ktx:1.3.5" implementation "androidx.fragment:fragment-ktx:1.3.6"
implementation "androidx.preference:preference-ktx:1.1.1" implementation "androidx.preference:preference-ktx:1.1.1"
implementation "androidx.recyclerview:recyclerview:1.2.1" implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "androidx.constraintlayout:constraintlayout:2.0.4" implementation "androidx.constraintlayout:constraintlayout:2.0.4"
implementation "androidx.gridlayout:gridlayout:1.0.0" implementation "androidx.gridlayout:gridlayout:1.0.0"
implementation "androidx.biometric:biometric:1.1.0" implementation "androidx.biometric:biometric:1.1.0"
implementation "androidx.work:work-runtime-ktx:2.6.0-beta01" implementation "androidx.work:work-runtime-ktx:2.6.0-beta02"
implementation 'org.kodein.di:kodein-di-framework-android-x:7.6.0' implementation 'org.kodein.di:kodein-di-framework-android-x:7.6.0'
@@ -104,7 +104,7 @@ dependencies {
implementation 'com.github.piasy:FrescoImageLoader:1.8.0' implementation 'com.github.piasy:FrescoImageLoader:1.8.0'
implementation 'com.github.piasy:FrescoImageViewFactory:1.8.0' implementation 'com.github.piasy:FrescoImageViewFactory:1.8.0'
implementation "org.jsoup:jsoup:1.13.1" implementation "org.jsoup:jsoup:1.14.1"
implementation "com.tbuonomo:dotsindicator:4.2" implementation "com.tbuonomo:dotsindicator:4.2"
@@ -120,7 +120,7 @@ dependencies {
debugImplementation "com.orhanobut:logger:2.2.0" debugImplementation "com.orhanobut:logger:2.2.0"
debugImplementation "com.squareup.leakcanary:leakcanary-android:2.6" debugImplementation "com.squareup.leakcanary:leakcanary-android:2.6"
testImplementation "junit:junit:4.13.1" testImplementation "junit:junit:4.13.2"
testImplementation "org.mockito:mockito-inline:3.11.2" testImplementation "org.mockito:mockito-inline:3.11.2"
androidTestImplementation "androidx.test.ext:junit:1.1.3" androidTestImplementation "androidx.test.ext:junit:1.1.3"

View File

@@ -107,25 +107,21 @@ data class ItemInfo(
} }
} }
enum class DefaultSortMode : SortModeInterface {
DEFAULT
}
@Parcelize @Parcelize
class DefaultSearchSuggestion(override val body: String) : SearchSuggestion class DefaultSearchSuggestion(override val body: String) : SearchSuggestion
interface SortModeInterface { interface SortModeInterface {
val ordinal: Int val ordinal: Int
val name: String val name: Int
} }
abstract class Source { abstract class Source {
abstract val name: String abstract val name: String
abstract val iconResID: Int abstract val iconResID: Int
abstract val preferenceID: 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 suggestion(query: String) : List<SearchSuggestion>
abstract suspend fun images(itemID: String) : List<String> abstract suspend fun images(itemID: String) : List<String>
abstract suspend fun info(itemID: String) : ItemInfo abstract suspend fun info(itemID: String) : ItemInfo
@@ -146,11 +142,13 @@ val sourceModule = DI.Module(name = "source") {
bindSet<SourceEntry>() bindSet<SourceEntry>()
bindSet<SourcePreferenceID>() bindSet<SourcePreferenceID>()
listOf<Source>( onReady {
Hitomi() listOf<Source>(
).forEach { source -> Hitomi(instance())
inSet { multiton { _: Unit -> source.name to source } } ).forEach { source ->
inSet { singleton { source.name to source.preferenceID } } inSet { multiton { _: Unit -> source.name to source } }
inSet { singleton { source.name to source.preferenceID } }
}
} }
bind { factory { source: String -> History(di, source) } } 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 get() = R.drawable.ic_download
override val preferenceID: Int override val preferenceID: Int
get() = R.xml.download_preferences 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() 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 downloads = downloadManager.downloads.toList()
val channel = Channel<ItemInfo>() val channel = Channel<ItemInfo>()

View File

@@ -40,9 +40,9 @@ class History(override val di: DI, source: String) : Source(), DIAware {
get() = source.iconResID get() = source.iconResID
override val preferenceID: Int override val preferenceID: Int
get() = source.preferenceID 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>() val channel = Channel<ItemInfo>()
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {

View File

@@ -18,6 +18,7 @@
package xyz.quaver.pupil.sources package xyz.quaver.pupil.sources
import android.app.Application
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.TextView import android.widget.TextView
import io.ktor.http.* import io.ktor.http.*
@@ -35,12 +36,7 @@ import xyz.quaver.pupil.util.wordCapitalize
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
class Hitomi : Source() { class Hitomi(app: Application) : Source() {
enum class SortMode : SortModeInterface {
NEWEST,
POPULAR
}
@Parcelize @Parcelize
data class TagSuggestion(val s: String, val t: Int, val u: String, val n: String) : SearchSuggestion { 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 name: String = "hitomi.la"
override val iconResID: Int = R.drawable.hitomi override val iconResID: Int = R.drawable.hitomi
override val preferenceID: Int = R.xml.hitomi_preferences 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 cachedQuery: String? = null
var cachedSortMode: SortMode? = null var cachedSortMode: Int = -1
val cache = mutableListOf<Int>() 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()) { if (cachedQuery != query || cachedSortMode != sortMode || cache.isEmpty()) {
cachedQuery = null cachedQuery = null
cache.clear() cache.clear()
yield() yield()
doSearch("$query ${Preferences["hitomi.default_query", ""]}", sortMode == SortMode.POPULAR).let { doSearch("$query ${Preferences["hitomi.default_query", ""]}", sortMode == 1).let {
yield() yield()
cache.addAll(it) 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 androidx.swiperefreshlayout.widget.CircularProgressDrawable
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter import com.daimajia.swipe.adapters.RecyclerSwipeAdapter
import com.google.android.material.navigation.NavigationView import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.orhanobut.logger.Logger import com.orhanobut.logger.Logger
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.kodein.di.DIAware import org.kodein.di.DIAware
@@ -96,8 +95,18 @@ class MainActivity :
binding.contents.searchview.binding.querySection.menuView.menuItems.findMenu(R.id.sort).subMenu.apply { binding.contents.searchview.binding.querySection.menuView.menuItems.findMenu(R.id.sort).subMenu.apply {
clear() clear()
it.forEach { it.forEachIndexed { index, sortMode ->
add(R.id.sort_mode_group_id, it.ordinal, Menu.NONE, it.name) 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) setGroupCheckable(R.id.sort_mode_group_id, true, true)
@@ -417,32 +426,23 @@ class MainActivity :
} }
private fun onActionMenuItemSelected(item: MenuItem?) { private fun onActionMenuItemSelected(item: MenuItem?) {
if (item?.groupId == R.id.sort_mode_group_id) { when(item?.itemId) {
model.setPage(1) R.id.main_menu_settings -> startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
model.sortMode.value = model.availableSortMode.value?.let { availableSortMode -> R.id.source -> SourceSelectDialog().apply {
availableSortMode.getOrElse(item.itemId) { availableSortMode.first() } 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 { override fun onNavigationItemSelected(item: MenuItem): Boolean {

View File

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