mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 15:30:31 +05:30
improve search
This commit is contained in:
parent
ba1fcbb7b4
commit
b03dd7933c
@ -3,6 +3,7 @@ import java.time.Instant
|
|||||||
plugins {
|
plugins {
|
||||||
id 'com.android.application'
|
id 'com.android.application'
|
||||||
id 'kotlin-android'
|
id 'kotlin-android'
|
||||||
|
id 'kotlin-android-extensions'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
@ -101,6 +102,10 @@ dependencies {
|
|||||||
implementation libs.cronet.embedded
|
implementation libs.cronet.embedded
|
||||||
implementation libs.cronet.okhttp
|
implementation libs.cronet.okhttp
|
||||||
implementation libs.coil
|
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() {
|
static def getUnixTime() {
|
||||||
|
@ -21,6 +21,7 @@ import androidx.appcompat.widget.SearchView
|
|||||||
import androidx.constraintlayout.motion.widget.MotionLayout
|
import androidx.constraintlayout.motion.widget.MotionLayout
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.constraintlayout.widget.ConstraintLayout
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
import androidx.navigation.ui.setupWithNavController
|
import androidx.navigation.ui.setupWithNavController
|
||||||
@ -30,6 +31,7 @@ import com.github.libretube.R
|
|||||||
import com.github.libretube.databinding.ActivityMainBinding
|
import com.github.libretube.databinding.ActivityMainBinding
|
||||||
import com.github.libretube.dialogs.ErrorDialog
|
import com.github.libretube.dialogs.ErrorDialog
|
||||||
import com.github.libretube.fragments.PlayerFragment
|
import com.github.libretube.fragments.PlayerFragment
|
||||||
|
import com.github.libretube.models.SearchViewModel
|
||||||
import com.github.libretube.preferences.PreferenceHelper
|
import com.github.libretube.preferences.PreferenceHelper
|
||||||
import com.github.libretube.preferences.PreferenceKeys
|
import com.github.libretube.preferences.PreferenceKeys
|
||||||
import com.github.libretube.services.ClosingService
|
import com.github.libretube.services.ClosingService
|
||||||
@ -176,6 +178,14 @@ class MainActivity : AppCompatActivity() {
|
|||||||
searchView = searchItem.actionView as SearchView
|
searchView = searchItem.actionView as SearchView
|
||||||
searchView.setMaxWidth(Integer.MAX_VALUE)
|
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 {
|
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
@ -185,9 +195,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onQueryTextChange(newText: String?): Boolean {
|
override fun onQueryTextChange(newText: String?): Boolean {
|
||||||
val bundle = Bundle()
|
if (navController.currentDestination?.id != R.id.searchFragment) {
|
||||||
bundle.putString("query", newText)
|
val bundle = Bundle()
|
||||||
navController.navigate(R.id.searchFragment, bundle)
|
bundle.putString("query", newText)
|
||||||
|
navController.navigate(R.id.searchFragment, bundle)
|
||||||
|
} else {
|
||||||
|
searchViewModel.setQuery(newText)
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -206,10 +220,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// automatically handle clicks on the Home/Up button, so long
|
// automatically handle clicks on the Home/Up button, so long
|
||||||
// as you specify a parent activity in AndroidManifest.xml.
|
// as you specify a parent activity in AndroidManifest.xml.
|
||||||
return when (item.itemId) {
|
return when (item.itemId) {
|
||||||
R.id.action_search -> {
|
|
||||||
navController.navigate(R.id.searchFragment)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
R.id.action_settings -> {
|
R.id.action_settings -> {
|
||||||
val settingsIntent = Intent(this, SettingsActivity::class.java)
|
val settingsIntent = Intent(this, SettingsActivity::class.java)
|
||||||
startActivity(settingsIntent)
|
startActivity(settingsIntent)
|
||||||
@ -357,6 +367,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
// remove focus from search
|
// remove focus from search
|
||||||
removeSearchFocus()
|
removeSearchFocus()
|
||||||
|
navController.popBackStack(R.id.searchFragment, false)
|
||||||
|
|
||||||
if (binding.mainMotionLayout.progress == 0F) {
|
if (binding.mainMotionLayout.progress == 0F) {
|
||||||
try {
|
try {
|
||||||
|
@ -6,14 +6,14 @@ import android.view.LayoutInflater
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.navigation.fragment.findNavController
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.github.libretube.R
|
|
||||||
import com.github.libretube.activities.MainActivity
|
import com.github.libretube.activities.MainActivity
|
||||||
import com.github.libretube.adapters.SearchHistoryAdapter
|
import com.github.libretube.adapters.SearchHistoryAdapter
|
||||||
import com.github.libretube.adapters.SearchSuggestionsAdapter
|
import com.github.libretube.adapters.SearchSuggestionsAdapter
|
||||||
import com.github.libretube.databinding.FragmentSearchBinding
|
import com.github.libretube.databinding.FragmentSearchBinding
|
||||||
|
import com.github.libretube.models.SearchViewModel
|
||||||
import com.github.libretube.preferences.PreferenceHelper
|
import com.github.libretube.preferences.PreferenceHelper
|
||||||
import com.github.libretube.util.RetrofitInstance
|
import com.github.libretube.util.RetrofitInstance
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
@ -22,6 +22,7 @@ import java.io.IOException
|
|||||||
class SearchFragment() : Fragment() {
|
class SearchFragment() : Fragment() {
|
||||||
private val TAG = "SearchFragment"
|
private val TAG = "SearchFragment"
|
||||||
private lateinit var binding: FragmentSearchBinding
|
private lateinit var binding: FragmentSearchBinding
|
||||||
|
private val viewModel: SearchViewModel by activityViewModels()
|
||||||
|
|
||||||
private var query: String? = null
|
private var query: String? = null
|
||||||
|
|
||||||
@ -43,9 +44,19 @@ class SearchFragment() : Fragment() {
|
|||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
binding.suggestionsRecycler.layoutManager = LinearLayoutManager(requireContext())
|
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
|
// fetch the search or history
|
||||||
|
binding.historyEmpty.visibility = View.GONE
|
||||||
|
binding.suggestionsRecycler.visibility = View.VISIBLE
|
||||||
if (query == null || query == "") showHistory()
|
if (query == null || query == "") showHistory()
|
||||||
else fetchSuggestions(query!!)
|
else fetchSuggestions(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchSuggestions(query: String) {
|
private fun fetchSuggestions(query: String) {
|
||||||
@ -68,7 +79,9 @@ class SearchFragment() : Fragment() {
|
|||||||
(activity as MainActivity).searchView
|
(activity as MainActivity).searchView
|
||||||
)
|
)
|
||||||
runOnUiThread {
|
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
|
if (!isAdded) return // Fragment not attached to an Activity
|
||||||
activity?.runOnUiThread(action)
|
activity?.runOnUiThread(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
// remove the backstack entries
|
|
||||||
findNavController().popBackStack(R.id.searchFragment, true)
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,7 @@ buildscript {
|
|||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
dependencies {
|
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'
|
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
|
Loading…
x
Reference in New Issue
Block a user