From 82526e07b2415faba09c8ff69396494c10e94abc Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 17 Nov 2023 09:28:17 +0100 Subject: [PATCH] Code cleanup Signed-off-by: alperozturk --- .../ui/fragment/UnifiedSearchFragment.kt | 148 +++++++++--------- .../ui/unifiedsearch/UnifiedSearchModel.kt | 10 ++ 2 files changed, 80 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/UnifiedSearchFragment.kt b/app/src/main/java/com/owncloud/android/ui/fragment/UnifiedSearchFragment.kt index 201966bcb7..7afe8c32c0 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/UnifiedSearchFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/UnifiedSearchFragment.kt @@ -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): 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 + private fun setupFileDisplayActivity() { + (activity as? FileDisplayActivity)?.run { + setMainFabVisible(false) + updateActionBarTitleAndHomeButtonByString(null) + } } - 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 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) { 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): 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(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 + } } diff --git a/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchModel.kt b/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchModel.kt index 84167b27cf..6b515a9821 100644 --- a/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchModel.kt +++ b/app/src/main/java/com/owncloud/android/ui/unifiedsearch/UnifiedSearchModel.kt @@ -10,3 +10,13 @@ data class UnifiedSearchSection( val entries: List, val hasMoreResults: Boolean ) + +fun List.filterOutHiddenFiles(listOfHiddenFiles: List): List { + return map { searchSection -> + val entriesWithoutHiddenFiles = searchSection.entries.filterNot { entry -> + listOfHiddenFiles.contains(entry.title) + } + + searchSection.copy(entries = entriesWithoutHiddenFiles) + }.filter { it.entries.isNotEmpty() } +}