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

182 lines
6.2 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
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
2021-12-28 01:37:07 +05:30
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
2022-05-09 22:10:51 +05:30
import android.widget.TextView
import android.widget.TextView.*
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
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
2022-02-05 21:20:16 +05:30
2021-12-28 01:37:07 +05:30
class SearchFragment : Fragment() {
2022-02-05 00:25:05 +05:30
private val TAG = "SearchFragment"
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-01-20 17:28:59 +05:30
val recyclerView = view.findViewById<RecyclerView>(R.id.search_recycler)
2022-05-09 22:10:51 +05:30
val historyRecycler = view.findViewById<RecyclerView>(R.id.history_recycler)
val history_tv = view.findViewById<TextView>(R.id.tv_history)
//show search history
recyclerView.visibility = GONE
historyRecycler.visibility = VISIBLE
history_tv.visibility = VISIBLE
recyclerView.layoutManager = LinearLayoutManager(view.context)
recyclerView.adapter
2022-01-20 17:28:59 +05:30
recyclerView.layoutManager = GridLayoutManager(view.context, 1)
2021-12-28 01:37:07 +05:30
val autoTextView = view.findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)
2022-03-15 14:21:31 +05:30
autoTextView.requestFocus()
val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm!!.showSoftInput(autoTextView, InputMethodManager.SHOW_IMPLICIT)
2021-12-28 01:37:07 +05:30
autoTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if(s!! != ""){
2022-01-20 17:28:59 +05:30
GlobalScope.launch {
2021-12-28 23:41:51 +05:30
fetchSuggestions(s.toString(), autoTextView)
2022-03-05 14:04:02 +05:30
delay(3000)
2022-05-09 22:10:51 +05:30
addtohistory(s.toString())
2022-01-20 17:28:59 +05:30
fetchSearch(s.toString(),recyclerView)
2021-12-28 23:41:51 +05:30
}
2021-12-28 01:37:07 +05:30
}
2021-12-28 23:41:51 +05:30
}
2021-12-28 01:37:07 +05:30
override fun afterTextChanged(s: Editable?) {
}
})
autoTextView.setOnEditorActionListener(OnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
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-01-20 17:28:59 +05:30
private fun fetchSearch(query: String, recyclerView: RecyclerView){
2021-12-28 23:41:51 +05:30
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getSearchResults(query, "all")
} 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-01-19 22:22:32 +05:30
if(response.items!!.isNotEmpty()){
2022-01-20 17:28:59 +05:30
runOnUiThread {
recyclerView.adapter = SearchAdapter(response.items)
}
2022-01-19 22:22:32 +05:30
}
2022-01-20 17:28:59 +05:30
2021-12-28 23:41:51 +05:30
}
}
2022-02-05 21:20:16 +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) {
var queryFromated = "|" + query
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
var history = sharedPreferences.getString("search_history", "")
var splited_history = history!!.split("|") + queryFromated
if (splited_history.size > 10) {
splited_history.drop(9)
}
sharedPreferences.edit().putString("search_history",splited_history.joinToString("|") ).apply()
}
2022-03-05 14:04:02 +05:30
}