Skip to content

Commit

Permalink
Merge pull request nextcloud#13184 from nextcloud/bugfix/unified-sear…
Browse files Browse the repository at this point in the history
…ch-limit

Bugfix Unified Search Load More
  • Loading branch information
alperozturk96 authored Jun 27, 2024
2 parents dc22177 + a971dbd commit f6de7a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ class UnifiedSearchListAdapter(
}

override fun onBindHeaderViewHolder(holder: SectionedViewHolder, section: Int, expanded: Boolean) {
val headerViewHolder = holder as UnifiedSearchHeaderViewHolder
headerViewHolder.bind(sections[section])
(holder as UnifiedSearchHeaderViewHolder).run {
bind(sections[section])
}
}

override fun onBindFooterViewHolder(holder: SectionedViewHolder, section: Int) {
if (sections[section].hasMoreResults) {
val footerViewHolder = holder as UnifiedSearchFooterViewHolder
footerViewHolder.bind(sections[section])
(holder as UnifiedSearchFooterViewHolder).run {
bind(sections[section])
}
}
}

Expand All @@ -126,17 +128,19 @@ class UnifiedSearchListAdapter(
absolutePosition: Int
) {
// TODO different binding (and also maybe diff UI) for non-file results
val itemViewHolder = holder as UnifiedSearchItemViewHolder
val entry = sections[section].entries[relativePosition]
itemViewHolder.bind(entry)
(holder as UnifiedSearchItemViewHolder).run {
val entry = sections[section].entries[relativePosition]
bind(entry)
}
}

override fun onViewAttachedToWindow(holder: SectionedViewHolder) {
if (holder is UnifiedSearchItemViewHolder) {
val thumbnailShimmer = holder.binding.thumbnailShimmer
if (thumbnailShimmer.visibility == View.VISIBLE) {
thumbnailShimmer.setImageResource(R.drawable.background)
thumbnailShimmer.resetLoader()
holder.binding.thumbnailShimmer.run {
if (visibility == View.VISIBLE) {
setImageResource(R.drawable.background)
resetLoader()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,20 @@ import javax.inject.Inject
class UnifiedSearchViewModel(application: Application) : AndroidViewModel(application), IUnifiedSearchViewModel {
companion object {
private const val TAG = "UnifiedSearchViewModel"
private const val DEFAULT_LIMIT = 5
private const val FILES_PROVIDER_ID = "files"
}

private data class UnifiedSearchMetadata(
var results: MutableList<SearchResult> = mutableListOf()
) {
fun nextCursor(): Int? = results.lastOrNull()?.cursor?.toInt()
fun name(): String? = results.lastOrNull()?.name
fun isFinished(): Boolean {
if (results.isEmpty()) {
return false
}
val lastResult = results.last()
return when {
!lastResult.isPaginated -> true
lastResult.entries.size < DEFAULT_LIMIT -> true
else -> false
fun nextCursor(): Int? {
return try {
results.lastOrNull()?.cursor?.toInt()
} catch (e: NumberFormatException) {
null
}
}
fun name(): String? = results.lastOrNull()?.name
}

private lateinit var currentAccountProvider: CurrentAccountProvider
Expand All @@ -62,7 +56,6 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
get() = getApplication<Application>().applicationContext

private lateinit var repository: IUnifiedSearchRepository
private var loadingStarted: Boolean = false
private var results: MutableMap<ProviderID, UnifiedSearchMetadata> = mutableMapOf()

override val isLoading = MutableLiveData(false)
Expand Down Expand Up @@ -94,14 +87,6 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
)
}

open fun startLoading(query: String) {
if (!loadingStarted) {
loadingStarted = true
this.query.value = query
initialQuery()
}
}

/**
* Clears data and queries all available providers
*/
Expand Down Expand Up @@ -146,6 +131,7 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
isLoading.value = false
}
}

else -> block()
}
}
Expand All @@ -166,6 +152,7 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
val fullUrl = serverUrl + result.resourceUrl
Uri.parse(fullUrl)
}

else -> uri
}
}
Expand All @@ -185,10 +172,6 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
}
}

open fun clearError() {
error.value = ""
}

fun onError(error: Throwable) {
Log_OC.e(TAG, "Error: " + error.stackTrace)
}
Expand All @@ -213,11 +196,13 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic
searchResults.value = results
.filter { it.value.results.isNotEmpty() }
.map { (key, value) ->
val isLastEntryHaveValue = results[key]?.results?.last()?.entries?.isEmpty() != true

UnifiedSearchSection(
providerID = key,
name = value.name()!!,
entries = value.results.flatMap { it.entries },
hasMoreResults = !value.isFinished()
hasMoreResults = isLastEntryHaveValue && results[key]?.nextCursor() != null
)
}
.sortedWith { o1, o2 ->
Expand Down

0 comments on commit f6de7a8

Please sign in to comment.