improve search

This commit is contained in:
Bnyro 2022-08-04 09:08:46 +02:00
parent ba1fcbb7b4
commit b03dd7933c
5 changed files with 53 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import java.time.Instant
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
@ -101,6 +102,10 @@ dependencies {
implementation libs.cronet.embedded
implementation libs.cronet.okhttp
implementation libs.coil
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
}
static def getUnixTime() {

View File

@ -21,6 +21,7 @@ import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
@ -30,6 +31,7 @@ import com.github.libretube.R
import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.dialogs.ErrorDialog
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.models.SearchViewModel
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.services.ClosingService
@ -176,6 +178,14 @@ class MainActivity : AppCompatActivity() {
searchView = searchItem.actionView as SearchView
searchView.setMaxWidth(Integer.MAX_VALUE)
val searchViewModel = ViewModelProvider(this).get(SearchViewModel::class.java)
searchView.setOnSearchClickListener {
if (navController.currentDestination?.id != R.id.searchResultFragment) {
navController.navigate(R.id.searchFragment)
}
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
val bundle = Bundle()
@ -185,9 +195,13 @@ class MainActivity : AppCompatActivity() {
}
override fun onQueryTextChange(newText: String?): Boolean {
val bundle = Bundle()
bundle.putString("query", newText)
navController.navigate(R.id.searchFragment, bundle)
if (navController.currentDestination?.id != R.id.searchFragment) {
val bundle = Bundle()
bundle.putString("query", newText)
navController.navigate(R.id.searchFragment, bundle)
} else {
searchViewModel.setQuery(newText)
}
return true
}
})
@ -206,10 +220,6 @@ 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)
@ -357,6 +367,7 @@ class MainActivity : AppCompatActivity() {
override fun onBackPressed() {
// remove focus from search
removeSearchFocus()
navController.popBackStack(R.id.searchFragment, false)
if (binding.mainMotionLayout.progress == 0F) {
try {

View File

@ -6,14 +6,14 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
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.models.SearchViewModel
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
@ -22,6 +22,7 @@ import java.io.IOException
class SearchFragment() : Fragment() {
private val TAG = "SearchFragment"
private lateinit var binding: FragmentSearchBinding
private val viewModel: SearchViewModel by activityViewModels()
private var query: String? = null
@ -43,9 +44,19 @@ class SearchFragment() : Fragment() {
super.onViewCreated(view, savedInstanceState)
binding.suggestionsRecycler.layoutManager = LinearLayoutManager(requireContext())
// waiting for the query to change
viewModel.searchQuery.observe(viewLifecycleOwner) {
showData(it)
}
}
private fun showData(query: String?) {
// fetch the search or history
binding.historyEmpty.visibility = View.GONE
binding.suggestionsRecycler.visibility = View.VISIBLE
if (query == null || query == "") showHistory()
else fetchSuggestions(query!!)
else fetchSuggestions(query)
}
private fun fetchSuggestions(query: String) {
@ -68,7 +79,9 @@ class SearchFragment() : Fragment() {
(activity as MainActivity).searchView
)
runOnUiThread {
binding.suggestionsRecycler.adapter = suggestionsAdapter
if (viewModel.searchQuery.value != "") {
binding.suggestionsRecycler.adapter = suggestionsAdapter
}
}
}
}
@ -94,10 +107,4 @@ class SearchFragment() : Fragment() {
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
override fun onDestroy() {
// remove the backstack entries
findNavController().popBackStack(R.id.searchFragment, true)
super.onDestroy()
}
}

View File

@ -0,0 +1,12 @@
package com.github.libretube.models
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class SearchViewModel : ViewModel() {
var searchQuery = MutableLiveData<String>()
fun setQuery(query: String?) {
this.searchQuery.value = query
}
}

View File

@ -10,7 +10,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
classpath 'com.android.tools.build:gradle:7.2.2'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
// NOTE: Do not place your application dependencies here; they belong