diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/UnifiedSearchListAdapter.kt b/app/src/main/java/com/owncloud/android/ui/adapter/UnifiedSearchListAdapter.kt index 07f4842422ba..d88e47c73c47 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/UnifiedSearchListAdapter.kt +++ b/app/src/main/java/com/owncloud/android/ui/adapter/UnifiedSearchListAdapter.kt @@ -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]) + } } } @@ -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() + } } } } diff --git a/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchViewModel.kt b/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchViewModel.kt index 0a3a3f6d279b..10f0239c02a8 100644 --- a/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchViewModel.kt +++ b/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchViewModel.kt @@ -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 = 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 @@ -62,7 +56,6 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic get() = getApplication().applicationContext private lateinit var repository: IUnifiedSearchRepository - private var loadingStarted: Boolean = false private var results: MutableMap = mutableMapOf() override val isLoading = MutableLiveData(false) @@ -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 */ @@ -146,6 +131,7 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic isLoading.value = false } } + else -> block() } } @@ -166,6 +152,7 @@ class UnifiedSearchViewModel(application: Application) : AndroidViewModel(applic val fullUrl = serverUrl + result.resourceUrl Uri.parse(fullUrl) } + else -> uri } } @@ -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) } @@ -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 ->