2022-06-03 00:40:16 +05:30
|
|
|
package com.github.libretube.fragments
|
2021-12-28 01:37:07 +05:30
|
|
|
|
2022-03-05 14:04:02 +05:30
|
|
|
import android.content.Context
|
2021-12-28 01:37:07 +05:30
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
|
|
|
import android.util.Log
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2022-05-21 13:32:04 +05:30
|
|
|
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
|
2022-04-02 16:50:58 +05:30
|
|
|
import android.view.inputmethod.EditorInfo
|
2022-03-05 14:04:02 +05:30
|
|
|
import android.view.inputmethod.InputMethodManager
|
2022-06-10 14:28:33 +05:30
|
|
|
import android.widget.EditText
|
2022-05-21 13:32:04 +05:30
|
|
|
import android.widget.TextView.GONE
|
|
|
|
import android.widget.TextView.OnEditorActionListener
|
|
|
|
import android.widget.TextView.VISIBLE
|
2022-03-05 14:04:02 +05:30
|
|
|
import androidx.fragment.app.Fragment
|
2021-12-28 01:37:07 +05:30
|
|
|
import androidx.lifecycle.lifecycleScope
|
2022-01-20 17:28:59 +05:30
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2022-05-09 22:10:51 +05:30
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.R
|
2022-03-05 14:04:02 +05:30
|
|
|
import com.github.libretube.adapters.SearchAdapter
|
2022-05-09 22:50:07 +05:30
|
|
|
import com.github.libretube.adapters.SearchHistoryAdapter
|
2022-06-10 14:28:33 +05:30
|
|
|
import com.github.libretube.adapters.SearchSuggestionsAdapter
|
2022-07-01 14:41:24 +05:30
|
|
|
import com.github.libretube.databinding.FragmentSearchBinding
|
2022-07-02 21:53:24 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
2022-07-17 21:48:39 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceKeys
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.util.RetrofitInstance
|
2022-07-24 01:40:03 +05:30
|
|
|
import com.github.libretube.util.hideKeyboard
|
2022-05-20 15:54:45 +05:30
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2021-12-28 01:37:07 +05:30
|
|
|
import retrofit2.HttpException
|
2022-06-24 20:56:36 +05:30
|
|
|
import java.io.IOException
|
2021-12-28 01:37:07 +05:30
|
|
|
|
|
|
|
class SearchFragment : Fragment() {
|
2022-02-05 00:25:05 +05:30
|
|
|
private val TAG = "SearchFragment"
|
2022-07-01 14:41:24 +05:30
|
|
|
private lateinit var binding: FragmentSearchBinding
|
|
|
|
|
2022-05-15 16:42:46 +05:30
|
|
|
private var selectedFilter = 0
|
2022-05-15 16:56:51 +05:30
|
|
|
private var apiSearchFilter = "all"
|
2022-05-20 03:52:10 +05:30
|
|
|
private var nextPage: String? = null
|
2022-07-01 14:41:24 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private var searchAdapter: SearchAdapter? = null
|
|
|
|
private var isLoading: Boolean = true
|
2022-06-10 17:52:32 +05:30
|
|
|
private var isFetchingSearch: Boolean = false
|
2022-05-15 16:42:46 +05:30
|
|
|
|
2021-12-28 01:37:07 +05:30
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
arguments?.let {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(
|
2022-05-20 03:52:10 +05:30
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
2021-12-28 01:37:07 +05:30
|
|
|
savedInstanceState: Bundle?
|
2022-07-01 14:41:24 +05:30
|
|
|
): View {
|
|
|
|
binding = FragmentSearchBinding.inflate(layoutInflater, container, false)
|
|
|
|
return binding.root
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2022-05-15 13:54:19 +05:30
|
|
|
|
|
|
|
var tempSelectedItem = 0
|
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.clearSearchImageView.setOnClickListener {
|
|
|
|
binding.autoCompleteTextView.text.clear()
|
2022-07-23 19:11:57 +05:30
|
|
|
showHistory()
|
2022-06-06 21:31:04 +05:30
|
|
|
}
|
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.filterMenuImageView.setOnClickListener {
|
2022-05-16 22:32:46 +05:30
|
|
|
val filterOptions = arrayOf(
|
|
|
|
getString(R.string.all),
|
|
|
|
getString(R.string.videos),
|
|
|
|
getString(R.string.channels),
|
|
|
|
getString(R.string.playlists),
|
|
|
|
getString(R.string.music_songs),
|
|
|
|
getString(R.string.music_videos),
|
|
|
|
getString(R.string.music_albums),
|
|
|
|
getString(R.string.music_playlists)
|
|
|
|
)
|
|
|
|
|
2022-05-20 15:54:45 +05:30
|
|
|
MaterialAlertDialogBuilder(view.context)
|
2022-05-15 13:54:19 +05:30
|
|
|
.setTitle(getString(R.string.choose_filter))
|
2022-06-10 14:28:33 +05:30
|
|
|
.setSingleChoiceItems(filterOptions, selectedFilter) { _, id ->
|
|
|
|
tempSelectedItem = id
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
.setPositiveButton(
|
2022-06-24 20:56:36 +05:30
|
|
|
getString(R.string.okay)
|
2022-06-10 14:28:33 +05:30
|
|
|
) { _, _ ->
|
|
|
|
selectedFilter = tempSelectedItem
|
|
|
|
apiSearchFilter = when (selectedFilter) {
|
|
|
|
0 -> "all"
|
|
|
|
1 -> "videos"
|
|
|
|
2 -> "channels"
|
|
|
|
3 -> "playlists"
|
|
|
|
4 -> "music_songs"
|
|
|
|
5 -> "music_videos"
|
|
|
|
6 -> "music_albums"
|
|
|
|
7 -> "music_playlists"
|
|
|
|
else -> "all"
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|
2022-07-01 14:41:24 +05:30
|
|
|
fetchSearch(binding.autoCompleteTextView.text.toString())
|
2022-06-10 14:28:33 +05:30
|
|
|
}
|
2022-05-15 14:00:07 +05:30
|
|
|
.setNegativeButton(getString(R.string.cancel), null)
|
2022-05-15 13:38:47 +05:30
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
2022-05-09 22:10:51 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
// show search history
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.historyRecycler.layoutManager = LinearLayoutManager(view.context)
|
2022-06-10 17:52:32 +05:30
|
|
|
showHistory()
|
2022-05-09 22:10:51 +05:30
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.layoutManager = GridLayoutManager(view.context, 1)
|
|
|
|
binding.autoCompleteTextView.requestFocus()
|
2022-05-09 23:20:36 +05:30
|
|
|
val imm =
|
|
|
|
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
2022-07-01 14:41:24 +05:30
|
|
|
imm.showSoftInput(binding.autoCompleteTextView, InputMethodManager.SHOW_IMPLICIT)
|
2022-06-10 17:52:32 +05:30
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.autoCompleteTextView.addTextChangedListener(object : TextWatcher {
|
2022-05-09 23:20:36 +05:30
|
|
|
override fun beforeTextChanged(
|
|
|
|
s: CharSequence?,
|
|
|
|
start: Int,
|
|
|
|
count: Int,
|
|
|
|
after: Int
|
|
|
|
) {
|
|
|
|
}
|
2021-12-28 01:37:07 +05:30
|
|
|
|
2022-05-09 23:20:36 +05:30
|
|
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
|
|
if (s!! != "") {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.adapter = null
|
2022-05-15 16:42:46 +05:30
|
|
|
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.viewTreeObserver
|
2022-05-15 16:42:46 +05:30
|
|
|
.addOnScrollChangedListener {
|
2022-07-01 14:41:24 +05:30
|
|
|
if (!binding.searchRecycler.canScrollVertically(1)) {
|
|
|
|
fetchNextSearchItems(binding.autoCompleteTextView.text.toString())
|
2022-05-15 16:42:46 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-01 14:41:24 +05:30
|
|
|
fetchSuggestions(s.toString(), binding.autoCompleteTextView)
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
2022-05-09 23:20:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
override fun afterTextChanged(s: Editable?) {
|
|
|
|
if (s!!.isEmpty()) {
|
2022-06-10 17:52:32 +05:30
|
|
|
showHistory()
|
2022-05-09 23:20:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.autoCompleteTextView.setOnEditorActionListener(
|
2022-05-20 03:52:10 +05:30
|
|
|
OnEditorActionListener { _, actionId, _ ->
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
2022-07-24 15:26:53 +05:30
|
|
|
view?.let { context?.hideKeyboard(it) }
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.visibility = VISIBLE
|
|
|
|
binding.historyRecycler.visibility = GONE
|
|
|
|
fetchSearch(binding.autoCompleteTextView.text.toString())
|
2022-05-20 03:52:10 +05:30
|
|
|
return@OnEditorActionListener true
|
|
|
|
}
|
|
|
|
false
|
2022-04-02 16:50:58 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
)
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
|
|
|
|
2022-06-10 14:28:33 +05:30
|
|
|
private fun fetchSuggestions(query: String, autoTextView: EditText) {
|
2022-06-10 17:52:32 +05:30
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.visibility = GONE
|
|
|
|
binding.historyRecycler.visibility = VISIBLE
|
2022-06-10 17:52:32 +05:30
|
|
|
val response = try {
|
|
|
|
RetrofitInstance.api.getSuggestions(query)
|
|
|
|
} catch (e: IOException) {
|
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
Log.e(TAG, "HttpException, unexpected response")
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
|
|
|
val suggestionsAdapter =
|
|
|
|
SearchSuggestionsAdapter(response, autoTextView, this@SearchFragment)
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.historyRecycler.adapter = suggestionsAdapter
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
|
|
|
}
|
2022-06-10 17:52:32 +05:30
|
|
|
if (!isFetchingSearch) run()
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-06-10 17:52:32 +05:30
|
|
|
fun fetchSearch(query: String) {
|
2022-07-01 15:39:54 +05:30
|
|
|
runOnUiThread {
|
|
|
|
binding.historyRecycler.visibility = GONE
|
|
|
|
}
|
2021-12-28 23:41:51 +05:30
|
|
|
lifecycleScope.launchWhenCreated {
|
2022-06-10 17:52:32 +05:30
|
|
|
isFetchingSearch = true
|
2022-07-24 15:26:53 +05:30
|
|
|
view?.let { context?.hideKeyboard(it) }
|
2021-12-28 23:41:51 +05:30
|
|
|
val response = try {
|
2022-05-15 16:56:51 +05:30
|
|
|
RetrofitInstance.api.getSearchResults(query, apiSearchFilter)
|
2021-12-28 23:41:51 +05:30
|
|
|
} catch (e: IOException) {
|
|
|
|
println(e)
|
2022-03-05 14:04:02 +05:30
|
|
|
Log.e(TAG, "IOException, you might not have internet connection $e")
|
2021-12-28 23:41:51 +05:30
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
Log.e(TAG, "HttpException, unexpected response")
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
2022-05-15 16:42:46 +05:30
|
|
|
nextPage = response.nextpage
|
2022-05-20 03:52:10 +05:30
|
|
|
if (response.items!!.isNotEmpty()) {
|
|
|
|
runOnUiThread {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.visibility = VISIBLE
|
2022-05-30 19:13:55 +05:30
|
|
|
searchAdapter = SearchAdapter(response.items, childFragmentManager)
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.adapter = searchAdapter
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|
2022-01-19 22:22:32 +05:30
|
|
|
}
|
2022-06-10 17:52:32 +05:30
|
|
|
addToHistory(query)
|
2022-05-16 00:13:29 +05:30
|
|
|
isLoading = false
|
2022-06-10 17:52:32 +05:30
|
|
|
isFetchingSearch = false
|
2021-12-28 23:41:51 +05:30
|
|
|
}
|
|
|
|
}
|
2022-02-05 21:20:16 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun fetchNextSearchItems(query: String) {
|
2022-05-15 16:42:46 +05:30
|
|
|
lifecycleScope.launchWhenCreated {
|
2022-05-15 17:17:24 +05:30
|
|
|
if (!isLoading) {
|
|
|
|
isLoading = true
|
2022-05-15 16:42:46 +05:30
|
|
|
val response = try {
|
2022-05-21 13:32:04 +05:30
|
|
|
RetrofitInstance.api.getSearchResultsNextPage(
|
|
|
|
query,
|
|
|
|
apiSearchFilter,
|
|
|
|
nextPage!!
|
|
|
|
)
|
2022-05-15 16:42:46 +05:30
|
|
|
} catch (e: IOException) {
|
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
Log.e(TAG, "HttpException, unexpected response," + e.response())
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
|
|
|
nextPage = response.nextpage
|
|
|
|
searchAdapter?.updateItems(response.items!!)
|
2022-05-15 17:17:24 +05:30
|
|
|
isLoading = false
|
2022-05-15 16:42:46 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|
2022-05-15 16:42:46 +05:30
|
|
|
|
2021-12-28 01:37:07 +05:30
|
|
|
private fun Fragment?.runOnUiThread(action: () -> Unit) {
|
|
|
|
this ?: return
|
|
|
|
if (!isAdded) return // Fragment not attached to an Activity
|
|
|
|
activity?.runOnUiThread(action)
|
|
|
|
}
|
2022-03-05 14:04:02 +05:30
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2022-05-21 13:32:04 +05:30
|
|
|
requireActivity().window.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
2022-03-05 14:04:02 +05:30
|
|
|
}
|
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
override fun onStop() {
|
|
|
|
super.onStop()
|
2022-07-24 15:26:53 +05:30
|
|
|
view?.let { context?.hideKeyboard(it) }
|
2022-03-31 23:04:19 +05:30
|
|
|
}
|
2022-05-09 22:10:51 +05:30
|
|
|
|
2022-06-10 17:52:32 +05:30
|
|
|
private fun showHistory() {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.searchRecycler.visibility = GONE
|
2022-07-24 15:26:53 +05:30
|
|
|
val historyList = PreferenceHelper.getSearchHistory()
|
2022-06-10 17:52:32 +05:30
|
|
|
if (historyList.isNotEmpty()) {
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.historyRecycler.adapter =
|
2022-07-01 15:41:30 +05:30
|
|
|
SearchHistoryAdapter(
|
|
|
|
historyList,
|
|
|
|
binding.autoCompleteTextView,
|
|
|
|
this
|
|
|
|
)
|
2022-07-01 14:41:24 +05:30
|
|
|
binding.historyRecycler.visibility = VISIBLE
|
2022-06-10 17:52:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 14:02:44 +05:30
|
|
|
private fun addToHistory(query: String) {
|
2022-06-26 15:50:56 +05:30
|
|
|
val searchHistoryEnabled =
|
2022-07-17 21:48:39 +05:30
|
|
|
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
|
2022-07-24 15:26:53 +05:30
|
|
|
if (searchHistoryEnabled && query != "") {
|
|
|
|
PreferenceHelper.saveToSearchHistory(query)
|
2022-05-10 21:53:59 +05:30
|
|
|
}
|
2022-05-09 22:10:51 +05:30
|
|
|
}
|
2022-03-05 14:04:02 +05:30
|
|
|
}
|