Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9219752 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Load suggestions only if search term has three or more characters

parent 757f325f
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -123,9 +123,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On

        // Bind search view suggestions adapter to search suggestions list in view model
        searchViewModel.getSuggestions().observe(this, Observer {
            if (it != null) {
            populateSuggestionsAdapter(it)
            }
        })

        // Bind recycler view adapter to search results list in view model
@@ -204,11 +202,13 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
        return true
    }

    private fun populateSuggestionsAdapter(suggestions: ArrayList<String>) {
    private fun populateSuggestionsAdapter(suggestions: ArrayList<String>?) {
        val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SUGGESTION_KEY))
        if (suggestions != null) {
            for (i in 0 until suggestions.size) {
                cursor.addRow(arrayOf(i, suggestions[i]))
            }
        }
        searchView.suggestionsAdapter.changeCursor(cursor)
    }

+8 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.AsyncTask
import foundation.e.apps.application.model.Application
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.utils.Common
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Error

class SearchModel : SearchModelInterface {
@@ -40,10 +41,14 @@ class SearchModel : SearchModelInterface {

    override fun searchSuggestions(context: Context, searchQuery: String) {
        this.searchQuery = searchQuery
        if (searchQuery.length >= Constants.MIN_SEARCH_TERM_LENGTH) {
            if (Common.isNetworkAvailable(context)) {
                SearchSuggestionsTask(searchQuery, applicationManager!!, this)
                        .executeOnExecutor(Common.EXECUTOR, context)
            }
        } else {
            suggestionList.value = null
        }
    }

    override fun onSearchSuggestionsRetrieved(
+1 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ object Constants {
    const val REQUEST_METHOD = "GET"

    // Search
    const val MIN_SEARCH_TERM_LENGTH = 3;
    const val RESULTS_PER_PAGE = 20
    const val SUGGESTION_KEY = "suggestion"
    const val SUGGESTIONS_RESULTS = 5