From 9d8aa44ef07bc586104c6b35acdfd42895ce65fe Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Mon, 22 Jan 2024 18:18:03 +0100 Subject: [PATCH 1/2] change `SearchRecyclerViewAdapter` to work with `SearchResult` --- .../adapters/SearchRecyclerViewAdapter.kt | 37 ++++++++++--------- .../ui/views/appbar/SearchView.java | 7 +--- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt b/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt index d9ea4c87ce..02ad78f390 100644 --- a/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt +++ b/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt @@ -31,27 +31,30 @@ import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.amaze.filemanager.R import com.amaze.filemanager.application.AppConfig -import com.amaze.filemanager.filesystem.HybridFileParcelable +import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult import com.amaze.filemanager.ui.activities.MainActivity import com.amaze.filemanager.ui.colors.ColorPreference import java.util.Random class SearchRecyclerViewAdapter : - ListAdapter( + ListAdapter( - object : DiffUtil.ItemCallback() { + object : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: HybridFileParcelable, - newItem: HybridFileParcelable + oldItem: SearchResult, + newItem: SearchResult ): Boolean { - return oldItem.path == newItem.path && oldItem.name == newItem.name + return oldItem.file.path == newItem.file.path && + oldItem.file.name == newItem.file.name } override fun areContentsTheSame( - oldItem: HybridFileParcelable, - newItem: HybridFileParcelable + oldItem: SearchResult, + newItem: SearchResult ): Boolean { - return oldItem.path == newItem.path && oldItem.name == newItem.name + return oldItem.file.path == newItem.file.path && + oldItem.file.name == newItem.file.name && + oldItem.matchRange == newItem.matchRange } } ) { @@ -62,17 +65,17 @@ class SearchRecyclerViewAdapter : } override fun onBindViewHolder(holder: SearchRecyclerViewAdapter.ViewHolder, position: Int) { - val item = getItem(position) + val (file, matchResult) = getItem(position) - holder.fileNameTV.text = item.name - holder.filePathTV.text = item.path.substring(0, item.path.lastIndexOf("/")) + holder.fileNameTV.text = file.name + holder.filePathTV.text = file.path.substring(0, file.path.lastIndexOf("/")) holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context)) val colorPreference = (AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference - if (item.isDirectory) { + if (file.isDirectory) { holder.colorView.setBackgroundColor(colorPreference.primaryFirstTab) } else { holder.colorView.setBackgroundColor(colorPreference.accent) @@ -93,16 +96,16 @@ class SearchRecyclerViewAdapter : view.setOnClickListener { - val item = getItem(adapterPosition) + val (file, _) = getItem(adapterPosition) - if (!item.isDirectory) { - item.openFile( + if (!file.isDirectory) { + file.openFile( AppConfig.getInstance().mainActivityContext as MainActivity?, false ) } else { (AppConfig.getInstance().mainActivityContext as MainActivity?) - ?.goToMain(item.path) + ?.goToMain(file.path) } (AppConfig.getInstance().mainActivityContext as MainActivity?) diff --git a/app/src/main/java/com/amaze/filemanager/ui/views/appbar/SearchView.java b/app/src/main/java/com/amaze/filemanager/ui/views/appbar/SearchView.java index c23e072a1d..2975035013 100644 --- a/app/src/main/java/com/amaze/filemanager/ui/views/appbar/SearchView.java +++ b/app/src/main/java/com/amaze/filemanager/ui/views/appbar/SearchView.java @@ -33,7 +33,6 @@ import com.amaze.filemanager.adapters.SearchRecyclerViewAdapter; import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult; import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResultListSorter; -import com.amaze.filemanager.filesystem.HybridFileParcelable; import com.amaze.filemanager.filesystem.files.sort.DirSortBy; import com.amaze.filemanager.filesystem.files.sort.SortBy; import com.amaze.filemanager.filesystem.files.sort.SortOrder; @@ -376,11 +375,7 @@ private void updateResultList(List newResults, String searchTerm) ArrayList items = new ArrayList<>(newResults); Collections.sort( items, new SearchResultListSorter(DirSortBy.NONE_ON_TOP, sortType, searchTerm)); - ArrayList files = new ArrayList<>(); - for (SearchResult searchResult : items) { - files.add(searchResult.getFile()); - } - searchRecyclerViewAdapter.submitList(files); + searchRecyclerViewAdapter.submitList(items); searchRecyclerViewAdapter.notifyDataSetChanged(); } From 053e834554902efe8baf6c1bc5052a857d2c55a8 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Mon, 22 Jan 2024 18:18:59 +0100 Subject: [PATCH 2/2] add highlight to the file name text view --- .../adapters/SearchRecyclerViewAdapter.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt b/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt index 02ad78f390..7859f68a06 100644 --- a/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt +++ b/app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt @@ -21,6 +21,9 @@ package com.amaze.filemanager.adapters import android.content.Context +import android.text.Spannable +import android.text.SpannableString +import android.text.style.ForegroundColorSpan import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -67,14 +70,22 @@ class SearchRecyclerViewAdapter : override fun onBindViewHolder(holder: SearchRecyclerViewAdapter.ViewHolder, position: Int) { val (file, matchResult) = getItem(position) - holder.fileNameTV.text = file.name + val colorPreference = + (AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference + + val fileName = SpannableString(file.name) + fileName.setSpan( + ForegroundColorSpan(colorPreference.accent), + matchResult.first, + matchResult.last + 1, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + ) + + holder.fileNameTV.text = fileName holder.filePathTV.text = file.path.substring(0, file.path.lastIndexOf("/")) holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context)) - val colorPreference = - (AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference - if (file.isDirectory) { holder.colorView.setBackgroundColor(colorPreference.primaryFirstTab) } else {