UnifiedSearchFragment.kt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2020 Tobias Kaminsky
  6. * Copyright (C) 2020 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.fragment
  22. import android.os.Bundle
  23. import android.view.LayoutInflater
  24. import android.view.View
  25. import android.view.ViewGroup
  26. import androidx.annotation.VisibleForTesting
  27. import androidx.fragment.app.Fragment
  28. import androidx.lifecycle.ViewModelProvider
  29. import androidx.recyclerview.widget.GridLayoutManager
  30. import com.nextcloud.client.account.CurrentAccountProvider
  31. import com.nextcloud.client.core.AsyncRunner
  32. import com.nextcloud.client.di.Injectable
  33. import com.nextcloud.client.di.ViewModelFactory
  34. import com.nextcloud.client.network.ClientFactory
  35. import com.owncloud.android.databinding.ListFragmentBinding
  36. import com.owncloud.android.datamodel.FileDataStorageManager
  37. import com.owncloud.android.lib.common.SearchResult
  38. import com.owncloud.android.lib.common.SearchResultEntry
  39. import com.owncloud.android.ui.activity.FileDisplayActivity
  40. import com.owncloud.android.ui.adapter.UnifiedSearchListAdapter
  41. import com.owncloud.android.ui.asynctasks.GetRemoteFileTask
  42. import com.owncloud.android.ui.interfaces.UnifiedSearchListInterface
  43. import com.owncloud.android.ui.unifiedsearch.UnifiedSearchViewModel
  44. import javax.inject.Inject
  45. /**
  46. * Starts query to all capable unified search providers and displays them Opens result in our app, redirect to other
  47. * apps, if installed, or opens browser
  48. */
  49. class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface {
  50. private lateinit var adapter: UnifiedSearchListAdapter
  51. private var _binding: ListFragmentBinding? = null
  52. private val binding get() = _binding!!
  53. lateinit var vm: UnifiedSearchViewModel
  54. @Inject
  55. lateinit var vmFactory: ViewModelFactory
  56. @Inject
  57. lateinit var storageManager: FileDataStorageManager
  58. @Inject
  59. lateinit var runner: AsyncRunner
  60. @Inject
  61. lateinit var currentAccountProvider: CurrentAccountProvider
  62. @Inject
  63. lateinit var clientFactory: ClientFactory
  64. override fun onCreate(savedInstanceState: Bundle?) {
  65. super.onCreate(savedInstanceState)
  66. vm = ViewModelProvider(this, vmFactory).get(UnifiedSearchViewModel::class.java)
  67. vm.searchResults.observe(this, this::onSearchResultChanged)
  68. val query = savedInstanceState?.getString(ARG_QUERY).orEmpty()
  69. if (query.isNotBlank()) {
  70. vm.startLoading(query)
  71. return
  72. }
  73. val queryArgument = arguments?.getString(ARG_QUERY).orEmpty()
  74. if (queryArgument.isNotBlank()) {
  75. vm.startLoading(queryArgument)
  76. }
  77. }
  78. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  79. _binding = ListFragmentBinding.inflate(inflater, container, false)
  80. return binding.root
  81. }
  82. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  83. super.onViewCreated(view, savedInstanceState)
  84. val gridLayoutManager = GridLayoutManager(requireContext(), 1)
  85. adapter = UnifiedSearchListAdapter(
  86. storageManager,
  87. this,
  88. currentAccountProvider.user,
  89. clientFactory,
  90. requireContext()
  91. )
  92. adapter.setLayoutManager(gridLayoutManager)
  93. binding.listRoot.layoutManager = gridLayoutManager
  94. binding.listRoot.adapter = adapter
  95. }
  96. override fun onPause() {
  97. super.onPause()
  98. // photoSearchTask?.cancel(true)
  99. }
  100. override fun onDestroyView() {
  101. super.onDestroyView()
  102. _binding = null
  103. }
  104. private fun showFile(result: GetRemoteFileTask.Result) {
  105. activity.let {
  106. if (activity is FileDisplayActivity) {
  107. val fda = activity as FileDisplayActivity
  108. fda.file = result.file
  109. fda.showFile("")
  110. }
  111. }
  112. }
  113. override fun onSearchResultClicked(searchResultEntry: SearchResultEntry) {
  114. openFile(searchResultEntry.remotePath())
  115. }
  116. fun openFile(fileUrl: String) {
  117. val user = currentAccountProvider.user
  118. val task = GetRemoteFileTask(
  119. requireContext(),
  120. fileUrl,
  121. clientFactory.create(currentAccountProvider.user),
  122. FileDataStorageManager(user.toPlatformAccount(), requireContext().contentResolver),
  123. user
  124. )
  125. runner.postQuickTask(task, onResult = this::showFile)
  126. }
  127. @VisibleForTesting
  128. fun onSearchResultChanged(list: MutableList<SearchResult>) {
  129. binding.emptyList.emptyListView.visibility = View.GONE
  130. adapter.setList(list)
  131. }
  132. @VisibleForTesting
  133. fun setViewModel(testViewModel: UnifiedSearchViewModel) {
  134. vm = testViewModel
  135. vm.searchResults.observe(this, this::onSearchResultChanged)
  136. }
  137. companion object {
  138. const val ARG_QUERY = "ARG_QUERY"
  139. /**
  140. * Public factory method to get fragment.
  141. */
  142. fun newInstance(query: String?): UnifiedSearchFragment {
  143. val fragment = UnifiedSearchFragment()
  144. val args = Bundle()
  145. args.putString(ARG_QUERY, query)
  146. fragment.arguments = args
  147. return fragment
  148. }
  149. }
  150. }