restore all functionality

This commit is contained in:
Bnyro 2022-07-31 22:41:15 +02:00
parent 022281e107
commit 329ccaeb00
5 changed files with 60 additions and 20 deletions

View File

@ -21,7 +21,6 @@ import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.fragment.app.replace
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
@ -30,7 +29,6 @@ import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.fragments.SearchFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.services.ClosingService
@ -49,7 +47,7 @@ class MainActivity : AppCompatActivity() {
lateinit var navController: NavController
private var startFragmentId = R.id.homeFragment
var autoRotationEnabled = false
private var searchFragment: SearchFragment? = null
lateinit var searchView: SearchView
override fun onCreate(savedInstanceState: Bundle?) {
// set the app theme (e.g. Material You)
@ -137,6 +135,7 @@ class MainActivity : AppCompatActivity() {
// clear backstack if it's the start fragment
if (startFragmentId == it.itemId) navController.backQueue.clear()
// set menu item on click listeners
removeSearchFocus()
when (it.itemId) {
R.id.homeFragment -> {
navController.navigate(R.id.homeFragment)
@ -155,13 +154,20 @@ class MainActivity : AppCompatActivity() {
}
}
private fun removeSearchFocus() {
searchView.setQuery("", false)
searchView.clearFocus()
searchView.onActionViewCollapsed()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.action_bar, menu)
val searchItem = menu.findItem(R.id.action_search)
// stuff for the search in the topBar
val searchView = searchItem.actionView as SearchView
val searchItem = menu.findItem(R.id.action_search)
searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
val bundle = Bundle()
@ -185,6 +191,10 @@ class MainActivity : AppCompatActivity() {
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_search -> {
navController.navigate(R.id.searchFragment)
true
}
R.id.action_settings -> {
val settingsIntent = Intent(this, SettingsActivity::class.java)
startActivity(settingsIntent)
@ -204,14 +214,6 @@ class MainActivity : AppCompatActivity() {
}
}
private fun addToHistory(query: String) {
val searchHistoryEnabled =
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
if (searchHistoryEnabled && query != "") {
PreferenceHelper.saveToSearchHistory(query)
}
}
override fun onStart() {
super.onStart()
val intentData: Uri? = intent?.data
@ -338,6 +340,9 @@ class MainActivity : AppCompatActivity() {
}
override fun onBackPressed() {
// remove focus from search
removeSearchFocus()
if (binding.mainMotionLayout.progress == 0F) {
try {
minimizePlayer()

View File

@ -2,12 +2,14 @@ package com.github.libretube.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.SearchhistoryRowBinding
import com.github.libretube.preferences.PreferenceHelper
class SearchHistoryAdapter(
private var historyList: List<String>
private var historyList: List<String>,
private val searchView: SearchView
) :
RecyclerView.Adapter<SearchHistoryViewHolder>() {
@ -33,6 +35,7 @@ class SearchHistoryAdapter(
}
root.setOnClickListener {
searchView.setQuery(historyQuery, true)
}
}
}

View File

@ -2,11 +2,13 @@ package com.github.libretube.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.SearchsuggestionRowBinding
class SearchSuggestionsAdapter(
private var suggestionsList: List<String>
private var suggestionsList: List<String>,
private val searchView: SearchView
) :
RecyclerView.Adapter<SearchSuggestionsViewHolder>() {
@ -27,6 +29,7 @@ class SearchSuggestionsAdapter(
holder.binding.apply {
suggestionText.text = suggestion
root.setOnClickListener {
searchView.setQuery(suggestion, true)
}
}
}

View File

@ -10,8 +10,12 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.SearchHistoryAdapter
import com.github.libretube.adapters.SearchSuggestionsAdapter
import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
@ -39,8 +43,21 @@ class SearchFragment() : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// fetch the search
fetchSuggestions(query!!)
// add the query to the history
if (query != null) addToHistory(query!!)
binding.suggestionsRecycler.layoutManager = LinearLayoutManager(requireContext())
// fetch the search or history
if (query == null || query == "") showHistory()
else fetchSuggestions(query!!)
}
private fun addToHistory(query: String) {
val searchHistoryEnabled =
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
if (searchHistoryEnabled && query != "") {
PreferenceHelper.saveToSearchHistory(query)
}
}
private fun fetchSuggestions(query: String) {
@ -59,10 +76,10 @@ class SearchFragment() : Fragment() {
// only load the suggestions if the input field didn't get cleared yet
val suggestionsAdapter =
SearchSuggestionsAdapter(
response
response,
(activity as MainActivity).searchView
)
runOnUiThread {
binding.suggestionsRecycler.layoutManager = LinearLayoutManager(requireContext())
binding.suggestionsRecycler.adapter = suggestionsAdapter
}
}
@ -70,6 +87,17 @@ class SearchFragment() : Fragment() {
run()
}
private fun showHistory() {
val historyList = PreferenceHelper.getSearchHistory()
if (historyList.isNotEmpty()) {
binding.suggestionsRecycler.adapter =
SearchHistoryAdapter(
historyList,
(activity as MainActivity).searchView
)
}
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity

View File

@ -7,7 +7,8 @@
android:icon="@drawable/ic_search"
android:title="@string/search_hint"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:focusableInTouchMode="true" />
<item
android:id="@+id/action_settings"