Skip to content

Commit

Permalink
Merge pull request #4065 from seelchen/feature/highlight-search-term
Browse files Browse the repository at this point in the history
Highlight match in search result
  • Loading branch information
VishalNehra authored Feb 15, 2024
2 parents 8fcbe7e + 053e834 commit 8e91233
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<HybridFileParcelable, SearchRecyclerViewAdapter.ViewHolder>(
ListAdapter<SearchResult, SearchRecyclerViewAdapter.ViewHolder>(

object : DiffUtil.ItemCallback<HybridFileParcelable>() {
object : DiffUtil.ItemCallback<SearchResult>() {
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
}
}
) {
Expand All @@ -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)
Expand All @@ -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?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -376,11 +375,7 @@ private void updateResultList(List<SearchResult> newResults, String searchTerm)
ArrayList<SearchResult> items = new ArrayList<>(newResults);
Collections.sort(
items, new SearchResultListSorter(DirSortBy.NONE_ON_TOP, sortType, searchTerm));
ArrayList<HybridFileParcelable> files = new ArrayList<>();
for (SearchResult searchResult : items) {
files.add(searchResult.getFile());
}
searchRecyclerViewAdapter.submitList(files);
searchRecyclerViewAdapter.submitList(items);
searchRecyclerViewAdapter.notifyDataSetChanged();
}

Expand Down

0 comments on commit 8e91233

Please sign in to comment.