LibreTube/app/src/main/java/com/github/libretube/SearchFragment.kt

299 lines
11 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-28 01:37:07 +05:30
2022-03-05 14:04:02 +05:30
import android.content.Context
2022-05-15 13:38:47 +05:30
import android.content.DialogInterface
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-03-05 14:04:02 +05:30
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
2022-03-05 14:04:02 +05:30
import android.view.inputmethod.InputMethodManager
2022-05-15 16:42:46 +05:30
import android.widget.*
2022-05-09 22:10:51 +05:30
import android.widget.TextView.*
2022-05-15 13:38:47 +05:30
import androidx.appcompat.app.AlertDialog
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-05-09 22:10:51 +05:30
import androidx.preference.PreferenceManager
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-01-20 17:28:59 +05:30
import androidx.recyclerview.widget.RecyclerView
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
2021-12-28 23:41:51 +05:30
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
2021-12-28 01:37:07 +05:30
import retrofit2.HttpException
import java.io.IOException
class SearchFragment : Fragment() {
2022-02-05 00:25:05 +05:30
private val TAG = "SearchFragment"
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-15 16:42:46 +05:30
private var nextPage : String? = null
private lateinit var searchRecView : RecyclerView
private var searchAdapter : SearchAdapter? = null
2022-05-16 00:13:29 +05:30
private var isLoading : Boolean = true
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 {
2022-02-05 21:20:16 +05:30
2021-12-28 01:37:07 +05:30
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_search, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-05-15 16:42:46 +05:30
searchRecView = view.findViewById<RecyclerView>(R.id.search_recycler)
2022-05-09 22:10:51 +05:30
2022-05-09 23:52:13 +05:30
val autoTextView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)
2022-05-09 22:10:51 +05:30
val historyRecycler = view.findViewById<RecyclerView>(R.id.history_recycler)
2022-05-15 13:38:47 +05:30
val filterImageView = view.findViewById<ImageView>(R.id.filterMenu_imageView)
var tempSelectedItem = 0
2022-05-15 13:38:47 +05:30
filterImageView.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-15 13:38:47 +05:30
AlertDialog.Builder(view.context)
.setTitle(getString(R.string.choose_filter))
2022-05-16 22:32:46 +05:30
.setSingleChoiceItems(filterOptions, selectedFilter, DialogInterface.OnClickListener {
2022-05-15 14:37:56 +05:30
_, id -> tempSelectedItem = id
})
2022-05-15 16:56:51 +05:30
.setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { _, _ ->
selectedFilter = tempSelectedItem
apiSearchFilter = when (selectedFilter) {
0 -> "all"
1 -> "videos"
2 -> "channels"
3 -> "playlists"
2022-05-16 22:32:46 +05:30
4 -> "music_songs"
5 -> "music_videos"
6 -> "music_albums"
7 -> "music_playlists"
2022-05-15 16:56:51 +05:30
else -> "all"
}
2022-05-15 16:42:46 +05:30
fetchSearch(autoTextView.text.toString())
2022-05-15 13:38:47 +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
//show search history
2022-05-15 16:42:46 +05:30
searchRecView.visibility = GONE
2022-05-09 22:10:51 +05:30
historyRecycler.visibility = VISIBLE
2022-05-09 23:20:36 +05:30
historyRecycler.layoutManager = LinearLayoutManager(view.context)
var historylist = getHistory()
2022-05-16 22:32:46 +05:30
if (historylist.isNotEmpty()) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
}
2022-05-09 22:10:51 +05:30
2022-05-15 16:42:46 +05:30
searchRecView.layoutManager = GridLayoutManager(view.context, 1)
2022-03-15 14:21:31 +05:30
autoTextView.requestFocus()
2022-05-09 23:20:36 +05:30
val imm =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
2022-05-16 22:32:46 +05:30
imm.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT)
2022-05-09 23:20:36 +05:30
autoTextView.addTextChangedListener(object : TextWatcher {
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
}
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-05-15 16:42:46 +05:30
searchRecView.visibility = VISIBLE
2022-05-09 23:20:36 +05:30
historyRecycler.visibility = GONE
2022-05-15 16:42:46 +05:30
searchRecView.adapter = null
searchRecView.viewTreeObserver
.addOnScrollChangedListener {
if (!searchRecView.canScrollVertically(1)) {
fetchNextSearchItems(autoTextView.text.toString())
}
}
2022-05-09 23:20:36 +05:30
GlobalScope.launch {
fetchSuggestions(s.toString(), autoTextView)
2022-05-15 14:37:56 +05:30
delay(1000)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
if (sharedPreferences.getBoolean("search_history_toggle", true)) addtohistory(s.toString())
2022-05-15 16:42:46 +05:30
fetchSearch(s.toString())
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-05-15 16:42:46 +05:30
searchRecView.visibility = GONE
2022-05-09 23:20:36 +05:30
historyRecycler.visibility = VISIBLE
var historylist = getHistory()
2022-05-16 22:32:46 +05:30
if (historylist.isNotEmpty()) {
historyRecycler.adapter =
SearchHistoryAdapter(requireContext(), historylist, autoTextView)
}
2022-05-09 23:20:36 +05:30
}
}
2021-12-28 01:37:07 +05:30
2022-05-09 23:20:36 +05:30
})
autoTextView.setOnEditorActionListener(OnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
2022-05-16 22:32:46 +05:30
hideKeyboard()
autoTextView.dismissDropDown()
return@OnEditorActionListener true
}
false
})
autoTextView.setOnItemClickListener { _, _, _, _ ->
hideKeyboard()
}
2021-12-28 01:37:07 +05:30
}
private fun fetchSuggestions(query: String, autoTextView: AutoCompleteTextView){
lifecycleScope.launchWhenCreated {
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
}
2022-02-05 00:25:05 +05:30
val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, response)
2021-12-28 01:37:07 +05:30
autoTextView.setAdapter(adapter)
}
}
2022-05-15 16:42:46 +05:30
private fun fetchSearch(query: String){
2021-12-28 23:41:51 +05:30
lifecycleScope.launchWhenCreated {
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-01-19 22:22:32 +05:30
if(response.items!!.isNotEmpty()){
2022-01-20 17:28:59 +05:30
runOnUiThread {
2022-05-15 16:56:51 +05:30
searchAdapter = SearchAdapter(response.items)
2022-05-15 16:42:46 +05:30
searchRecView.adapter = searchAdapter
2022-01-20 17:28:59 +05:30
}
2022-01-19 22:22:32 +05:30
}
2022-05-16 00:13:29 +05:30
isLoading = false
2021-12-28 23:41:51 +05:30
}
}
2022-02-05 21:20:16 +05:30
2022-05-15 16:42:46 +05:30
private fun fetchNextSearchItems(query: String){
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-16 22:32:46 +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-15 17:17:24 +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()
requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
}
2022-03-31 23:04:19 +05:30
override fun onStop() {
super.onStop()
hideKeyboard()
}
2022-05-09 22:10:51 +05:30
private fun addtohistory(query: String) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
2022-05-10 21:12:17 +05:30
var historyList = getHistory()
2022-05-09 22:50:07 +05:30
2022-05-10 21:12:17 +05:30
if (historyList.size != 0 && query == historyList.get(historyList.size - 1)) {
2022-05-09 22:50:07 +05:30
return
2022-05-09 23:20:36 +05:30
} else if (query == "") {
return
2022-05-09 22:50:07 +05:30
} else {
2022-05-10 21:12:17 +05:30
historyList = historyList + query
2022-05-09 22:50:07 +05:30
}
2022-05-09 22:10:51 +05:30
2022-05-09 23:39:03 +05:30
2022-05-10 21:12:17 +05:30
if (historyList.size > 10) {
historyList = historyList.takeLast(10)
2022-05-09 22:10:51 +05:30
}
2022-05-10 21:12:17 +05:30
var set: Set<String> = HashSet(historyList)
2022-05-09 22:10:51 +05:30
2022-05-10 21:12:17 +05:30
sharedPreferences.edit().putStringSet("search_history", set)
2022-05-09 22:50:07 +05:30
.apply()
}
private fun getHistory(): List<String> {
try {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val set: Set<String> = sharedPreferences.getStringSet("search_history", HashSet())!!
return set.toList()
} catch (e: Exception) {
return emptyList()
}
2022-05-09 22:10:51 +05:30
}
2022-03-05 14:04:02 +05:30
}
2022-05-09 22:50:07 +05:30