Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ suspend fun FileDataStorageManager.getGalleryItemsPageSuspended(
return result
}

fun FileDataStorageManager.searchFilesByName(file: OCFile, accountName: String, query: String): List<OCFile> =
fileDao.searchFilesInFolder(file.fileId, accountName, query).map {
createFileInstance(it)
}
fun FileDataStorageManager.searchFilesByName(file: OCFile, accountName: String, query: String): List<OCFile> {
val terms = query.toSearchTerms()
return fileDao.searchFilesInFolder(file.fileId, accountName, terms.firstOrNull().orEmpty())
.filter { it.name.orEmpty().containsAllTerms(terms) }
.map { createFileInstance(it) }
}

fun FileDataStorageManager.getDecryptedPath(file: OCFile): String {
val paths = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ fun String.extension(): String {

fun String.truncateWithEllipsis(limit: Int) = take(limit) + if (length > limit) StringConstants.THREE_DOT else ""

private val whitespaceRegex = Regex("""\s+""")

fun String.toSearchTerms(): List<String> = split(whitespaceRegex).filter { it.isNotEmpty() }

fun String.containsAllTerms(terms: List<String>): Boolean = terms.all { contains(it, ignoreCase = true) }

object StringConstants {
const val SLASH = "/"
const val DOT = "."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ package com.owncloud.android.ui.unifiedsearch

import com.nextcloud.android.lib.resources.search.UnifiedSearchRemoteOperation
import com.nextcloud.common.NextcloudClient
import com.nextcloud.utils.extensions.containsAllTerms
import com.nextcloud.utils.extensions.toSearchTerms
import com.owncloud.android.lib.common.SearchResult
import com.owncloud.android.lib.common.SearchResultEntry
import com.owncloud.android.lib.common.utils.Log_OC

class SearchOnProviderTask(
Expand All @@ -20,22 +23,52 @@ class SearchOnProviderTask(
) : () -> SearchOnProviderTask.Result {
companion object {
private const val TAG = "SearchOnProviderTask"
private const val FILES_PROVIDER_ID = "files"
private const val MAX_SERVER_PAGE_SIZE = 25
}

data class Result(val success: Boolean = false, val searchResult: SearchResult = SearchResult())

override fun invoke(): Result {
Log_OC.d(TAG, "Run task")
val result = UnifiedSearchRemoteOperation(provider, query, cursor, limit).execute(client)
val terms = query.toSearchTerms()
val result = if (provider == FILES_PROVIDER_ID && terms.size > 1) {
searchFilesMatchingAllTerms(terms)
} else {
search(query, cursor, limit)
}

Log_OC.d(TAG, "Task finished: " + result.isSuccess)
return if (result.isSuccess && result.resultData != null) {
Result(
success = true,
searchResult = result.resultData as SearchResult
)
Log_OC.d(TAG, "Task finished: " + result.success)
return result
}

private fun search(query: String, cursor: Int?, limit: Int): Result {
val result = UnifiedSearchRemoteOperation(provider, query, cursor, limit).execute(client)
val searchResult = result.resultData
return if (result.isSuccess && searchResult != null) {
Result(success = true, searchResult = searchResult)
} else {
Result()
}
}

private fun searchFilesMatchingAllTerms(terms: List<String>): Result {
val serverQuery = terms.maxBy { it.length }
val matches = mutableListOf<SearchResultEntry>()
var nextCursor = cursor
var page: SearchResult

do {
val result = search(serverQuery, nextCursor, MAX_SERVER_PAGE_SIZE)
if (!result.success) {
return result
}

page = result.searchResult
matches += page.entries.filter { it.title.containsAllTerms(terms) }
nextCursor = page.cursor?.toIntOrNull()
} while (matches.size < limit && page.entries.size == MAX_SERVER_PAGE_SIZE && nextCursor != null)

return Result(success = true, searchResult = page.copy(entries = matches))
}
}
Loading