Code cleanup

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2023-11-17 09:28:17 +01:00
parent 0106ba3478
commit 82526e07b2
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
2 changed files with 80 additions and 78 deletions

View file

@ -25,6 +25,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView 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.ProviderID
import com.owncloud.android.ui.unifiedsearch.UnifiedSearchSection import com.owncloud.android.ui.unifiedsearch.UnifiedSearchSection
import com.owncloud.android.ui.unifiedsearch.UnifiedSearchViewModel 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.DisplayUtils
import com.owncloud.android.utils.theme.ViewThemeUtils import com.owncloud.android.utils.theme.ViewThemeUtils
import javax.inject.Inject import javax.inject.Inject
@ -68,6 +70,22 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
private var searchView: SearchView? = null private var searchView: SearchView? = null
lateinit var vm: IUnifiedSearchViewModel 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 @Inject
lateinit var vmFactory: ViewModelFactory 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() { private fun setUpViewModel() {
vm.searchResults.observe(this, this::onSearchResultChanged) vm.searchResults.observe(this, this::onSearchResultChanged)
vm.isLoading.observe(this) { loading -> vm.isLoading.observe(this) { loading ->
@ -156,25 +210,14 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
} }
} }
@Suppress("DEPRECATION") private fun setupFileDisplayActivity() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { (activity as? FileDisplayActivity)?.run {
_binding = ListFragmentBinding.inflate(inflater, container, false) setMainFabVisible(false)
binding.listRoot.updatePadding(top = resources.getDimension(R.dimen.standard_half_padding).toInt()) updateActionBarTitleAndHomeButtonByString(null)
setUpBinding() }
setHasOptionsMenu(true)
return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { private fun setupAdapter() {
super.onViewCreated(view, savedInstanceState)
if (activity is FileDisplayActivity) {
val fileDisplayActivity = activity as FileDisplayActivity
fileDisplayActivity.setMainFabVisible(false)
fileDisplayActivity.updateActionBarTitleAndHomeButtonByString(null)
}
val gridLayoutManager = GridLayoutManager(requireContext(), 1) val gridLayoutManager = GridLayoutManager(requireContext(), 1)
adapter = UnifiedSearchListAdapter( adapter = UnifiedSearchListAdapter(
storageManager, storageManager,
@ -190,18 +233,10 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
binding.listRoot.adapter = adapter binding.listRoot.adapter = adapter
} }
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun showFile(file: OCFile) { private fun showFile(file: OCFile) {
activity.let { (activity as? FileDisplayActivity)?.let {
if (activity is FileDisplayActivity) { it.file = file
val fda = activity as FileDisplayActivity it.showFile("")
fda.file = file
fda.showFile("")
}
} }
} }
@ -217,16 +252,7 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
fun onSearchResultChanged(result: List<UnifiedSearchSection>) { fun onSearchResultChanged(result: List<UnifiedSearchSection>) {
Log_OC.d(TAG, "result") Log_OC.d(TAG, "result")
binding.emptyList.emptyListView.visibility = View.GONE binding.emptyList.emptyListView.visibility = View.GONE
adapter.setData(result.filterOutHiddenFiles(listOfHiddenFiles))
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)
} }
@VisibleForTesting @VisibleForTesting
@ -235,41 +261,6 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
setUpViewModel() 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 { override fun onQueryTextSubmit(query: String): Boolean {
vm.setQuery(query) vm.setQuery(query)
vm.initialQuery() vm.initialQuery()
@ -278,11 +269,12 @@ class UnifiedSearchFragment : Fragment(), Injectable, UnifiedSearchListInterface
override fun onQueryTextChange(newText: String?): Boolean { override fun onQueryTextChange(newText: String?): Boolean {
val closeButton = searchView?.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn) val closeButton = searchView?.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn)
if (newText?.isEmpty() == true) { closeButton?.visibility = if (newText?.isEmpty() == true) View.INVISIBLE else View.VISIBLE
closeButton?.visibility = View.INVISIBLE
} else {
closeButton?.visibility = View.VISIBLE
}
return true return true
} }
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
} }

View file

@ -10,3 +10,13 @@ data class UnifiedSearchSection(
val entries: List<SearchResultEntry>, val entries: List<SearchResultEntry>,
val hasMoreResults: Boolean 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() }
}