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..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 @@ -31,27 +34,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 +68,25 @@ class SearchRecyclerViewAdapter : } override fun onBindViewHolder(holder: SearchRecyclerViewAdapter.ViewHolder, position: Int) { - val item = getItem(position) - - holder.fileNameTV.text = item.name - holder.filePathTV.text = item.path.substring(0, item.path.lastIndexOf("/")) - - holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context)) + val (file, matchResult) = getItem(position) val colorPreference = (AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference - if (item.isDirectory) { + 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)) + + if (file.isDirectory) { holder.colorView.setBackgroundColor(colorPreference.primaryFirstTab) } else { holder.colorView.setBackgroundColor(colorPreference.accent) @@ -93,16 +107,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 afec6fd7e9..a0ccf27139 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(); }