Use API Search Filter

This commit is contained in:
Bnyro 2022-05-15 13:26:51 +02:00
parent 4ed137c174
commit 5207486c36
2 changed files with 27 additions and 30 deletions

View File

@ -33,6 +33,7 @@ import java.io.IOException
class SearchFragment : Fragment() { class SearchFragment : Fragment() {
private val TAG = "SearchFragment" private val TAG = "SearchFragment"
private var selectedFilter = 0 private var selectedFilter = 0
private var apiSearchFilter = "all"
private var nextPage : String? = null private var nextPage : String? = null
private lateinit var searchRecView : RecyclerView private lateinit var searchRecView : RecyclerView
private var searchAdapter : SearchAdapter? = null private var searchAdapter : SearchAdapter? = null
@ -72,8 +73,15 @@ class SearchFragment : Fragment() {
.setSingleChoiceItems(options, selectedFilter, DialogInterface.OnClickListener { .setSingleChoiceItems(options, selectedFilter, DialogInterface.OnClickListener {
_, id -> tempSelectedItem = id _, id -> tempSelectedItem = id
}) })
.setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { .setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { _, _ ->
_, _ -> selectedFilter = tempSelectedItem selectedFilter = tempSelectedItem
apiSearchFilter = when (selectedFilter) {
0 -> "all"
1 -> "videos"
2 -> "channels"
3 -> "playlists"
else -> "all"
}
fetchSearch(autoTextView.text.toString()) fetchSearch(autoTextView.text.toString())
}) })
.setNegativeButton(getString(R.string.cancel), null) .setNegativeButton(getString(R.string.cancel), null)
@ -177,7 +185,7 @@ class SearchFragment : Fragment() {
private fun fetchSearch(query: String){ private fun fetchSearch(query: String){
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getSearchResults(query, "videos") RetrofitInstance.api.getSearchResults(query, apiSearchFilter)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection $e") Log.e(TAG, "IOException, you might not have internet connection $e")
@ -189,7 +197,7 @@ class SearchFragment : Fragment() {
nextPage = response.nextpage nextPage = response.nextpage
if(response.items!!.isNotEmpty()){ if(response.items!!.isNotEmpty()){
runOnUiThread { runOnUiThread {
searchAdapter = SearchAdapter(response.items, selectedFilter) searchAdapter = SearchAdapter(response.items)
searchRecView.adapter = searchAdapter searchRecView.adapter = searchAdapter
} }
} }
@ -200,7 +208,7 @@ class SearchFragment : Fragment() {
private fun fetchNextSearchItems(query: String){ private fun fetchNextSearchItems(query: String){
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getSearchResultsNextPage(query!!, "videos", nextPage!!) RetrofitInstance.api.getSearchResultsNextPage(query!!, apiSearchFilter, nextPage!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -14,16 +14,11 @@ import com.github.libretube.MainActivity
import com.github.libretube.PlayerFragment import com.github.libretube.PlayerFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.formatShort import com.github.libretube.formatShort
import com.github.libretube.obj.Comment
import com.github.libretube.obj.SearchItem import com.github.libretube.obj.SearchItem
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import kotlinx.coroutines.NonDisposableHandle.parent
private var showVideos = true
private var showChannels = true
private var showPlaylists = true
class SearchAdapter(private val searchItems: MutableList<SearchItem>, private val selectedFilter : Int): RecyclerView.Adapter<CustomViewHolder1>() { class SearchAdapter(private val searchItems: MutableList<SearchItem>): RecyclerView.Adapter<CustomViewHolder1>() {
fun updateItems(newItems: List<SearchItem>){ fun updateItems(newItems: List<SearchItem>){
var searchItemsSize = searchItems.size var searchItemsSize = searchItems.size
@ -36,20 +31,11 @@ class SearchAdapter(private val searchItems: MutableList<SearchItem>, private va
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder1 { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder1 {
when (selectedFilter) { val layout = when (viewType) {
0 -> { showChannels = true; showVideos = true; showPlaylists = true } 0 -> R.layout.video_search_row
1 -> { showChannels = false; showVideos = true; showPlaylists = false } 1 -> R.layout.channel_search_row
2 -> { showChannels = true; showVideos = false; showPlaylists = false } 2 -> R.layout.playlist_search_row
3 -> { showChannels = false; showVideos = false; showPlaylists = true } else -> throw IllegalArgumentException("Invalid type")
}
val layout = if (viewType == 0 && showVideos) {
R.layout.video_search_row
} else if (viewType == 1 && showChannels){
R.layout.channel_search_row
} else if (viewType == 2 && showPlaylists) {
R.layout.playlist_search_row
} else {
R.layout.layout_empty
} }
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(layout,parent,false) val cell = layoutInflater.inflate(layout,parent,false)
@ -141,9 +127,12 @@ class CustomViewHolder1(private val v: View): RecyclerView.ViewHolder(v){
} }
fun bind(searchItem: SearchItem) { fun bind(searchItem: SearchItem) {
if (searchItem.url!!.startsWith("/watch",false) && showVideos) bindWatch(searchItem) when {
else if (searchItem.url!!.startsWith("/channel",false) && showChannels) bindChannel(searchItem) searchItem.url!!.startsWith("/watch",false) -> bindWatch(searchItem)
else if (searchItem.url!!.startsWith("/playlist",false) && showPlaylists) bindPlaylist(searchItem) searchItem.url!!.startsWith("/channel",false) -> bindChannel(searchItem)
else {} searchItem.url!!.startsWith("/playlist",false) -> bindPlaylist(searchItem)
else -> {
}
}
} }
} }