Skip to main content

Search

Use the following code example to find files and folders whose names contain a given string.

var disposable: Disposable? = null
var searchPath = SELECTED_PATH // search root path

// 1. Create subject that connected to search edit text listener.
var queryStringSource: Subject<String> = BehaviorSubject.createDefault("")

// 2. Connect subject to search edit text listener.
private val searchQueryListener: SearchView.OnQueryTextListener =
object : SearchView.OnQueryTextListener {

// Each time user enters the text, edit text listener calls subject.onNext() and
// observable returned by search method will emit new list of file items.

override fun onQueryTextSubmit(query: String): Boolean {
queryStringSource.onNext(query)
return false
}

override fun onQueryTextChange(newText: String): Boolean {
queryStringSource.onNext(newText)
return false
}
}

// ...

// 3. Call search with that subject as queryStringSource and subscribe on returned observable that emits result of search.
disposable = FileManager.browser.search(queryStringSource, searchPath)
.subscribe { // it: List<FileItem>!
// Update your recycler view adapter with received list.
}

// You must call dispose() on disposable that returned by subscribe() method,
// when it no longer needed, for example in your fragment’s onStop() or onPause().
disposable?.dispose()

Links - FileTreeBrowser, FileItem.