diff --git a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt index 826fb761e7d7ea5487d5db92d06f909952ff94c0..0a948b9182eec163de0d37c3af7b571a944d6437 100644 --- a/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt +++ b/app/src/main/java/foundation/e/apps/data/playstore/utils/GPlayHttpClient.kt @@ -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 { diff --git a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt index 492b3b6dc0f2d73571e609558aea82d05bcfd24a..e3db9336cefb9a30f13fa2defd8edf9e6f4c7745 100644 --- a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt @@ -1,6 +1,5 @@ /* - * 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 . + * */ 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 + } }