-
Notifications
You must be signed in to change notification settings - Fork 727
Add search suggestions to search UI #2294
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
NivinCNC
wants to merge
6
commits into
recloudstream:master
Choose a base branch
from
NivinCNC:search_sugg
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.
+351
−7
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
550099f
Add search suggestions to search UI
NivinCNC cdbf1cf
Refactor search layouts to use FrameLayout overlay
NivinCNC 1be2ee9
Add toggle for search suggestions feature
NivinCNC e7077f5
Increase search suggestion text size to match search_history_item
NivinCNC 62c2ee0
Add clear suggestions button to search suggestions overlay
NivinCNC 994d11b
Refactor search UI focus handling for phone layout
NivinCNC 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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchSuggestionAdapter.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,48 @@ | ||
| package com.lagradost.cloudstream3.ui.search | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import com.lagradost.cloudstream3.databinding.SearchSuggestionItemBinding | ||
| import com.lagradost.cloudstream3.ui.BaseDiffCallback | ||
| import com.lagradost.cloudstream3.ui.NoStateAdapter | ||
| import com.lagradost.cloudstream3.ui.ViewHolderState | ||
|
|
||
| const val SEARCH_SUGGESTION_CLICK = 0 | ||
| const val SEARCH_SUGGESTION_FILL = 1 | ||
|
|
||
| data class SearchSuggestionCallback( | ||
| val suggestion: String, | ||
| val clickAction: Int, | ||
| ) | ||
|
|
||
| class SearchSuggestionAdapter( | ||
| private val clickCallback: (SearchSuggestionCallback) -> Unit, | ||
| ) : NoStateAdapter<String>(diffCallback = BaseDiffCallback(itemSame = { a, b -> a == b })) { | ||
|
|
||
| override fun onCreateContent(parent: ViewGroup): ViewHolderState<Any> { | ||
| return ViewHolderState( | ||
| SearchSuggestionItemBinding.inflate(LayoutInflater.from(parent.context), parent, false), | ||
| ) | ||
| } | ||
|
|
||
| override fun onBindContent( | ||
| holder: ViewHolderState<Any>, | ||
| item: String, | ||
| position: Int | ||
| ) { | ||
| val binding = holder.view as? SearchSuggestionItemBinding ?: return | ||
| binding.apply { | ||
| suggestionText.text = item | ||
|
|
||
| // Click on the whole item to search | ||
| suggestionItem.setOnClickListener { | ||
| clickCallback.invoke(SearchSuggestionCallback(item, SEARCH_SUGGESTION_CLICK)) | ||
| } | ||
|
|
||
| // Click on the arrow to fill the search box without searching | ||
| suggestionFill.setOnClickListener { | ||
| clickCallback.invoke(SearchSuggestionCallback(item, SEARCH_SUGGESTION_FILL)) | ||
| } | ||
| } | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchSuggestionApi.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,60 @@ | ||
| package com.lagradost.cloudstream3.ui.search | ||
|
|
||
| import com.lagradost.cloudstream3.app | ||
| import com.lagradost.cloudstream3.mvvm.logError | ||
| import com.lagradost.nicehttp.NiceResponse | ||
|
|
||
| /** | ||
| * API for fetching search suggestions from external sources. | ||
| * Uses Google's suggestion API which provides movie/show related suggestions. | ||
| */ | ||
| object SearchSuggestionApi { | ||
| private const val GOOGLE_SUGGESTION_URL = "https://suggestqueries.google.com/complete/search" | ||
|
|
||
| /** | ||
| * Fetches search suggestions from Google's autocomplete API. | ||
| * | ||
| * @param query The search query to get suggestions for | ||
| * @return List of suggestion strings, empty list on failure | ||
| */ | ||
| suspend fun getSuggestions(query: String): List<String> { | ||
| if (query.isBlank() || query.length < 2) return emptyList() | ||
|
|
||
| return try { | ||
| val response = app.get( | ||
| GOOGLE_SUGGESTION_URL, | ||
| params = mapOf( | ||
| "client" to "firefox", // Returns JSON format | ||
| "q" to query, | ||
| "hl" to "en" // Language hint | ||
| ), | ||
| cacheTime = 60 * 24 // Cache for 1 day (cacheUnit default is Minutes) | ||
| ) | ||
|
|
||
| // Response format: ["query",["suggestion1","suggestion2",...]] | ||
| parseSuggestions(response) | ||
| } catch (e: Exception) { | ||
| logError(e) | ||
| emptyList() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses the Google suggestion JSON response. | ||
| * Format: ["query",["suggestion1","suggestion2",...]] | ||
| */ | ||
| private fun parseSuggestions(response: NiceResponse): List<String> { | ||
| return try { | ||
| val parsed = response.parsed<Array<Any>>() | ||
| val suggestions = parsed.getOrNull(1) | ||
| when (suggestions) { | ||
| is List<*> -> suggestions.filterIsInstance<String>().take(10) | ||
| is Array<*> -> suggestions.filterIsInstance<String>().take(10) | ||
| else -> emptyList() | ||
| } | ||
| } catch (e: Exception) { | ||
| logError(e) | ||
| emptyList() | ||
| } | ||
| } | ||
| } |
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
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,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="@android:color/white" | ||
| android:pathData="M5,15h2v-4.586l7.293,7.293l1.414,-1.414L8.414,9H13V7H5V15z"/> | ||
| </vector> |
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
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.
Uh oh!
There was an error while loading. Please reload this page.