forked from AndreyAsadchy/Xtra
-
Notifications
You must be signed in to change notification settings - Fork 70
✨ Append the Recent Searches history #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burikoni
wants to merge
1
commit into
crackededed:master
Choose a base branch
from
burikoni:recent-searches-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/github/andreyasadchy/xtra/db/RecentSearchDao.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.github.andreyasadchy.xtra.db | ||
|
|
||
| import androidx.room.Dao | ||
| import androidx.room.Delete | ||
| import androidx.room.Insert | ||
| import androidx.room.OnConflictStrategy | ||
| import androidx.room.Query | ||
| import com.github.andreyasadchy.xtra.model.ui.RecentSearch | ||
|
|
||
| @Dao | ||
| interface RecentSearchDao { | ||
|
|
||
| @Query("SELECT * FROM recent_search ORDER BY lastSearched DESC LIMIT :limit") | ||
| fun recentSearches(limit: Int): List<RecentSearch> | ||
|
|
||
| @Query("SELECT * FROM recent_search WHERE `query` = :query") | ||
| fun find(query: String) : RecentSearch? | ||
|
|
||
| @Insert(onConflict = OnConflictStrategy.REPLACE) | ||
| fun insert(recentSearch: RecentSearch) | ||
|
|
||
| @Delete | ||
| fun delete(recentSearch: RecentSearch) | ||
|
|
||
| @Query("DELETE FROM recent_search") | ||
| fun deleteAll() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/github/andreyasadchy/xtra/model/ui/RecentSearch.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.github.andreyasadchy.xtra.model.ui | ||
|
|
||
| import androidx.room.Entity | ||
| import androidx.room.PrimaryKey | ||
|
|
||
| @Entity(tableName = "recent_search") | ||
| class RecentSearch( | ||
| val query: String, | ||
| var lastSearched: Long, | ||
| ) { | ||
| @PrimaryKey(autoGenerate = true) | ||
| var id = 0 | ||
| } |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/github/andreyasadchy/xtra/repository/RecentSearchRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.github.andreyasadchy.xtra.repository | ||
|
|
||
| import android.util.Log | ||
| import com.github.andreyasadchy.xtra.db.RecentSearchDao | ||
| import com.github.andreyasadchy.xtra.model.ui.RecentSearch | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class RecentSearchRepository @Inject constructor( | ||
| private val recentSearchDao: RecentSearchDao | ||
| ) { | ||
| suspend fun loadRecentSearches(limit: Int): List<RecentSearch> = withContext(Dispatchers.IO) { | ||
| recentSearchDao.recentSearches(limit) | ||
| } | ||
|
|
||
| suspend fun find(query: String) = withContext(Dispatchers.IO) { | ||
| recentSearchDao.find(query) | ||
| } | ||
|
|
||
| suspend fun save(recentSearch: RecentSearch) = withContext(Dispatchers.IO) { | ||
| recentSearchDao.insert(recentSearch) | ||
| } | ||
|
|
||
| suspend fun delete(recentSearch: RecentSearch) = withContext(Dispatchers.IO) { | ||
| recentSearchDao.delete(recentSearch) | ||
| } | ||
|
|
||
| suspend fun deleteAll() = withContext(Dispatchers.IO) { | ||
| recentSearchDao.deleteAll() | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/github/andreyasadchy/xtra/ui/search/RecentSearchAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.github.andreyasadchy.xtra.ui.search | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.LinearLayout | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.DiffUtil | ||
| import androidx.recyclerview.widget.ListAdapter | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.github.andreyasadchy.xtra.R | ||
| import com.github.andreyasadchy.xtra.model.ui.RecentSearch | ||
|
|
||
| class RecentSearchAdapter( | ||
| private val onRecentSearchItemSelected: (String) -> Unit, | ||
| private val onRecentSearchItemInserted: (String) -> Unit, | ||
| private val onRecentSearchItemLongClick: (RecentSearch) -> Unit, | ||
| ) : ListAdapter<RecentSearch, RecentSearchAdapter.ViewHolder>( | ||
| object : DiffUtil.ItemCallback<RecentSearch>() { | ||
| override fun areItemsTheSame(oldItem: RecentSearch, newItem: RecentSearch): Boolean = oldItem.query == newItem.query | ||
| override fun areContentsTheSame(oldItem: RecentSearch, newItem: RecentSearch): Boolean = oldItem.query == newItem.query | ||
| }) { | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
| val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_recent_search_list_item, parent, false) | ||
| return ViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
| val item = getItem(position) | ||
| holder.bind(item) | ||
| } | ||
|
|
||
| inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| private val textView: TextView = itemView.findViewById(R.id.recentSearchText) | ||
| private val recentSearchIcon : LinearLayout = itemView.findViewById(R.id.recentSearchSelect) | ||
| private val recentSearchInsert : LinearLayout = itemView.findViewById(R.id.recentSearchInsert) | ||
|
|
||
| fun bind(item: RecentSearch) { | ||
| textView.text = item.query | ||
| recentSearchIcon.setOnClickListener { | ||
| onRecentSearchItemSelected(item.query) | ||
| } | ||
| recentSearchIcon.setOnLongClickListener { | ||
| onRecentSearchItemLongClick(item) | ||
| true | ||
| } | ||
| recentSearchInsert.setOnClickListener { | ||
| onRecentSearchItemInserted(item.query) | ||
| } | ||
| } | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/github/andreyasadchy/xtra/ui/search/RecentSearchesFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.github.andreyasadchy.xtra.ui.search | ||
|
|
||
| import android.content.Context | ||
| import android.content.DialogInterface | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.viewModels | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.lifecycle.repeatOnLifecycle | ||
| import com.github.andreyasadchy.xtra.R | ||
| import com.github.andreyasadchy.xtra.databinding.FragmentRecentSearchBinding | ||
| import com.github.andreyasadchy.xtra.model.ui.RecentSearch | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import kotlinx.coroutines.flow.collectLatest | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| @AndroidEntryPoint | ||
| class RecentSearchesFragment : Fragment() { | ||
| private var _binding: FragmentRecentSearchBinding? = null | ||
| private val binding get() = _binding!! | ||
| private val viewModel by viewModels<SearchPagerViewModel>(ownerProducer = { requireParentFragment() }) | ||
| private lateinit var onItemSelected: (String) -> Unit | ||
| private lateinit var onItemInserted: (String) -> Unit | ||
| private lateinit var onItemLongClicked: (RecentSearch) -> Unit | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| onItemSelected = (requireParentFragment() as SearchPagerFragment)::setSelectedQuery | ||
| onItemInserted = (requireParentFragment() as SearchPagerFragment)::setInsertedQuery | ||
| onItemLongClicked = { item -> | ||
| AlertDialog.Builder(requireContext()) | ||
| .setMessage(getString(R.string.delete_recent_search_item)).setTitle(item.query) | ||
| .setPositiveButton(getString(R.string.delete)) | ||
| { _: DialogInterface, _: Int -> | ||
| viewModel.deleteRecentSearch(item) | ||
| } | ||
| .setNegativeButton(getString(android.R.string.cancel), null) | ||
| .setCancelable(false) | ||
| .create().show() | ||
| } | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
|
|
||
| val recentSearchAdapter = RecentSearchAdapter( | ||
| onItemSelected, | ||
| onItemInserted, | ||
| onItemLongClicked | ||
| ) | ||
|
|
||
| viewLifecycleOwner.lifecycleScope.launch { | ||
| repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
| viewModel.recentSearches.collectLatest { | ||
| recentSearchAdapter.submitList(it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| binding.recentSearchesList.adapter = recentSearchAdapter | ||
| } | ||
|
|
||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
| _binding = FragmentRecentSearchBinding.inflate(inflater, container, false) | ||
| return binding.root | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| _binding = null | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it would be nicer to have a recyclerview adapter on each search fragment that contains the recent search items so that you could just swap the adapter to show recent searches instead of having to deal with this fragment here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you mean there. Are you referring to StreamSearchFragment, VideoSearchFragment, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the RecentSearches would only need to be related to the SearchPager. I don’t see what connection it would have with the fragments of the results.
I think we are thinking about different implementations.
Screen_recording_20251107_064804.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the recyclerview could show the recent searches instead of the search results when the search text box is empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understand what you're saying: it's about placing the recent search items in the same RecyclerView and not creating a new fragment, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes