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

Commit c116c97f authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

feat: implement debounced search in getting search suggestions

Add a delay of 500ms to get search suggestions on search query change.

It reduces extra network calls to `https://android.clients.google.com/fdfe/searchSuggest?q=<query>`.
parent bb7c4407
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -48,7 +48,8 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject

class GPlayHttpClient @Inject constructor(
    private val cache: Cache, loggingInterceptor: HttpLoggingInterceptor
    private val cache: Cache,
    loggingInterceptor: HttpLoggingInterceptor
) : IHttpClient {

    companion object {
+20 −5
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 * Copyright (C) 2021-2024 MURENA SAS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
@@ -14,6 +13,7 @@
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.ui.search
@@ -44,9 +44,9 @@ import com.facebook.shimmer.ShimmerFrameLayout
import com.google.android.material.chip.Chip
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.R
import foundation.e.apps.data.enums.Status
import foundation.e.apps.data.application.ApplicationInstaller
import foundation.e.apps.data.application.data.Application
import foundation.e.apps.data.enums.Status
import foundation.e.apps.data.install.models.AppInstall
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.login.exceptions.GPlayLoginException
@@ -61,6 +61,9 @@ import foundation.e.apps.ui.application.subFrags.ApplicationDialogFragment
import foundation.e.apps.ui.applicationlist.ApplicationListRVAdapter
import foundation.e.apps.ui.parentFragment.TimeoutFragment
import foundation.e.apps.utils.isNetworkAvailable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@@ -102,6 +105,8 @@ class SearchFragment :
     */
    private var searchText = ""

    private var searchJob: Job? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = FragmentSearchBinding.bind(view)
@@ -451,12 +456,18 @@ class SearchFragment :
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        newText?.let { text ->
        newText?.takeIf { it.isNotEmpty() }?.let(::doDebouncedSearch)
        return true
    }

    private fun doDebouncedSearch(text: String) {
        searchJob?.cancel()
        searchJob = lifecycleScope.launch(Dispatchers.Main.immediate) {
            delay(SEARCH_DEBOUNCE_DELAY_MILLIS)
            authObjects.value?.find { it is AuthObject.GPlayAuth }?.run {
                searchViewModel.getSearchSuggestions(text, this as AuthObject.GPlayAuth)
            }
        }
        return true
    }

    override fun onSuggestionSelect(position: Int): Boolean {
@@ -525,4 +536,8 @@ class SearchFragment :
    override fun cancelDownload(app: Application) {
        mainActivityViewModel.cancelDownload(app)
    }

    companion object {
        private const val SEARCH_DEBOUNCE_DELAY_MILLIS = 500L
    }
}