fix: show progress bar while search in progress

This commit is contained in:
Bnyro 2023-07-29 11:58:57 +02:00
parent 00936cfe2e
commit a7d2974921
5 changed files with 127 additions and 112 deletions

View File

@ -14,4 +14,5 @@ object IntentData {
const val fragmentToOpen = "fragmentToOpen"
const val comment = "comment"
const val minimizeByDefault = "minimizeByDefault"
const val query = "query"
}

View File

@ -309,7 +309,7 @@ class MainActivity : BaseActivity() {
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
navController.navigate(R.id.searchResultFragment, bundleOf("query" to query))
navController.navigate(R.id.searchResultFragment, bundleOf(IntentData.query to query))
searchView.clearFocus()
return true
}
@ -335,7 +335,7 @@ class MainActivity : BaseActivity() {
}
if (navController.currentDestination?.id != R.id.searchFragment) {
navController.navigate(R.id.searchFragment, bundleOf("query" to newText))
navController.navigate(R.id.searchFragment, bundleOf(IntentData.query to newText))
} else {
searchViewModel.setQuery(newText)
}

View File

@ -12,6 +12,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.db.DatabaseHolder.Database
@ -35,7 +36,7 @@ class SearchFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
query = arguments?.getString("query")
query = arguments?.getString(IntentData.query)
}
override fun onCreateView(

View File

@ -5,6 +5,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
@ -13,20 +14,20 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentSearchResultBinding
import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.SearchAdapter
import com.github.libretube.util.deArrow
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
class SearchResultFragment : Fragment() {
private var _binding: FragmentSearchResultBinding? = null
@ -40,7 +41,7 @@ class SearchResultFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
query = arguments?.getString("query").toString()
query = arguments?.getString(IntentData.query).toString()
}
override fun onCreateView(
@ -90,6 +91,9 @@ class SearchResultFragment : Fragment() {
}
private fun fetchSearch() {
_binding?.progress?.isVisible = true
_binding?.searchResultsLayout?.isGone = true
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
view?.let { context?.hideKeyboard(it) }
@ -99,12 +103,9 @@ class SearchResultFragment : Fragment() {
items = items.deArrow()
}
}
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection $e")
return@repeatOnLifecycle
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
} catch (e: Exception) {
Log.e(TAG(), e.toString())
context?.toastFromMainDispatcher(R.string.unknown_error)
return@repeatOnLifecycle
}
@ -112,7 +113,10 @@ class SearchResultFragment : Fragment() {
searchAdapter = SearchAdapter()
binding.searchRecycler.adapter = searchAdapter
searchAdapter.submitList(response.items)
binding.searchResultsLayout.isVisible = true
binding.progress.isGone = true
binding.noSearchResult.isVisible = response.items.isEmpty()
nextPage = response.nextpage
}
}
@ -131,15 +135,11 @@ class SearchResultFragment : Fragment() {
items = items.deArrow()
}
}
} catch (e: IOException) {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@repeatOnLifecycle
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response," + e.response())
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@repeatOnLifecycle
}
nextPage = response.nextpage!!
nextPage = response.nextpage
if (response.items.isNotEmpty()) {
searchAdapter.submitList(searchAdapter.currentList + response.items)
}

View File

@ -1,105 +1,118 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.fragments.SearchFragment">
<HorizontalScrollView
android:id="@+id/filter_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="10dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@id/recycler_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/filter_chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedChip="@id/chip_all"
app:selectionRequired="true"
app:singleLine="true"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_all"
style="@style/Chip"
android:text="@string/all" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_videos"
style="@style/Chip"
android:text="@string/videos" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_channels"
style="@style/Chip"
android:text="@string/channels" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_playlists"
style="@style/Chip"
android:text="@string/playlists" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_songs"
style="@style/Chip"
android:text="@string/music_songs" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_videos"
style="@style/Chip"
android:text="@string/music_videos" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_albums"
style="@style/Chip"
android:text="@string/music_albums" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_playlists"
style="@style/Chip"
android:text="@string/music_playlists" />
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp" />
tools:context=".ui.fragments.SearchResultFragment">
<LinearLayout
android:id="@+id/no_search_result"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical"
android:paddingTop="150dp"
android:visibility="gone">
android:id="@+id/search_results_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginVertical="10dp"
android:src="@drawable/ic_search" />
<TextView
android:layout_width="wrap_content"
<HorizontalScrollView
android:id="@+id/filter_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_search_result"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
android:paddingHorizontal="10dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@id/recycler_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/filter_chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedChip="@id/chip_all"
app:selectionRequired="true"
app:singleLine="true"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_all"
style="@style/Chip"
android:text="@string/all" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_videos"
style="@style/Chip"
android:text="@string/videos" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_channels"
style="@style/Chip"
android:text="@string/channels" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_playlists"
style="@style/Chip"
android:text="@string/playlists" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_songs"
style="@style/Chip"
android:text="@string/music_songs" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_videos"
style="@style/Chip"
android:text="@string/music_videos" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_albums"
style="@style/Chip"
android:text="@string/music_albums" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_music_playlists"
style="@style/Chip"
android:text="@string/music_playlists" />
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp" />
<LinearLayout
android:id="@+id/no_search_result"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical"
android:paddingTop="150dp"
android:visibility="gone">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginVertical="10dp"
android:src="@drawable/ic_search" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_search_result"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>