Add search button

This commit is contained in:
Blatzar 2022-08-13 05:53:35 +02:00
parent 34131bdf62
commit b7a11048f0
4 changed files with 83 additions and 12 deletions

View file

@ -100,7 +100,9 @@ dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.5.0' // dont change this to 1.6.0 it looks ugly af
// dont change this to 1.6.0 it looks ugly af
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.1'
@ -187,6 +189,8 @@ dependencies {
implementation 'com.github.TeamNewPipe:NewPipeExtractor:master-SNAPSHOT'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
// Library/extensions searching with Levenshtein distance
implementation 'me.xdrop:fuzzywuzzy:1.4.0'
}
task androidSourcesJar(type: Jar) {
@ -195,8 +199,9 @@ task androidSourcesJar(type: Jar) {
}
task makeJar(type: Copy) {
from('build/intermediates/compile_app_classes_jar/debug') // after modifying here, you can export. Jar
into('build') // output location
include('classes.jar') // the classes file of the imported rack package
dependsOn build
// after modifying here, you can export. Jar
from('build/intermediates/compile_app_classes_jar/debug')
into('build') // output location
include('classes.jar') // the classes file of the imported rack package
dependsOn build
}

View file

@ -1,8 +1,8 @@
package com.lagradost.cloudstream3.ui.settings.extensions
import android.content.Context
import android.os.Bundle
import android.view.*
import androidx.appcompat.view.menu.MenuBuilder
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.lagradost.cloudstream3.R
@ -49,6 +49,40 @@ class PluginsFragment : Fragment() {
return@setOnMenuItemClickListener true
}
val searchView =
settings_toolbar?.menu?.findItem(R.id.search_button)?.actionView as? RepoSearchView
// Don't go back if active query
settings_toolbar?.setNavigationOnClickListener {
if (searchView?.isIconified == false) {
searchView?.isIconified = true
} else {
activity?.onBackPressed()
}
}
// searchView?.onActionViewCollapsed = {
// pluginViewModel.search(null)
// }
// Because onActionViewCollapsed doesn't wanna work we need this workaround :(
searchView?.setOnQueryTextFocusChangeListener { _, hasFocus ->
if (!hasFocus) pluginViewModel.search(null)
}
searchView?.setOnQueryTextListener(object : android.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
pluginViewModel.search(query)
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
pluginViewModel.search(newText)
return true
}
})
plugin_recycler_view?.adapter =
PluginAdapter {
pluginViewModel.handlePluginAction(activity, url, it, isLocal)
@ -56,11 +90,12 @@ class PluginsFragment : Fragment() {
observe(pluginViewModel.plugins) {
(plugin_recycler_view?.adapter as? PluginAdapter?)?.updateList(it)
plugin_recycler_view?.scrollToPosition(0)
}
if (isLocal) {
// No download button
settings_toolbar?.menu?.clear()
settings_toolbar?.menu?.findItem(R.id.download_all)?.isVisible = false
pluginViewModel.updatePluginListLocal()
} else {
pluginViewModel.updatePluginList(url)
@ -75,5 +110,14 @@ class PluginsFragment : Fragment() {
putBoolean(PLUGINS_BUNDLE_LOCAL, isLocal)
}
}
class RepoSearchView(context: Context) : android.widget.SearchView(context) {
// var onActionViewCollapsed = {}
//
// override fun onActionViewCollapsed() {
// onActionViewCollapsed()
// }
}
}
}

View file

@ -19,6 +19,7 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.Coroutines.runOnMainThread
import kotlinx.coroutines.launch
import me.xdrop.fuzzywuzzy.FuzzySearch
typealias Plugin = Pair<String, SitePlugin>
@ -168,6 +169,20 @@ class PluginsViewModel : ViewModel() {
updatePluginListPrivate(repositoryUrl)
}
fun search(query: String?) {
val currentPlugins = plugins.value ?: return
// Return list to base state if no query
val newValue = if (query == null) {
currentPlugins.sortedBy { it.plugin.second.name }
} else {
currentPlugins.sortedBy {
-FuzzySearch.ratio(it.plugin.second.name, query)
}
}
_plugins.postValue(newValue)
}
/**
* Update the list but only with the local data. Used for file management.

View file

@ -1,9 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:id="@+id/download_all"
android:icon="@drawable/netflix_download"
android:title="@string/batch_download" />
android:id="@+id/search_button"
android:icon="@drawable/search_icon"
android:title="@string/title_search"
app:actionViewClass="com.lagradost.cloudstream3.ui.settings.extensions.PluginsFragment$Companion$RepoSearchView"
app:searchHintIcon="@drawable/search_icon"
app:showAsAction="ifRoom" />
<item
android:id="@+id/download_all"
android:icon="@drawable/netflix_download"
android:title="@string/batch_download"
app:showAsAction="collapseActionView|ifRoom" />
</menu>