@@ -24,6 +24,7 @@ import com.github.droidworksstudio.common.searchCustomSearchEngine
2424import com.github.droidworksstudio.common.searchOnPlayStore
2525import com.github.droidworksstudio.common.showKeyboard
2626import com.github.droidworksstudio.common.showLongToast
27+ import com.github.droidworksstudio.fuzzywuzzy.FuzzyFinder
2728import com.github.droidworksstudio.launcher.R
2829import com.github.droidworksstudio.launcher.adapter.drawer.DrawAdapter
2930import com.github.droidworksstudio.launcher.data.entities.AppInfo
@@ -35,6 +36,7 @@ import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
3536import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
3637import com.github.droidworksstudio.launcher.listener.ScrollEventListener
3738import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.AppInfoBottomSheetFragment
39+ import com.github.droidworksstudio.launcher.utils.Constants
3840import com.github.droidworksstudio.launcher.viewmodel.AppViewModel
3941import dagger.hilt.android.AndroidEntryPoint
4042import kotlinx.coroutines.launch
@@ -194,7 +196,7 @@ class DrawFragment : Fragment(),
194196 // Use repeatOnLifecycle to manage the lifecycle state
195197 repeatOnLifecycle(Lifecycle .State .CREATED ) {
196198 val trimmedQuery = searchQuery.trim()
197- viewModel.searchAppInfo(trimmedQuery ).collect { searchResults ->
199+ viewModel.searchAppInfo().collect { searchResults ->
198200 val numberOfItemsLeft = searchResults.size
199201 val appResults = searchResults.firstOrNull()
200202 if (numberOfItemsLeft == 0 && ! requireContext().searchOnPlayStore(trimmedQuery)) {
@@ -211,34 +213,70 @@ class DrawFragment : Fragment(),
211213 }
212214
213215 private fun searchApp (query : String ) {
214- val searchQuery = " %$query %"
215-
216216 // Launch a coroutine tied to the lifecycle of the view
217217 viewLifecycleOwner.lifecycleScope.launch {
218218 // Repeat the block when the lifecycle is at least CREATED
219219 repeatOnLifecycle(Lifecycle .State .CREATED ) {
220+ val trimmedQuery = query.trim()
221+
220222 // Collect search results from the ViewModel
221- viewModel.searchAppInfo(searchQuery).collect { searchResults ->
222- val numberOfItemsLeft = searchResults.size
223- val appResults = searchResults.firstOrNull()
223+ viewModel.searchAppInfo().collect { searchResults ->
224+ // Filter and score results using FuzzyFinder
225+ val filteredResults = searchResults
226+ .map { appInfo ->
227+ val score = FuzzyFinder .scoreApp(appInfo, trimmedQuery, Constants .FILTER_STRENGTH_MAX )
228+ appInfo to score // Pairing app info with its score
229+ }
230+ .filter { it.second > 25 } // Only keep results with a positive score
231+ .sortedByDescending { it.second } // Sort results by score, descending
232+
233+ // Applying additional filtering based on preferences
234+ val scoredApps = filteredResults.toMap()
235+
236+ val finalResults = if (preferenceHelper.filterStrength >= 1 ) {
237+ // Filtering based on score strength
238+ if (preferenceHelper.searchFromStart) {
239+ // Filter apps that start with the search query and score higher than the filter strength
240+ scoredApps.filter { (app, _) ->
241+ app.appName.startsWith(trimmedQuery, ignoreCase = true )
242+ }
243+ .filter { (_, score) -> score > preferenceHelper.filterStrength }
244+ .map { it.key }
245+ .toMutableList()
246+ } else {
247+ // Filter based on score strength alone
248+ scoredApps.filterValues { it > preferenceHelper.filterStrength }
249+ .keys
250+ .toMutableList()
251+ }
252+ } else {
253+ // If filter strength is less than 1, normalize app names for both cases
254+ searchResults.filter { app ->
255+ FuzzyFinder .normalizeString(app.appName, trimmedQuery)
256+ }.toMutableList()
257+ }
258+
259+
260+ val numberOfItemsLeft = finalResults.size
261+ val appResults = finalResults.firstOrNull()
262+
224263 when (numberOfItemsLeft) {
225264 1 -> {
226265 appResults?.let { appInfo ->
227266 if (preferenceHelper.automaticOpenApp) observeBioAuthCheck(appInfo)
228267 }
229- drawAdapter.submitList(searchResults )
268+ drawAdapter.submitList(finalResults )
230269 }
231270
232271 else -> {
233- drawAdapter.submitList(searchResults )
272+ drawAdapter.submitList(finalResults )
234273 }
235274 }
236275 }
237276 }
238277 }
239278 }
240279
241-
242280 private fun showSelectedApp (appInfo : AppInfo ) {
243281 binding.searchViewText.setQuery(" " , false )
244282
0 commit comments