UnifiedSearchViewModel.kt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.ui.unifiedsearch
  21. import android.content.Context
  22. import android.content.res.Resources
  23. import androidx.annotation.VisibleForTesting
  24. import androidx.lifecycle.MutableLiveData
  25. import androidx.lifecycle.ViewModel
  26. import com.nextcloud.client.account.CurrentAccountProvider
  27. import com.nextcloud.client.core.AsyncRunner
  28. import com.nextcloud.client.network.ClientFactory
  29. import com.owncloud.android.R
  30. import com.owncloud.android.datamodel.FileDataStorageManager
  31. import com.owncloud.android.lib.common.SearchResult
  32. import com.owncloud.android.lib.common.utils.Log_OC
  33. import com.owncloud.android.ui.activities.GetRemoteFileTask
  34. import javax.inject.Inject
  35. @Suppress("LongParameterList")
  36. open // legacy code had large dependencies
  37. class UnifiedSearchViewModel() : ViewModel() {
  38. lateinit var currentAccountProvider: CurrentAccountProvider
  39. lateinit var runner: AsyncRunner
  40. lateinit var clientFactory: ClientFactory
  41. lateinit var resources: Resources
  42. lateinit var context: Context
  43. private lateinit var repository: IUnifiedSearchRepository
  44. private var loadingStarted: Boolean = false
  45. private var last: Int = -1
  46. val isLoading: MutableLiveData<Boolean> = MutableLiveData<Boolean>(false)
  47. val searchResults = MutableLiveData<MutableList<SearchResult>>(mutableListOf())
  48. val error: MutableLiveData<String> = MutableLiveData<String>("")
  49. val query: MutableLiveData<String> = MutableLiveData()
  50. @Inject
  51. constructor(
  52. currentAccountProvider: CurrentAccountProvider,
  53. runner: AsyncRunner,
  54. clientFactory: ClientFactory,
  55. resources: Resources,
  56. context: Context) : this() {
  57. this.currentAccountProvider = currentAccountProvider
  58. this.runner = runner
  59. this.clientFactory = clientFactory
  60. this.resources = resources
  61. this.context = context
  62. repository = UnifiedSearchRemoteRepository(
  63. clientFactory,
  64. currentAccountProvider,
  65. runner
  66. )
  67. }
  68. open fun refresh() {
  69. last = -1
  70. searchResults.value = mutableListOf()
  71. loadMore()
  72. }
  73. open fun startLoading(query: String) {
  74. if (!loadingStarted) {
  75. loadingStarted = true
  76. this.query.value = query
  77. loadMore()
  78. }
  79. }
  80. open fun loadMore() {
  81. val queryTerm = query.value.orEmpty()
  82. if (isLoading.value != true && queryTerm.isNotBlank()) {
  83. isLoading.value = true
  84. repository.loadMore(queryTerm, this)
  85. }
  86. }
  87. fun openFile(fileUrl: String) {
  88. if (isLoading.value == false) {
  89. isLoading.value = true
  90. val user = currentAccountProvider.user
  91. val task = GetRemoteFileTask(
  92. context,
  93. fileUrl,
  94. clientFactory.create(currentAccountProvider.user),
  95. FileDataStorageManager(user.toPlatformAccount(), context.contentResolver),
  96. user
  97. )
  98. runner.postQuickTask(task, onResult = this::onFileRequestResult)
  99. }
  100. }
  101. open fun clearError() {
  102. error.value = ""
  103. }
  104. fun onError(error: Throwable) {
  105. Log_OC.d("Unified Search", "Error: " + error.stackTrace)
  106. }
  107. fun onSearchResult(result: SearchOnProviderTask.Result) {
  108. isLoading.value = false
  109. if (result.success) {
  110. // TODO append if already exists
  111. searchResults.value = mutableListOf(result.searchResult)
  112. } else {
  113. error.value = resources.getString(R.string.search_error)
  114. }
  115. Log_OC.d("Unified Search", "Success: " + result.success)
  116. Log_OC.d("Unified Search", "Size: " + result.searchResult.entries.size)
  117. }
  118. @VisibleForTesting
  119. fun setRepository(repository: IUnifiedSearchRepository) {
  120. this.repository = repository
  121. }
  122. // private fun onActivitiesRequestResult(result: GetActivityListTask.Result) {
  123. // isLoading.value = false
  124. // if (result.success) {
  125. // val existingActivities = activities.value ?: emptyList()
  126. // val newActivities = listOf(existingActivities, result.activities).flatten()
  127. // last = result.last
  128. // activities.value = newActivities
  129. // } else {
  130. // error.value = resources.getString(R.string.activities_error)
  131. // }
  132. // }
  133. private fun onFileRequestResult(result: GetRemoteFileTask.Result) {
  134. isLoading.value = false
  135. if (result.success) {
  136. // unifiedSearchFragment.showFile(result.file)
  137. //(file as MutableLiveData).value = result.file
  138. } else {
  139. error.value = "Error showing search result"
  140. }
  141. }
  142. }