mirror of
https://github.com/nextcloud/android.git
synced 2024-12-18 06:51:55 +03:00
Code cleanup
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
0106ba3478
commit
82526e07b2
2 changed files with 80 additions and 78 deletions
|
@ -25,6 +25,7 @@ import android.os.Bundle
|
|||
import android.view.LayoutInflater
|
||||
import android.view.Menu
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
|
@ -53,6 +54,7 @@ import com.owncloud.android.ui.unifiedsearch.IUnifiedSearchViewModel
|
|||
import com.owncloud.android.ui.unifiedsearch.ProviderID
|
||||
import com.owncloud.android.ui.unifiedsearch.UnifiedSearchSection
|
||||
import com.owncloud.android.ui.unifiedsearch.UnifiedSearchViewModel
|
||||
import com.owncloud.android.ui.unifiedsearch.filterOutHiddenFiles
|
||||
import com.owncloud.android.utils.DisplayUtils
|
||||
import com.owncloud.android.utils.theme.ViewThemeUtils
|
||||
import javax.inject.Inject
|
||||
|
@ -68,6 +70,22 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
private var searchView: SearchView? = null
|
||||
lateinit var vm: IUnifiedSearchViewModel
|
||||
|
||||
companion object {
|
||||
private const val TAG = "UnifiedSearchFragment"
|
||||
|
||||
const val ARG_QUERY = "ARG_QUERY"
|
||||
const val ARG_HIDDEN_FILES = "ARG_HIDDEN_FILES"
|
||||
|
||||
fun newInstance(query: String?, listOfHiddenFiles: ArrayList<String>): UnifiedSearchFragment {
|
||||
val fragment = UnifiedSearchFragment()
|
||||
val args = Bundle()
|
||||
args.putString(ARG_QUERY, query)
|
||||
args.putStringArrayList(ARG_HIDDEN_FILES, listOfHiddenFiles)
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
lateinit var vmFactory: ViewModelFactory
|
||||
|
||||
|
@ -104,6 +122,42 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = ListFragmentBinding.inflate(inflater, container, false)
|
||||
binding.listRoot.updatePadding(top = resources.getDimension(R.dimen.standard_half_padding).toInt())
|
||||
setUpBinding()
|
||||
|
||||
setHasOptionsMenu(true)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
setupFileDisplayActivity()
|
||||
setupAdapter()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
val item = menu.findItem(R.id.action_search)
|
||||
setupSearchView(item)
|
||||
}
|
||||
|
||||
private fun setupSearchView(item: MenuItem) {
|
||||
(item.actionView as? SearchView?)?.run {
|
||||
// Required to align with TextView width.
|
||||
// Because this fragment is opened with TextView onClick on the previous screen
|
||||
maxWidth = Integer.MAX_VALUE
|
||||
viewThemeUtils.androidx.themeToolbarSearchView(this)
|
||||
setQuery(vm.query.value, false)
|
||||
setOnQueryTextListener(this@UnifiedSearchFragment)
|
||||
isIconified = false
|
||||
clearFocus()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setUpViewModel() {
|
||||
vm.searchResults.observe(this, this::onSearchResultChanged)
|
||||
vm.isLoading.observe(this) { loading ->
|
||||
|
@ -156,25 +210,14 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = ListFragmentBinding.inflate(inflater, container, false)
|
||||
binding.listRoot.updatePadding(top = resources.getDimension(R.dimen.standard_half_padding).toInt())
|
||||
setUpBinding()
|
||||
|
||||
setHasOptionsMenu(true)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
if (activity is FileDisplayActivity) {
|
||||
val fileDisplayActivity = activity as FileDisplayActivity
|
||||
fileDisplayActivity.setMainFabVisible(false)
|
||||
fileDisplayActivity.updateActionBarTitleAndHomeButtonByString(null)
|
||||
private fun setupFileDisplayActivity() {
|
||||
(activity as? FileDisplayActivity)?.run {
|
||||
setMainFabVisible(false)
|
||||
updateActionBarTitleAndHomeButtonByString(null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupAdapter() {
|
||||
val gridLayoutManager = GridLayoutManager(requireContext(), 1)
|
||||
adapter = UnifiedSearchListAdapter(
|
||||
storageManager,
|
||||
|
@ -190,18 +233,10 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
binding.listRoot.adapter = adapter
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
private fun showFile(file: OCFile) {
|
||||
activity.let {
|
||||
if (activity is FileDisplayActivity) {
|
||||
val fda = activity as FileDisplayActivity
|
||||
fda.file = file
|
||||
fda.showFile("")
|
||||
}
|
||||
(activity as? FileDisplayActivity)?.let {
|
||||
it.file = file
|
||||
it.showFile("")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -217,16 +252,7 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
fun onSearchResultChanged(result: List<UnifiedSearchSection>) {
|
||||
Log_OC.d(TAG, "result")
|
||||
binding.emptyList.emptyListView.visibility = View.GONE
|
||||
|
||||
val filteredResult = result.map { searchSection ->
|
||||
val entriesWithoutHiddenFiles = searchSection.entries.filterNot { entry ->
|
||||
listOfHiddenFiles.contains(entry.title)
|
||||
}
|
||||
|
||||
searchSection.copy(entries = entriesWithoutHiddenFiles)
|
||||
}.filter { it.entries.isNotEmpty() }
|
||||
|
||||
adapter.setData(filteredResult)
|
||||
adapter.setData(result.filterOutHiddenFiles(listOfHiddenFiles))
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
@ -235,41 +261,6 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
setUpViewModel()
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
val item = menu.findItem(R.id.action_search)
|
||||
searchView = item.actionView as SearchView?
|
||||
|
||||
// Required to align with TextView width.
|
||||
// Because this fragment is opened with TextView onClick on the previous screen
|
||||
searchView?.maxWidth = Integer.MAX_VALUE
|
||||
|
||||
viewThemeUtils.androidx.themeToolbarSearchView(searchView!!)
|
||||
|
||||
searchView?.setQuery(vm.query.value, false)
|
||||
searchView?.setOnQueryTextListener(this)
|
||||
searchView?.isIconified = false
|
||||
searchView?.clearFocus()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "UnifiedSearchFragment"
|
||||
const val ARG_QUERY = "ARG_QUERY"
|
||||
const val ARG_HIDDEN_FILES = "ARG_HIDDEN_FILES"
|
||||
|
||||
/**
|
||||
* Public factory method to get fragment.
|
||||
*/
|
||||
fun newInstance(query: String?, listOfHiddenFiles: ArrayList<String>): UnifiedSearchFragment {
|
||||
val fragment = UnifiedSearchFragment()
|
||||
val args = Bundle()
|
||||
args.putString(ARG_QUERY, query)
|
||||
args.putStringArrayList(ARG_HIDDEN_FILES, listOfHiddenFiles)
|
||||
fragment.arguments = args
|
||||
return fragment
|
||||
}
|
||||
}
|
||||
|
||||
override fun onQueryTextSubmit(query: String): Boolean {
|
||||
vm.setQuery(query)
|
||||
vm.initialQuery()
|
||||
|
@ -278,11 +269,12 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
|
|||
|
||||
override fun onQueryTextChange(newText: String?): Boolean {
|
||||
val closeButton = searchView?.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn)
|
||||
if (newText?.isEmpty() == true) {
|
||||
closeButton?.visibility = View.INVISIBLE
|
||||
} else {
|
||||
closeButton?.visibility = View.VISIBLE
|
||||
}
|
||||
closeButton?.visibility = if (newText?.isEmpty() == true) View.INVISIBLE else View.VISIBLE
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,3 +10,13 @@ data class UnifiedSearchSection(
|
|||
val entries: List<SearchResultEntry>,
|
||||
val hasMoreResults: Boolean
|
||||
)
|
||||
|
||||
fun List<UnifiedSearchSection>.filterOutHiddenFiles(listOfHiddenFiles: List<String>): List<UnifiedSearchSection> {
|
||||
return map { searchSection ->
|
||||
val entriesWithoutHiddenFiles = searchSection.entries.filterNot { entry ->
|
||||
listOfHiddenFiles.contains(entry.title)
|
||||
}
|
||||
|
||||
searchSection.copy(entries = entriesWithoutHiddenFiles)
|
||||
}.filter { it.entries.isNotEmpty() }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue