From 5207486c3619f669cd49eca1e5e2f675606cc2ff Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 15 May 2022 13:26:51 +0200 Subject: [PATCH] Use API Search Filter --- .../com/github/libretube/SearchFragment.kt | 18 ++++++--- .../libretube/adapters/SearchAdapter.kt | 39 +++++++------------ 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index fc5279753..ff35ce9cb 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -33,6 +33,7 @@ import java.io.IOException class SearchFragment : Fragment() { private val TAG = "SearchFragment" private var selectedFilter = 0 + private var apiSearchFilter = "all" private var nextPage : String? = null private lateinit var searchRecView : RecyclerView private var searchAdapter : SearchAdapter? = null @@ -72,8 +73,15 @@ class SearchFragment : Fragment() { .setSingleChoiceItems(options, selectedFilter, DialogInterface.OnClickListener { _, id -> tempSelectedItem = id }) - .setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { - _, _ -> selectedFilter = tempSelectedItem + .setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { _, _ -> + selectedFilter = tempSelectedItem + apiSearchFilter = when (selectedFilter) { + 0 -> "all" + 1 -> "videos" + 2 -> "channels" + 3 -> "playlists" + else -> "all" + } fetchSearch(autoTextView.text.toString()) }) .setNegativeButton(getString(R.string.cancel), null) @@ -177,7 +185,7 @@ class SearchFragment : Fragment() { private fun fetchSearch(query: String){ lifecycleScope.launchWhenCreated { val response = try { - RetrofitInstance.api.getSearchResults(query, "videos") + RetrofitInstance.api.getSearchResults(query, apiSearchFilter) } catch (e: IOException) { println(e) Log.e(TAG, "IOException, you might not have internet connection $e") @@ -189,7 +197,7 @@ class SearchFragment : Fragment() { nextPage = response.nextpage if(response.items!!.isNotEmpty()){ runOnUiThread { - searchAdapter = SearchAdapter(response.items, selectedFilter) + searchAdapter = SearchAdapter(response.items) searchRecView.adapter = searchAdapter } } @@ -200,7 +208,7 @@ class SearchFragment : Fragment() { private fun fetchNextSearchItems(query: String){ lifecycleScope.launchWhenCreated { val response = try { - RetrofitInstance.api.getSearchResultsNextPage(query!!, "videos", nextPage!!) + RetrofitInstance.api.getSearchResultsNextPage(query!!, apiSearchFilter, nextPage!!) } catch (e: IOException) { println(e) Log.e(TAG, "IOException, you might not have internet connection") diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index bf99aecf9..077fac1ad 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -14,16 +14,11 @@ import com.github.libretube.MainActivity import com.github.libretube.PlayerFragment import com.github.libretube.R import com.github.libretube.formatShort -import com.github.libretube.obj.Comment import com.github.libretube.obj.SearchItem 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, private val selectedFilter : Int): RecyclerView.Adapter() { +class SearchAdapter(private val searchItems: MutableList): RecyclerView.Adapter() { fun updateItems(newItems: List){ var searchItemsSize = searchItems.size @@ -36,20 +31,11 @@ class SearchAdapter(private val searchItems: MutableList, private va } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder1 { - when (selectedFilter) { - 0 -> { showChannels = true; showVideos = true; showPlaylists = true } - 1 -> { showChannels = false; showVideos = true; showPlaylists = false } - 2 -> { showChannels = true; showVideos = false; showPlaylists = false } - 3 -> { showChannels = false; showVideos = false; showPlaylists = true } - } - 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 layout = when (viewType) { + 0 -> R.layout.video_search_row + 1 -> R.layout.channel_search_row + 2 -> R.layout.playlist_search_row + else -> throw IllegalArgumentException("Invalid type") } val layoutInflater = LayoutInflater.from(parent.context) val cell = layoutInflater.inflate(layout,parent,false) @@ -141,9 +127,12 @@ class CustomViewHolder1(private val v: View): RecyclerView.ViewHolder(v){ } fun bind(searchItem: SearchItem) { - if (searchItem.url!!.startsWith("/watch",false) && showVideos) bindWatch(searchItem) - else if (searchItem.url!!.startsWith("/channel",false) && showChannels) bindChannel(searchItem) - else if (searchItem.url!!.startsWith("/playlist",false) && showPlaylists) bindPlaylist(searchItem) - else {} + when { + searchItem.url!!.startsWith("/watch",false) -> bindWatch(searchItem) + searchItem.url!!.startsWith("/channel",false) -> bindChannel(searchItem) + searchItem.url!!.startsWith("/playlist",false) -> bindPlaylist(searchItem) + else -> { + } + } } -} +} \ No newline at end of file