Skip to content

Commit 8e91233

Browse files
authored
Merge pull request #4065 from seelchen/feature/highlight-search-term
Highlight match in search result
2 parents 8fcbe7e + 053e834 commit 8e91233

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

app/src/main/java/com/amaze/filemanager/adapters/SearchRecyclerViewAdapter.kt

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
package com.amaze.filemanager.adapters
2222

2323
import android.content.Context
24+
import android.text.Spannable
25+
import android.text.SpannableString
26+
import android.text.style.ForegroundColorSpan
2427
import android.view.LayoutInflater
2528
import android.view.View
2629
import android.view.ViewGroup
@@ -31,27 +34,30 @@ import androidx.recyclerview.widget.ListAdapter
3134
import androidx.recyclerview.widget.RecyclerView
3235
import com.amaze.filemanager.R
3336
import com.amaze.filemanager.application.AppConfig
34-
import com.amaze.filemanager.filesystem.HybridFileParcelable
37+
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult
3538
import com.amaze.filemanager.ui.activities.MainActivity
3639
import com.amaze.filemanager.ui.colors.ColorPreference
3740
import java.util.Random
3841

3942
class SearchRecyclerViewAdapter :
40-
ListAdapter<HybridFileParcelable, SearchRecyclerViewAdapter.ViewHolder>(
43+
ListAdapter<SearchResult, SearchRecyclerViewAdapter.ViewHolder>(
4144

42-
object : DiffUtil.ItemCallback<HybridFileParcelable>() {
45+
object : DiffUtil.ItemCallback<SearchResult>() {
4346
override fun areItemsTheSame(
44-
oldItem: HybridFileParcelable,
45-
newItem: HybridFileParcelable
47+
oldItem: SearchResult,
48+
newItem: SearchResult
4649
): Boolean {
47-
return oldItem.path == newItem.path && oldItem.name == newItem.name
50+
return oldItem.file.path == newItem.file.path &&
51+
oldItem.file.name == newItem.file.name
4852
}
4953

5054
override fun areContentsTheSame(
51-
oldItem: HybridFileParcelable,
52-
newItem: HybridFileParcelable
55+
oldItem: SearchResult,
56+
newItem: SearchResult
5357
): Boolean {
54-
return oldItem.path == newItem.path && oldItem.name == newItem.name
58+
return oldItem.file.path == newItem.file.path &&
59+
oldItem.file.name == newItem.file.name &&
60+
oldItem.matchRange == newItem.matchRange
5561
}
5662
}
5763
) {
@@ -62,17 +68,25 @@ class SearchRecyclerViewAdapter :
6268
}
6369

6470
override fun onBindViewHolder(holder: SearchRecyclerViewAdapter.ViewHolder, position: Int) {
65-
val item = getItem(position)
66-
67-
holder.fileNameTV.text = item.name
68-
holder.filePathTV.text = item.path.substring(0, item.path.lastIndexOf("/"))
69-
70-
holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context))
71+
val (file, matchResult) = getItem(position)
7172

7273
val colorPreference =
7374
(AppConfig.getInstance().mainActivityContext as MainActivity).currentColorPreference
7475

75-
if (item.isDirectory) {
76+
val fileName = SpannableString(file.name)
77+
fileName.setSpan(
78+
ForegroundColorSpan(colorPreference.accent),
79+
matchResult.first,
80+
matchResult.last + 1,
81+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
82+
)
83+
84+
holder.fileNameTV.text = fileName
85+
holder.filePathTV.text = file.path.substring(0, file.path.lastIndexOf("/"))
86+
87+
holder.colorView.setBackgroundColor(getRandomColor(holder.colorView.context))
88+
89+
if (file.isDirectory) {
7690
holder.colorView.setBackgroundColor(colorPreference.primaryFirstTab)
7791
} else {
7892
holder.colorView.setBackgroundColor(colorPreference.accent)
@@ -93,16 +107,16 @@ class SearchRecyclerViewAdapter :
93107

94108
view.setOnClickListener {
95109

96-
val item = getItem(adapterPosition)
110+
val (file, _) = getItem(adapterPosition)
97111

98-
if (!item.isDirectory) {
99-
item.openFile(
112+
if (!file.isDirectory) {
113+
file.openFile(
100114
AppConfig.getInstance().mainActivityContext as MainActivity?,
101115
false
102116
)
103117
} else {
104118
(AppConfig.getInstance().mainActivityContext as MainActivity?)
105-
?.goToMain(item.path)
119+
?.goToMain(file.path)
106120
}
107121

108122
(AppConfig.getInstance().mainActivityContext as MainActivity?)

app/src/main/java/com/amaze/filemanager/ui/views/appbar/SearchView.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.amaze.filemanager.adapters.SearchRecyclerViewAdapter;
3434
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResult;
3535
import com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem.SearchResultListSorter;
36-
import com.amaze.filemanager.filesystem.HybridFileParcelable;
3736
import com.amaze.filemanager.filesystem.files.sort.DirSortBy;
3837
import com.amaze.filemanager.filesystem.files.sort.SortBy;
3938
import com.amaze.filemanager.filesystem.files.sort.SortOrder;
@@ -376,11 +375,7 @@ private void updateResultList(List<SearchResult> newResults, String searchTerm)
376375
ArrayList<SearchResult> items = new ArrayList<>(newResults);
377376
Collections.sort(
378377
items, new SearchResultListSorter(DirSortBy.NONE_ON_TOP, sortType, searchTerm));
379-
ArrayList<HybridFileParcelable> files = new ArrayList<>();
380-
for (SearchResult searchResult : items) {
381-
files.add(searchResult.getFile());
382-
}
383-
searchRecyclerViewAdapter.submitList(files);
378+
searchRecyclerViewAdapter.submitList(items);
384379
searchRecyclerViewAdapter.notifyDataSetChanged();
385380
}
386381

0 commit comments

Comments
 (0)