Skip to content

Commit

Permalink
refactor basic search into subclass of FileSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Jan 15, 2024
1 parent e255500 commit 7f38062
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2014-2024 Arpit Khurana <[email protected]>, Vishal Nehra <[email protected]>,
* Emmanuel Messulam<[email protected]>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem

import android.content.Context
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.filesystem.root.ListFilesCommand.listFiles

class BasicSearch(context: Context) : FileSearch() {
private val applicationContext = context.applicationContext

override suspend fun search(
path: String,
filter: SearchFilter,
searchParameters: SearchParameters
) {
listFiles(
path,
SearchParameter.ROOT in searchParameters,
SearchParameter.SHOW_HIDDEN_FILES in searchParameters,
{ }
) { hybridFileParcelable: HybridFileParcelable ->
if (filter.searchFilter(hybridFileParcelable.getName(applicationContext)) &&
(
SearchParameter.SHOW_HIDDEN_FILES in searchParameters ||
!hybridFileParcelable.isHidden
)
) {
publishProgress(hybridFileParcelable)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ import android.content.Intent
import android.provider.MediaStore
import androidx.collection.LruCache
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager
import com.amaze.filemanager.R
import com.amaze.filemanager.adapters.data.LayoutElementParcelable
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.BasicSearch
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.DeepSearch
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.IndexedSearch
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchParameters
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.searchParametersFromBoolean
import com.amaze.filemanager.fileoperations.filesystem.OpenMode
import com.amaze.filemanager.filesystem.HybridFile
import com.amaze.filemanager.filesystem.HybridFileParcelable
import com.amaze.filemanager.filesystem.RootHelper
import com.amaze.filemanager.filesystem.files.MediaConnectionUtils.scanFile
import com.amaze.filemanager.filesystem.root.ListFilesCommand.listFiles
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_REGEX
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_REGEX_MATCHES
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_SHOW_HIDDENFILES
Expand All @@ -48,7 +50,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.slf4j.LoggerFactory
import java.io.File
import java.util.Locale

class MainActivityViewModel(val applicationContext: Application) :
AndroidViewModel(applicationContext) {
Expand Down Expand Up @@ -101,37 +102,18 @@ class MainActivityViewModel(val applicationContext: Application) :
* Perform basic search: searches on the current directory
*/
fun basicSearch(mainActivity: MainActivity, query: String):
MutableLiveData<ArrayList<HybridFileParcelable>> {
val hybridFileParcelables = ArrayList<HybridFileParcelable>()
LiveData<List<HybridFileParcelable>> {
val searchParameters = createSearchParameters(mainActivity)

val mutableLiveData:
MutableLiveData<ArrayList<HybridFileParcelable>> =
MutableLiveData(hybridFileParcelables)
val path = mainActivity.currentMainFragment?.currentPath ?: ""

val showHiddenFiles = PreferenceManager
.getDefaultSharedPreferences(mainActivity)
.getBoolean(PREFERENCE_SHOW_HIDDENFILES, false)
val basicSearch = BasicSearch(this.applicationContext)

viewModelScope.launch(Dispatchers.IO) {
listFiles(
mainActivity.currentMainFragment!!.currentPath!!,
mainActivity.isRootExplorer,
showHiddenFiles,
{ _: OpenMode? -> null }
) { hybridFileParcelable: HybridFileParcelable ->
if (hybridFileParcelable.getName(mainActivity)
.lowercase(Locale.getDefault())
.contains(query.lowercase(Locale.getDefault())) &&
(showHiddenFiles || !hybridFileParcelable.isHidden)
) {
hybridFileParcelables.add(hybridFileParcelable)

mutableLiveData.postValue(hybridFileParcelables)
}
}
basicSearch.search(query, path, searchParameters)
}

return mutableLiveData
return basicSearch.foundFilesLiveData
}

/**
Expand Down

0 comments on commit 7f38062

Please sign in to comment.